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 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Tooling/Transformer/SourceCode.h"
10 #include "TestVisitor.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "llvm/Testing/Support/Annotations.h"
13 #include "llvm/Testing/Support/SupportHelpers.h"
14 #include <gmock/gmock.h>
15 #include <gtest/gtest.h>
16 
17 using namespace clang;
18 
19 using llvm::ValueIs;
20 using tooling::getExtendedText;
21 using tooling::getRangeForEdit;
22 using tooling::getText;
23 
24 namespace {
25 
26 struct IntLitVisitor : TestVisitor<IntLitVisitor> {
27   bool VisitIntegerLiteral(IntegerLiteral *Expr) {
28     OnIntLit(Expr, Context);
29     return true;
30   }
31 
32   std::function<void(IntegerLiteral *, ASTContext *Context)> OnIntLit;
33 };
34 
35 struct CallsVisitor : TestVisitor<CallsVisitor> {
36   bool VisitCallExpr(CallExpr *Expr) {
37     OnCall(Expr, Context);
38     return true;
39   }
40 
41   std::function<void(CallExpr *, ASTContext *Context)> OnCall;
42 };
43 
44 // Equality matcher for `clang::CharSourceRange`, which lacks `operator==`.
45 MATCHER_P(EqualsRange, R, "") {
46   return arg.isTokenRange() == R.isTokenRange() &&
47          arg.getBegin() == R.getBegin() && arg.getEnd() == R.getEnd();
48 }
49 
50 static ::testing::Matcher<CharSourceRange> AsRange(const SourceManager &SM,
51                                                    llvm::Annotations::Range R) {
52   return EqualsRange(CharSourceRange::getCharRange(
53       SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(R.Begin),
54       SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(R.End)));
55 }
56 
57 TEST(SourceCodeTest, getText) {
58   CallsVisitor Visitor;
59 
60   Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
61     EXPECT_EQ("foo(x, y)", getText(*CE, *Context));
62   };
63   Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
64 
65   Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
66     EXPECT_EQ("APPLY(foo, x, y)", getText(*CE, *Context));
67   };
68   Visitor.runOver("#define APPLY(f, x, y) f(x, y)\n"
69                   "void foo(int x, int y) { APPLY(foo, x, y); }");
70 }
71 
72 TEST(SourceCodeTest, getTextWithMacro) {
73   CallsVisitor Visitor;
74 
75   Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
76     EXPECT_EQ("F OO", getText(*CE, *Context));
77     Expr *P0 = CE->getArg(0);
78     Expr *P1 = CE->getArg(1);
79     EXPECT_EQ("", getText(*P0, *Context));
80     EXPECT_EQ("", getText(*P1, *Context));
81   };
82   Visitor.runOver("#define F foo(\n"
83                   "#define OO x, y)\n"
84                   "void foo(int x, int y) { F OO ; }");
85 
86   Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
87     EXPECT_EQ("", getText(*CE, *Context));
88     Expr *P0 = CE->getArg(0);
89     Expr *P1 = CE->getArg(1);
90     EXPECT_EQ("x", getText(*P0, *Context));
91     EXPECT_EQ("y", getText(*P1, *Context));
92   };
93   Visitor.runOver("#define FOO(x, y) (void)x; (void)y; foo(x, y);\n"
94                   "void foo(int x, int y) { FOO(x,y) }");
95 }
96 
97 TEST(SourceCodeTest, getExtendedText) {
98   CallsVisitor Visitor;
99 
100   Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
101     EXPECT_EQ("foo(x, y);",
102               getExtendedText(*CE, tok::TokenKind::semi, *Context));
103 
104     Expr *P0 = CE->getArg(0);
105     Expr *P1 = CE->getArg(1);
106     EXPECT_EQ("x", getExtendedText(*P0, tok::TokenKind::semi, *Context));
107     EXPECT_EQ("x,", getExtendedText(*P0, tok::TokenKind::comma, *Context));
108     EXPECT_EQ("y", getExtendedText(*P1, tok::TokenKind::semi, *Context));
109   };
110   Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
111   Visitor.runOver("void foo(int x, int y) { if (true) foo(x, y); }");
112   Visitor.runOver("int foo(int x, int y) { if (true) return 3 + foo(x, y); }");
113   Visitor.runOver("void foo(int x, int y) { for (foo(x, y);;) ++x; }");
114   Visitor.runOver(
115       "bool foo(int x, int y) { for (;foo(x, y);) x = 1; return true; }");
116 
117   Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
118     EXPECT_EQ("foo()", getExtendedText(*CE, tok::TokenKind::semi, *Context));
119   };
120   Visitor.runOver("bool foo() { if (foo()) return true; return false; }");
121   Visitor.runOver("void foo() { int x; for (;; foo()) ++x; }");
122   Visitor.runOver("int foo() { return foo() + 3; }");
123 }
124 
125 TEST(SourceCodeTest, EditRangeWithMacroExpansionsShouldSucceed) {
126   // The call expression, whose range we are extracting, includes two macro
127   // expansions.
128   llvm::Annotations Code(R"cpp(
129 #define M(a) a * 13
130 int foo(int x, int y);
131 int a = $r[[foo(M(1), M(2))]];
132 )cpp");
133 
134   CallsVisitor Visitor;
135 
136   Visitor.OnCall = [&Code](CallExpr *CE, ASTContext *Context) {
137     auto Range = CharSourceRange::getTokenRange(CE->getSourceRange());
138     EXPECT_THAT(getRangeForEdit(Range, *Context),
139                 ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
140   };
141   Visitor.runOver(Code.code());
142 }
143 
144 TEST(SourceCodeTest, EditWholeMacroExpansionShouldSucceed) {
145   llvm::Annotations Code(R"cpp(
146 #define FOO 10
147 int a = $r[[FOO]];
148 )cpp");
149 
150   IntLitVisitor Visitor;
151   Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
152     auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
153     EXPECT_THAT(getRangeForEdit(Range, *Context),
154                 ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
155   };
156   Visitor.runOver(Code.code());
157 }
158 
159 TEST(SourceCodeTest, EditPartialMacroExpansionShouldFail) {
160   std::string Code = R"cpp(
161 #define BAR 10+
162 int c = BAR 3.0;
163 )cpp";
164 
165   IntLitVisitor Visitor;
166   Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
167     auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
168     EXPECT_FALSE(getRangeForEdit(Range, *Context).hasValue());
169   };
170   Visitor.runOver(Code);
171 }
172 
173 TEST(SourceCodeTest, EditWholeMacroArgShouldSucceed) {
174   llvm::Annotations Code(R"cpp(
175 #define FOO(a) a + 7.0;
176 int a = FOO($r[[10]]);
177 )cpp");
178 
179   IntLitVisitor Visitor;
180   Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
181     auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
182     EXPECT_THAT(getRangeForEdit(Range, *Context),
183                 ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
184   };
185   Visitor.runOver(Code.code());
186 }
187 
188 TEST(SourceCodeTest, EditPartialMacroArgShouldSucceed) {
189   llvm::Annotations Code(R"cpp(
190 #define FOO(a) a + 7.0;
191 int a = FOO($r[[10]] + 10.0);
192 )cpp");
193 
194   IntLitVisitor Visitor;
195   Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
196     auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
197     EXPECT_THAT(getRangeForEdit(Range, *Context),
198                 ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
199   };
200   Visitor.runOver(Code.code());
201 }
202 
203 } // end anonymous namespace
204