[AMDGPU] Add True16 register classes.
[llvm-project.git] / clang / unittests / Tooling / SourceCodeTest.cpp
blob3d1dbceb63a7ffa9ab1568ebd9e6c270f987913a
1 //===- unittest/Tooling/SourceCodeTest.cpp --------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "clang/Tooling/Transformer/SourceCode.h"
10 #include "TestVisitor.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "clang/Basic/SourceLocation.h"
13 #include "clang/Lex/Lexer.h"
14 #include "llvm/Testing/Annotations/Annotations.h"
15 #include "llvm/Testing/Support/Error.h"
16 #include "llvm/Testing/Support/SupportHelpers.h"
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
20 using namespace clang;
22 using llvm::Failed;
23 using llvm::Succeeded;
24 using llvm::ValueIs;
25 using tooling::getAssociatedRange;
26 using tooling::getExtendedRange;
27 using tooling::getExtendedText;
28 using tooling::getFileRangeForEdit;
29 using tooling::getText;
30 using tooling::maybeExtendRange;
31 using tooling::validateEditRange;
33 namespace {
35 struct IntLitVisitor : TestVisitor<IntLitVisitor> {
36 bool VisitIntegerLiteral(IntegerLiteral *Expr) {
37 OnIntLit(Expr, Context);
38 return true;
41 std::function<void(IntegerLiteral *, ASTContext *Context)> OnIntLit;
44 struct CallsVisitor : TestVisitor<CallsVisitor> {
45 bool VisitCallExpr(CallExpr *Expr) {
46 OnCall(Expr, Context);
47 return true;
50 std::function<void(CallExpr *, ASTContext *Context)> OnCall;
53 // Equality matcher for `clang::CharSourceRange`, which lacks `operator==`.
54 MATCHER_P(EqualsRange, R, "") {
55 return arg.isTokenRange() == R.isTokenRange() &&
56 arg.getBegin() == R.getBegin() && arg.getEnd() == R.getEnd();
59 MATCHER_P2(EqualsAnnotatedRange, Context, R, "") {
60 if (arg.getBegin().isMacroID()) {
61 *result_listener << "which starts in a macro";
62 return false;
64 if (arg.getEnd().isMacroID()) {
65 *result_listener << "which ends in a macro";
66 return false;
69 CharSourceRange Range = Lexer::getAsCharRange(
70 arg, Context->getSourceManager(), Context->getLangOpts());
71 unsigned Begin = Context->getSourceManager().getFileOffset(Range.getBegin());
72 unsigned End = Context->getSourceManager().getFileOffset(Range.getEnd());
74 *result_listener << "which is a " << (arg.isTokenRange() ? "Token" : "Char")
75 << " range [" << Begin << "," << End << ")";
76 return Begin == R.Begin && End == R.End;
79 static ::testing::Matcher<CharSourceRange> AsRange(const SourceManager &SM,
80 llvm::Annotations::Range R) {
81 return EqualsRange(CharSourceRange::getCharRange(
82 SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(R.Begin),
83 SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(R.End)));
86 // Base class for visitors that expect a single match corresponding to a
87 // specific annotated range.
88 template <typename T> class AnnotatedCodeVisitor : public TestVisitor<T> {
89 protected:
90 int MatchCount = 0;
91 llvm::Annotations Code;
93 public:
94 AnnotatedCodeVisitor() : Code("$r[[]]") {}
95 // Helper for tests of `getAssociatedRange`.
96 bool VisitDeclHelper(Decl *Decl) {
97 // Only consider explicit declarations.
98 if (Decl->isImplicit())
99 return true;
101 ++MatchCount;
102 EXPECT_THAT(getAssociatedRange(*Decl, *this->Context),
103 EqualsAnnotatedRange(this->Context, Code.range("r")))
104 << Code.code();
105 return true;
108 bool runOverAnnotated(llvm::StringRef AnnotatedCode,
109 std::vector<std::string> Args = {}) {
110 Code = llvm::Annotations(AnnotatedCode);
111 MatchCount = 0;
112 Args.push_back("-std=c++11");
113 Args.push_back("-fno-delayed-template-parsing");
114 bool result = tooling::runToolOnCodeWithArgs(this->CreateTestAction(),
115 Code.code(), Args);
116 EXPECT_EQ(MatchCount, 1) << AnnotatedCode;
117 return result;
121 TEST(SourceCodeTest, getText) {
122 CallsVisitor Visitor;
124 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
125 EXPECT_EQ("foo(x, y)", getText(*CE, *Context));
127 Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
129 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
130 EXPECT_EQ("APPLY(foo, x, y)", getText(*CE, *Context));
132 Visitor.runOver("#define APPLY(f, x, y) f(x, y)\n"
133 "void foo(int x, int y) { APPLY(foo, x, y); }");
136 TEST(SourceCodeTest, getTextWithMacro) {
137 CallsVisitor Visitor;
139 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
140 EXPECT_EQ("F OO", getText(*CE, *Context));
141 Expr *P0 = CE->getArg(0);
142 Expr *P1 = CE->getArg(1);
143 EXPECT_EQ("", getText(*P0, *Context));
144 EXPECT_EQ("", getText(*P1, *Context));
146 Visitor.runOver("#define F foo(\n"
147 "#define OO x, y)\n"
148 "void foo(int x, int y) { F OO ; }");
150 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
151 EXPECT_EQ("", getText(*CE, *Context));
152 Expr *P0 = CE->getArg(0);
153 Expr *P1 = CE->getArg(1);
154 EXPECT_EQ("x", getText(*P0, *Context));
155 EXPECT_EQ("y", getText(*P1, *Context));
157 Visitor.runOver("#define FOO(x, y) (void)x; (void)y; foo(x, y);\n"
158 "void foo(int x, int y) { FOO(x,y) }");
161 TEST(SourceCodeTest, getExtendedText) {
162 CallsVisitor Visitor;
164 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
165 EXPECT_EQ("foo(x, y);",
166 getExtendedText(*CE, tok::TokenKind::semi, *Context));
168 Expr *P0 = CE->getArg(0);
169 Expr *P1 = CE->getArg(1);
170 EXPECT_EQ("x", getExtendedText(*P0, tok::TokenKind::semi, *Context));
171 EXPECT_EQ("x,", getExtendedText(*P0, tok::TokenKind::comma, *Context));
172 EXPECT_EQ("y", getExtendedText(*P1, tok::TokenKind::semi, *Context));
174 Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
175 Visitor.runOver("void foo(int x, int y) { if (true) foo(x, y); }");
176 Visitor.runOver("int foo(int x, int y) { if (true) return 3 + foo(x, y); }");
177 Visitor.runOver("void foo(int x, int y) { for (foo(x, y);;) ++x; }");
178 Visitor.runOver(
179 "bool foo(int x, int y) { for (;foo(x, y);) x = 1; return true; }");
181 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
182 EXPECT_EQ("foo()", getExtendedText(*CE, tok::TokenKind::semi, *Context));
184 Visitor.runOver("bool foo() { if (foo()) return true; return false; }");
185 Visitor.runOver("void foo() { int x; for (;; foo()) ++x; }");
186 Visitor.runOver("int foo() { return foo() + 3; }");
189 TEST(SourceCodeTest, maybeExtendRange_TokenRange) {
190 struct ExtendTokenRangeVisitor
191 : AnnotatedCodeVisitor<ExtendTokenRangeVisitor> {
192 bool VisitCallExpr(CallExpr *CE) {
193 ++MatchCount;
194 EXPECT_THAT(getExtendedRange(*CE, tok::TokenKind::semi, *Context),
195 EqualsAnnotatedRange(Context, Code.range("r")));
196 return true;
200 ExtendTokenRangeVisitor Visitor;
201 // Extends to include semicolon.
202 Visitor.runOverAnnotated("void f(int x, int y) { $r[[f(x, y);]] }");
203 // Does not extend to include semicolon.
204 Visitor.runOverAnnotated(
205 "int f(int x, int y) { if (0) return $r[[f(x, y)]] + 3; }");
208 TEST(SourceCodeTest, maybeExtendRange_CharRange) {
209 struct ExtendCharRangeVisitor : AnnotatedCodeVisitor<ExtendCharRangeVisitor> {
210 bool VisitCallExpr(CallExpr *CE) {
211 ++MatchCount;
212 CharSourceRange Call = Lexer::getAsCharRange(CE->getSourceRange(),
213 Context->getSourceManager(),
214 Context->getLangOpts());
215 EXPECT_THAT(maybeExtendRange(Call, tok::TokenKind::semi, *Context),
216 EqualsAnnotatedRange(Context, Code.range("r")));
217 return true;
220 ExtendCharRangeVisitor Visitor;
221 // Extends to include semicolon.
222 Visitor.runOverAnnotated("void f(int x, int y) { $r[[f(x, y);]] }");
223 // Does not extend to include semicolon.
224 Visitor.runOverAnnotated(
225 "int f(int x, int y) { if (0) return $r[[f(x, y)]] + 3; }");
228 TEST(SourceCodeTest, getAssociatedRange) {
229 struct VarDeclsVisitor : AnnotatedCodeVisitor<VarDeclsVisitor> {
230 bool VisitVarDecl(VarDecl *Decl) { return VisitDeclHelper(Decl); }
232 VarDeclsVisitor Visitor;
234 // Includes semicolon.
235 Visitor.runOverAnnotated("$r[[int x = 4;]]");
237 // Includes newline and semicolon.
238 Visitor.runOverAnnotated("$r[[int x = 4;\n]]");
240 // Includes trailing comments.
241 Visitor.runOverAnnotated("$r[[int x = 4; // Comment\n]]");
242 Visitor.runOverAnnotated("$r[[int x = 4; /* Comment */\n]]");
244 // Does *not* include trailing comments when another entity appears between
245 // the decl and the comment.
246 Visitor.runOverAnnotated("$r[[int x = 4;]] class C {}; // Comment\n");
248 // Includes attributes.
249 Visitor.runOverAnnotated(R"cpp(
250 $r[[__attribute__((deprecated("message")))
251 int x;]])cpp");
253 // Includes attributes and comments together.
254 Visitor.runOverAnnotated(R"cpp(
255 $r[[__attribute__((deprecated("message")))
256 // Comment.
257 int x;]])cpp");
259 // Includes attributes through macro expansion.
260 Visitor.runOverAnnotated(R"cpp(
261 #define MACRO_EXPANSION __attribute__((deprecated("message")))
262 $r[[MACRO_EXPANSION
263 int x;]])cpp");
265 // Includes attributes through macro expansion with comments.
266 Visitor.runOverAnnotated(R"cpp(
267 #define MACRO_EXPANSION __attribute__((deprecated("message")))
268 $r[[MACRO_EXPANSION
269 // Comment.
270 int x;]])cpp");
273 TEST(SourceCodeTest, getAssociatedRangeClasses) {
274 struct RecordDeclsVisitor : AnnotatedCodeVisitor<RecordDeclsVisitor> {
275 bool VisitRecordDecl(RecordDecl *Decl) { return VisitDeclHelper(Decl); }
277 RecordDeclsVisitor Visitor;
279 Visitor.runOverAnnotated("$r[[class A;]]");
280 Visitor.runOverAnnotated("$r[[class A {};]]");
282 // Includes leading template annotation.
283 Visitor.runOverAnnotated("$r[[template <typename T> class A;]]");
284 Visitor.runOverAnnotated("$r[[template <typename T> class A {};]]");
287 TEST(SourceCodeTest, getAssociatedRangeClassTemplateSpecializations) {
288 struct CXXRecordDeclsVisitor : AnnotatedCodeVisitor<CXXRecordDeclsVisitor> {
289 bool VisitCXXRecordDecl(CXXRecordDecl *Decl) {
290 return Decl->getTemplateSpecializationKind() !=
291 TSK_ExplicitSpecialization ||
292 VisitDeclHelper(Decl);
295 CXXRecordDeclsVisitor Visitor;
297 Visitor.runOverAnnotated(R"cpp(
298 template <typename T> class A{};
299 $r[[template <> class A<int>;]])cpp");
300 Visitor.runOverAnnotated(R"cpp(
301 template <typename T> class A{};
302 $r[[template <> class A<int> {};]])cpp");
305 TEST(SourceCodeTest, getAssociatedRangeFunctions) {
306 struct FunctionDeclsVisitor : AnnotatedCodeVisitor<FunctionDeclsVisitor> {
307 bool VisitFunctionDecl(FunctionDecl *Decl) { return VisitDeclHelper(Decl); }
309 FunctionDeclsVisitor Visitor;
311 Visitor.runOverAnnotated("$r[[int f();]]");
312 Visitor.runOverAnnotated("$r[[int f() { return 0; }]]");
313 // Includes leading template annotation.
314 Visitor.runOverAnnotated("$r[[template <typename T> int f();]]");
315 Visitor.runOverAnnotated("$r[[template <typename T> int f() { return 0; }]]");
318 TEST(SourceCodeTest, getAssociatedRangeMemberTemplates) {
319 struct CXXMethodDeclsVisitor : AnnotatedCodeVisitor<CXXMethodDeclsVisitor> {
320 bool VisitCXXMethodDecl(CXXMethodDecl *Decl) {
321 // Only consider the definition of the template.
322 return !Decl->doesThisDeclarationHaveABody() || VisitDeclHelper(Decl);
325 CXXMethodDeclsVisitor Visitor;
327 Visitor.runOverAnnotated(R"cpp(
328 template <typename C>
329 struct A { template <typename T> int member(T v); };
331 $r[[template <typename C>
332 template <typename T>
333 int A<C>::member(T v) { return 0; }]])cpp");
336 TEST(SourceCodeTest, getAssociatedRangeWithComments) {
337 struct VarDeclsVisitor : AnnotatedCodeVisitor<VarDeclsVisitor> {
338 bool VisitVarDecl(VarDecl *Decl) { return VisitDeclHelper(Decl); }
341 VarDeclsVisitor Visitor;
342 auto Visit = [&](llvm::StringRef AnnotatedCode) {
343 Visitor.runOverAnnotated(AnnotatedCode, {"-fparse-all-comments"});
346 // Includes leading comments.
347 Visit("$r[[// Comment.\nint x = 4;]]");
348 Visit("$r[[// Comment.\nint x = 4;\n]]");
349 Visit("$r[[/* Comment.*/\nint x = 4;\n]]");
350 // ... even if separated by (extra) horizontal whitespace.
351 Visit("$r[[/* Comment.*/ \nint x = 4;\n]]");
353 // Includes comments even in the presence of trailing whitespace.
354 Visit("$r[[// Comment.\nint x = 4;]] ");
356 // Includes comments when the declaration is followed by the beginning or end
357 // of a compound statement.
358 Visit(R"cpp(
359 void foo() {
360 $r[[/* C */
361 int x = 4;
362 ]]};)cpp");
363 Visit(R"cpp(
364 void foo() {
365 $r[[/* C */
366 int x = 4;
367 ]]{ class Foo {}; }
368 })cpp");
370 // Includes comments inside macros (when decl is in the same macro).
371 Visit(R"cpp(
372 #define DECL /* Comment */ int x
373 $r[[DECL;]])cpp");
375 // Does not include comments when only the decl or the comment come from a
376 // macro.
377 // FIXME: Change code to allow this.
378 Visit(R"cpp(
379 #define DECL int x
380 // Comment
381 $r[[DECL;]])cpp");
382 Visit(R"cpp(
383 #define COMMENT /* Comment */
384 COMMENT
385 $r[[int x;]])cpp");
387 // Includes multi-line comments.
388 Visit(R"cpp(
389 $r[[/* multi
390 * line
391 * comment
393 int x;]])cpp");
394 Visit(R"cpp(
395 $r[[// multi
396 // line
397 // comment
398 int x;]])cpp");
400 // Does not include comments separated by multiple empty lines.
401 Visit("// Comment.\n\n\n$r[[int x = 4;\n]]");
402 Visit("/* Comment.*/\n\n\n$r[[int x = 4;\n]]");
404 // Does not include comments before a *series* of declarations.
405 Visit(R"cpp(
406 // Comment.
407 $r[[int x = 4;
408 ]]class foo {};)cpp");
410 // Does not include IfThisThenThat comments
411 Visit("// LINT.IfChange.\n$r[[int x = 4;]]");
412 Visit("// LINT.ThenChange.\n$r[[int x = 4;]]");
414 // Includes attributes.
415 Visit(R"cpp(
416 $r[[__attribute__((deprecated("message")))
417 int x;]])cpp");
419 // Includes attributes and comments together.
420 Visit(R"cpp(
421 $r[[__attribute__((deprecated("message")))
422 // Comment.
423 int x;]])cpp");
425 // Includes attributes through macro expansion.
426 Visitor.runOverAnnotated(R"cpp(
427 #define MACRO_EXPANSION __attribute__((deprecated("message")))
428 $r[[MACRO_EXPANSION
429 int x;]])cpp");
431 // Includes attributes through macro expansion with comments.
432 Visitor.runOverAnnotated(R"cpp(
433 #define MACRO_EXPANSION __attribute__((deprecated("message")))
434 $r[[MACRO_EXPANSION
435 // Comment.
436 int x;]])cpp");
439 TEST(SourceCodeTest, getAssociatedRangeInvalidForPartialExpansions) {
440 struct FailingVarDeclsVisitor : TestVisitor<FailingVarDeclsVisitor> {
441 FailingVarDeclsVisitor() {}
442 bool VisitVarDecl(VarDecl *Decl) {
443 EXPECT_TRUE(getAssociatedRange(*Decl, *Context).isInvalid());
444 return true;
448 FailingVarDeclsVisitor Visitor;
449 // Should fail because it only includes a part of the expansion.
450 std::string Code = R"cpp(
451 #define DECL class foo { }; int x
452 DECL;)cpp";
453 Visitor.runOver(Code);
456 class GetFileRangeForEditTest : public testing::TestWithParam<bool> {};
457 INSTANTIATE_TEST_SUITE_P(WithAndWithoutExpansions, GetFileRangeForEditTest,
458 testing::Bool());
460 TEST_P(GetFileRangeForEditTest, EditRangeWithMacroExpansionsShouldSucceed) {
461 // The call expression, whose range we are extracting, includes two macro
462 // expansions.
463 llvm::Annotations Code(R"cpp(
464 #define M(a) a * 13
465 int foo(int x, int y);
466 int a = $r[[foo(M(1), M(2))]];
467 )cpp");
469 CallsVisitor Visitor;
470 Visitor.OnCall = [&Code](CallExpr *CE, ASTContext *Context) {
471 auto Range = CharSourceRange::getTokenRange(CE->getSourceRange());
472 EXPECT_THAT(getFileRangeForEdit(Range, *Context, GetParam()),
473 ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
475 Visitor.runOver(Code.code());
478 TEST(SourceCodeTest, EditWholeMacroExpansionShouldSucceed) {
479 llvm::Annotations Code(R"cpp(
480 #define FOO 10
481 int a = $r[[FOO]];
482 )cpp");
484 IntLitVisitor Visitor;
485 Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
486 auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
487 EXPECT_THAT(getFileRangeForEdit(Range, *Context),
488 ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
490 Visitor.runOver(Code.code());
493 TEST(SourceCodeTest, EditInvolvingExpansionIgnoringExpansionShouldFail) {
494 // If we specify to ignore macro expansions, none of these call expressions
495 // should have an editable range.
496 llvm::Annotations Code(R"cpp(
497 #define M1(x) x(1)
498 #define M2(x, y) x ## y
499 #define M3(x) foobar(x)
500 int foobar(int);
501 int a = M1(foobar);
502 int b = M2(foo, bar(2));
503 int c = M3(3);
504 )cpp");
506 CallsVisitor Visitor;
507 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
508 auto Range = CharSourceRange::getTokenRange(CE->getSourceRange());
509 EXPECT_FALSE(
510 getFileRangeForEdit(Range, *Context, /*IncludeMacroExpansion=*/false));
512 Visitor.runOver(Code.code());
515 TEST_P(GetFileRangeForEditTest, EditPartialMacroExpansionShouldFail) {
516 std::string Code = R"cpp(
517 #define BAR 10+
518 int c = BAR 3.0;
519 )cpp";
521 IntLitVisitor Visitor;
522 Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
523 auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
524 EXPECT_FALSE(getFileRangeForEdit(Range, *Context, GetParam()));
526 Visitor.runOver(Code);
529 TEST_P(GetFileRangeForEditTest, EditWholeMacroArgShouldSucceed) {
530 llvm::Annotations Code(R"cpp(
531 #define FOO(a) a + 7.0;
532 int a = FOO($r[[10]]);
533 )cpp");
535 IntLitVisitor Visitor;
536 Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
537 auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
538 EXPECT_THAT(getFileRangeForEdit(Range, *Context, GetParam()),
539 ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
541 Visitor.runOver(Code.code());
544 TEST_P(GetFileRangeForEditTest, EditPartialMacroArgShouldSucceed) {
545 llvm::Annotations Code(R"cpp(
546 #define FOO(a) a + 7.0;
547 int a = FOO($r[[10]] + 10.0);
548 )cpp");
550 IntLitVisitor Visitor;
551 Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
552 auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
553 EXPECT_THAT(getFileRangeForEdit(Range, *Context, GetParam()),
554 ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
556 Visitor.runOver(Code.code());
559 TEST(SourceCodeTest, EditRangeWithMacroExpansionsIsValid) {
560 // The call expression, whose range we are extracting, includes two macro
561 // expansions.
562 llvm::StringRef Code = R"cpp(
563 #define M(a) a * 13
564 int foo(int x, int y);
565 int a = foo(M(1), M(2));
566 )cpp";
568 CallsVisitor Visitor;
569 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
570 auto Range = CharSourceRange::getTokenRange(CE->getSourceRange());
571 EXPECT_THAT_ERROR(validateEditRange(Range, Context->getSourceManager()),
572 Succeeded());
574 Visitor.runOver(Code);
577 TEST(SourceCodeTest, SpellingRangeOfMacroArgIsValid) {
578 llvm::StringRef Code = R"cpp(
579 #define FOO(a) a + 7.0;
580 int a = FOO(10);
581 )cpp";
583 IntLitVisitor Visitor;
584 Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
585 SourceLocation ArgLoc =
586 Context->getSourceManager().getSpellingLoc(Expr->getBeginLoc());
587 // The integer literal is a single token.
588 auto ArgRange = CharSourceRange::getTokenRange(ArgLoc);
589 EXPECT_THAT_ERROR(validateEditRange(ArgRange, Context->getSourceManager()),
590 Succeeded());
592 Visitor.runOver(Code);
595 TEST(SourceCodeTest, InvalidEditRangeIsInvalid) {
596 llvm::StringRef Code = "int c = 10;";
598 // We use the visitor just to get a valid context.
599 IntLitVisitor Visitor;
600 Visitor.OnIntLit = [](IntegerLiteral *, ASTContext *Context) {
601 CharSourceRange Invalid;
602 EXPECT_THAT_ERROR(validateEditRange(Invalid, Context->getSourceManager()),
603 Failed());
605 Visitor.runOver(Code);
608 TEST(SourceCodeTest, InvertedEditRangeIsInvalid) {
609 llvm::StringRef Code = R"cpp(
610 int foo(int x);
611 int a = foo(2);
612 )cpp";
614 CallsVisitor Visitor;
615 Visitor.OnCall = [](CallExpr *Expr, ASTContext *Context) {
616 auto InvertedRange = CharSourceRange::getTokenRange(
617 SourceRange(Expr->getEndLoc(), Expr->getBeginLoc()));
618 EXPECT_THAT_ERROR(
619 validateEditRange(InvertedRange, Context->getSourceManager()),
620 Failed());
622 Visitor.runOver(Code);
625 TEST(SourceCodeTest, MacroArgIsInvalid) {
626 llvm::StringRef Code = R"cpp(
627 #define FOO(a) a + 7.0;
628 int a = FOO(10);
629 )cpp";
631 IntLitVisitor Visitor;
632 Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
633 auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
634 EXPECT_THAT_ERROR(validateEditRange(Range, Context->getSourceManager()),
635 Failed());
637 Visitor.runOver(Code);
640 TEST(SourceCodeTest, EditWholeMacroExpansionIsInvalid) {
641 llvm::StringRef Code = R"cpp(
642 #define FOO 10
643 int a = FOO;
644 )cpp";
646 IntLitVisitor Visitor;
647 Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
648 auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
649 EXPECT_THAT_ERROR(validateEditRange(Range, Context->getSourceManager()),
650 Failed());
653 Visitor.runOver(Code);
656 TEST(SourceCodeTest, EditPartialMacroExpansionIsInvalid) {
657 llvm::StringRef Code = R"cpp(
658 #define BAR 10+
659 int c = BAR 3.0;
660 )cpp";
662 IntLitVisitor Visitor;
663 Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
664 auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
665 EXPECT_THAT_ERROR(validateEditRange(Range, Context->getSourceManager()),
666 Failed());
668 Visitor.runOver(Code);
671 TEST(SourceCodeTest, GetCallReturnType_Dependent) {
672 llvm::Annotations Code{R"cpp(
673 template<class T, class F>
674 void templ(const T& t, F f) {}
676 template<class T, class F>
677 void templ1(const T& t, F f) {
678 $test1[[f(t)]];
681 int f_overload(int) { return 1; }
682 int f_overload(double) { return 2; }
684 void f1() {
685 int i = 0;
686 templ(i, [](const auto &p) {
687 $test2[[f_overload(p)]];
691 struct A {
692 void f_overload(int);
693 void f_overload(double);
696 void f2() {
697 int i = 0;
698 templ(i, [](const auto &p) {
699 A a;
700 $test3[[a.f_overload(p)]];
703 )cpp"};
705 llvm::Annotations::Range R1 = Code.range("test1");
706 llvm::Annotations::Range R2 = Code.range("test2");
707 llvm::Annotations::Range R3 = Code.range("test3");
709 CallsVisitor Visitor;
710 Visitor.OnCall = [&R1, &R2, &R3](CallExpr *Expr, ASTContext *Context) {
711 unsigned Begin = Context->getSourceManager().getFileOffset(
712 Expr->getSourceRange().getBegin());
713 unsigned End = Context->getSourceManager().getFileOffset(
714 Expr->getSourceRange().getEnd());
715 llvm::Annotations::Range R{Begin, End + 1};
717 QualType CalleeType = Expr->getCallee()->getType();
718 if (R == R1) {
719 ASSERT_TRUE(CalleeType->isDependentType());
720 EXPECT_EQ(Expr->getCallReturnType(*Context), Context->DependentTy);
721 } else if (R == R2) {
722 ASSERT_FALSE(CalleeType->isDependentType());
723 ASSERT_TRUE(CalleeType->isSpecificPlaceholderType(BuiltinType::Overload));
724 ASSERT_TRUE(isa<UnresolvedLookupExpr>(Expr->getCallee()));
725 EXPECT_EQ(Expr->getCallReturnType(*Context), Context->DependentTy);
726 } else if (R == R3) {
727 ASSERT_FALSE(CalleeType->isDependentType());
728 ASSERT_TRUE(
729 CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember));
730 ASSERT_TRUE(isa<UnresolvedMemberExpr>(Expr->getCallee()));
731 EXPECT_EQ(Expr->getCallReturnType(*Context), Context->DependentTy);
734 Visitor.runOver(Code.code(), CallsVisitor::Lang_CXX14);
737 } // end anonymous namespace