SourceCodeTest.cpp
19.8 KB
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
//===- 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/Basic/Diagnostic.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Testing/Support/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 llvm::Failed;
using llvm::Succeeded;
using llvm::ValueIs;
using tooling::getAssociatedRange;
using tooling::getExtendedRange;
using tooling::getExtendedText;
using tooling::getRangeForEdit;
using tooling::getText;
using tooling::maybeExtendRange;
using tooling::validateEditRange;
namespace {
struct IntLitVisitor : TestVisitor<IntLitVisitor> {
bool VisitIntegerLiteral(IntegerLiteral *Expr) {
OnIntLit(Expr, Context);
return true;
}
std::function<void(IntegerLiteral *, ASTContext *Context)> OnIntLit;
};
struct CallsVisitor : TestVisitor<CallsVisitor> {
bool VisitCallExpr(CallExpr *Expr) {
OnCall(Expr, Context);
return true;
}
std::function<void(CallExpr *, ASTContext *Context)> OnCall;
};
// 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.
template <typename T> class AnnotatedCodeVisitor : public TestVisitor<T> {
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<ExtendTokenRangeVisitor> {
bool VisitCallExpr(CallExpr *CE) {
++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<ExtendCharRangeVisitor> {
bool VisitCallExpr(CallExpr *CE) {
++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<VarDeclsVisitor> {
bool VisitVarDecl(VarDecl *Decl) { 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(
#define ATTR __attribute__((deprecated("message")))
$r[[ATTR
int x;]])cpp");
// Includes attributes and comments together.
Visitor.runOverAnnotated(R"cpp(
#define ATTR __attribute__((deprecated("message")))
$r[[ATTR
// Commment.
int x;]])cpp");
}
TEST(SourceCodeTest, getAssociatedRangeClasses) {
struct RecordDeclsVisitor : AnnotatedCodeVisitor<RecordDeclsVisitor> {
bool VisitRecordDecl(RecordDecl *Decl) { 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<CXXRecordDeclsVisitor> {
bool VisitCXXRecordDecl(CXXRecordDecl *Decl) {
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<FunctionDeclsVisitor> {
bool VisitFunctionDecl(FunctionDecl *Decl) { 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<CXXMethodDeclsVisitor> {
bool VisitCXXMethodDecl(CXXMethodDecl *Decl) {
// 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<VarDeclsVisitor> {
bool VisitVarDecl(VarDecl *Decl) { 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");
// Does not include comments when only the decl or the comment come from a
// macro.
// FIXME: Change code to allow this.
Visit(R"cpp(
#define DECL int x
// Comment
$r[[DECL;]])cpp");
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(
#define ATTR __attribute__((deprecated("message")))
$r[[ATTR
int x;]])cpp");
// Includes attributes and comments together.
Visit(R"cpp(
#define ATTR __attribute__((deprecated("message")))
$r[[ATTR
// Commment.
int x;]])cpp");
}
TEST(SourceCodeTest, getAssociatedRangeInvalidForPartialExpansions) {
struct FailingVarDeclsVisitor : TestVisitor<FailingVarDeclsVisitor> {
FailingVarDeclsVisitor() {}
bool VisitVarDecl(VarDecl *Decl) {
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);
}
TEST(SourceCodeTest, 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(getRangeForEdit(Range, *Context),
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(getRangeForEdit(Range, *Context),
ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
};
Visitor.runOver(Code.code());
}
TEST(SourceCodeTest, 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(getRangeForEdit(Range, *Context).hasValue());
};
Visitor.runOver(Code);
}
TEST(SourceCodeTest, 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(getRangeForEdit(Range, *Context),
ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
};
Visitor.runOver(Code.code());
}
TEST(SourceCodeTest, 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(getRangeForEdit(Range, *Context),
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);
}
} // end anonymous namespace