summaryrefslogtreecommitdiffstats
path: root/clang/unittests/AST/ASTContextParentMapTest.cpp
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2013-02-28 13:21:39 +0000
committerManuel Klimek <klimek@google.com>2013-02-28 13:21:39 +0000
commitd4be4084b4c35f8a872eea88fa0184a760b1754b (patch)
tree74bd96c2ee715afe3eaaca1de7e7068aab5e6646 /clang/unittests/AST/ASTContextParentMapTest.cpp
parentabebb5727216bcf09bc616ac780a20b33f5b18fd (diff)
downloadbcm5719-llvm-d4be4084b4c35f8a872eea88fa0184a760b1754b.tar.gz
bcm5719-llvm-d4be4084b4c35f8a872eea88fa0184a760b1754b.zip
First step towards adding a parent map to the ASTContext.
This does not yet implement the LimitNode approach discussed. The impact of this is an O(n) in the number of nodes in the AST reduction of complexity for certain kinds of matchers (as otherwise the parent map gets recreated for every new MatchFinder). See FIXMEs in the comments for the direction of future work. llvm-svn: 176251
Diffstat (limited to 'clang/unittests/AST/ASTContextParentMapTest.cpp')
-rw-r--r--clang/unittests/AST/ASTContextParentMapTest.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/clang/unittests/AST/ASTContextParentMapTest.cpp b/clang/unittests/AST/ASTContextParentMapTest.cpp
new file mode 100644
index 00000000000..c1910a82310
--- /dev/null
+++ b/clang/unittests/AST/ASTContextParentMapTest.cpp
@@ -0,0 +1,71 @@
+//===- unittest/AST/ASTContextParentMapTest.cpp - AST parent map test -----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Tests for the getParents(...) methods of ASTContext.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Tooling/Tooling.h"
+#include "gtest/gtest.h"
+#include "MatchVerifier.h"
+
+namespace clang {
+namespace ast_matchers {
+
+using clang::tooling::newFrontendActionFactory;
+using clang::tooling::runToolOnCodeWithArgs;
+using clang::tooling::FrontendActionFactory;
+
+TEST(GetParents, ReturnsParentForDecl) {
+ MatchVerifier<Decl> Verifier;
+ EXPECT_TRUE(Verifier.match("class C { void f(); };",
+ methodDecl(hasParent(recordDecl(hasName("C"))))));
+}
+
+TEST(GetParents, ReturnsParentForStmt) {
+ MatchVerifier<Stmt> Verifier;
+ EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };",
+ ifStmt(hasParent(compoundStmt()))));
+}
+
+TEST(GetParents, ReturnsParentInsideTemplateInstantiations) {
+ MatchVerifier<Decl> DeclVerifier;
+ EXPECT_TRUE(DeclVerifier.match(
+ "template<typename T> struct C { void f() {} };"
+ "void g() { C<int> c; c.f(); }",
+ methodDecl(hasName("f"),
+ hasParent(recordDecl(isTemplateInstantiation())))));
+ EXPECT_TRUE(DeclVerifier.match(
+ "template<typename T> struct C { void f() {} };"
+ "void g() { C<int> c; c.f(); }",
+ methodDecl(hasName("f"),
+ hasParent(recordDecl(unless(isTemplateInstantiation()))))));
+ EXPECT_FALSE(DeclVerifier.match(
+ "template<typename T> struct C { void f() {} };"
+ "void g() { C<int> c; c.f(); }",
+ methodDecl(hasName("f"),
+ allOf(hasParent(recordDecl(unless(isTemplateInstantiation()))),
+ hasParent(recordDecl(isTemplateInstantiation()))))));
+}
+
+TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) {
+ MatchVerifier<Stmt> TemplateVerifier;
+ EXPECT_TRUE(TemplateVerifier.match(
+ "template<typename T> struct C { void f() {} };"
+ "void g() { C<int> c; c.f(); }",
+ compoundStmt(
+ allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
+ hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
+}
+
+} // end namespace ast_matchers
+} // end namespace clang
OpenPOWER on IntegriCloud