1 //===- unittest/AST/SourceLocationTest.cpp - AST source loc unit tests ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains tests for SourceLocation and SourceRange fields
12 // FIXME: In the long-term, when we test more than source locations, we may
13 // want to have a unit test file for an AST node (or group of related nodes),
14 // rather than a unit test file for source locations for all AST nodes.
16 //===----------------------------------------------------------------------===//
18 #include "MatchVerifier.h"
19 #include "clang/AST/ASTConcept.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/ASTFwd.h"
22 #include "clang/AST/DeclTemplate.h"
23 #include "clang/AST/ExprConcepts.h"
24 #include "clang/ASTMatchers/ASTMatchFinder.h"
25 #include "clang/ASTMatchers/ASTMatchers.h"
26 #include "clang/Tooling/Tooling.h"
27 #include "llvm/Testing/Annotations/Annotations.h"
28 #include "gtest/gtest.h"
30 using namespace clang
;
31 using namespace clang::ast_matchers
;
35 // FIXME: Pull the *Verifier tests into their own test file.
37 TEST(MatchVerifier
, ParseError
) {
38 LocationVerifier
<VarDecl
> Verifier
;
39 Verifier
.expectLocation(1, 1);
40 EXPECT_FALSE(Verifier
.match("int i", varDecl()));
43 TEST(MatchVerifier
, NoMatch
) {
44 LocationVerifier
<VarDecl
> Verifier
;
45 Verifier
.expectLocation(1, 1);
46 EXPECT_FALSE(Verifier
.match("int i;", recordDecl()));
49 TEST(MatchVerifier
, WrongType
) {
50 LocationVerifier
<RecordDecl
> Verifier
;
51 Verifier
.expectLocation(1, 1);
52 EXPECT_FALSE(Verifier
.match("int i;", varDecl()));
55 TEST(LocationVerifier
, WrongLocation
) {
56 LocationVerifier
<VarDecl
> Verifier
;
57 Verifier
.expectLocation(1, 1);
58 EXPECT_FALSE(Verifier
.match("int i;", varDecl()));
61 TEST(RangeVerifier
, WrongRange
) {
62 RangeVerifier
<VarDecl
> Verifier
;
63 Verifier
.expectRange(1, 1, 1, 1);
64 EXPECT_FALSE(Verifier
.match("int i;", varDecl()));
67 class WhileParenLocationVerifier
: public MatchVerifier
<WhileStmt
> {
68 unsigned ExpectLParenLine
= 0, ExpectLParenColumn
= 0;
69 unsigned ExpectRParenLine
= 0, ExpectRParenColumn
= 0;
72 void expectLocations(unsigned LParenLine
, unsigned LParenColumn
,
73 unsigned RParenLine
, unsigned RParenColumn
) {
74 ExpectLParenLine
= LParenLine
;
75 ExpectLParenColumn
= LParenColumn
;
76 ExpectRParenLine
= RParenLine
;
77 ExpectRParenColumn
= RParenColumn
;
81 void verify(const MatchFinder::MatchResult
&Result
,
82 const WhileStmt
&Node
) override
{
83 SourceLocation LParenLoc
= Node
.getLParenLoc();
84 SourceLocation RParenLoc
= Node
.getRParenLoc();
86 Result
.SourceManager
->getSpellingLineNumber(LParenLoc
);
87 unsigned LParenColumn
=
88 Result
.SourceManager
->getSpellingColumnNumber(LParenLoc
);
90 Result
.SourceManager
->getSpellingLineNumber(RParenLoc
);
91 unsigned RParenColumn
=
92 Result
.SourceManager
->getSpellingColumnNumber(RParenLoc
);
94 if (LParenLine
!= ExpectLParenLine
|| LParenColumn
!= ExpectLParenColumn
||
95 RParenLine
!= ExpectRParenLine
|| RParenColumn
!= ExpectRParenColumn
) {
97 llvm::raw_string_ostream
Msg(MsgStr
);
98 Msg
<< "Expected LParen Location <" << ExpectLParenLine
<< ":"
99 << ExpectLParenColumn
<< ">, found <";
100 LParenLoc
.print(Msg
, *Result
.SourceManager
);
103 Msg
<< "Expected RParen Location <" << ExpectRParenLine
<< ":"
104 << ExpectRParenColumn
<< ">, found <";
105 RParenLoc
.print(Msg
, *Result
.SourceManager
);
108 this->setFailure(Msg
.str());
113 TEST(LocationVerifier
, WhileParenLoc
) {
114 WhileParenLocationVerifier Verifier
;
115 Verifier
.expectLocations(1, 17, 1, 38);
116 EXPECT_TRUE(Verifier
.match("void f() { while(true/*some comment*/) {} }",
120 class LabelDeclRangeVerifier
: public RangeVerifier
<LabelStmt
> {
122 SourceRange
getRange(const LabelStmt
&Node
) override
{
123 return Node
.getDecl()->getSourceRange();
127 TEST(LabelDecl
, Range
) {
128 LabelDeclRangeVerifier Verifier
;
129 Verifier
.expectRange(1, 12, 1, 12);
130 EXPECT_TRUE(Verifier
.match("void f() { l: return; }", labelStmt()));
133 TEST(LabelStmt
, Range
) {
134 RangeVerifier
<LabelStmt
> Verifier
;
135 Verifier
.expectRange(1, 12, 1, 15);
136 EXPECT_TRUE(Verifier
.match("void f() { l: return; }", labelStmt()));
139 TEST(ParmVarDecl
, KNRLocation
) {
140 LocationVerifier
<ParmVarDecl
> Verifier
;
141 Verifier
.expectLocation(1, 8);
142 EXPECT_TRUE(Verifier
.match("void f(i) {}", varDecl(), Lang_C89
));
144 Verifier
.expectLocation(1, 15);
145 EXPECT_TRUE(Verifier
.match("void f(i) int i; {}", varDecl(), Lang_C99
));
148 TEST(ParmVarDecl
, KNRRange
) {
149 RangeVerifier
<ParmVarDecl
> Verifier
;
150 Verifier
.expectRange(1, 8, 1, 8);
151 EXPECT_TRUE(Verifier
.match("void f(i) {}", varDecl(), Lang_C89
));
153 Verifier
.expectRange(1, 11, 1, 15);
154 EXPECT_TRUE(Verifier
.match("void f(i) int i; {}", varDecl(), Lang_C99
));
157 TEST(CXXNewExpr
, ArrayRange
) {
158 RangeVerifier
<CXXNewExpr
> Verifier
;
159 Verifier
.expectRange(1, 12, 1, 22);
160 EXPECT_TRUE(Verifier
.match("void f() { new int[10]; }", cxxNewExpr()));
163 TEST(CXXNewExpr
, ParenRange
) {
164 RangeVerifier
<CXXNewExpr
> Verifier
;
165 Verifier
.expectRange(1, 12, 1, 20);
166 EXPECT_TRUE(Verifier
.match("void f() { new int(); }", cxxNewExpr()));
169 TEST(MemberExpr
, ImplicitMemberRange
) {
170 RangeVerifier
<MemberExpr
> Verifier
;
171 Verifier
.expectRange(2, 30, 2, 30);
172 EXPECT_TRUE(Verifier
.match("struct S { operator int() const; };\n"
173 "int foo(const S& s) { return s; }",
177 class MemberExprArrowLocVerifier
: public RangeVerifier
<MemberExpr
> {
179 SourceRange
getRange(const MemberExpr
&Node
) override
{
180 return Node
.getOperatorLoc();
184 TEST(MemberExpr
, ArrowRange
) {
185 MemberExprArrowLocVerifier Verifier
;
186 Verifier
.expectRange(2, 19, 2, 19);
187 EXPECT_TRUE(Verifier
.match("struct S { int x; };\n"
188 "void foo(S *s) { s->x = 0; }",
192 TEST(MemberExpr
, MacroArrowRange
) {
193 MemberExprArrowLocVerifier Verifier
;
194 Verifier
.expectRange(1, 24, 1, 24);
195 EXPECT_TRUE(Verifier
.match("#define MEMBER(a, b) (a->b)\n"
196 "struct S { int x; };\n"
197 "void foo(S *s) { MEMBER(s, x) = 0; }",
201 TEST(MemberExpr
, ImplicitArrowRange
) {
202 MemberExprArrowLocVerifier Verifier
;
203 Verifier
.expectRange(0, 0, 0, 0);
204 EXPECT_TRUE(Verifier
.match("struct S { int x; void Test(); };\n"
205 "void S::Test() { x = 1; }",
209 TEST(VarDecl
, VMTypeFixedVarDeclRange
) {
210 RangeVerifier
<VarDecl
> Verifier
;
211 Verifier
.expectRange(1, 1, 1, 23);
212 EXPECT_TRUE(Verifier
.match("int a[(int)(void*)1234];",
213 varDecl(), Lang_C89
));
216 TEST(TypeLoc
, IntRange
) {
217 RangeVerifier
<TypeLoc
> Verifier
;
218 Verifier
.expectRange(1, 1, 1, 1);
219 EXPECT_TRUE(Verifier
.match("int a;", typeLoc()));
222 TEST(TypeLoc
, LongRange
) {
223 RangeVerifier
<TypeLoc
> Verifier
;
224 Verifier
.expectRange(1, 1, 1, 1);
225 EXPECT_TRUE(Verifier
.match("long a;", typeLoc()));
228 TEST(TypeLoc
, DecltypeTypeLocRange
) {
229 llvm::Annotations
Code(R
"(
230 $full1[[decltype(1)]] a;
231 struct A {struct B{};} var;
232 $full2[[decltype(var)]]::B c;
234 auto AST
= tooling::buildASTFromCodeWithArgs(Code
.code(), /*Args=*/{});
235 ASTContext
&Ctx
= AST
->getASTContext();
236 const auto &SM
= Ctx
.getSourceManager();
238 auto MatchedLocs
= clang::ast_matchers::match(
239 typeLoc(loc(decltypeType())).bind("target"), Ctx
);
240 ASSERT_EQ(MatchedLocs
.size(), 2u);
241 auto verify
= [&](SourceRange ActualRange
,
242 const llvm::Annotations::Range
&Expected
) {
243 auto ActualCharRange
=
244 Lexer::getAsCharRange(ActualRange
, SM
, Ctx
.getLangOpts());
245 EXPECT_EQ(SM
.getFileOffset(ActualCharRange
.getBegin()), Expected
.Begin
);
246 EXPECT_EQ(SM
.getFileOffset(ActualCharRange
.getEnd()), Expected
.End
);
248 const auto *Target1
= MatchedLocs
[0].getNodeAs
<DecltypeTypeLoc
>("target");
249 verify(Target1
->getSourceRange(), Code
.range("full1"));
251 const auto *Target2
= MatchedLocs
[1].getNodeAs
<DecltypeTypeLoc
>("target");
252 verify(Target2
->getSourceRange(), Code
.range("full2"));
255 TEST(TypeLoc
, AutoTypeLocRange
) {
256 RangeVerifier
<TypeLoc
> Verifier
;
257 Verifier
.expectRange(1, 1, 1, 14);
258 EXPECT_TRUE(Verifier
.match("decltype(auto) a = 1;", typeLoc(loc(autoType())),
262 R
"cpp(template <typename T> concept C = true;
265 // Should include "C auto" tokens.
266 Verifier
.expectRange(2, 1, 2, 3); // token range.
267 EXPECT_TRUE(Verifier
.match(Code
, typeLoc(loc(autoType())), Lang_CXX20
));
270 TEST(TypeLoc
, LongDoubleRange
) {
271 RangeVerifier
<TypeLoc
> Verifier
;
272 Verifier
.expectRange(1, 1, 1, 6);
273 EXPECT_TRUE(Verifier
.match("long double a;", typeLoc()));
276 TEST(TypeLoc
, DoubleLongRange
) {
277 RangeVerifier
<TypeLoc
> Verifier
;
278 Verifier
.expectRange(1, 1, 1, 8);
279 EXPECT_TRUE(Verifier
.match("double long a;", typeLoc()));
282 TEST(TypeLoc
, LongIntRange
) {
283 RangeVerifier
<TypeLoc
> Verifier
;
284 Verifier
.expectRange(1, 1, 1, 6);
285 EXPECT_TRUE(Verifier
.match("long int a;", typeLoc()));
288 TEST(TypeLoc
, IntLongRange
) {
289 RangeVerifier
<TypeLoc
> Verifier
;
290 Verifier
.expectRange(1, 1, 1, 5);
291 EXPECT_TRUE(Verifier
.match("int long a;", typeLoc()));
294 TEST(TypeLoc
, UnsignedIntRange
) {
295 RangeVerifier
<TypeLoc
> Verifier
;
296 Verifier
.expectRange(1, 1, 1, 10);
297 EXPECT_TRUE(Verifier
.match("unsigned int a;", typeLoc()));
300 TEST(TypeLoc
, IntUnsignedRange
) {
301 RangeVerifier
<TypeLoc
> Verifier
;
302 Verifier
.expectRange(1, 1, 1, 5);
303 EXPECT_TRUE(Verifier
.match("int unsigned a;", typeLoc()));
306 TEST(TypeLoc
, LongLongRange
) {
307 RangeVerifier
<TypeLoc
> Verifier
;
308 Verifier
.expectRange(1, 1, 1, 6);
309 EXPECT_TRUE(Verifier
.match("long long a;", typeLoc()));
312 TEST(TypeLoc
, UnsignedLongLongRange
) {
313 RangeVerifier
<TypeLoc
> Verifier
;
314 Verifier
.expectRange(1, 1, 1, 15);
315 EXPECT_TRUE(Verifier
.match("unsigned long long a;", typeLoc()));
318 TEST(TypeLoc
, LongUnsignedLongRange
) {
319 RangeVerifier
<TypeLoc
> Verifier
;
320 Verifier
.expectRange(1, 1, 1, 15);
321 EXPECT_TRUE(Verifier
.match("long unsigned long a;", typeLoc()));
324 TEST(TypeLoc
, LongLongUnsignedRange
) {
325 RangeVerifier
<TypeLoc
> Verifier
;
326 Verifier
.expectRange(1, 1, 1, 11);
327 EXPECT_TRUE(Verifier
.match("long long unsigned a;", typeLoc()));
330 TEST(TypeLoc
, ConstLongLongRange
) {
331 RangeVerifier
<TypeLoc
> Verifier
;
332 Verifier
.expectRange(1, 7, 1, 12);
333 EXPECT_TRUE(Verifier
.match("const long long a = 0;", typeLoc()));
336 TEST(TypeLoc
, LongConstLongRange
) {
337 RangeVerifier
<TypeLoc
> Verifier
;
338 Verifier
.expectRange(1, 1, 1, 12);
339 EXPECT_TRUE(Verifier
.match("long const long a = 0;", typeLoc()));
342 TEST(TypeLoc
, LongLongConstRange
) {
343 RangeVerifier
<TypeLoc
> Verifier
;
344 Verifier
.expectRange(1, 1, 1, 6);
345 EXPECT_TRUE(Verifier
.match("long long const a = 0;", typeLoc()));
348 TEST(CXXConstructorDecl
, NoRetFunTypeLocRange
) {
349 RangeVerifier
<CXXConstructorDecl
> Verifier
;
350 Verifier
.expectRange(1, 11, 1, 13);
351 EXPECT_TRUE(Verifier
.match("class C { C(); };", functionDecl()));
354 TEST(CXXConstructorDecl
, DefaultedCtorLocRange
) {
355 RangeVerifier
<CXXConstructorDecl
> Verifier
;
356 Verifier
.expectRange(1, 11, 1, 23);
357 EXPECT_TRUE(Verifier
.match("class C { C() = default; };", functionDecl()));
360 TEST(CXXConstructorDecl
, DeletedCtorLocRange
) {
361 RangeVerifier
<CXXConstructorDecl
> Verifier
;
362 Verifier
.expectRange(1, 11, 1, 22);
363 EXPECT_TRUE(Verifier
.match("class C { C() = delete; };", functionDecl()));
366 TEST(CompoundLiteralExpr
, CompoundVectorLiteralRange
) {
367 RangeVerifier
<CompoundLiteralExpr
> Verifier
;
368 Verifier
.expectRange(2, 11, 2, 22);
369 EXPECT_TRUE(Verifier
.match(
370 "typedef int int2 __attribute__((ext_vector_type(2)));\n"
371 "int2 i2 = (int2){1, 2};", compoundLiteralExpr()));
374 TEST(CompoundLiteralExpr
, ParensCompoundVectorLiteralRange
) {
375 RangeVerifier
<CompoundLiteralExpr
> Verifier
;
376 Verifier
.expectRange(2, 20, 2, 31);
378 Verifier
.match("typedef int int2 __attribute__((ext_vector_type(2)));\n"
379 "constant int2 i2 = (int2)(1, 2);",
380 compoundLiteralExpr(), Lang_OpenCL
));
383 TEST(InitListExpr
, VectorLiteralListBraceRange
) {
384 RangeVerifier
<InitListExpr
> Verifier
;
385 Verifier
.expectRange(2, 17, 2, 22);
386 EXPECT_TRUE(Verifier
.match(
387 "typedef int int2 __attribute__((ext_vector_type(2)));\n"
388 "int2 i2 = (int2){1, 2};", initListExpr()));
391 TEST(InitListExpr
, VectorLiteralInitListParens
) {
392 RangeVerifier
<InitListExpr
> Verifier
;
393 Verifier
.expectRange(2, 26, 2, 31);
394 EXPECT_TRUE(Verifier
.match(
395 "typedef int int2 __attribute__((ext_vector_type(2)));\n"
396 "constant int2 i2 = (int2)(1, 2);", initListExpr(), Lang_OpenCL
));
399 class TemplateAngleBracketLocRangeVerifier
: public RangeVerifier
<TypeLoc
> {
401 SourceRange
getRange(const TypeLoc
&Node
) override
{
402 TemplateSpecializationTypeLoc T
=
403 Node
.getUnqualifiedLoc().castAs
<TemplateSpecializationTypeLoc
>();
405 return SourceRange(T
.getLAngleLoc(), T
.getRAngleLoc());
409 TEST(TemplateSpecializationTypeLoc
, AngleBracketLocations
) {
410 TemplateAngleBracketLocRangeVerifier Verifier
;
411 Verifier
.expectRange(2, 8, 2, 10);
412 EXPECT_TRUE(Verifier
.match(
413 "template<typename T> struct A {}; struct B{}; void f(\n"
415 loc(templateSpecializationType())));
418 TEST(CXXNewExpr
, TypeParenRange
) {
419 RangeVerifier
<CXXNewExpr
> Verifier
;
420 Verifier
.expectRange(1, 10, 1, 18);
421 EXPECT_TRUE(Verifier
.match("int* a = new (int);", cxxNewExpr()));
424 class UnaryTransformTypeLocParensRangeVerifier
: public RangeVerifier
<TypeLoc
> {
426 SourceRange
getRange(const TypeLoc
&Node
) override
{
427 UnaryTransformTypeLoc T
=
428 Node
.getUnqualifiedLoc().castAs
<UnaryTransformTypeLoc
>();
430 return SourceRange(T
.getLParenLoc(), T
.getRParenLoc());
434 TEST(UnaryTransformTypeLoc
, ParensRange
) {
435 UnaryTransformTypeLocParensRangeVerifier Verifier
;
436 Verifier
.expectRange(3, 26, 3, 28);
437 EXPECT_TRUE(Verifier
.match(
438 "template <typename T>\n"
440 "typedef __underlying_type(T) type;\n"
442 loc(unaryTransformType())));
445 TEST(PointerTypeLoc
, StarLoc
) {
446 llvm::Annotations
Example(R
"c(
450 auto AST
= tooling::buildASTFromCode(Example
.code());
451 SourceManager
&SM
= AST
->getSourceManager();
452 auto &Ctx
= AST
->getASTContext();
454 auto *VD
= selectFirst
<VarDecl
>("vd", match(varDecl(hasName("var")).bind("vd"), Ctx
));
455 ASSERT_NE(VD
, nullptr);
458 VD
->getTypeSourceInfo()->getTypeLoc().castAs
<PointerTypeLoc
>();
459 ASSERT_EQ(SM
.getFileOffset(TL
.getStarLoc()), Example
.point("star"));
462 TEST(PointerTypeLoc
, StarLocBehindSugar
) {
463 llvm::Annotations
Example(R
"c(
464 #define NODEREF __attribute__((noderef))
465 char $1st^* NODEREF _Nonnull $2nd^* var;
468 auto AST
= tooling::buildASTFromCode(Example
.code());
469 SourceManager
&SM
= AST
->getSourceManager();
470 auto &Ctx
= AST
->getASTContext();
472 auto *VD
= selectFirst
<VarDecl
>("vd", match(varDecl(hasName("var")).bind("vd"), Ctx
));
473 ASSERT_NE(VD
, nullptr);
475 auto TL
= VD
->getTypeSourceInfo()->getTypeLoc().castAs
<PointerTypeLoc
>();
476 EXPECT_EQ(SM
.getFileOffset(TL
.getStarLoc()), Example
.point("2nd"));
478 // Cast intermediate TypeLoc to make sure the structure matches expectations.
479 auto InnerPtrTL
= TL
.getPointeeLoc().castAs
<AttributedTypeLoc
>()
480 .getNextTypeLoc().castAs
<MacroQualifiedTypeLoc
>()
481 .getNextTypeLoc().castAs
<AttributedTypeLoc
>()
482 .getNextTypeLoc().castAs
<PointerTypeLoc
>();
483 EXPECT_EQ(SM
.getFileOffset(InnerPtrTL
.getStarLoc()), Example
.point("1st"));
486 TEST(CXXFunctionalCastExpr
, SourceRange
) {
487 RangeVerifier
<CXXFunctionalCastExpr
> Verifier
;
488 Verifier
.expectRange(2, 10, 2, 14);
489 EXPECT_TRUE(Verifier
.match(
493 cxxFunctionalCastExpr(), Lang_CXX11
));
496 TEST(CXXConstructExpr
, SourceRange
) {
497 RangeVerifier
<CXXConstructExpr
> Verifier
;
498 Verifier
.expectRange(3, 14, 3, 19);
499 EXPECT_TRUE(Verifier
.match(
500 "struct A { A(int, int); };\n"
502 "void g() { f({0, 0}); }",
503 cxxConstructExpr(), Lang_CXX11
));
506 TEST(CXXTemporaryObjectExpr
, SourceRange
) {
507 RangeVerifier
<CXXTemporaryObjectExpr
> Verifier
;
508 Verifier
.expectRange(2, 6, 2, 12);
509 EXPECT_TRUE(Verifier
.match(
510 "struct A { A(int, int); };\n"
512 cxxTemporaryObjectExpr(), Lang_CXX11
));
515 TEST(CXXUnresolvedConstructExpr
, SourceRange
) {
516 RangeVerifier
<CXXUnresolvedConstructExpr
> Verifier
;
517 Verifier
.expectRange(3, 10, 3, 12);
518 std::vector
<std::string
> Args
;
519 Args
.push_back("-fno-delayed-template-parsing");
520 EXPECT_TRUE(Verifier
.match(
521 "template <typename U>\n"
525 cxxUnresolvedConstructExpr(), Args
, Lang_CXX11
));
528 TEST(UsingDecl
, SourceRange
) {
529 RangeVerifier
<UsingDecl
> Verifier
;
530 Verifier
.expectRange(2, 22, 2, 25);
531 EXPECT_TRUE(Verifier
.match(
532 "class B { protected: int i; };\n"
533 "class D : public B { B::i; };",
537 TEST(UnresolvedUsingValueDecl
, SourceRange
) {
538 RangeVerifier
<UnresolvedUsingValueDecl
> Verifier
;
539 Verifier
.expectRange(3, 3, 3, 6);
540 EXPECT_TRUE(Verifier
.match(
541 "template <typename B>\n"
542 "class D : public B {\n"
545 unresolvedUsingValueDecl()));
548 TEST(FriendDecl
, FriendNonMemberFunctionLocation
) {
549 LocationVerifier
<FriendDecl
> Verifier
;
550 Verifier
.expectLocation(2, 13);
551 EXPECT_TRUE(Verifier
.match("struct A {\n"
557 TEST(FriendDecl
, FriendNonMemberFunctionRange
) {
558 RangeVerifier
<FriendDecl
> Verifier
;
559 Verifier
.expectRange(2, 1, 2, 15);
560 EXPECT_TRUE(Verifier
.match("struct A {\n"
566 TEST(FriendDecl
, FriendNonMemberFunctionDefinitionLocation
) {
567 LocationVerifier
<FriendDecl
> Verifier
;
568 Verifier
.expectLocation(2, 12);
569 EXPECT_TRUE(Verifier
.match("struct A {\n"
570 "friend int f() { return 0; }\n"
575 TEST(FriendDecl
, FriendNonMemberFunctionDefinitionRange
) {
576 RangeVerifier
<FriendDecl
> Verifier
;
577 Verifier
.expectRange(2, 1, 2, 28);
578 EXPECT_TRUE(Verifier
.match("struct A {\n"
579 "friend int f() { return 0; }\n"
584 TEST(FriendDecl
, FriendElaboratedTypeLocation
) {
585 LocationVerifier
<FriendDecl
> Verifier
;
586 Verifier
.expectLocation(2, 8);
587 EXPECT_TRUE(Verifier
.match("struct A {\n"
593 TEST(FriendDecl
, FriendElaboratedTypeRange
) {
594 RangeVerifier
<FriendDecl
> Verifier
;
595 Verifier
.expectRange(2, 1, 2, 14);
596 EXPECT_TRUE(Verifier
.match("struct A {\n"
602 TEST(FriendDecl
, FriendSimpleTypeLocation
) {
603 LocationVerifier
<FriendDecl
> Verifier
;
604 Verifier
.expectLocation(3, 8);
605 EXPECT_TRUE(Verifier
.match("class B;\n"
609 friendDecl(), Lang_CXX11
));
612 TEST(FriendDecl
, FriendSimpleTypeRange
) {
613 RangeVerifier
<FriendDecl
> Verifier
;
614 Verifier
.expectRange(3, 1, 3, 8);
615 EXPECT_TRUE(Verifier
.match("class B;\n"
619 friendDecl(), Lang_CXX11
));
622 TEST(FriendDecl
, FriendTemplateParameterLocation
) {
623 LocationVerifier
<FriendDecl
> Verifier
;
624 Verifier
.expectLocation(3, 8);
625 EXPECT_TRUE(Verifier
.match("template <typename T>\n"
629 friendDecl(), Lang_CXX11
));
632 TEST(FriendDecl
, FriendTemplateParameterRange
) {
633 RangeVerifier
<FriendDecl
> Verifier
;
634 Verifier
.expectRange(3, 1, 3, 8);
635 EXPECT_TRUE(Verifier
.match("template <typename T>\n"
639 friendDecl(), Lang_CXX11
));
642 TEST(FriendDecl
, FriendDecltypeLocation
) {
643 LocationVerifier
<FriendDecl
> Verifier
;
644 Verifier
.expectLocation(4, 8);
645 EXPECT_TRUE(Verifier
.match("struct A;\n"
648 "friend decltype(foo());\n"
650 friendDecl(), Lang_CXX11
));
653 TEST(FriendDecl
, FriendDecltypeRange
) {
654 RangeVerifier
<FriendDecl
> Verifier
;
655 Verifier
.expectRange(4, 1, 4, 22);
656 EXPECT_TRUE(Verifier
.match("struct A;\n"
659 "friend decltype(foo());\n"
661 friendDecl(), Lang_CXX11
));
664 TEST(FriendDecl
, FriendConstructorDestructorLocation
) {
665 const std::string Code
= "struct B {\n"
670 "friend B::B(), B::~B();\n"
672 LocationVerifier
<FriendDecl
> ConstructorVerifier
;
673 ConstructorVerifier
.expectLocation(6, 11);
674 EXPECT_TRUE(ConstructorVerifier
.match(
675 Code
, friendDecl(has(cxxConstructorDecl(ofClass(hasName("B")))))));
676 LocationVerifier
<FriendDecl
> DestructorVerifier
;
677 DestructorVerifier
.expectLocation(6, 19);
678 EXPECT_TRUE(DestructorVerifier
.match(
679 Code
, friendDecl(has(cxxDestructorDecl(ofClass(hasName("B")))))));
682 TEST(FriendDecl
, FriendConstructorDestructorRange
) {
683 const std::string Code
= "struct B {\n"
688 "friend B::B(), B::~B();\n"
690 RangeVerifier
<FriendDecl
> ConstructorVerifier
;
691 ConstructorVerifier
.expectRange(6, 1, 6, 13);
692 EXPECT_TRUE(ConstructorVerifier
.match(
693 Code
, friendDecl(has(cxxConstructorDecl(ofClass(hasName("B")))))));
694 RangeVerifier
<FriendDecl
> DestructorVerifier
;
695 DestructorVerifier
.expectRange(6, 1, 6, 22);
696 EXPECT_TRUE(DestructorVerifier
.match(
697 Code
, friendDecl(has(cxxDestructorDecl(ofClass(hasName("B")))))));
700 TEST(FriendDecl
, FriendTemplateFunctionLocation
) {
701 LocationVerifier
<FriendDecl
> Verifier
;
702 Verifier
.expectLocation(3, 13);
703 EXPECT_TRUE(Verifier
.match("struct A {\n"
704 "template <typename T>\n"
710 TEST(FriendDecl
, FriendTemplateFunctionRange
) {
711 RangeVerifier
<FriendDecl
> Verifier
;
712 Verifier
.expectRange(2, 1, 3, 15);
713 EXPECT_TRUE(Verifier
.match("struct A {\n"
714 "template <typename T>\n"
720 TEST(FriendDecl
, FriendTemplateClassLocation
) {
721 LocationVerifier
<FriendDecl
> Verifier
;
722 Verifier
.expectLocation(3, 14);
723 EXPECT_TRUE(Verifier
.match("struct A {\n"
724 "template <typename T>\n"
730 TEST(FriendDecl
, FriendTemplateClassRange
) {
731 RangeVerifier
<FriendDecl
> Verifier
;
732 Verifier
.expectRange(2, 1, 3, 14);
733 EXPECT_TRUE(Verifier
.match("struct A {\n"
734 "template <typename T>\n"
740 TEST(FriendDecl
, FriendInlineFunctionLocation
) {
741 LocationVerifier
<FriendDecl
> Verifier
;
742 Verifier
.expectLocation(2, 19);
743 EXPECT_TRUE(Verifier
.match("struct A {\n"
744 "int inline friend f() { return 0; }"
749 TEST(FriendDecl
, FriendInlineFunctionRange
) {
750 RangeVerifier
<FriendDecl
> Verifier
;
751 Verifier
.expectRange(2, 1, 2, 35);
752 EXPECT_TRUE(Verifier
.match("struct A {\n"
753 "int inline friend f() { return 0; }"
755 friendDecl(), Lang_CXX11
));
758 TEST(FriendDecl
, InstantiationSourceRange
) {
759 RangeVerifier
<FriendDecl
> Verifier
;
760 Verifier
.expectRange(4, 3, 4, 35);
761 EXPECT_TRUE(Verifier
.match(
762 "template <typename T> class S;\n"
763 "template<class T> void operator+(S<T> x);\n"
764 "template<class T> struct S {\n"
765 " friend void operator+<>(S<T> src);\n"
767 "void test(S<double> s) { +s; }",
768 friendDecl(hasParent(cxxRecordDecl(isTemplateInstantiation())))));
771 TEST(ObjCMessageExpr
, ParenExprRange
) {
772 RangeVerifier
<ParenExpr
> Verifier
;
773 Verifier
.expectRange(5, 25, 5, 27);
774 EXPECT_TRUE(Verifier
.match("struct A { int a; };\n"
776 "+ (void) f1: (A)arg;\n"
778 "void f2() { A a; [B f1: (a)]; }\n",
779 traverse(TK_AsIs
, parenExpr()), Lang_OBJCXX
));
782 TEST(FunctionDecl
, FunctionDeclWithThrowSpecification
) {
783 RangeVerifier
<FunctionDecl
> Verifier
;
784 Verifier
.expectRange(1, 1, 1, 16);
785 EXPECT_TRUE(Verifier
.match(
786 "void f() throw();\n",
790 TEST(FunctionDecl
, FunctionDeclWithNoExceptSpecification
) {
791 RangeVerifier
<FunctionDecl
> Verifier
;
792 Verifier
.expectRange(1, 1, 1, 24);
793 EXPECT_TRUE(Verifier
.match("void f() noexcept(false);\n", functionDecl(),
797 class FunctionDeclParametersRangeVerifier
: public RangeVerifier
<FunctionDecl
> {
799 SourceRange
getRange(const FunctionDecl
&Function
) override
{
800 return Function
.getParametersSourceRange();
804 TEST(FunctionDeclParameters
, FunctionDeclOnlyVariadic
) {
805 FunctionDeclParametersRangeVerifier Verifier
;
806 Verifier
.expectRange(1, 8, 1, 8);
807 EXPECT_TRUE(Verifier
.match("void f(...);\n", functionDecl()));
810 TEST(FunctionDeclParameters
, FunctionDeclVariadic
) {
811 FunctionDeclParametersRangeVerifier Verifier
;
812 Verifier
.expectRange(1, 8, 1, 15);
813 EXPECT_TRUE(Verifier
.match("void f(int a, ...);\n", functionDecl()));
816 TEST(FunctionDeclParameters
, FunctionDeclMacroVariadic
) {
817 FunctionDeclParametersRangeVerifier Verifier
;
818 Verifier
.expectRange(2, 8, 1, 18);
819 EXPECT_TRUE(Verifier
.match("#define VARIADIC ...\n"
820 "void f(int a, VARIADIC);\n",
824 TEST(FunctionDeclParameters
, FunctionDeclMacroParams
) {
825 FunctionDeclParametersRangeVerifier Verifier
;
826 Verifier
.expectRange(1, 16, 2, 20);
827 EXPECT_TRUE(Verifier
.match("#define PARAMS int a, int b\n"
828 "void f(PARAMS, int c);",
832 TEST(FunctionDeclParameters
, FunctionDeclSingleParameter
) {
833 FunctionDeclParametersRangeVerifier Verifier
;
834 Verifier
.expectRange(1, 8, 1, 12);
835 EXPECT_TRUE(Verifier
.match("void f(int a);\n", functionDecl()));
838 TEST(FunctionDeclParameters
, MemberFunctionDecl
) {
839 FunctionDeclParametersRangeVerifier Verifier
;
840 Verifier
.expectRange(2, 8, 2, 12);
841 EXPECT_TRUE(Verifier
.match("class A{\n"
847 TEST(FunctionDeclParameters
, MemberFunctionDeclVariadic
) {
848 FunctionDeclParametersRangeVerifier Verifier
;
849 Verifier
.expectRange(2, 8, 2, 15);
850 EXPECT_TRUE(Verifier
.match("class A{\n"
851 "void f(int a, ...);\n"
856 TEST(FunctionDeclParameters
, StaticFunctionDecl
) {
857 FunctionDeclParametersRangeVerifier Verifier
;
858 Verifier
.expectRange(2, 15, 2, 19);
859 EXPECT_TRUE(Verifier
.match("class A{\n"
860 "static void f(int a);\n"
865 TEST(FunctionDeclParameters
, FunctionDeclMultipleParameters
) {
866 FunctionDeclParametersRangeVerifier Verifier
;
867 Verifier
.expectRange(1, 8, 1, 28);
869 Verifier
.match("void f(int a, int b, char *c);\n", functionDecl()));
872 TEST(FunctionDeclParameters
, FunctionDeclWithDefaultValue
) {
873 FunctionDeclParametersRangeVerifier Verifier
;
874 Verifier
.expectRange(1, 8, 1, 16);
875 EXPECT_TRUE(Verifier
.match("void f(int a = 5);\n", functionDecl()));
878 TEST(FunctionDeclParameters
, FunctionDeclWithVolatile
) {
879 FunctionDeclParametersRangeVerifier Verifier
;
880 Verifier
.expectRange(1, 8, 1, 22);
881 EXPECT_TRUE(Verifier
.match("void f(volatile int *i);", functionDecl()));
884 TEST(FunctionDeclParameters
, FunctionDeclWithConstParam
) {
885 FunctionDeclParametersRangeVerifier Verifier
;
886 Verifier
.expectRange(1, 8, 1, 19);
887 EXPECT_TRUE(Verifier
.match("void f(const int *i);", functionDecl()));
890 TEST(FunctionDeclParameters
, FunctionDeclWithConstVolatileParam
) {
891 FunctionDeclParametersRangeVerifier Verifier
;
892 Verifier
.expectRange(1, 8, 1, 28);
893 EXPECT_TRUE(Verifier
.match("void f(const volatile int *i);", functionDecl()));
896 TEST(FunctionDeclParameters
, FunctionDeclWithParamAttribute
) {
897 FunctionDeclParametersRangeVerifier Verifier
;
898 Verifier
.expectRange(1, 8, 1, 36);
899 EXPECT_TRUE(Verifier
.match("void f(__attribute__((unused)) int a) {}",
903 TEST(CXXMethodDecl
, CXXMethodDeclWithThrowSpecification
) {
904 RangeVerifier
<FunctionDecl
> Verifier
;
905 Verifier
.expectRange(2, 1, 2, 16);
906 EXPECT_TRUE(Verifier
.match(
908 "void f() throw();\n"
913 TEST(CXXMethodDecl
, CXXMethodDeclWithNoExceptSpecification
) {
914 RangeVerifier
<FunctionDecl
> Verifier
;
915 Verifier
.expectRange(2, 1, 2, 24);
916 EXPECT_TRUE(Verifier
.match("class A {\n"
917 "void f() noexcept(false);\n"
919 functionDecl(), Lang_CXX11
));
922 class ExceptionSpecRangeVerifier
: public RangeVerifier
<TypeLoc
> {
924 SourceRange
getRange(const TypeLoc
&Node
) override
{
926 Node
.getUnqualifiedLoc().castAs
<FunctionProtoTypeLoc
>();
928 return T
.getExceptionSpecRange();
932 class ParmVarExceptionSpecRangeVerifier
: public RangeVerifier
<ParmVarDecl
> {
934 SourceRange
getRange(const ParmVarDecl
&Node
) override
{
935 if (const TypeSourceInfo
*TSI
= Node
.getTypeSourceInfo()) {
936 TypeLoc TL
= TSI
->getTypeLoc();
937 if (TL
.getType()->isPointerType()) {
938 TL
= TL
.getNextTypeLoc().IgnoreParens();
939 if (auto FPTL
= TL
.getAs
<FunctionProtoTypeLoc
>()) {
940 return FPTL
.getExceptionSpecRange();
944 return SourceRange();
948 TEST(FunctionDecl
, ExceptionSpecifications
) {
949 ExceptionSpecRangeVerifier Verifier
;
951 Verifier
.expectRange(1, 10, 1, 16);
952 EXPECT_TRUE(Verifier
.match("void f() throw();\n", loc(functionType())));
954 Verifier
.expectRange(1, 10, 1, 34);
955 EXPECT_TRUE(Verifier
.match("void f() throw(void(void) throw());\n",
956 loc(functionType())));
958 Verifier
.expectRange(1, 10, 1, 19);
959 std::vector
<std::string
> Args
;
960 Args
.push_back("-fms-extensions");
961 EXPECT_TRUE(Verifier
.match("void f() throw(...);\n", loc(functionType()),
964 Verifier
.expectRange(1, 10, 1, 10);
966 Verifier
.match("void f() noexcept;\n", loc(functionType()), Lang_CXX11
));
968 Verifier
.expectRange(1, 10, 1, 24);
969 EXPECT_TRUE(Verifier
.match("void f() noexcept(false);\n", loc(functionType()),
972 Verifier
.expectRange(1, 10, 1, 32);
973 EXPECT_TRUE(Verifier
.match("void f() noexcept(noexcept(1+1));\n",
974 loc(functionType()), Lang_CXX11
));
976 ParmVarExceptionSpecRangeVerifier Verifier2
;
977 Verifier2
.expectRange(1, 25, 1, 31);
978 EXPECT_TRUE(Verifier2
.match("void g(void (*fp)(void) throw());\n",
979 parmVarDecl(hasType(pointerType(pointee(
980 parenType(innerType(functionType()))))))));
982 Verifier2
.expectRange(1, 25, 1, 38);
983 EXPECT_TRUE(Verifier2
.match("void g(void (*fp)(void) noexcept(true));\n",
984 parmVarDecl(hasType(pointerType(pointee(
985 parenType(innerType(functionType())))))),
989 TEST(Decl
, MemberPointerStarLoc
) {
990 llvm::Annotations
Example(R
"cpp(
995 auto AST
= tooling::buildASTFromCode(Example
.code());
996 SourceManager
&SM
= AST
->getSourceManager();
997 auto &Ctx
= AST
->getASTContext();
999 auto *VD
= selectFirst
<VarDecl
>("vd", match(varDecl().bind("vd"), Ctx
));
1000 ASSERT_TRUE(VD
!= nullptr);
1003 VD
->getTypeSourceInfo()->getTypeLoc().castAs
<MemberPointerTypeLoc
>();
1004 ASSERT_EQ(SM
.getFileOffset(TL
.getStarLoc()), Example
.point("star"));
1007 class AutoTypeLocConceptReferenceRangeVerifier
1008 : public RangeVerifier
<AutoTypeLoc
> {
1010 SourceRange
getRange(const AutoTypeLoc
&Node
) override
{
1011 if (const ConceptReference
*ConceptRef
= Node
.getConceptReference()) {
1012 return ConceptRef
->getSourceRange();
1014 return SourceRange();
1018 TEST(LocationVerifier
, AutoTypeLocConceptReference
) {
1019 AutoTypeLocConceptReferenceRangeVerifier Verifier
;
1022 R
"cpp(template <typename T> concept CCC = true;
1025 Verifier
.expectRange(2, 1, 2, 1);
1026 EXPECT_TRUE(Verifier
.match(Code
, typeLoc(loc(autoType())), Lang_CXX20
));
1029 R
"cpp(template <typename T, int> concept CCC = true;
1032 Verifier
.expectRange(2, 1, 2, 7);
1033 EXPECT_TRUE(Verifier
.match(Code2
, typeLoc(loc(autoType())), Lang_CXX20
));
1036 R
"cpp(namespace NS {
1037 template <typename T, int> concept CCC = true;
1039 NS::CCC<10> auto abc();
1041 Verifier
.expectRange(4, 1, 4, 11);
1042 EXPECT_TRUE(Verifier
.match(Code3
, typeLoc(loc(autoType())), Lang_CXX20
));
1045 R
"cpp(template <typename T> concept CCC = true;
1048 Verifier
.expectRange(2, 1, 2, 5);
1049 EXPECT_TRUE(Verifier
.match(Code4
, typeLoc(loc(autoType())), Lang_CXX20
));
1052 class TemplateTypeParmDeclConceptReferenceRangeVerifier
1053 : public RangeVerifier
<TemplateTypeParmDecl
> {
1055 SourceRange
getRange(const TemplateTypeParmDecl
&Node
) override
{
1056 if (const TypeConstraint
*TC
= Node
.getTypeConstraint()) {
1057 if (const ConceptReference
*ConceptRef
= TC
->getConceptReference()) {
1058 return ConceptRef
->getSourceRange();
1061 return SourceRange();
1065 TEST(LocationVerifier
, TemplateTypeParmDeclConceptReference
) {
1066 TemplateTypeParmDeclConceptReferenceRangeVerifier Verifier
;
1069 R
"cpp(template <typename S> concept CCC = true;
1070 template <CCC T> void print(T object);
1072 Verifier
.expectRange(2, 11, 2, 11);
1073 EXPECT_TRUE(Verifier
.match(Code
, templateTypeParmDecl(), Lang_CXX20
));
1076 R
"cpp(template <typename S, typename T> concept CCC = true;
1077 template <CCC<int> T> void print(T object);
1079 Verifier
.expectRange(2, 11, 2, 18);
1080 EXPECT_TRUE(Verifier
.match(Code2
, templateTypeParmDecl(), Lang_CXX20
));
1084 template <typename S, typename T> concept CCC = true;
1086 template <X::CCC<int> T> void print(T object);
1088 Verifier
.expectRange(4, 11, 4, 21);
1089 EXPECT_TRUE(Verifier
.match(Code3
, templateTypeParmDecl(), Lang_CXX20
));
1092 class ConceptSpecializationExprConceptReferenceRangeVerifier
1093 : public RangeVerifier
<VarTemplateDecl
> {
1095 SourceRange
getRange(const VarTemplateDecl
&Node
) override
{
1096 assert(Node
.hasAssociatedConstraints());
1097 SmallVector
<const Expr
*, 3> ACs
;
1098 Node
.getAssociatedConstraints(ACs
);
1099 for (const Expr
*Constraint
: ACs
) {
1100 if (const ConceptSpecializationExpr
*CSConstraint
=
1101 dyn_cast
<ConceptSpecializationExpr
>(Constraint
)) {
1102 return CSConstraint
->getConceptReference()->getSourceRange();
1105 return SourceRange();
1109 const internal::VariadicDynCastAllOfMatcher
<Decl
, VarTemplateDecl
>
1112 TEST(LocationVerifier
, ConceptSpecializationExprConceptReference
) {
1113 ConceptSpecializationExprConceptReferenceRangeVerifier Verifier
;
1116 R
"cpp(template <int X> concept CCC = true;
1117 template <int X> requires CCC<X> int z = X;
1119 Verifier
.expectRange(2, 27, 2, 32);
1120 EXPECT_TRUE(Verifier
.match(Code
, varTemplateDecl(hasName("z")), Lang_CXX20
));
1123 R
"cpp(namespace NS {
1124 template <int X> concept CCC = true;
1126 template <int X> requires NS::CCC<X> int z = X;
1128 Verifier
.expectRange(4, 27, 4, 36);
1129 EXPECT_TRUE(Verifier
.match(Code2
, varTemplateDecl(hasName("z")), Lang_CXX20
));