1 //===- unittest/AST/SourceLocationTest.cpp - AST source loc 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 // This file contains tests for SourceLocation and SourceRange fields
11 // in AST nodes.
12 //
13 // FIXME: In the long-term, when we test more than source locations, we may
14 // want to have a unit test file for an AST node (or group of related nodes),
15 // rather than a unit test file for source locations for all AST nodes.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "clang/AST/ASTContext.h"
20 #include "clang/ASTMatchers/ASTMatchFinder.h"
21 #include "clang/ASTMatchers/ASTMatchers.h"
22 #include "clang/Tooling/Tooling.h"
23 #include "gtest/gtest.h"
24 #include "MatchVerifier.h"
25
26 namespace clang {
27 namespace ast_matchers {
28
29 // FIXME: Pull the *Verifier tests into their own test file.
30
TEST(MatchVerifier,ParseError)31 TEST(MatchVerifier, ParseError) {
32 LocationVerifier<VarDecl> Verifier;
33 Verifier.expectLocation(1, 1);
34 EXPECT_FALSE(Verifier.match("int i", varDecl()));
35 }
36
TEST(MatchVerifier,NoMatch)37 TEST(MatchVerifier, NoMatch) {
38 LocationVerifier<VarDecl> Verifier;
39 Verifier.expectLocation(1, 1);
40 EXPECT_FALSE(Verifier.match("int i;", recordDecl()));
41 }
42
TEST(MatchVerifier,WrongType)43 TEST(MatchVerifier, WrongType) {
44 LocationVerifier<RecordDecl> Verifier;
45 Verifier.expectLocation(1, 1);
46 EXPECT_FALSE(Verifier.match("int i;", varDecl()));
47 }
48
TEST(LocationVerifier,WrongLocation)49 TEST(LocationVerifier, WrongLocation) {
50 LocationVerifier<VarDecl> Verifier;
51 Verifier.expectLocation(1, 1);
52 EXPECT_FALSE(Verifier.match("int i;", varDecl()));
53 }
54
TEST(RangeVerifier,WrongRange)55 TEST(RangeVerifier, WrongRange) {
56 RangeVerifier<VarDecl> Verifier;
57 Verifier.expectRange(1, 1, 1, 1);
58 EXPECT_FALSE(Verifier.match("int i;", varDecl()));
59 }
60
61 class LabelDeclRangeVerifier : public RangeVerifier<LabelStmt> {
62 protected:
getRange(const LabelStmt & Node)63 virtual SourceRange getRange(const LabelStmt &Node) {
64 return Node.getDecl()->getSourceRange();
65 }
66 };
67
TEST(LabelDecl,Range)68 TEST(LabelDecl, Range) {
69 LabelDeclRangeVerifier Verifier;
70 Verifier.expectRange(1, 12, 1, 12);
71 EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt()));
72 }
73
TEST(LabelStmt,Range)74 TEST(LabelStmt, Range) {
75 RangeVerifier<LabelStmt> Verifier;
76 Verifier.expectRange(1, 12, 1, 15);
77 EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt()));
78 }
79
TEST(ParmVarDecl,KNRLocation)80 TEST(ParmVarDecl, KNRLocation) {
81 LocationVerifier<ParmVarDecl> Verifier;
82 Verifier.expectLocation(1, 8);
83 EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C));
84 }
85
TEST(ParmVarDecl,KNRRange)86 TEST(ParmVarDecl, KNRRange) {
87 RangeVerifier<ParmVarDecl> Verifier;
88 Verifier.expectRange(1, 8, 1, 8);
89 EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C));
90 }
91
TEST(CXXNewExpr,ArrayRange)92 TEST(CXXNewExpr, ArrayRange) {
93 RangeVerifier<CXXNewExpr> Verifier;
94 Verifier.expectRange(1, 12, 1, 22);
95 EXPECT_TRUE(Verifier.match("void f() { new int[10]; }", newExpr()));
96 }
97
TEST(CXXNewExpr,ParenRange)98 TEST(CXXNewExpr, ParenRange) {
99 RangeVerifier<CXXNewExpr> Verifier;
100 Verifier.expectRange(1, 12, 1, 20);
101 EXPECT_TRUE(Verifier.match("void f() { new int(); }", newExpr()));
102 }
103
TEST(MemberExpr,ImplicitMemberRange)104 TEST(MemberExpr, ImplicitMemberRange) {
105 RangeVerifier<MemberExpr> Verifier;
106 Verifier.expectRange(2, 30, 2, 30);
107 EXPECT_TRUE(Verifier.match("struct S { operator int() const; };\n"
108 "int foo(const S& s) { return s; }",
109 memberExpr()));
110 }
111
TEST(VarDecl,VMTypeFixedVarDeclRange)112 TEST(VarDecl, VMTypeFixedVarDeclRange) {
113 RangeVerifier<VarDecl> Verifier;
114 Verifier.expectRange(1, 1, 1, 23);
115 EXPECT_TRUE(Verifier.match("int a[(int)(void*)1234];",
116 varDecl(), Lang_C89));
117 }
118
TEST(CXXConstructorDecl,NoRetFunTypeLocRange)119 TEST(CXXConstructorDecl, NoRetFunTypeLocRange) {
120 RangeVerifier<CXXConstructorDecl> Verifier;
121 Verifier.expectRange(1, 11, 1, 13);
122 EXPECT_TRUE(Verifier.match("class C { C(); };", functionDecl()));
123 }
124
TEST(CompoundLiteralExpr,CompoundVectorLiteralRange)125 TEST(CompoundLiteralExpr, CompoundVectorLiteralRange) {
126 RangeVerifier<CompoundLiteralExpr> Verifier;
127 Verifier.expectRange(2, 11, 2, 22);
128 EXPECT_TRUE(Verifier.match(
129 "typedef int int2 __attribute__((ext_vector_type(2)));\n"
130 "int2 i2 = (int2){1, 2};", compoundLiteralExpr()));
131 }
132
TEST(CompoundLiteralExpr,ParensCompoundVectorLiteralRange)133 TEST(CompoundLiteralExpr, ParensCompoundVectorLiteralRange) {
134 RangeVerifier<CompoundLiteralExpr> Verifier;
135 Verifier.expectRange(2, 20, 2, 31);
136 EXPECT_TRUE(Verifier.match(
137 "typedef int int2 __attribute__((ext_vector_type(2)));\n"
138 "constant int2 i2 = (int2)(1, 2);",
139 compoundLiteralExpr(), Lang_OpenCL));
140 }
141
TEST(InitListExpr,VectorLiteralListBraceRange)142 TEST(InitListExpr, VectorLiteralListBraceRange) {
143 RangeVerifier<InitListExpr> Verifier;
144 Verifier.expectRange(2, 17, 2, 22);
145 EXPECT_TRUE(Verifier.match(
146 "typedef int int2 __attribute__((ext_vector_type(2)));\n"
147 "int2 i2 = (int2){1, 2};", initListExpr()));
148 }
149
TEST(InitListExpr,VectorLiteralInitListParens)150 TEST(InitListExpr, VectorLiteralInitListParens) {
151 RangeVerifier<InitListExpr> Verifier;
152 Verifier.expectRange(2, 26, 2, 31);
153 EXPECT_TRUE(Verifier.match(
154 "typedef int int2 __attribute__((ext_vector_type(2)));\n"
155 "constant int2 i2 = (int2)(1, 2);", initListExpr(), Lang_OpenCL));
156 }
157
158 class TemplateAngleBracketLocRangeVerifier : public RangeVerifier<TypeLoc> {
159 protected:
getRange(const TypeLoc & Node)160 virtual SourceRange getRange(const TypeLoc &Node) {
161 TemplateSpecializationTypeLoc T =
162 Node.getUnqualifiedLoc().castAs<TemplateSpecializationTypeLoc>();
163 assert(!T.isNull());
164 return SourceRange(T.getLAngleLoc(), T.getRAngleLoc());
165 }
166 };
167
TEST(TemplateSpecializationTypeLoc,AngleBracketLocations)168 TEST(TemplateSpecializationTypeLoc, AngleBracketLocations) {
169 TemplateAngleBracketLocRangeVerifier Verifier;
170 Verifier.expectRange(2, 8, 2, 10);
171 EXPECT_TRUE(Verifier.match(
172 "template<typename T> struct A {}; struct B{}; void f(\n"
173 "const A<B>&);",
174 loc(templateSpecializationType())));
175 }
176
TEST(CXXNewExpr,TypeParenRange)177 TEST(CXXNewExpr, TypeParenRange) {
178 RangeVerifier<CXXNewExpr> Verifier;
179 Verifier.expectRange(1, 10, 1, 18);
180 EXPECT_TRUE(Verifier.match("int* a = new (int);", newExpr()));
181 }
182
183 class UnaryTransformTypeLocParensRangeVerifier : public RangeVerifier<TypeLoc> {
184 protected:
getRange(const TypeLoc & Node)185 virtual SourceRange getRange(const TypeLoc &Node) {
186 UnaryTransformTypeLoc T =
187 Node.getUnqualifiedLoc().castAs<UnaryTransformTypeLoc>();
188 assert(!T.isNull());
189 return SourceRange(T.getLParenLoc(), T.getRParenLoc());
190 }
191 };
192
TEST(UnaryTransformTypeLoc,ParensRange)193 TEST(UnaryTransformTypeLoc, ParensRange) {
194 UnaryTransformTypeLocParensRangeVerifier Verifier;
195 Verifier.expectRange(3, 26, 3, 28);
196 EXPECT_TRUE(Verifier.match(
197 "template <typename T>\n"
198 "struct S {\n"
199 "typedef __underlying_type(T) type;\n"
200 "};",
201 loc(unaryTransformType())));
202 }
203
TEST(CXXFunctionalCastExpr,SourceRange)204 TEST(CXXFunctionalCastExpr, SourceRange) {
205 RangeVerifier<CXXFunctionalCastExpr> Verifier;
206 Verifier.expectRange(2, 10, 2, 14);
207 EXPECT_TRUE(Verifier.match(
208 "int foo() {\n"
209 " return int{};\n"
210 "}",
211 functionalCastExpr(), Lang_CXX11));
212 }
213
TEST(CXXUnresolvedConstructExpr,SourceRange)214 TEST(CXXUnresolvedConstructExpr, SourceRange) {
215 RangeVerifier<CXXUnresolvedConstructExpr> Verifier;
216 Verifier.expectRange(3, 10, 3, 12);
217 std::vector<std::string> Args;
218 Args.push_back("-fno-delayed-template-parsing");
219 EXPECT_TRUE(Verifier.match(
220 "template <typename U>\n"
221 "U foo() {\n"
222 " return U{};\n"
223 "}",
224 unresolvedConstructExpr(), Args, Lang_CXX11));
225 }
226
TEST(UsingDecl,SourceRange)227 TEST(UsingDecl, SourceRange) {
228 RangeVerifier<UsingDecl> Verifier;
229 Verifier.expectRange(2, 22, 2, 25);
230 EXPECT_TRUE(Verifier.match(
231 "class B { protected: int i; };\n"
232 "class D : public B { B::i; };",
233 usingDecl()));
234 }
235
TEST(UnresolvedUsingValueDecl,SourceRange)236 TEST(UnresolvedUsingValueDecl, SourceRange) {
237 RangeVerifier<UnresolvedUsingValueDecl> Verifier;
238 Verifier.expectRange(3, 3, 3, 6);
239 EXPECT_TRUE(Verifier.match(
240 "template <typename B>\n"
241 "class D : public B {\n"
242 " B::i;\n"
243 "};",
244 unresolvedUsingValueDecl()));
245 }
246
TEST(FriendDecl,InstantiationSourceRange)247 TEST(FriendDecl, InstantiationSourceRange) {
248 RangeVerifier<FriendDecl> Verifier;
249 Verifier.expectRange(4, 3, 4, 35);
250 EXPECT_TRUE(Verifier.match(
251 "template <typename T> class S;\n"
252 "template<class T> void operator+(S<T> x);\n"
253 "template<class T> struct S {\n"
254 " friend void operator+<>(S<T> src);\n"
255 "};\n"
256 "void test(S<double> s) { +s; }",
257 friendDecl(hasParent(recordDecl(isTemplateInstantiation())))));
258 }
259
260 } // end namespace ast_matchers
261 } // end namespace clang
262