aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Kelly <steveire@gmail.com>2019-01-12 16:35:37 +0000
committerStephen Kelly <steveire@gmail.com>2019-01-12 16:35:37 +0000
commitf4295e64ab4d0f5737616e3b0803e51389dad854 (patch)
tree6b94d961775bcba322f5b8931906128a3cec0e2e
parentb5a64e2c24a16007c0faae3280d3262606ae2a0f (diff)
Implement TemplateArgument dumping in terms of Visitor
Summary: Split the output streaming from the traversal to other AST nodes. Reviewers: aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D55491 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@351012 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/AST/TemplateArgumentVisitor.h99
-rw-r--r--include/clang/AST/TextNodeDumper.h17
-rw-r--r--lib/AST/ASTDumper.cpp57
-rw-r--r--lib/AST/TextNodeDumper.cpp55
4 files changed, 183 insertions, 45 deletions
diff --git a/include/clang/AST/TemplateArgumentVisitor.h b/include/clang/AST/TemplateArgumentVisitor.h
new file mode 100644
index 0000000000..e1cc392a17
--- /dev/null
+++ b/include/clang/AST/TemplateArgumentVisitor.h
@@ -0,0 +1,99 @@
+//===- TemplateArgumentVisitor.h - Visitor for TArg subclasses --*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the TemplateArgumentVisitor interface.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_AST_TEMPLATEARGUMENTVISITOR_H
+#define LLVM_CLANG_AST_TEMPLATEARGUMENTVISITOR_H
+
+#include "clang/AST/TemplateBase.h"
+
+namespace clang {
+
+namespace templateargumentvisitor {
+
+/// A simple visitor class that helps create template argument visitors.
+template <template <typename> class Ref, typename ImplClass,
+ typename RetTy = void, typename... ParamTys>
+class Base {
+public:
+#define REF(CLASS) typename Ref<CLASS>::type
+#define DISPATCH(NAME) \
+ case TemplateArgument::NAME: \
+ return static_cast<ImplClass *>(this)->Visit##NAME##TemplateArgument( \
+ TA, std::forward<ParamTys>(P)...)
+
+ RetTy Visit(REF(TemplateArgument) TA, ParamTys... P) {
+ switch (TA.getKind()) {
+ DISPATCH(Null);
+ DISPATCH(Type);
+ DISPATCH(Declaration);
+ DISPATCH(NullPtr);
+ DISPATCH(Integral);
+ DISPATCH(Template);
+ DISPATCH(TemplateExpansion);
+ DISPATCH(Expression);
+ DISPATCH(Pack);
+ }
+ llvm_unreachable("TemplateArgument is not covered in switch!");
+ }
+
+ // If the implementation chooses not to implement a certain visit
+ // method, fall back to the parent.
+
+#define VISIT_METHOD(CATEGORY) \
+ RetTy Visit##CATEGORY##TemplateArgument(REF(TemplateArgument) TA, \
+ ParamTys... P) { \
+ return VisitTemplateArgument(TA, std::forward<ParamTys>(P)...); \
+ }
+
+ VISIT_METHOD(Null);
+ VISIT_METHOD(Type);
+ VISIT_METHOD(Declaration);
+ VISIT_METHOD(NullPtr);
+ VISIT_METHOD(Integral);
+ VISIT_METHOD(Template);
+ VISIT_METHOD(TemplateExpansion);
+ VISIT_METHOD(Expression);
+ VISIT_METHOD(Pack);
+
+ RetTy VisitTemplateArgument(REF(TemplateArgument), ParamTys...) {
+ return RetTy();
+ }
+
+#undef REF
+#undef DISPATCH
+#undef VISIT_METHOD
+};
+
+} // namespace templateargumentvisitor
+
+/// A simple visitor class that helps create template argument visitors.
+///
+/// This class does not preserve constness of TemplateArgument references (see
+/// also ConstTemplateArgumentVisitor).
+template <typename ImplClass, typename RetTy = void, typename... ParamTys>
+class TemplateArgumentVisitor
+ : public templateargumentvisitor::Base<std::add_lvalue_reference, ImplClass,
+ RetTy, ParamTys...> {};
+
+/// A simple visitor class that helps create template argument visitors.
+///
+/// This class preserves constness of TemplateArgument references (see also
+/// TemplateArgumentVisitor).
+template <typename ImplClass, typename RetTy = void, typename... ParamTys>
+class ConstTemplateArgumentVisitor
+ : public templateargumentvisitor::Base<llvm::make_const_ref, ImplClass,
+ RetTy, ParamTys...> {};
+
+} // namespace clang
+
+#endif // LLVM_CLANG_AST_TEMPLATEARGUMENTVISITOR_H
diff --git a/include/clang/AST/TextNodeDumper.h b/include/clang/AST/TextNodeDumper.h
index 2c3cc7294c..d5521199f9 100644
--- a/include/clang/AST/TextNodeDumper.h
+++ b/include/clang/AST/TextNodeDumper.h
@@ -20,6 +20,7 @@
#include "clang/AST/CommentCommandTraits.h"
#include "clang/AST/CommentVisitor.h"
#include "clang/AST/ExprCXX.h"
+#include "clang/AST/TemplateArgumentVisitor.h"
namespace clang {
@@ -123,7 +124,8 @@ class TextNodeDumper
: public TextTreeStructure,
public comments::ConstCommentVisitor<TextNodeDumper, void,
const comments::FullComment *>,
- public ConstAttrVisitor<TextNodeDumper> {
+ public ConstAttrVisitor<TextNodeDumper>,
+ public ConstTemplateArgumentVisitor<TextNodeDumper> {
raw_ostream &OS;
const bool ShowColors;
@@ -150,6 +152,9 @@ public:
void Visit(const Attr *A);
+ void Visit(const TemplateArgument &TA, SourceRange R,
+ const Decl *From = nullptr, StringRef Label = {});
+
void dumpPointer(const void *Ptr);
void dumpLocation(SourceLocation Loc);
void dumpSourceRange(SourceRange R);
@@ -186,6 +191,16 @@ public:
// Implements Visit methods for Attrs.
#include "clang/AST/AttrTextNodeDump.inc"
+
+ void VisitNullTemplateArgument(const TemplateArgument &TA);
+ void VisitTypeTemplateArgument(const TemplateArgument &TA);
+ void VisitDeclarationTemplateArgument(const TemplateArgument &TA);
+ void VisitNullPtrTemplateArgument(const TemplateArgument &TA);
+ void VisitIntegralTemplateArgument(const TemplateArgument &TA);
+ void VisitTemplateTemplateArgument(const TemplateArgument &TA);
+ void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA);
+ void VisitExpressionTemplateArgument(const TemplateArgument &TA);
+ void VisitPackTemplateArgument(const TemplateArgument &TA);
};
} // namespace clang
diff --git a/lib/AST/ASTDumper.cpp b/lib/AST/ASTDumper.cpp
index 513f2ec029..1865d0db47 100644
--- a/lib/AST/ASTDumper.cpp
+++ b/lib/AST/ASTDumper.cpp
@@ -24,6 +24,7 @@
#include "clang/AST/DeclVisitor.h"
#include "clang/AST/LocInfoType.h"
#include "clang/AST/StmtVisitor.h"
+#include "clang/AST/TemplateArgumentVisitor.h"
#include "clang/AST/TextNodeDumper.h"
#include "clang/AST/TypeVisitor.h"
#include "clang/Basic/Builtins.h"
@@ -44,7 +45,8 @@ namespace {
public ConstStmtVisitor<ASTDumper>,
public ConstCommentVisitor<ASTDumper, void, const FullComment *>,
public TypeVisitor<ASTDumper>,
- public ConstAttrVisitor<ASTDumper> {
+ public ConstAttrVisitor<ASTDumper>,
+ public ConstTemplateArgumentVisitor<ASTDumper> {
TextNodeDumper NodeDumper;
@@ -440,6 +442,14 @@ namespace {
// Comments.
void dumpComment(const Comment *C, const FullComment *FC);
+ void VisitExpressionTemplateArgument(const TemplateArgument &TA) {
+ dumpStmt(TA.getAsExpr());
+ }
+ void VisitPackTemplateArgument(const TemplateArgument &TA) {
+ for (const auto& TArg : TA.pack_elements())
+ dumpTemplateArgument(TArg);
+ }
+
// Implements Visit methods for Attrs.
#include "clang/AST/AttrNodeTraverse.inc"
};
@@ -670,49 +680,8 @@ void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R,
const Decl *From, const char *Label) {
dumpChild([=] {
- OS << "TemplateArgument";
- if (R.isValid())
- NodeDumper.dumpSourceRange(R);
-
- if (From)
- NodeDumper.dumpDeclRef(From, Label);
-
- switch (A.getKind()) {
- case TemplateArgument::Null:
- OS << " null";
- break;
- case TemplateArgument::Type:
- OS << " type";
- NodeDumper.dumpType(A.getAsType());
- break;
- case TemplateArgument::Declaration:
- OS << " decl";
- NodeDumper.dumpDeclRef(A.getAsDecl());
- break;
- case TemplateArgument::NullPtr:
- OS << " nullptr";
- break;
- case TemplateArgument::Integral:
- OS << " integral " << A.getAsIntegral();
- break;
- case TemplateArgument::Template:
- OS << " template ";
- A.getAsTemplate().dump(OS);
- break;
- case TemplateArgument::TemplateExpansion:
- OS << " template expansion ";
- A.getAsTemplateOrTemplatePattern().dump(OS);
- break;
- case TemplateArgument::Expression:
- OS << " expr";
- dumpStmt(A.getAsExpr());
- break;
- case TemplateArgument::Pack:
- OS << " pack";
- for (const auto& TArg : A.pack_elements())
- dumpTemplateArgument(TArg);
- break;
- }
+ NodeDumper.Visit(A, R, From, Label);
+ ConstTemplateArgumentVisitor<ASTDumper>::Visit(A);
});
}
diff --git a/lib/AST/TextNodeDumper.cpp b/lib/AST/TextNodeDumper.cpp
index 9c0496dd69..c4d2f4c751 100644
--- a/lib/AST/TextNodeDumper.cpp
+++ b/lib/AST/TextNodeDumper.cpp
@@ -64,6 +64,19 @@ void TextNodeDumper::Visit(const Attr *A) {
ConstAttrVisitor<TextNodeDumper>::Visit(A);
}
+void TextNodeDumper::Visit(const TemplateArgument &TA, SourceRange R,
+ const Decl *From, StringRef Label) {
+ OS << "TemplateArgument";
+ if (R.isValid())
+ dumpSourceRange(R);
+
+ if (From) {
+ dumpDeclRef(From, Label);
+ }
+
+ ConstTemplateArgumentVisitor<TextNodeDumper>::Visit(TA);
+}
+
void TextNodeDumper::dumpPointer(const void *Ptr) {
ColorScope Color(OS, ShowColors, AddressColor);
OS << ' ' << Ptr;
@@ -317,3 +330,45 @@ void TextNodeDumper::visitVerbatimLineComment(
const comments::VerbatimLineComment *C, const comments::FullComment *) {
OS << " Text=\"" << C->getText() << "\"";
}
+
+void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) {
+ OS << " null";
+}
+
+void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) {
+ OS << " type";
+ dumpType(TA.getAsType());
+}
+
+void TextNodeDumper::VisitDeclarationTemplateArgument(
+ const TemplateArgument &TA) {
+ OS << " decl";
+ dumpDeclRef(TA.getAsDecl());
+}
+
+void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) {
+ OS << " nullptr";
+}
+
+void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) {
+ OS << " integral " << TA.getAsIntegral();
+}
+
+void TextNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) {
+ OS << " template ";
+ TA.getAsTemplate().dump(OS);
+}
+
+void TextNodeDumper::VisitTemplateExpansionTemplateArgument(
+ const TemplateArgument &TA) {
+ OS << " template expansion ";
+ TA.getAsTemplateOrTemplatePattern().dump(OS);
+}
+
+void TextNodeDumper::VisitExpressionTemplateArgument(const TemplateArgument &) {
+ OS << " expr";
+}
+
+void TextNodeDumper::VisitPackTemplateArgument(const TemplateArgument &) {
+ OS << " pack";
+}