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 "MatchVerifier.h"
21#include "clang/ASTMatchers/ASTMatchFinder.h"
22#include "clang/ASTMatchers/ASTMatchers.h"
23#include "clang/Tooling/Tooling.h"
24#include "gtest/gtest.h"
25
26namespace clang {
27namespace ast_matchers {
28
29// FIXME: Pull the *Verifier tests into their own test file.
30
31TEST(MatchVerifier, ParseError) {
32  LocationVerifier<VarDecl> Verifier;
33  Verifier.expectLocation(1, 1);
34  EXPECT_FALSE(Verifier.match("int i", varDecl()));
35}
36
37TEST(MatchVerifier, NoMatch) {
38  LocationVerifier<VarDecl> Verifier;
39  Verifier.expectLocation(1, 1);
40  EXPECT_FALSE(Verifier.match("int i;", recordDecl()));
41}
42
43TEST(MatchVerifier, WrongType) {
44  LocationVerifier<RecordDecl> Verifier;
45  Verifier.expectLocation(1, 1);
46  EXPECT_FALSE(Verifier.match("int i;", varDecl()));
47}
48
49TEST(LocationVerifier, WrongLocation) {
50  LocationVerifier<VarDecl> Verifier;
51  Verifier.expectLocation(1, 1);
52  EXPECT_FALSE(Verifier.match("int i;", varDecl()));
53}
54
55TEST(RangeVerifier, WrongRange) {
56  RangeVerifier<VarDecl> Verifier;
57  Verifier.expectRange(1, 1, 1, 1);
58  EXPECT_FALSE(Verifier.match("int i;", varDecl()));
59}
60
61class LabelDeclRangeVerifier : public RangeVerifier<LabelStmt> {
62protected:
63  SourceRange getRange(const LabelStmt &Node) override {
64    return Node.getDecl()->getSourceRange();
65  }
66};
67
68TEST(LabelDecl, Range) {
69  LabelDeclRangeVerifier Verifier;
70  Verifier.expectRange(1, 12, 1, 12);
71  EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt()));
72}
73
74TEST(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
80TEST(ParmVarDecl, KNRLocation) {
81  LocationVerifier<ParmVarDecl> Verifier;
82  Verifier.expectLocation(1, 8);
83  EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C));
84}
85
86TEST(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
92TEST(CXXNewExpr, ArrayRange) {
93  RangeVerifier<CXXNewExpr> Verifier;
94  Verifier.expectRange(1, 12, 1, 22);
95  EXPECT_TRUE(Verifier.match("void f() { new int[10]; }", cxxNewExpr()));
96}
97
98TEST(CXXNewExpr, ParenRange) {
99  RangeVerifier<CXXNewExpr> Verifier;
100  Verifier.expectRange(1, 12, 1, 20);
101  EXPECT_TRUE(Verifier.match("void f() { new int(); }", cxxNewExpr()));
102}
103
104TEST(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
112class MemberExprArrowLocVerifier : public RangeVerifier<MemberExpr> {
113protected:
114  SourceRange getRange(const MemberExpr &Node) override {
115     return Node.getOperatorLoc();
116  }
117};
118
119TEST(MemberExpr, ArrowRange) {
120  MemberExprArrowLocVerifier Verifier;
121  Verifier.expectRange(2, 19, 2, 19);
122  EXPECT_TRUE(Verifier.match("struct S { int x; };\n"
123                             "void foo(S *s) { s->x = 0; }",
124                             memberExpr()));
125}
126
127TEST(MemberExpr, MacroArrowRange) {
128  MemberExprArrowLocVerifier Verifier;
129  Verifier.expectRange(1, 24, 1, 24);
130  EXPECT_TRUE(Verifier.match("#define MEMBER(a, b) (a->b)\n"
131                             "struct S { int x; };\n"
132                             "void foo(S *s) { MEMBER(s, x) = 0; }",
133                             memberExpr()));
134}
135
136TEST(MemberExpr, ImplicitArrowRange) {
137  MemberExprArrowLocVerifier Verifier;
138  Verifier.expectRange(0, 0, 0, 0);
139  EXPECT_TRUE(Verifier.match("struct S { int x; void Test(); };\n"
140                             "void S::Test() { x = 1; }",
141                             memberExpr()));
142}
143
144TEST(VarDecl, VMTypeFixedVarDeclRange) {
145  RangeVerifier<VarDecl> Verifier;
146  Verifier.expectRange(1, 1, 1, 23);
147  EXPECT_TRUE(Verifier.match("int a[(int)(void*)1234];",
148                             varDecl(), Lang_C89));
149}
150
151TEST(CXXConstructorDecl, NoRetFunTypeLocRange) {
152  RangeVerifier<CXXConstructorDecl> Verifier;
153  Verifier.expectRange(1, 11, 1, 13);
154  EXPECT_TRUE(Verifier.match("class C { C(); };", functionDecl()));
155}
156
157TEST(CXXConstructorDecl, DefaultedCtorLocRange) {
158  RangeVerifier<CXXConstructorDecl> Verifier;
159  Verifier.expectRange(1, 11, 1, 23);
160  EXPECT_TRUE(Verifier.match("class C { C() = default; };", functionDecl()));
161}
162
163TEST(CXXConstructorDecl, DeletedCtorLocRange) {
164  RangeVerifier<CXXConstructorDecl> Verifier;
165  Verifier.expectRange(1, 11, 1, 22);
166  EXPECT_TRUE(Verifier.match("class C { C() = delete; };", functionDecl()));
167}
168
169TEST(CompoundLiteralExpr, CompoundVectorLiteralRange) {
170  RangeVerifier<CompoundLiteralExpr> Verifier;
171  Verifier.expectRange(2, 11, 2, 22);
172  EXPECT_TRUE(Verifier.match(
173                  "typedef int int2 __attribute__((ext_vector_type(2)));\n"
174                  "int2 i2 = (int2){1, 2};", compoundLiteralExpr()));
175}
176
177TEST(CompoundLiteralExpr, ParensCompoundVectorLiteralRange) {
178  RangeVerifier<CompoundLiteralExpr> Verifier;
179  Verifier.expectRange(2, 20, 2, 31);
180  EXPECT_TRUE(Verifier.match(
181                  "typedef int int2 __attribute__((ext_vector_type(2)));\n"
182                  "constant int2 i2 = (int2)(1, 2);",
183                  compoundLiteralExpr(), Lang_OpenCL));
184}
185
186TEST(InitListExpr, VectorLiteralListBraceRange) {
187  RangeVerifier<InitListExpr> Verifier;
188  Verifier.expectRange(2, 17, 2, 22);
189  EXPECT_TRUE(Verifier.match(
190                  "typedef int int2 __attribute__((ext_vector_type(2)));\n"
191                  "int2 i2 = (int2){1, 2};", initListExpr()));
192}
193
194TEST(InitListExpr, VectorLiteralInitListParens) {
195  RangeVerifier<InitListExpr> Verifier;
196  Verifier.expectRange(2, 26, 2, 31);
197  EXPECT_TRUE(Verifier.match(
198                  "typedef int int2 __attribute__((ext_vector_type(2)));\n"
199                  "constant int2 i2 = (int2)(1, 2);", initListExpr(), Lang_OpenCL));
200}
201
202class TemplateAngleBracketLocRangeVerifier : public RangeVerifier<TypeLoc> {
203protected:
204  SourceRange getRange(const TypeLoc &Node) override {
205    TemplateSpecializationTypeLoc T =
206        Node.getUnqualifiedLoc().castAs<TemplateSpecializationTypeLoc>();
207    assert(!T.isNull());
208    return SourceRange(T.getLAngleLoc(), T.getRAngleLoc());
209  }
210};
211
212TEST(TemplateSpecializationTypeLoc, AngleBracketLocations) {
213  TemplateAngleBracketLocRangeVerifier Verifier;
214  Verifier.expectRange(2, 8, 2, 10);
215  EXPECT_TRUE(Verifier.match(
216      "template<typename T> struct A {}; struct B{}; void f(\n"
217      "const A<B>&);",
218      loc(templateSpecializationType())));
219}
220
221TEST(CXXNewExpr, TypeParenRange) {
222  RangeVerifier<CXXNewExpr> Verifier;
223  Verifier.expectRange(1, 10, 1, 18);
224  EXPECT_TRUE(Verifier.match("int* a = new (int);", cxxNewExpr()));
225}
226
227class UnaryTransformTypeLocParensRangeVerifier : public RangeVerifier<TypeLoc> {
228protected:
229  SourceRange getRange(const TypeLoc &Node) override {
230    UnaryTransformTypeLoc T =
231        Node.getUnqualifiedLoc().castAs<UnaryTransformTypeLoc>();
232    assert(!T.isNull());
233    return SourceRange(T.getLParenLoc(), T.getRParenLoc());
234  }
235};
236
237TEST(UnaryTransformTypeLoc, ParensRange) {
238  UnaryTransformTypeLocParensRangeVerifier Verifier;
239  Verifier.expectRange(3, 26, 3, 28);
240  EXPECT_TRUE(Verifier.match(
241      "template <typename T>\n"
242      "struct S {\n"
243      "typedef __underlying_type(T) type;\n"
244      "};",
245      loc(unaryTransformType())));
246}
247
248TEST(CXXFunctionalCastExpr, SourceRange) {
249  RangeVerifier<CXXFunctionalCastExpr> Verifier;
250  Verifier.expectRange(2, 10, 2, 14);
251  EXPECT_TRUE(Verifier.match(
252      "int foo() {\n"
253      "  return int{};\n"
254      "}",
255      cxxFunctionalCastExpr(), Lang_CXX11));
256}
257
258TEST(CXXConstructExpr, SourceRange) {
259  RangeVerifier<CXXConstructExpr> Verifier;
260  Verifier.expectRange(3, 14, 3, 19);
261  EXPECT_TRUE(Verifier.match(
262      "struct A { A(int, int); };\n"
263      "void f(A a);\n"
264      "void g() { f({0, 0}); }",
265      cxxConstructExpr(), Lang_CXX11));
266}
267
268TEST(CXXTemporaryObjectExpr, SourceRange) {
269  RangeVerifier<CXXTemporaryObjectExpr> Verifier;
270  Verifier.expectRange(2, 6, 2, 12);
271  EXPECT_TRUE(Verifier.match(
272      "struct A { A(int, int); };\n"
273      "A a( A{0, 0} );",
274      cxxTemporaryObjectExpr(), Lang_CXX11));
275}
276
277TEST(CXXUnresolvedConstructExpr, SourceRange) {
278  RangeVerifier<CXXUnresolvedConstructExpr> Verifier;
279  Verifier.expectRange(3, 10, 3, 12);
280  std::vector<std::string> Args;
281  Args.push_back("-fno-delayed-template-parsing");
282  EXPECT_TRUE(Verifier.match(
283      "template <typename U>\n"
284      "U foo() {\n"
285      "  return U{};\n"
286      "}",
287      cxxUnresolvedConstructExpr(), Args, Lang_CXX11));
288}
289
290TEST(UsingDecl, SourceRange) {
291  RangeVerifier<UsingDecl> Verifier;
292  Verifier.expectRange(2, 22, 2, 25);
293  EXPECT_TRUE(Verifier.match(
294      "class B { protected: int i; };\n"
295      "class D : public B { B::i; };",
296      usingDecl()));
297}
298
299TEST(UnresolvedUsingValueDecl, SourceRange) {
300  RangeVerifier<UnresolvedUsingValueDecl> Verifier;
301  Verifier.expectRange(3, 3, 3, 6);
302  EXPECT_TRUE(Verifier.match(
303      "template <typename B>\n"
304      "class D : public B {\n"
305      "  B::i;\n"
306      "};",
307      unresolvedUsingValueDecl()));
308}
309
310TEST(FriendDecl, FriendNonMemberFunctionLocation) {
311  LocationVerifier<FriendDecl> Verifier;
312  Verifier.expectLocation(2, 13);
313  EXPECT_TRUE(Verifier.match("struct A {\n"
314                             "friend void f();\n"
315                             "};\n",
316                             friendDecl()));
317}
318
319TEST(FriendDecl, FriendNonMemberFunctionRange) {
320  RangeVerifier<FriendDecl> Verifier;
321  Verifier.expectRange(2, 1, 2, 15);
322  EXPECT_TRUE(Verifier.match("struct A {\n"
323                             "friend void f();\n"
324                             "};\n",
325                             friendDecl()));
326}
327
328TEST(FriendDecl, FriendNonMemberFunctionDefinitionLocation) {
329  LocationVerifier<FriendDecl> Verifier;
330  Verifier.expectLocation(2, 12);
331  EXPECT_TRUE(Verifier.match("struct A {\n"
332                             "friend int f() { return 0; }\n"
333                             "};\n",
334                             friendDecl()));
335}
336
337TEST(FriendDecl, FriendNonMemberFunctionDefinitionRange) {
338  RangeVerifier<FriendDecl> Verifier;
339  Verifier.expectRange(2, 1, 2, 28);
340  EXPECT_TRUE(Verifier.match("struct A {\n"
341                             "friend int f() { return 0; }\n"
342                             "};\n",
343                             friendDecl()));
344}
345
346TEST(FriendDecl, FriendElaboratedTypeLocation) {
347  LocationVerifier<FriendDecl> Verifier;
348  Verifier.expectLocation(2, 8);
349  EXPECT_TRUE(Verifier.match("struct A {\n"
350                             "friend class B;\n"
351                             "};\n",
352                             friendDecl()));
353}
354
355TEST(FriendDecl, FriendElaboratedTypeRange) {
356  RangeVerifier<FriendDecl> Verifier;
357  Verifier.expectRange(2, 1, 2, 14);
358  EXPECT_TRUE(Verifier.match("struct A {\n"
359                             "friend class B;\n"
360                             "};\n",
361                             friendDecl()));
362}
363
364TEST(FriendDecl, FriendSimpleTypeLocation) {
365  LocationVerifier<FriendDecl> Verifier;
366  Verifier.expectLocation(3, 8);
367  EXPECT_TRUE(Verifier.match("class B;\n"
368                             "struct A {\n"
369                             "friend B;\n"
370                             "};\n",
371                             friendDecl(), Lang_CXX11));
372}
373
374TEST(FriendDecl, FriendSimpleTypeRange) {
375  RangeVerifier<FriendDecl> Verifier;
376  Verifier.expectRange(3, 1, 3, 8);
377  EXPECT_TRUE(Verifier.match("class B;\n"
378                             "struct A {\n"
379                             "friend B;\n"
380                             "};\n",
381                             friendDecl(), Lang_CXX11));
382}
383
384TEST(FriendDecl, FriendTemplateParameterLocation) {
385  LocationVerifier<FriendDecl> Verifier;
386  Verifier.expectLocation(3, 8);
387  EXPECT_TRUE(Verifier.match("template <typename T>\n"
388                             "struct A {\n"
389                             "friend T;\n"
390                             "};\n",
391                             friendDecl(), Lang_CXX11));
392}
393
394TEST(FriendDecl, FriendTemplateParameterRange) {
395  RangeVerifier<FriendDecl> Verifier;
396  Verifier.expectRange(3, 1, 3, 8);
397  EXPECT_TRUE(Verifier.match("template <typename T>\n"
398                             "struct A {\n"
399                             "friend T;\n"
400                             "};\n",
401                             friendDecl(), Lang_CXX11));
402}
403
404TEST(FriendDecl, FriendDecltypeLocation) {
405  LocationVerifier<FriendDecl> Verifier;
406  Verifier.expectLocation(4, 8);
407  EXPECT_TRUE(Verifier.match("struct A;\n"
408                             "A foo();\n"
409                             "struct A {\n"
410                             "friend decltype(foo());\n"
411                             "};\n",
412                             friendDecl(), Lang_CXX11));
413}
414
415TEST(FriendDecl, FriendDecltypeRange) {
416  RangeVerifier<FriendDecl> Verifier;
417  Verifier.expectRange(4, 1, 4, 8);
418  EXPECT_TRUE(Verifier.match("struct A;\n"
419                             "A foo();\n"
420                             "struct A {\n"
421                             "friend decltype(foo());\n"
422                             "};\n",
423                             friendDecl(), Lang_CXX11));
424}
425
426TEST(FriendDecl, FriendConstructorDestructorLocation) {
427  const std::string Code = "struct B {\n"
428                           "B();\n"
429                           "~B();\n"
430                           "};\n"
431                           "struct A {\n"
432                           "friend B::B(), B::~B();\n"
433                           "};\n";
434  LocationVerifier<FriendDecl> ConstructorVerifier;
435  ConstructorVerifier.expectLocation(6, 11);
436  EXPECT_TRUE(ConstructorVerifier.match(
437      Code, friendDecl(has(cxxConstructorDecl(ofClass(hasName("B")))))));
438  LocationVerifier<FriendDecl> DestructorVerifier;
439  DestructorVerifier.expectLocation(6, 19);
440  EXPECT_TRUE(DestructorVerifier.match(
441      Code, friendDecl(has(cxxDestructorDecl(ofClass(hasName("B")))))));
442}
443
444TEST(FriendDecl, FriendConstructorDestructorRange) {
445  const std::string Code = "struct B {\n"
446                           "B();\n"
447                           "~B();\n"
448                           "};\n"
449                           "struct A {\n"
450                           "friend B::B(), B::~B();\n"
451                           "};\n";
452  RangeVerifier<FriendDecl> ConstructorVerifier;
453  ConstructorVerifier.expectRange(6, 1, 6, 13);
454  EXPECT_TRUE(ConstructorVerifier.match(
455      Code, friendDecl(has(cxxConstructorDecl(ofClass(hasName("B")))))));
456  RangeVerifier<FriendDecl> DestructorVerifier;
457  DestructorVerifier.expectRange(6, 1, 6, 22);
458  EXPECT_TRUE(DestructorVerifier.match(
459      Code, friendDecl(has(cxxDestructorDecl(ofClass(hasName("B")))))));
460}
461
462TEST(FriendDecl, FriendTemplateFunctionLocation) {
463  LocationVerifier<FriendDecl> Verifier;
464  Verifier.expectLocation(3, 13);
465  EXPECT_TRUE(Verifier.match("struct A {\n"
466                             "template <typename T>\n"
467                             "friend void f();\n"
468                             "};\n",
469                             friendDecl()));
470}
471
472TEST(FriendDecl, FriendTemplateFunctionRange) {
473  RangeVerifier<FriendDecl> Verifier;
474  Verifier.expectRange(2, 1, 3, 15);
475  EXPECT_TRUE(Verifier.match("struct A {\n"
476                             "template <typename T>\n"
477                             "friend void f();\n"
478                             "};\n",
479                             friendDecl()));
480}
481
482TEST(FriendDecl, FriendTemplateClassLocation) {
483  LocationVerifier<FriendDecl> Verifier;
484  Verifier.expectLocation(3, 14);
485  EXPECT_TRUE(Verifier.match("struct A {\n"
486                             "template <typename T>\n"
487                             "friend class B;\n"
488                             "};\n",
489                             friendDecl()));
490}
491
492TEST(FriendDecl, FriendTemplateClassRange) {
493  RangeVerifier<FriendDecl> Verifier;
494  Verifier.expectRange(2, 1, 3, 14);
495  EXPECT_TRUE(Verifier.match("struct A {\n"
496                             "template <typename T>\n"
497                             "friend class B;\n"
498                             "};\n",
499                             friendDecl()));
500}
501
502TEST(FriendDecl, FriendInlineFunctionLocation) {
503  LocationVerifier<FriendDecl> Verifier;
504  Verifier.expectLocation(2, 19);
505  EXPECT_TRUE(Verifier.match("struct A {\n"
506                             "int inline friend f() { return 0; }"
507                             "};\n",
508                             friendDecl()));
509}
510
511TEST(FriendDecl, FriendInlineFunctionRange) {
512  RangeVerifier<FriendDecl> Verifier;
513  Verifier.expectRange(2, 1, 2, 35);
514  EXPECT_TRUE(Verifier.match("struct A {\n"
515                             "int inline friend f() { return 0; }"
516                             "};\n",
517                             friendDecl(), Lang_CXX11));
518}
519
520TEST(FriendDecl, InstantiationSourceRange) {
521  RangeVerifier<FriendDecl> Verifier;
522  Verifier.expectRange(4, 3, 4, 35);
523  EXPECT_TRUE(Verifier.match(
524      "template <typename T> class S;\n"
525      "template<class T> void operator+(S<T> x);\n"
526      "template<class T> struct S {\n"
527      "  friend void operator+<>(S<T> src);\n"
528      "};\n"
529      "void test(S<double> s) { +s; }",
530      friendDecl(hasParent(cxxRecordDecl(isTemplateInstantiation())))));
531}
532
533TEST(ObjCMessageExpr, CXXConstructExprRange) {
534  RangeVerifier<CXXConstructExpr> Verifier;
535  Verifier.expectRange(5, 25, 5, 27);
536  EXPECT_TRUE(Verifier.match(
537      "struct A { int a; };\n"
538      "@interface B {}\n"
539      "+ (void) f1: (A)arg;\n"
540      "@end\n"
541      "void f2() { A a; [B f1: (a)]; }\n",
542      cxxConstructExpr(), Lang_OBJCXX));
543}
544
545} // end namespace ast_matchers
546} // end namespace clang
547