blob: f06f32bf7677e386e56bbbdca71a4a1cd74323ed [file] [log] [blame]
Manuel Klimekd4be4082013-02-28 13:21:39 +00001//===- 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"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000015#include "MatchVerifier.h"
Manuel Klimekd4be4082013-02-28 13:21:39 +000016#include "clang/ASTMatchers/ASTMatchFinder.h"
17#include "clang/ASTMatchers/ASTMatchers.h"
18#include "clang/Tooling/Tooling.h"
19#include "gtest/gtest.h"
Sam McCall814e7972018-11-14 10:33:30 +000020#include "gmock/gmock.h"
21
22using testing::ElementsAre;
Manuel Klimekd4be4082013-02-28 13:21:39 +000023
24namespace clang {
25namespace ast_matchers {
26
Manuel Klimekd4be4082013-02-28 13:21:39 +000027TEST(GetParents, ReturnsParentForDecl) {
28 MatchVerifier<Decl> Verifier;
Aaron Ballman512fb642015-09-17 13:30:52 +000029 EXPECT_TRUE(
30 Verifier.match("class C { void f(); };",
31 cxxMethodDecl(hasParent(recordDecl(hasName("C"))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000032}
33
34TEST(GetParents, ReturnsParentForStmt) {
35 MatchVerifier<Stmt> Verifier;
36 EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };",
37 ifStmt(hasParent(compoundStmt()))));
38}
39
Benjamin Kramer94355ae2015-10-23 09:04:55 +000040TEST(GetParents, ReturnsParentForTypeLoc) {
41 MatchVerifier<TypeLoc> Verifier;
42 EXPECT_TRUE(
43 Verifier.match("namespace a { class b {}; } void f(a::b) {}",
44 typeLoc(hasParent(typeLoc(hasParent(functionDecl()))))));
45}
46
47TEST(GetParents, ReturnsParentForNestedNameSpecifierLoc) {
48 MatchVerifier<NestedNameSpecifierLoc> Verifier;
49 EXPECT_TRUE(Verifier.match("namespace a { class b {}; } void f(a::b) {}",
50 nestedNameSpecifierLoc(hasParent(typeLoc()))));
51}
52
Manuel Klimekd4be4082013-02-28 13:21:39 +000053TEST(GetParents, ReturnsParentInsideTemplateInstantiations) {
54 MatchVerifier<Decl> DeclVerifier;
55 EXPECT_TRUE(DeclVerifier.match(
56 "template<typename T> struct C { void f() {} };"
57 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000058 cxxMethodDecl(hasName("f"),
59 hasParent(cxxRecordDecl(isTemplateInstantiation())))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000060 EXPECT_TRUE(DeclVerifier.match(
61 "template<typename T> struct C { void f() {} };"
62 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000063 cxxMethodDecl(hasName("f"),
64 hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000065 EXPECT_FALSE(DeclVerifier.match(
66 "template<typename T> struct C { void f() {} };"
67 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000068 cxxMethodDecl(
69 hasName("f"),
70 allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))),
71 hasParent(cxxRecordDecl(isTemplateInstantiation()))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000072}
73
74TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) {
75 MatchVerifier<Stmt> TemplateVerifier;
76 EXPECT_TRUE(TemplateVerifier.match(
77 "template<typename T> struct C { void f() {} };"
78 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000079 compoundStmt(allOf(
80 hasAncestor(cxxRecordDecl(isTemplateInstantiation())),
81 hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation())))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000082}
83
Sam McCall814e7972018-11-14 10:33:30 +000084TEST(GetParents, RespectsTraversalScope) {
85 auto AST =
86 tooling::buildASTFromCode("struct foo { int bar; };", "foo.cpp",
87 std::make_shared<PCHContainerOperations>());
88 auto &Ctx = AST->getASTContext();
89 auto &TU = *Ctx.getTranslationUnitDecl();
90 auto &Foo = *TU.lookup(&Ctx.Idents.get("foo")).front();
91 auto &Bar = *cast<DeclContext>(Foo).lookup(&Ctx.Idents.get("bar")).front();
92
93 using ast_type_traits::DynTypedNode;
94 // Initially, scope is the whole TU.
95 EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo)));
96 EXPECT_THAT(Ctx.getParents(Foo), ElementsAre(DynTypedNode::create(TU)));
97
98 // Restrict the scope, now some parents are gone.
99 Ctx.setTraversalScope({&Foo});
100 EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo)));
101 EXPECT_THAT(Ctx.getParents(Foo), ElementsAre());
102
103 // Reset the scope, we get back the original results.
104 Ctx.setTraversalScope({&TU});
105 EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo)));
106 EXPECT_THAT(Ctx.getParents(Foo), ElementsAre(DynTypedNode::create(TU)));
107}
108
Manuel Klimekd4be4082013-02-28 13:21:39 +0000109} // end namespace ast_matchers
110} // end namespace clang