Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 1 | //===- unittest/AST/ASTContextParentMapTest.cpp - AST parent map test -----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Tests for the getParents(...) methods of ASTContext. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 14 | #include "MatchVerifier.h" |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 15 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 16 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 17 | #include "clang/Tooling/Tooling.h" |
| 18 | #include "gtest/gtest.h" |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 19 | #include "gmock/gmock.h" |
| 20 | |
| 21 | using testing::ElementsAre; |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
| 24 | namespace ast_matchers { |
| 25 | |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 26 | TEST(GetParents, ReturnsParentForDecl) { |
| 27 | MatchVerifier<Decl> Verifier; |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 28 | EXPECT_TRUE( |
| 29 | Verifier.match("class C { void f(); };", |
| 30 | cxxMethodDecl(hasParent(recordDecl(hasName("C")))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST(GetParents, ReturnsParentForStmt) { |
| 34 | MatchVerifier<Stmt> Verifier; |
| 35 | EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };", |
| 36 | ifStmt(hasParent(compoundStmt())))); |
| 37 | } |
| 38 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 39 | TEST(GetParents, ReturnsParentForTypeLoc) { |
| 40 | MatchVerifier<TypeLoc> Verifier; |
| 41 | EXPECT_TRUE( |
| 42 | Verifier.match("namespace a { class b {}; } void f(a::b) {}", |
| 43 | typeLoc(hasParent(typeLoc(hasParent(functionDecl())))))); |
| 44 | } |
| 45 | |
| 46 | TEST(GetParents, ReturnsParentForNestedNameSpecifierLoc) { |
| 47 | MatchVerifier<NestedNameSpecifierLoc> Verifier; |
| 48 | EXPECT_TRUE(Verifier.match("namespace a { class b {}; } void f(a::b) {}", |
| 49 | nestedNameSpecifierLoc(hasParent(typeLoc())))); |
| 50 | } |
| 51 | |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 52 | TEST(GetParents, ReturnsParentInsideTemplateInstantiations) { |
| 53 | MatchVerifier<Decl> DeclVerifier; |
| 54 | EXPECT_TRUE(DeclVerifier.match( |
| 55 | "template<typename T> struct C { void f() {} };" |
| 56 | "void g() { C<int> c; c.f(); }", |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 57 | cxxMethodDecl(hasName("f"), |
| 58 | hasParent(cxxRecordDecl(isTemplateInstantiation()))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 59 | EXPECT_TRUE(DeclVerifier.match( |
| 60 | "template<typename T> struct C { void f() {} };" |
| 61 | "void g() { C<int> c; c.f(); }", |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 62 | cxxMethodDecl(hasName("f"), |
| 63 | hasParent(cxxRecordDecl(unless(isTemplateInstantiation())))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 64 | EXPECT_FALSE(DeclVerifier.match( |
| 65 | "template<typename T> struct C { void f() {} };" |
| 66 | "void g() { C<int> c; c.f(); }", |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 67 | cxxMethodDecl( |
| 68 | hasName("f"), |
| 69 | allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))), |
| 70 | hasParent(cxxRecordDecl(isTemplateInstantiation())))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) { |
| 74 | MatchVerifier<Stmt> TemplateVerifier; |
| 75 | EXPECT_TRUE(TemplateVerifier.match( |
| 76 | "template<typename T> struct C { void f() {} };" |
| 77 | "void g() { C<int> c; c.f(); }", |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 78 | compoundStmt(allOf( |
| 79 | hasAncestor(cxxRecordDecl(isTemplateInstantiation())), |
| 80 | hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation()))))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 83 | TEST(GetParents, RespectsTraversalScope) { |
| 84 | auto AST = |
| 85 | tooling::buildASTFromCode("struct foo { int bar; };", "foo.cpp", |
| 86 | std::make_shared<PCHContainerOperations>()); |
| 87 | auto &Ctx = AST->getASTContext(); |
| 88 | auto &TU = *Ctx.getTranslationUnitDecl(); |
| 89 | auto &Foo = *TU.lookup(&Ctx.Idents.get("foo")).front(); |
| 90 | auto &Bar = *cast<DeclContext>(Foo).lookup(&Ctx.Idents.get("bar")).front(); |
| 91 | |
| 92 | using ast_type_traits::DynTypedNode; |
| 93 | // Initially, scope is the whole TU. |
| 94 | EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo))); |
| 95 | EXPECT_THAT(Ctx.getParents(Foo), ElementsAre(DynTypedNode::create(TU))); |
| 96 | |
| 97 | // Restrict the scope, now some parents are gone. |
| 98 | Ctx.setTraversalScope({&Foo}); |
| 99 | EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo))); |
| 100 | EXPECT_THAT(Ctx.getParents(Foo), ElementsAre()); |
| 101 | |
| 102 | // Reset the scope, we get back the original results. |
| 103 | Ctx.setTraversalScope({&TU}); |
| 104 | EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo))); |
| 105 | EXPECT_THAT(Ctx.getParents(Foo), ElementsAre(DynTypedNode::create(TU))); |
| 106 | } |
| 107 | |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 108 | TEST(GetParents, ImplicitLambdaNodes) { |
| 109 | MatchVerifier<Decl> LambdaVerifier; |
| 110 | EXPECT_TRUE(LambdaVerifier.match( |
| 111 | "auto x = []{int y;};", |
| 112 | varDecl(hasName("y"), hasAncestor(functionDecl( |
| 113 | hasOverloadedOperatorName("()"), |
| 114 | hasParent(cxxRecordDecl( |
| 115 | isImplicit(), hasParent(lambdaExpr())))))), |
| 116 | Lang_CXX11)); |
| 117 | } |
| 118 | |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 119 | } // end namespace ast_matchers |
| 120 | } // end namespace clang |