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("Decl", DNT<Decl>().asStringRef());
111 EXPECT_EQ("CallExpr", DNT<CallExpr>().asStringRef());
112 EXPECT_EQ("ConstantArrayType", DNT<ConstantArrayType>().asStringRef());
113 EXPECT_EQ("<None>", ASTNodeKind().asStringRef());
114 }
115
TEST(DynTypedNode,DeclSourceRange)116 TEST(DynTypedNode, DeclSourceRange) {
117 RangeVerifier<DynTypedNode> Verifier;
118 Verifier.expectRange(1, 1, 1, 11);
119 EXPECT_TRUE(Verifier.match("void f() {}", decl()));
120 }
121
TEST(DynTypedNode,StmtSourceRange)122 TEST(DynTypedNode, StmtSourceRange) {
123 RangeVerifier<DynTypedNode> Verifier;
124 Verifier.expectRange(1, 10, 1, 11);
125 EXPECT_TRUE(Verifier.match("void f() {}", stmt()));
126 }
127
TEST(DynTypedNode,TypeLocSourceRange)128 TEST(DynTypedNode, TypeLocSourceRange) {
129 RangeVerifier<DynTypedNode> Verifier;
130 Verifier.expectRange(1, 1, 1, 8);
131 EXPECT_TRUE(Verifier.match("void f() {}", typeLoc(loc(functionType()))));
132 }
133
TEST(DynTypedNode,NNSLocSourceRange)134 TEST(DynTypedNode, NNSLocSourceRange) {
135 RangeVerifier<DynTypedNode> Verifier;
136 Verifier.expectRange(1, 33, 1, 34);
137 EXPECT_TRUE(Verifier.match("namespace N { typedef void T; } N::T f() {}",
138 nestedNameSpecifierLoc()));
139 }
140
TEST(DynTypedNode,DeclDump)141 TEST(DynTypedNode, DeclDump) {
142 DumpVerifier Verifier;
143 Verifier.expectSubstring("FunctionDecl");
144 EXPECT_TRUE(Verifier.match("void f() {}", functionDecl()));
145 }
146
TEST(DynTypedNode,StmtDump)147 TEST(DynTypedNode, StmtDump) {
148 DumpVerifier Verifier;
149 Verifier.expectSubstring("CompoundStmt");
150 EXPECT_TRUE(Verifier.match("void f() {}", stmt()));
151 }
152
TEST(DynTypedNode,DeclPrint)153 TEST(DynTypedNode, DeclPrint) {
154 PrintVerifier Verifier;
155 Verifier.expectString("void f() {\n}\n\n");
156 EXPECT_TRUE(Verifier.match("void f() {}", functionDecl()));
157 }
158
TEST(DynTypedNode,StmtPrint)159 TEST(DynTypedNode, StmtPrint) {
160 PrintVerifier Verifier;
161 Verifier.expectString("{\n}\n");
162 EXPECT_TRUE(Verifier.match("void f() {}", stmt()));
163 }
164
TEST(DynTypedNode,QualType)165 TEST(DynTypedNode, QualType) {
166 QualType Q;
167 DynTypedNode Node = DynTypedNode::create(Q);
168 EXPECT_TRUE(Node == Node);
169 EXPECT_FALSE(Node < Node);
170 }
171
172 } // namespace ast_type_traits
173 } // namespace clang
174