blob: b1d7db4164ef2685bcc0bd29848777a039ec35e0 [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"
Manuel Klimekd4be4082013-02-28 13:21:39 +000020
21namespace clang {
22namespace ast_matchers {
23
24using clang::tooling::newFrontendActionFactory;
25using clang::tooling::runToolOnCodeWithArgs;
26using clang::tooling::FrontendActionFactory;
27
28TEST(GetParents, ReturnsParentForDecl) {
29 MatchVerifier<Decl> Verifier;
Aaron Ballman512fb642015-09-17 13:30:52 +000030 EXPECT_TRUE(
31 Verifier.match("class C { void f(); };",
32 cxxMethodDecl(hasParent(recordDecl(hasName("C"))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000033}
34
35TEST(GetParents, ReturnsParentForStmt) {
36 MatchVerifier<Stmt> Verifier;
37 EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };",
38 ifStmt(hasParent(compoundStmt()))));
39}
40
Benjamin Kramer94355ae2015-10-23 09:04:55 +000041TEST(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
48TEST(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
Manuel Klimekd4be4082013-02-28 13:21:39 +000054TEST(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(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000059 cxxMethodDecl(hasName("f"),
60 hasParent(cxxRecordDecl(isTemplateInstantiation())))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000061 EXPECT_TRUE(DeclVerifier.match(
62 "template<typename T> struct C { void f() {} };"
63 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000064 cxxMethodDecl(hasName("f"),
65 hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000066 EXPECT_FALSE(DeclVerifier.match(
67 "template<typename T> struct C { void f() {} };"
68 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000069 cxxMethodDecl(
70 hasName("f"),
71 allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))),
72 hasParent(cxxRecordDecl(isTemplateInstantiation()))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000073}
74
75TEST(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(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000080 compoundStmt(allOf(
81 hasAncestor(cxxRecordDecl(isTemplateInstantiation())),
82 hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation())))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000083}
84
85} // end namespace ast_matchers
86} // end namespace clang