summaryrefslogtreecommitdiff
path: root/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
diff options
context:
space:
mode:
authorJohannes Altmanninger <aclopte@gmail.com>2017-09-06 13:11:13 +0000
committerJohannes Altmanninger <aclopte@gmail.com>2017-09-06 13:11:13 +0000
commit32efa8614dd80f2bb0c635c99cb021a80ff8bc0e (patch)
tree6c99f1651def36a301704ed95eebab040d52966f /unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
parenta6292799d40a059ff30fc557cb356d7b376323ad (diff)
downloadclang-32efa8614dd80f2bb0c635c99cb021a80ff8bc0e.tar.gz
[AST] Traverse templates in LexicallyOrderedRecursiveASTVisitor
Summary: We need to specialize this because RecursiveASTVisitor visits template template parameters after the templated declaration, unlike the order in which they appear in the source code. Reviewers: arphaman Reviewed By: arphaman Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D36998 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312631 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp')
-rw-r--r--unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp30
1 files changed, 26 insertions, 4 deletions
diff --git a/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp b/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
index 72aa6042af..2f2c5d6ee1 100644
--- a/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
+++ b/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
@@ -21,8 +21,9 @@ class LexicallyOrderedDeclVisitor
: public LexicallyOrderedRecursiveASTVisitor<LexicallyOrderedDeclVisitor> {
public:
LexicallyOrderedDeclVisitor(DummyMatchVisitor &Matcher,
- const SourceManager &SM)
- : LexicallyOrderedRecursiveASTVisitor(SM), Matcher(Matcher) {}
+ const SourceManager &SM, bool EmitIndices)
+ : LexicallyOrderedRecursiveASTVisitor(SM), Matcher(Matcher),
+ EmitIndices(EmitIndices) {}
bool TraverseDecl(Decl *D) {
TraversalStack.push_back(D);
@@ -35,15 +36,20 @@ public:
private:
DummyMatchVisitor &Matcher;
+ bool EmitIndices;
+ unsigned Index = 0;
llvm::SmallVector<Decl *, 8> TraversalStack;
};
class DummyMatchVisitor : public ExpectedLocationVisitor<DummyMatchVisitor> {
+ bool EmitIndices;
+
public:
+ DummyMatchVisitor(bool EmitIndices = false) : EmitIndices(EmitIndices) {}
bool VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
const ASTContext &Context = TU->getASTContext();
const SourceManager &SM = Context.getSourceManager();
- LexicallyOrderedDeclVisitor SubVisitor(*this, SM);
+ LexicallyOrderedDeclVisitor SubVisitor(*this, SM, EmitIndices);
SubVisitor.TraverseDecl(TU);
return false;
}
@@ -64,9 +70,11 @@ bool LexicallyOrderedDeclVisitor::VisitNamedDecl(const NamedDecl *D) {
OS << ND->getNameAsString();
else
OS << "???";
- if (isa<DeclContext>(D))
+ if (isa<DeclContext>(D) or isa<TemplateDecl>(D))
OS << "/";
}
+ if (EmitIndices)
+ OS << "@" << Index++;
Matcher.match(OS.str(), D);
return true;
}
@@ -138,4 +146,18 @@ MACRO_F(2)
EXPECT_TRUE(Visitor.runOver(Source, DummyMatchVisitor::Lang_OBJC));
}
+TEST(LexicallyOrderedRecursiveASTVisitor, VisitTemplateDecl) {
+ StringRef Source = R"(
+template <class T> T f();
+template <class U, class = void> class Class {};
+)";
+ DummyMatchVisitor Visitor(/*EmitIndices=*/true);
+ Visitor.ExpectMatch("/f/T@1", 2, 11);
+ Visitor.ExpectMatch("/f/f/@2", 2, 20);
+ Visitor.ExpectMatch("/Class/U@4", 3, 11);
+ Visitor.ExpectMatch("/Class/@5", 3, 20);
+ Visitor.ExpectMatch("/Class/Class/@6", 3, 34);
+ EXPECT_TRUE(Visitor.runOver(Source));
+}
+
} // end anonymous namespace