1 //===- unittest/AST/ASTTypeTraits.cpp - AST type traits unit tests ------===//
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
11 #include "clang/AST/ASTTypeTraits.h"
12 #include "MatchVerifier.h"
13 #include "gtest/gtest.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace ast_type_traits {
19
TEST(ASTNodeKind,NoKind)20 TEST(ASTNodeKind, NoKind) {
21 EXPECT_FALSE(ASTNodeKind().isBaseOf(ASTNodeKind()));
22 EXPECT_FALSE(ASTNodeKind().isSame(ASTNodeKind()));
23 }
24
DNT()25 template <typename T> static ASTNodeKind DNT() {
26 return ASTNodeKind::getFromNodeKind<T>();
27 }
28
TEST(ASTNodeKind,IsNone)29 TEST(ASTNodeKind, IsNone) {
30 EXPECT_TRUE(ASTNodeKind().isNone());
31 EXPECT_FALSE(DNT<Decl>().isNone());
32 EXPECT_FALSE(DNT<VarDecl>().isNone());
33 }
34
TEST(ASTNodeKind,Bases)35 TEST(ASTNodeKind, Bases) {
36 EXPECT_TRUE(DNT<Decl>().isBaseOf(DNT<VarDecl>()));
37 EXPECT_FALSE(DNT<Decl>().isSame(DNT<VarDecl>()));
38 EXPECT_FALSE(DNT<VarDecl>().isBaseOf(DNT<Decl>()));
39
40 EXPECT_TRUE(DNT<Decl>().isSame(DNT<Decl>()));
41 }
42
TEST(ASTNodeKind,BaseDistances)43 TEST(ASTNodeKind, BaseDistances) {
44 unsigned Distance = 1;
45 EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<Expr>(), &Distance));
46 EXPECT_EQ(0u, Distance);
47
48 EXPECT_TRUE(DNT<Stmt>().isBaseOf(DNT<IfStmt>(), &Distance));
49 EXPECT_EQ(1u, Distance);
50
51 Distance = 3;
52 EXPECT_TRUE(DNT<DeclaratorDecl>().isBaseOf(DNT<ParmVarDecl>(), &Distance));
53 EXPECT_EQ(2u, Distance);
54 }
55
TEST(ASTNodeKind,SameBase)56 TEST(ASTNodeKind, SameBase) {
57 EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<CallExpr>()));
58 EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<BinaryOperator>()));
59 EXPECT_FALSE(DNT<CallExpr>().isBaseOf(DNT<BinaryOperator>()));
60 EXPECT_FALSE(DNT<BinaryOperator>().isBaseOf(DNT<CallExpr>()));
61 }
62
TEST(ASTNodeKind,DiffBase)63 TEST(ASTNodeKind, DiffBase) {
64 EXPECT_FALSE(DNT<Expr>().isBaseOf(DNT<ArrayType>()));
65 EXPECT_FALSE(DNT<QualType>().isBaseOf(DNT<FunctionDecl>()));
66 EXPECT_FALSE(DNT<Type>().isSame(DNT<QualType>()));
67 }
68
TEST(ASTNodeKind,MostDerivedType)69 TEST(ASTNodeKind, MostDerivedType) {
70 EXPECT_TRUE(DNT<BinaryOperator>().isSame(
71 ASTNodeKind::getMostDerivedType(DNT<Expr>(), DNT<BinaryOperator>())));
72 EXPECT_TRUE(DNT<BinaryOperator>().isSame(
73 ASTNodeKind::getMostDerivedType(DNT<BinaryOperator>(), DNT<Expr>())));
74 EXPECT_TRUE(DNT<VarDecl>().isSame(
75 ASTNodeKind::getMostDerivedType(DNT<VarDecl>(), DNT<VarDecl>())));
76
77 // Not related. Returns nothing.
78 EXPECT_TRUE(
79 ASTNodeKind::getMostDerivedType(DNT<IfStmt>(), DNT<VarDecl>()).isNone());
80 EXPECT_TRUE(ASTNodeKind::getMostDerivedType(DNT<IfStmt>(),
81 DNT<BinaryOperator>()).isNone());
82 }
83
TEST(ASTNodeKind,MostDerivedCommonAncestor)84 TEST(ASTNodeKind, MostDerivedCommonAncestor) {
85 EXPECT_TRUE(DNT<Expr>().isSame(ASTNodeKind::getMostDerivedCommonAncestor(
86 DNT<Expr>(), DNT<BinaryOperator>())));
87 EXPECT_TRUE(DNT<Expr>().isSame(ASTNodeKind::getMostDerivedCommonAncestor(
88 DNT<BinaryOperator>(), DNT<Expr>())));
89 EXPECT_TRUE(DNT<VarDecl>().isSame(ASTNodeKind::getMostDerivedCommonAncestor(
90 DNT<VarDecl>(), DNT<VarDecl>())));
91
92 // A little related. Returns the ancestor.
93 EXPECT_TRUE(
94 DNT<NamedDecl>().isSame(ASTNodeKind::getMostDerivedCommonAncestor(
95 DNT<CXXMethodDecl>(), DNT<RecordDecl>())));
96
97 // Not related. Returns nothing.
98 EXPECT_TRUE(ASTNodeKind::getMostDerivedCommonAncestor(
99 DNT<IfStmt>(), DNT<VarDecl>()).isNone());
100 }
101
102 struct Foo {};
103
TEST(ASTNodeKind,UnknownKind)104 TEST(ASTNodeKind, UnknownKind) {
105 // We can construct one, but it is nowhere in the hierarchy.
106 EXPECT_FALSE(DNT<Foo>().isSame(DNT<Foo>()));
107 }
108
TEST(ASTNodeKind,Name)109 TEST(ASTNodeKind, Name) {
110 EXPECT_EQ("<None>", ASTNodeKind().asStringRef());
111 #define VERIFY_NAME(Node) EXPECT_EQ(#Node, DNT<Node>().asStringRef());
112 VERIFY_NAME(TemplateArgument);
113 VERIFY_NAME(NestedNameSpecifierLoc);
114 VERIFY_NAME(QualType);
115 VERIFY_NAME(TypeLoc);
116 VERIFY_NAME(CXXCtorInitializer);
117 VERIFY_NAME(NestedNameSpecifier);
118 VERIFY_NAME(Decl);
119 VERIFY_NAME(CXXRecordDecl);
120 VERIFY_NAME(Stmt);
121 VERIFY_NAME(CallExpr);
122 VERIFY_NAME(Type);
123 VERIFY_NAME(ConstantArrayType);
124 #undef VERIFY_NAME
125 }
126
TEST(DynTypedNode,DeclSourceRange)127 TEST(DynTypedNode, DeclSourceRange) {
128 RangeVerifier<DynTypedNode> Verifier;
129 Verifier.expectRange(1, 1, 1, 11);
130 EXPECT_TRUE(Verifier.match("void f() {}", decl()));
131 }
132
TEST(DynTypedNode,StmtSourceRange)133 TEST(DynTypedNode, StmtSourceRange) {
134 RangeVerifier<DynTypedNode> Verifier;
135 Verifier.expectRange(1, 10, 1, 11);
136 EXPECT_TRUE(Verifier.match("void f() {}", stmt()));
137 }
138
TEST(DynTypedNode,TypeLocSourceRange)139 TEST(DynTypedNode, TypeLocSourceRange) {
140 RangeVerifier<DynTypedNode> Verifier;
141 Verifier.expectRange(1, 1, 1, 8);
142 EXPECT_TRUE(Verifier.match("void f() {}", typeLoc(loc(functionType()))));
143 }
144
TEST(DynTypedNode,NNSLocSourceRange)145 TEST(DynTypedNode, NNSLocSourceRange) {
146 RangeVerifier<DynTypedNode> Verifier;
147 Verifier.expectRange(1, 33, 1, 34);
148 EXPECT_TRUE(Verifier.match("namespace N { typedef void T; } N::T f() {}",
149 nestedNameSpecifierLoc()));
150 }
151
TEST(DynTypedNode,DeclDump)152 TEST(DynTypedNode, DeclDump) {
153 DumpVerifier Verifier;
154 Verifier.expectSubstring("FunctionDecl");
155 EXPECT_TRUE(Verifier.match("void f() {}", functionDecl()));
156 }
157
TEST(DynTypedNode,StmtDump)158 TEST(DynTypedNode, StmtDump) {
159 DumpVerifier Verifier;
160 Verifier.expectSubstring("CompoundStmt");
161 EXPECT_TRUE(Verifier.match("void f() {}", stmt()));
162 }
163
TEST(DynTypedNode,DeclPrint)164 TEST(DynTypedNode, DeclPrint) {
165 PrintVerifier Verifier;
166 Verifier.expectString("void f() {\n}\n\n");
167 EXPECT_TRUE(Verifier.match("void f() {}", functionDecl()));
168 }
169
TEST(DynTypedNode,StmtPrint)170 TEST(DynTypedNode, StmtPrint) {
171 PrintVerifier Verifier;
172 Verifier.expectString("{\n}\n");
173 EXPECT_TRUE(Verifier.match("void f() {}", stmt()));
174 }
175
TEST(DynTypedNode,QualType)176 TEST(DynTypedNode, QualType) {
177 QualType Q;
178 DynTypedNode Node = DynTypedNode::create(Q);
179 EXPECT_TRUE(Node == Node);
180 EXPECT_FALSE(Node < Node);
181 }
182
183 } // namespace ast_type_traits
184 } // namespace clang
185