1 //===- unittest/AST/ASTContextParentMapTest.cpp - AST parent map test -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Tests for the getParents(...) methods of ASTContext.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/ASTContext.h"
15 #include "MatchVerifier.h"
16 #include "clang/ASTMatchers/ASTMatchFinder.h"
17 #include "clang/ASTMatchers/ASTMatchers.h"
18 #include "clang/Tooling/Tooling.h"
19 #include "gtest/gtest.h"
20
21 namespace clang {
22 namespace ast_matchers {
23
24 using clang::tooling::newFrontendActionFactory;
25 using clang::tooling::runToolOnCodeWithArgs;
26 using clang::tooling::FrontendActionFactory;
27
TEST(GetParents,ReturnsParentForDecl)28 TEST(GetParents, ReturnsParentForDecl) {
29 MatchVerifier<Decl> Verifier;
30 EXPECT_TRUE(
31 Verifier.match("class C { void f(); };",
32 cxxMethodDecl(hasParent(recordDecl(hasName("C"))))));
33 }
34
TEST(GetParents,ReturnsParentForStmt)35 TEST(GetParents, ReturnsParentForStmt) {
36 MatchVerifier<Stmt> Verifier;
37 EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };",
38 ifStmt(hasParent(compoundStmt()))));
39 }
40
TEST(GetParents,ReturnsParentForTypeLoc)41 TEST(GetParents, ReturnsParentForTypeLoc) {
42 MatchVerifier<TypeLoc> Verifier;
43 EXPECT_TRUE(
44 Verifier.match("namespace a { class b {}; } void f(a::b) {}",
45 typeLoc(hasParent(typeLoc(hasParent(functionDecl()))))));
46 }
47
TEST(GetParents,ReturnsParentForNestedNameSpecifierLoc)48 TEST(GetParents, ReturnsParentForNestedNameSpecifierLoc) {
49 MatchVerifier<NestedNameSpecifierLoc> Verifier;
50 EXPECT_TRUE(Verifier.match("namespace a { class b {}; } void f(a::b) {}",
51 nestedNameSpecifierLoc(hasParent(typeLoc()))));
52 }
53
TEST(GetParents,ReturnsParentInsideTemplateInstantiations)54 TEST(GetParents, ReturnsParentInsideTemplateInstantiations) {
55 MatchVerifier<Decl> DeclVerifier;
56 EXPECT_TRUE(DeclVerifier.match(
57 "template<typename T> struct C { void f() {} };"
58 "void g() { C<int> c; c.f(); }",
59 cxxMethodDecl(hasName("f"),
60 hasParent(cxxRecordDecl(isTemplateInstantiation())))));
61 EXPECT_TRUE(DeclVerifier.match(
62 "template<typename T> struct C { void f() {} };"
63 "void g() { C<int> c; c.f(); }",
64 cxxMethodDecl(hasName("f"),
65 hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))))));
66 EXPECT_FALSE(DeclVerifier.match(
67 "template<typename T> struct C { void f() {} };"
68 "void g() { C<int> c; c.f(); }",
69 cxxMethodDecl(
70 hasName("f"),
71 allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))),
72 hasParent(cxxRecordDecl(isTemplateInstantiation()))))));
73 }
74
TEST(GetParents,ReturnsMultipleParentsInTemplateInstantiations)75 TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) {
76 MatchVerifier<Stmt> TemplateVerifier;
77 EXPECT_TRUE(TemplateVerifier.match(
78 "template<typename T> struct C { void f() {} };"
79 "void g() { C<int> c; c.f(); }",
80 compoundStmt(allOf(
81 hasAncestor(cxxRecordDecl(isTemplateInstantiation())),
82 hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation())))))));
83 }
84
85 } // end namespace ast_matchers
86 } // end namespace clang
87