1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
//===- unittest/Tooling/SourceCodeTest.cpp --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Tooling/Transformer/SourceCode.h"
#include "TestVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Testing/Annotations/Annotations.h"
#include "llvm/Testing/Support/Error.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace clang;
using namespace clang::ast_matchers;
using llvm::Failed;
using llvm::Succeeded;
using llvm::ValueIs;
using testing::Optional;
using tooling::getAssociatedRange;
using tooling::getExtendedRange;
using tooling::getExtendedText;
using tooling::getFileRangeForEdit;
using tooling::getText;
using tooling::maybeExtendRange;
using tooling::validateEditRange;
namespace {
struct IntLitVisitor : TestVisitor {
bool VisitIntegerLiteral(IntegerLiteral *Expr) override {
OnIntLit(Expr, Context);
return true;
}
std::function<void(IntegerLiteral *, ASTContext *Context)> OnIntLit;
};
struct CallsVisitor : TestVisitor {
bool VisitCallExpr(CallExpr *Expr) override {
OnCall(Expr, Context);
return true;
}
std::function<void(CallExpr *, ASTContext *Context)> OnCall;
};
struct TypeLocVisitor : TestVisitor {
bool VisitTypeLoc(TypeLoc TL) override {
OnTypeLoc(TL, Context);
return true;
}
std::function<void(TypeLoc, ASTContext *Context)> OnTypeLoc;
};
// Equality matcher for `clang::CharSourceRange`, which lacks `operator==`.
MATCHER_P(EqualsRange, R, "") {
return arg.isTokenRange() == R.isTokenRange() &&
arg.getBegin() == R.getBegin() && arg.getEnd() == R.getEnd();
}
MATCHER_P2(EqualsAnnotatedRange, Context, R, "") {
if (arg.getBegin().isMacroID()) {
*result_listener << "which starts in a macro";
return false;
}
if (arg.getEnd().isMacroID()) {
*result_listener << "which ends in a macro";
return false;
}
CharSourceRange Range = Lexer::getAsCharRange(
arg, Context->getSourceManager(), Context->getLangOpts());
unsigned Begin = Context->getSourceManager().getFileOffset(Range.getBegin());
unsigned End = Context->getSourceManager().getFileOffset(Range.getEnd());
*result_listener << "which is a " << (arg.isTokenRange() ? "Token" : "Char")
<< " range [" << Begin << "," << End << ")";
return Begin == R.Begin && End == R.End;
}
static ::testing::Matcher<CharSourceRange> AsRange(const SourceManager &SM,
llvm::Annotations::Range R) {
return EqualsRange(CharSourceRange::getCharRange(
SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(R.Begin),
SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(R.End)));
}
// Base class for visitors that expect a single match corresponding to a
// specific annotated range.
class AnnotatedCodeVisitor : public TestVisitor {
protected:
int MatchCount = 0;
llvm::Annotations Code;
public:
AnnotatedCodeVisitor() : Code("$r[[]]") {}
// Helper for tests of `getAssociatedRange`.
bool VisitDeclHelper(Decl *Decl) {
// Only consider explicit declarations.
if (Decl->isImplicit())
return true;
++MatchCount;
EXPECT_THAT(getAssociatedRange(*Decl, *this->Context),
EqualsAnnotatedRange(this->Context, Code.range("r")))
<< Code.code();
return true;
}
bool runOverAnnotated(llvm::StringRef AnnotatedCode,
std::vector<std::string> Args = {}) {
Code = llvm::Annotations(AnnotatedCode);
MatchCount = 0;
Args.push_back("-std=c++11");
Args.push_back("-fno-delayed-template-parsing");
bool result = tooling::runToolOnCodeWithArgs(this->CreateTestAction(),
Code.code(), Args);
EXPECT_EQ(MatchCount, 1) << AnnotatedCode;
return result;
}
};
TEST(SourceCodeTest, getText) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("foo(x, y)", getText(*CE, *Context));
};
Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("APPLY(foo, x, y)", getText(*CE, *Context));
};
Visitor.runOver("#define APPLY(f, x, y) f(x, y)\n"
"void foo(int x, int y) { APPLY(foo, x, y); }");
}
TEST(SourceCodeTest, getTextWithMacro) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("F OO", getText(*CE, *Context));
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
EXPECT_EQ("", getText(*P0, *Context));
EXPECT_EQ("", getText(*P1, *Context));
};
Visitor.runOver("#define F foo(\n"
"#define OO x, y)\n"
"void foo(int x, int y) { F OO ; }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("", getText(*CE, *Context));
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
EXPECT_EQ("x", getText(*P0, *Context));
EXPECT_EQ("y", getText(*P1, *Context));
};
Visitor.runOver("#define FOO(x, y) (void)x; (void)y; foo(x, y);\n"
"void foo(int x, int y) { FOO(x,y) }");
}
TEST(SourceCodeTest, getExtendedText) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("foo(x, y);",
getExtendedText(*CE, tok::TokenKind::semi, *Context));
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
EXPECT_EQ("x", getExtendedText(*P0, tok::TokenKind::semi, *Context));
EXPECT_EQ("x,", getExtendedText(*P0, tok::TokenKind::comma, *Context));
EXPECT_EQ("y", getExtendedText(*P1, tok::TokenKind::semi, *Context));
};
Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
Visitor.runOver("void foo(int x, int y) { if (true) foo(x, y); }");
Visitor.runOver("int foo(int x, int y) { if (true) return 3 + foo(x, y); }");
Visitor.runOver("void foo(int x, int y) { for (foo(x, y);;) ++x; }");
Visitor.runOver(
"bool foo(int x, int y) { for (;foo(x, y);) x = 1; return true; }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("foo()", getExtendedText(*CE, tok::TokenKind::semi, *Context));
};
Visitor.runOver("bool foo() { if (foo()) return true; return false; }");
Visitor.runOver("void foo() { int x; for (;; foo()) ++x; }");
Visitor.runOver("int foo() { return foo() + 3; }");
}
TEST(SourceCodeTest, maybeExtendRange_TokenRange) {
struct ExtendTokenRangeVisitor : AnnotatedCodeVisitor {
bool VisitCallExpr(CallExpr *CE) override {
++MatchCount;
EXPECT_THAT(getExtendedRange(*CE, tok::TokenKind::semi, *Context),
EqualsAnnotatedRange(Context, Code.range("r")));
return true;
}
};
ExtendTokenRangeVisitor Visitor;
// Extends to include semicolon.
Visitor.runOverAnnotated("void f(int x, int y) { $r[[f(x, y);]] }");
// Does not extend to include semicolon.
Visitor.runOverAnnotated(
"int f(int x, int y) { if (0) return $r[[f(x, y)]] + 3; }");
}
TEST(SourceCodeTest, maybeExtendRange_CharRange) {
struct ExtendCharRangeVisitor : AnnotatedCodeVisitor {
bool VisitCallExpr(CallExpr *CE) override {
++MatchCount;
CharSourceRange Call = Lexer::getAsCharRange(CE->getSourceRange(),
Context->getSourceManager(),
Context->getLangOpts());
EXPECT_THAT(maybeExtendRange(Call, tok::TokenKind::semi, *Context),
EqualsAnnotatedRange(Context, Code.range("r")));
return true;
}
};
ExtendCharRangeVisitor Visitor;
// Extends to include semicolon.
Visitor.runOverAnnotated("void f(int x, int y) { $r[[f(x, y);]] }");
// Does not extend to include semicolon.
Visitor.runOverAnnotated(
"int f(int x, int y) { if (0) return $r[[f(x, y)]] + 3; }");
}
TEST(SourceCodeTest, getAssociatedRange) {
struct VarDeclsVisitor : AnnotatedCodeVisitor {
bool VisitVarDecl(VarDecl *Decl) override { return VisitDeclHelper(Decl); }
};
VarDeclsVisitor Visitor;
// Includes semicolon.
Visitor.runOverAnnotated("$r[[int x = 4;]]");
// Includes newline and semicolon.
Visitor.runOverAnnotated("$r[[int x = 4;\n]]");
// Includes trailing comments.
Visitor.runOverAnnotated("$r[[int x = 4; // Comment\n]]");
Visitor.runOverAnnotated("$r[[int x = 4; /* Comment */\n]]");
// Does *not* include trailing comments when another entity appears between
// the decl and the comment.
Visitor.runOverAnnotated("$r[[int x = 4;]] class C {}; // Comment\n");
// Includes attributes.
Visitor.runOverAnnotated(R"cpp(
$r[[__attribute__((deprecated("message")))
int x;]])cpp");
// Includes attributes and comments together.
Visitor.runOverAnnotated(R"cpp(
$r[[__attribute__((deprecated("message")))
// Comment.
int x;]])cpp");
// Includes attributes through macro expansion.
Visitor.runOverAnnotated(R"cpp(
#define MACRO_EXPANSION __attribute__((deprecated("message")))
$r[[MACRO_EXPANSION
int x;]])cpp");
// Includes attributes through macro expansion with comments.
Visitor.runOverAnnotated(R"cpp(
#define MACRO_EXPANSION __attribute__((deprecated("message")))
$r[[MACRO_EXPANSION
// Comment.
int x;]])cpp");
}
TEST(SourceCodeTest, getAssociatedRangeClasses) {
struct RecordDeclsVisitor : AnnotatedCodeVisitor {
bool VisitRecordDecl(RecordDecl *Decl) override {
return VisitDeclHelper(Decl);
}
};
RecordDeclsVisitor Visitor;
Visitor.runOverAnnotated("$r[[class A;]]");
Visitor.runOverAnnotated("$r[[class A {};]]");
// Includes leading template annotation.
Visitor.runOverAnnotated("$r[[template <typename T> class A;]]");
Visitor.runOverAnnotated("$r[[template <typename T> class A {};]]");
}
TEST(SourceCodeTest, getAssociatedRangeClassTemplateSpecializations) {
struct CXXRecordDeclsVisitor : AnnotatedCodeVisitor {
bool VisitCXXRecordDecl(CXXRecordDecl *Decl) override {
return Decl->getTemplateSpecializationKind() !=
TSK_ExplicitSpecialization ||
VisitDeclHelper(Decl);
}
};
CXXRecordDeclsVisitor Visitor;
Visitor.runOverAnnotated(R"cpp(
template <typename T> class A{};
$r[[template <> class A<int>;]])cpp");
Visitor.runOverAnnotated(R"cpp(
template <typename T> class A{};
$r[[template <> class A<int> {};]])cpp");
}
TEST(SourceCodeTest, getAssociatedRangeFunctions) {
struct FunctionDeclsVisitor : AnnotatedCodeVisitor {
bool VisitFunctionDecl(FunctionDecl *Decl) override {
return VisitDeclHelper(Decl);
}
};
FunctionDeclsVisitor Visitor;
Visitor.runOverAnnotated("$r[[int f();]]");
Visitor.runOverAnnotated("$r[[int f() { return 0; }]]");
// Includes leading template annotation.
Visitor.runOverAnnotated("$r[[template <typename T> int f();]]");
Visitor.runOverAnnotated("$r[[template <typename T> int f() { return 0; }]]");
}
TEST(SourceCodeTest, getAssociatedRangeMemberTemplates) {
struct CXXMethodDeclsVisitor : AnnotatedCodeVisitor {
bool VisitCXXMethodDecl(CXXMethodDecl *Decl) override {
// Only consider the definition of the template.
return !Decl->doesThisDeclarationHaveABody() || VisitDeclHelper(Decl);
}
};
CXXMethodDeclsVisitor Visitor;
Visitor.runOverAnnotated(R"cpp(
template <typename C>
struct A { template <typename T> int member(T v); };
$r[[template <typename C>
template <typename T>
int A<C>::member(T v) { return 0; }]])cpp");
}
TEST(SourceCodeTest, getAssociatedRangeWithComments) {
struct VarDeclsVisitor : AnnotatedCodeVisitor {
bool VisitVarDecl(VarDecl *Decl) override { return VisitDeclHelper(Decl); }
};
VarDeclsVisitor Visitor;
auto Visit = [&](llvm::StringRef AnnotatedCode) {
Visitor.runOverAnnotated(AnnotatedCode, {"-fparse-all-comments"});
};
// Includes leading comments.
Visit("$r[[// Comment.\nint x = 4;]]");
Visit("$r[[// Comment.\nint x = 4;\n]]");
Visit("$r[[/* Comment.*/\nint x = 4;\n]]");
// ... even if separated by (extra) horizontal whitespace.
Visit("$r[[/* Comment.*/ \nint x = 4;\n]]");
// Includes comments even in the presence of trailing whitespace.
Visit("$r[[// Comment.\nint x = 4;]] ");
// Includes comments when the declaration is followed by the beginning or end
// of a compound statement.
Visit(R"cpp(
void foo() {
$r[[/* C */
int x = 4;
]]};)cpp");
Visit(R"cpp(
void foo() {
$r[[/* C */
int x = 4;
]]{ class Foo {}; }
})cpp");
// Includes comments inside macros (when decl is in the same macro).
Visit(R"cpp(
#define DECL /* Comment */ int x
$r[[DECL;]])cpp");
Visit(R"cpp(
#define DECL int x
$r[[// Comment
DECL;]])cpp");
// Does not include comments when only the comment come from a macro.
Visit(R"cpp(
#define COMMENT /* Comment */
COMMENT
$r[[int x;]])cpp");
// Includes multi-line comments.
Visit(R"cpp(
$r[[/* multi
* line
* comment
*/
int x;]])cpp");
Visit(R"cpp(
$r[[// multi
// line
// comment
int x;]])cpp");
// Does not include comments separated by multiple empty lines.
Visit("// Comment.\n\n\n$r[[int x = 4;\n]]");
Visit("/* Comment.*/\n\n\n$r[[int x = 4;\n]]");
// Does not include comments before a *series* of declarations.
Visit(R"cpp(
// Comment.
$r[[int x = 4;
]]class foo {};)cpp");
// Does not include IfThisThenThat comments
Visit("// LINT.IfChange.\n$r[[int x = 4;]]");
Visit("// LINT.ThenChange.\n$r[[int x = 4;]]");
// Includes attributes.
Visit(R"cpp(
$r[[__attribute__((deprecated("message")))
int x;]])cpp");
// Includes attributes and comments together.
Visit(R"cpp(
$r[[__attribute__((deprecated("message")))
// Comment.
int x;]])cpp");
// Includes attributes through macro expansion.
Visitor.runOverAnnotated(R"cpp(
#define MACRO_EXPANSION __attribute__((deprecated("message")))
$r[[MACRO_EXPANSION
int x;]])cpp");
// Includes attributes through macro expansion with comments.
Visitor.runOverAnnotated(R"cpp(
#define MACRO_EXPANSION __attribute__((deprecated("message")))
$r[[MACRO_EXPANSION
// Comment.
int x;]])cpp");
}
TEST(SourceCodeTest, getAssociatedRangeInvalidForPartialExpansions) {
struct FailingVarDeclsVisitor : TestVisitor {
FailingVarDeclsVisitor() {}
bool VisitVarDecl(VarDecl *Decl) override {
EXPECT_TRUE(getAssociatedRange(*Decl, *Context).isInvalid());
return true;
}
};
FailingVarDeclsVisitor Visitor;
// Should fail because it only includes a part of the expansion.
std::string Code = R"cpp(
#define DECL class foo { }; int x
DECL;)cpp";
Visitor.runOver(Code);
}
class GetFileRangeForEditTest : public testing::TestWithParam<bool> {};
INSTANTIATE_TEST_SUITE_P(WithAndWithoutExpansions, GetFileRangeForEditTest,
testing::Bool());
TEST_P(GetFileRangeForEditTest, EditRangeWithMacroExpansionsShouldSucceed) {
// The call expression, whose range we are extracting, includes two macro
// expansions.
llvm::Annotations Code(R"cpp(
#define M(a) a * 13
int foo(int x, int y);
int a = $r[[foo(M(1), M(2))]];
)cpp");
CallsVisitor Visitor;
Visitor.OnCall = [&Code](CallExpr *CE, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(CE->getSourceRange());
EXPECT_THAT(getFileRangeForEdit(Range, *Context, GetParam()),
ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
};
Visitor.runOver(Code.code());
}
TEST(SourceCodeTest, EditWholeMacroExpansionShouldSucceed) {
llvm::Annotations Code(R"cpp(
#define FOO 10
int a = $r[[FOO]];
)cpp");
IntLitVisitor Visitor;
Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
EXPECT_THAT(getFileRangeForEdit(Range, *Context),
ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
};
Visitor.runOver(Code.code());
}
TEST(SourceCodeTest, EditInvolvingExpansionIgnoringExpansionShouldFail) {
// If we specify to ignore macro expansions, none of these call expressions
// should have an editable range.
llvm::Annotations Code(R"cpp(
#define M1(x) x(1)
#define M2(x, y) x ## y
#define M3(x) foobar(x)
int foobar(int);
int a = M1(foobar);
int b = M2(foo, bar(2));
int c = M3(3);
)cpp");
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(CE->getSourceRange());
EXPECT_FALSE(
getFileRangeForEdit(Range, *Context, /*IncludeMacroExpansion=*/false));
};
Visitor.runOver(Code.code());
}
TEST(SourceCodeTest, InnerNestedTemplate) {
llvm::Annotations Code(R"cpp(
template <typename T>
struct A {};
template <typename T>
struct B {};
template <typename T>
struct C {};
void f(A<B<C<int>$r[[>>]]);
)cpp");
TypeLocVisitor Visitor;
Visitor.OnTypeLoc = [&](TypeLoc TL, ASTContext *Context) {
if (TL.getSourceRange().isInvalid())
return;
// There are no macros, so every TypeLoc's range should be valid.
auto Range = CharSourceRange::getTokenRange(TL.getSourceRange());
auto LastTokenRange = CharSourceRange::getTokenRange(TL.getEndLoc());
EXPECT_TRUE(getFileRangeForEdit(Range, *Context,
/*IncludeMacroExpansion=*/false))
<< TL.getSourceRange().printToString(Context->getSourceManager());
EXPECT_TRUE(getFileRangeForEdit(LastTokenRange, *Context,
/*IncludeMacroExpansion=*/false))
<< TL.getEndLoc().printToString(Context->getSourceManager());
if (auto matches = match(
templateSpecializationTypeLoc(
loc(templateSpecializationType(
hasDeclaration(cxxRecordDecl(hasName("A"))))),
hasTemplateArgumentLoc(
0, templateArgumentLoc(hasTypeLoc(typeLoc().bind("b"))))),
TL, *Context);
!matches.empty()) {
// A range where the start token is split, but the end token is not.
auto OuterTL = TL;
auto MiddleTL = *matches[0].getNodeAs<TypeLoc>("b");
EXPECT_THAT(
getFileRangeForEdit(CharSourceRange::getTokenRange(
MiddleTL.getEndLoc(), OuterTL.getEndLoc()),
*Context, /*IncludeMacroExpansion=*/false),
Optional(EqualsAnnotatedRange(Context, Code.range("r"))));
}
};
Visitor.runOver(Code.code(), TypeLocVisitor::Lang_CXX11);
}
TEST_P(GetFileRangeForEditTest, EditPartialMacroExpansionShouldFail) {
std::string Code = R"cpp(
#define BAR 10+
int c = BAR 3.0;
)cpp";
IntLitVisitor Visitor;
Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
EXPECT_FALSE(getFileRangeForEdit(Range, *Context, GetParam()));
};
Visitor.runOver(Code);
}
TEST_P(GetFileRangeForEditTest, EditWholeMacroArgShouldSucceed) {
llvm::Annotations Code(R"cpp(
#define FOO(a) a + 7.0;
int a = FOO($r[[10]]);
)cpp");
IntLitVisitor Visitor;
Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
EXPECT_THAT(getFileRangeForEdit(Range, *Context, GetParam()),
ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
};
Visitor.runOver(Code.code());
}
TEST_P(GetFileRangeForEditTest, EditPartialMacroArgShouldSucceed) {
llvm::Annotations Code(R"cpp(
#define FOO(a) a + 7.0;
int a = FOO($r[[10]] + 10.0);
)cpp");
IntLitVisitor Visitor;
Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
EXPECT_THAT(getFileRangeForEdit(Range, *Context, GetParam()),
ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
};
Visitor.runOver(Code.code());
}
TEST(SourceCodeTest, EditRangeWithMacroExpansionsIsValid) {
// The call expression, whose range we are extracting, includes two macro
// expansions.
llvm::StringRef Code = R"cpp(
#define M(a) a * 13
int foo(int x, int y);
int a = foo(M(1), M(2));
)cpp";
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(CE->getSourceRange());
EXPECT_THAT_ERROR(validateEditRange(Range, Context->getSourceManager()),
Succeeded());
};
Visitor.runOver(Code);
}
TEST(SourceCodeTest, SpellingRangeOfMacroArgIsValid) {
llvm::StringRef Code = R"cpp(
#define FOO(a) a + 7.0;
int a = FOO(10);
)cpp";
IntLitVisitor Visitor;
Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
SourceLocation ArgLoc =
Context->getSourceManager().getSpellingLoc(Expr->getBeginLoc());
// The integer literal is a single token.
auto ArgRange = CharSourceRange::getTokenRange(ArgLoc);
EXPECT_THAT_ERROR(validateEditRange(ArgRange, Context->getSourceManager()),
Succeeded());
};
Visitor.runOver(Code);
}
TEST(SourceCodeTest, InvalidEditRangeIsInvalid) {
llvm::StringRef Code = "int c = 10;";
// We use the visitor just to get a valid context.
IntLitVisitor Visitor;
Visitor.OnIntLit = [](IntegerLiteral *, ASTContext *Context) {
CharSourceRange Invalid;
EXPECT_THAT_ERROR(validateEditRange(Invalid, Context->getSourceManager()),
Failed());
};
Visitor.runOver(Code);
}
TEST(SourceCodeTest, InvertedEditRangeIsInvalid) {
llvm::StringRef Code = R"cpp(
int foo(int x);
int a = foo(2);
)cpp";
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *Expr, ASTContext *Context) {
auto InvertedRange = CharSourceRange::getTokenRange(
SourceRange(Expr->getEndLoc(), Expr->getBeginLoc()));
EXPECT_THAT_ERROR(
validateEditRange(InvertedRange, Context->getSourceManager()),
Failed());
};
Visitor.runOver(Code);
}
TEST(SourceCodeTest, MacroArgIsInvalid) {
llvm::StringRef Code = R"cpp(
#define FOO(a) a + 7.0;
int a = FOO(10);
)cpp";
IntLitVisitor Visitor;
Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
EXPECT_THAT_ERROR(validateEditRange(Range, Context->getSourceManager()),
Failed());
};
Visitor.runOver(Code);
}
TEST(SourceCodeTest, EditWholeMacroExpansionIsInvalid) {
llvm::StringRef Code = R"cpp(
#define FOO 10
int a = FOO;
)cpp";
IntLitVisitor Visitor;
Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
EXPECT_THAT_ERROR(validateEditRange(Range, Context->getSourceManager()),
Failed());
};
Visitor.runOver(Code);
}
TEST(SourceCodeTest, EditPartialMacroExpansionIsInvalid) {
llvm::StringRef Code = R"cpp(
#define BAR 10+
int c = BAR 3.0;
)cpp";
IntLitVisitor Visitor;
Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
EXPECT_THAT_ERROR(validateEditRange(Range, Context->getSourceManager()),
Failed());
};
Visitor.runOver(Code);
}
TEST(SourceCodeTest, GetCallReturnType_Dependent) {
llvm::Annotations Code{R"cpp(
template<class T, class F>
void templ(const T& t, F f) {}
template<class T, class F>
void templ1(const T& t, F f) {
$test1[[f(t)]];
}
int f_overload(int) { return 1; }
int f_overload(double) { return 2; }
void f1() {
int i = 0;
templ(i, [](const auto &p) {
$test2[[f_overload(p)]];
});
}
struct A {
void f_overload(int);
void f_overload(double);
};
void f2() {
int i = 0;
templ(i, [](const auto &p) {
A a;
$test3[[a.f_overload(p)]];
});
}
)cpp"};
llvm::Annotations::Range R1 = Code.range("test1");
llvm::Annotations::Range R2 = Code.range("test2");
llvm::Annotations::Range R3 = Code.range("test3");
CallsVisitor Visitor;
Visitor.OnCall = [&R1, &R2, &R3](CallExpr *Expr, ASTContext *Context) {
unsigned Begin = Context->getSourceManager().getFileOffset(
Expr->getSourceRange().getBegin());
unsigned End = Context->getSourceManager().getFileOffset(
Expr->getSourceRange().getEnd());
llvm::Annotations::Range R{Begin, End + 1};
QualType CalleeType = Expr->getCallee()->getType();
if (R == R1) {
ASSERT_TRUE(CalleeType->isDependentType());
EXPECT_EQ(Expr->getCallReturnType(*Context), Context->DependentTy);
} else if (R == R2) {
ASSERT_FALSE(CalleeType->isDependentType());
ASSERT_TRUE(CalleeType->isSpecificPlaceholderType(BuiltinType::Overload));
ASSERT_TRUE(isa<UnresolvedLookupExpr>(Expr->getCallee()));
EXPECT_EQ(Expr->getCallReturnType(*Context), Context->DependentTy);
} else if (R == R3) {
ASSERT_FALSE(CalleeType->isDependentType());
ASSERT_TRUE(
CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember));
ASSERT_TRUE(isa<UnresolvedMemberExpr>(Expr->getCallee()));
EXPECT_EQ(Expr->getCallReturnType(*Context), Context->DependentTy);
}
};
Visitor.runOver(Code.code(), CallsVisitor::Lang_CXX14);
}
} // end anonymous namespace
|