blob: a39189620b69cd27e96a4d61a3c4acc678a4d010 [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
Manuel Klimekd4be4082013-02-28 13:21:39 +000024TEST(GetParents, ReturnsParentForDecl) {
25 MatchVerifier<Decl> Verifier;
Aaron Ballman512fb642015-09-17 13:30:52 +000026 EXPECT_TRUE(
27 Verifier.match("class C { void f(); };",
28 cxxMethodDecl(hasParent(recordDecl(hasName("C"))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000029}
30
31TEST(GetParents, ReturnsParentForStmt) {
32 MatchVerifier<Stmt> Verifier;
33 EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };",
34 ifStmt(hasParent(compoundStmt()))));
35}
36
Benjamin Kramer94355ae2015-10-23 09:04:55 +000037TEST(GetParents, ReturnsParentForTypeLoc) {
38 MatchVerifier<TypeLoc> Verifier;
39 EXPECT_TRUE(
40 Verifier.match("namespace a { class b {}; } void f(a::b) {}",
41 typeLoc(hasParent(typeLoc(hasParent(functionDecl()))))));
42}
43
44TEST(GetParents, ReturnsParentForNestedNameSpecifierLoc) {
45 MatchVerifier<NestedNameSpecifierLoc> Verifier;
46 EXPECT_TRUE(Verifier.match("namespace a { class b {}; } void f(a::b) {}",
47 nestedNameSpecifierLoc(hasParent(typeLoc()))));
48}
49
Manuel Klimekd4be4082013-02-28 13:21:39 +000050TEST(GetParents, ReturnsParentInsideTemplateInstantiations) {
51 MatchVerifier<Decl> DeclVerifier;
52 EXPECT_TRUE(DeclVerifier.match(
53 "template<typename T> struct C { void f() {} };"
54 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000055 cxxMethodDecl(hasName("f"),
56 hasParent(cxxRecordDecl(isTemplateInstantiation())))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000057 EXPECT_TRUE(DeclVerifier.match(
58 "template<typename T> struct C { void f() {} };"
59 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000060 cxxMethodDecl(hasName("f"),
61 hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000062 EXPECT_FALSE(DeclVerifier.match(
63 "template<typename T> struct C { void f() {} };"
64 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000065 cxxMethodDecl(
66 hasName("f"),
67 allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))),
68 hasParent(cxxRecordDecl(isTemplateInstantiation()))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000069}
70
71TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) {
72 MatchVerifier<Stmt> TemplateVerifier;
73 EXPECT_TRUE(TemplateVerifier.match(
74 "template<typename T> struct C { void f() {} };"
75 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000076 compoundStmt(allOf(
77 hasAncestor(cxxRecordDecl(isTemplateInstantiation())),
78 hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation())))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000079}
80
81} // end namespace ast_matchers
82} // end namespace clang