blob: 0bf012a9fc552e3ff80d0f04f420fbf28b4a71f7 [file] [log] [blame]
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +00001//===- unittest/Tooling/TransformerTest.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/Refactoring/Transformer.h"
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +000010#include "clang/ASTMatchers/ASTMatchers.h"
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +000011#include "clang/Tooling/Refactoring/RangeSelector.h"
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +000012#include "clang/Tooling/Tooling.h"
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +000013#include "llvm/Support/Errc.h"
14#include "llvm/Support/Error.h"
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +000015#include "gmock/gmock.h"
16#include "gtest/gtest.h"
17
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +000018using namespace clang;
19using namespace tooling;
20using namespace ast_matchers;
21
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +000022namespace {
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +000023using ::testing::IsEmpty;
24
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +000025constexpr char KHeaderContents[] = R"cc(
26 struct string {
27 string(const char*);
28 char* c_str();
29 int size();
30 };
31 int strlen(const char*);
32
33 namespace proto {
34 struct PCFProto {
35 int foo();
36 };
37 struct ProtoCommandLineFlag : PCFProto {
38 PCFProto& GetProto();
39 };
40 } // namespace proto
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +000041 class Logger {};
42 void operator<<(Logger& l, string msg);
43 Logger& log(int level);
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +000044)cc";
45
46static ast_matchers::internal::Matcher<clang::QualType>
47isOrPointsTo(const clang::ast_matchers::DeclarationMatcher &TypeMatcher) {
48 return anyOf(hasDeclaration(TypeMatcher), pointsTo(TypeMatcher));
49}
50
51static std::string format(StringRef Code) {
52 const std::vector<Range> Ranges(1, Range(0, Code.size()));
53 auto Style = format::getLLVMStyle();
54 const auto Replacements = format::reformat(Style, Code, Ranges);
55 auto Formatted = applyAllReplacements(Code, Replacements);
56 if (!Formatted) {
57 ADD_FAILURE() << "Could not format code: "
58 << llvm::toString(Formatted.takeError());
59 return std::string();
60 }
61 return *Formatted;
62}
63
64static void compareSnippets(StringRef Expected,
65 const llvm::Optional<std::string> &MaybeActual) {
66 ASSERT_TRUE(MaybeActual) << "Rewrite failed. Expecting: " << Expected;
67 auto Actual = *MaybeActual;
68 std::string HL = "#include \"header.h\"\n";
69 auto I = Actual.find(HL);
70 if (I != std::string::npos)
71 Actual.erase(I, HL.size());
72 EXPECT_EQ(format(Expected), format(Actual));
73}
74
75// FIXME: consider separating this class into its own file(s).
76class ClangRefactoringTestBase : public testing::Test {
77protected:
78 void appendToHeader(StringRef S) { FileContents[0].second += S; }
79
80 void addFile(StringRef Filename, StringRef Content) {
81 FileContents.emplace_back(Filename, Content);
82 }
83
84 llvm::Optional<std::string> rewrite(StringRef Input) {
85 std::string Code = ("#include \"header.h\"\n" + Input).str();
86 auto Factory = newFrontendActionFactory(&MatchFinder);
87 if (!runToolOnCodeWithArgs(
88 Factory->create(), Code, std::vector<std::string>(), "input.cc",
89 "clang-tool", std::make_shared<PCHContainerOperations>(),
90 FileContents)) {
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +000091 llvm::errs() << "Running tool failed.\n";
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +000092 return None;
93 }
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +000094 if (ErrorCount != 0) {
95 llvm::errs() << "Generating changes failed.\n";
96 return None;
97 }
98 auto ChangedCode =
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +000099 applyAtomicChanges("input.cc", Code, Changes, ApplyChangesSpec());
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000100 if (!ChangedCode) {
101 llvm::errs() << "Applying changes failed: "
102 << llvm::toString(ChangedCode.takeError()) << "\n";
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000103 return None;
104 }
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000105 return *ChangedCode;
106 }
107
108 Transformer::ChangeConsumer consumer() {
109 return [this](Expected<AtomicChange> C) {
110 if (C) {
111 Changes.push_back(std::move(*C));
112 } else {
113 consumeError(C.takeError());
114 ++ErrorCount;
115 }
116 };
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000117 }
118
Yitzhak Mandelbaum8369a9b2019-05-17 14:23:33 +0000119 template <typename R>
120 void testRule(R Rule, StringRef Input, StringRef Expected) {
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000121 Transformer T(std::move(Rule), consumer());
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000122 T.registerMatchers(&MatchFinder);
123 compareSnippets(Expected, rewrite(Input));
124 }
125
126 clang::ast_matchers::MatchFinder MatchFinder;
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000127 // Records whether any errors occurred in individual changes.
128 int ErrorCount = 0;
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000129 AtomicChanges Changes;
130
131private:
132 FileContentMappings FileContents = {{"header.h", ""}};
133};
134
135class TransformerTest : public ClangRefactoringTestBase {
136protected:
137 TransformerTest() { appendToHeader(KHeaderContents); }
138};
139
140// Given string s, change strlen($s.c_str()) to $s.size().
141static RewriteRule ruleStrlenSize() {
142 StringRef StringExpr = "strexpr";
143 auto StringType = namedDecl(hasAnyName("::basic_string", "::string"));
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000144 auto R = makeRule(
145 callExpr(callee(functionDecl(hasName("strlen"))),
146 hasArgument(0, cxxMemberCallExpr(
147 on(expr(hasType(isOrPointsTo(StringType)))
148 .bind(StringExpr)),
149 callee(cxxMethodDecl(hasName("c_str")))))),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000150 change(text("REPLACED")));
Yitzhak Mandelbaum8369a9b2019-05-17 14:23:33 +0000151 R.Cases[0].Explanation = text("Use size() method directly on string.");
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000152 return R;
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000153}
154
155TEST_F(TransformerTest, StrlenSize) {
156 std::string Input = "int f(string s) { return strlen(s.c_str()); }";
157 std::string Expected = "int f(string s) { return REPLACED; }";
158 testRule(ruleStrlenSize(), Input, Expected);
159}
160
161// Tests that no change is applied when a match is not expected.
162TEST_F(TransformerTest, NoMatch) {
163 std::string Input = "int f(string s) { return s.size(); }";
164 testRule(ruleStrlenSize(), Input, Input);
165}
166
167// Tests that expressions in macro arguments are rewritten (when applicable).
168TEST_F(TransformerTest, StrlenSizeMacro) {
169 std::string Input = R"cc(
170#define ID(e) e
171 int f(string s) { return ID(strlen(s.c_str())); })cc";
172 std::string Expected = R"cc(
173#define ID(e) e
174 int f(string s) { return ID(REPLACED); })cc";
175 testRule(ruleStrlenSize(), Input, Expected);
176}
177
178// Tests replacing an expression.
179TEST_F(TransformerTest, Flag) {
180 StringRef Flag = "flag";
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000181 RewriteRule Rule = makeRule(
182 cxxMemberCallExpr(on(expr(hasType(cxxRecordDecl(
183 hasName("proto::ProtoCommandLineFlag"))))
184 .bind(Flag)),
185 unless(callee(cxxMethodDecl(hasName("GetProto"))))),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000186 change(node(Flag), text("EXPR")));
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000187
188 std::string Input = R"cc(
189 proto::ProtoCommandLineFlag flag;
190 int x = flag.foo();
191 int y = flag.GetProto().foo();
192 )cc";
193 std::string Expected = R"cc(
194 proto::ProtoCommandLineFlag flag;
195 int x = EXPR.foo();
196 int y = flag.GetProto().foo();
197 )cc";
198
199 testRule(std::move(Rule), Input, Expected);
200}
201
202TEST_F(TransformerTest, NodePartNameNamedDecl) {
203 StringRef Fun = "fun";
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000204 RewriteRule Rule = makeRule(functionDecl(hasName("bad")).bind(Fun),
205 change(name(Fun), text("good")));
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000206
207 std::string Input = R"cc(
208 int bad(int x);
209 int bad(int x) { return x * x; }
210 )cc";
211 std::string Expected = R"cc(
212 int good(int x);
213 int good(int x) { return x * x; }
214 )cc";
215
216 testRule(Rule, Input, Expected);
217}
218
219TEST_F(TransformerTest, NodePartNameDeclRef) {
220 std::string Input = R"cc(
221 template <typename T>
222 T bad(T x) {
223 return x;
224 }
225 int neutral(int x) { return bad<int>(x) * x; }
226 )cc";
227 std::string Expected = R"cc(
228 template <typename T>
229 T bad(T x) {
230 return x;
231 }
232 int neutral(int x) { return good<int>(x) * x; }
233 )cc";
234
235 StringRef Ref = "ref";
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000236 testRule(makeRule(declRefExpr(to(functionDecl(hasName("bad")))).bind(Ref),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000237 change(name(Ref), text("good"))),
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000238 Input, Expected);
239}
240
241TEST_F(TransformerTest, NodePartNameDeclRefFailure) {
242 std::string Input = R"cc(
243 struct Y {
244 int operator*();
245 };
246 int neutral(int x) {
247 Y y;
248 int (Y::*ptr)() = &Y::operator*;
249 return *y + x;
250 }
251 )cc";
252
253 StringRef Ref = "ref";
Yitzhak Mandelbauma5dadbe2019-04-30 17:24:36 +0000254 Transformer T(makeRule(declRefExpr(to(functionDecl())).bind(Ref),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000255 change(name(Ref), text("good"))),
Yitzhak Mandelbauma5dadbe2019-04-30 17:24:36 +0000256 consumer());
257 T.registerMatchers(&MatchFinder);
258 EXPECT_FALSE(rewrite(Input));
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000259}
260
261TEST_F(TransformerTest, NodePartMember) {
262 StringRef E = "expr";
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000263 RewriteRule Rule = makeRule(memberExpr(member(hasName("bad"))).bind(E),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000264 change(member(E), text("good")));
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000265
266 std::string Input = R"cc(
267 struct S {
268 int bad;
269 };
270 int g() {
271 S s;
272 return s.bad;
273 }
274 )cc";
275 std::string Expected = R"cc(
276 struct S {
277 int bad;
278 };
279 int g() {
280 S s;
281 return s.good;
282 }
283 )cc";
284
285 testRule(Rule, Input, Expected);
286}
287
288TEST_F(TransformerTest, NodePartMemberQualified) {
289 std::string Input = R"cc(
290 struct S {
291 int bad;
292 int good;
293 };
294 struct T : public S {
295 int bad;
296 };
297 int g() {
298 T t;
299 return t.S::bad;
300 }
301 )cc";
302 std::string Expected = R"cc(
303 struct S {
304 int bad;
305 int good;
306 };
307 struct T : public S {
308 int bad;
309 };
310 int g() {
311 T t;
312 return t.S::good;
313 }
314 )cc";
315
316 StringRef E = "expr";
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000317 testRule(makeRule(memberExpr().bind(E), change(member(E), text("good"))),
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000318 Input, Expected);
319}
320
321TEST_F(TransformerTest, NodePartMemberMultiToken) {
322 std::string Input = R"cc(
323 struct Y {
324 int operator*();
325 int good();
326 template <typename T> void foo(T t);
327 };
328 int neutral(int x) {
329 Y y;
330 y.template foo<int>(3);
331 return y.operator *();
332 }
333 )cc";
334 std::string Expected = R"cc(
335 struct Y {
336 int operator*();
337 int good();
338 template <typename T> void foo(T t);
339 };
340 int neutral(int x) {
341 Y y;
342 y.template good<int>(3);
343 return y.good();
344 }
345 )cc";
346
347 StringRef MemExpr = "member";
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000348 testRule(makeRule(memberExpr().bind(MemExpr),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000349 change(member(MemExpr), text("good"))),
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000350 Input, Expected);
351}
352
353TEST_F(TransformerTest, MultiChange) {
354 std::string Input = R"cc(
355 void foo() {
356 if (10 > 1.0)
357 log(1) << "oh no!";
358 else
359 log(0) << "ok";
360 }
361 )cc";
362 std::string Expected = R"(
363 void foo() {
364 if (true) { /* then */ }
365 else { /* else */ }
366 }
367 )";
368
369 StringRef C = "C", T = "T", E = "E";
370 testRule(makeRule(ifStmt(hasCondition(expr().bind(C)),
371 hasThen(stmt().bind(T)), hasElse(stmt().bind(E))),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000372 {change(node(C), text("true")),
373 change(statement(T), text("{ /* then */ }")),
374 change(statement(E), text("{ /* else */ }"))}),
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000375 Input, Expected);
376}
377
Yitzhak Mandelbaum8369a9b2019-05-17 14:23:33 +0000378TEST_F(TransformerTest, OrderedRuleUnrelated) {
379 StringRef Flag = "flag";
380 RewriteRule FlagRule = makeRule(
381 cxxMemberCallExpr(on(expr(hasType(cxxRecordDecl(
382 hasName("proto::ProtoCommandLineFlag"))))
383 .bind(Flag)),
384 unless(callee(cxxMethodDecl(hasName("GetProto"))))),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000385 change(node(Flag), text("PROTO")));
Yitzhak Mandelbaum8369a9b2019-05-17 14:23:33 +0000386
387 std::string Input = R"cc(
388 proto::ProtoCommandLineFlag flag;
389 int x = flag.foo();
390 int y = flag.GetProto().foo();
391 int f(string s) { return strlen(s.c_str()); }
392 )cc";
393 std::string Expected = R"cc(
394 proto::ProtoCommandLineFlag flag;
395 int x = PROTO.foo();
396 int y = flag.GetProto().foo();
397 int f(string s) { return REPLACED; }
398 )cc";
399
400 testRule(applyFirst({ruleStrlenSize(), FlagRule}), Input, Expected);
401}
402
403// Version of ruleStrlenSizeAny that inserts a method with a different name than
404// ruleStrlenSize, so we can tell their effect apart.
405RewriteRule ruleStrlenSizeDistinct() {
406 StringRef S;
407 return makeRule(
408 callExpr(callee(functionDecl(hasName("strlen"))),
409 hasArgument(0, cxxMemberCallExpr(
410 on(expr().bind(S)),
411 callee(cxxMethodDecl(hasName("c_str")))))),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000412 change(text("DISTINCT")));
Yitzhak Mandelbaum8369a9b2019-05-17 14:23:33 +0000413}
414
415TEST_F(TransformerTest, OrderedRuleRelated) {
416 std::string Input = R"cc(
417 namespace foo {
418 struct mystring {
419 char* c_str();
420 };
421 int f(mystring s) { return strlen(s.c_str()); }
422 } // namespace foo
423 int g(string s) { return strlen(s.c_str()); }
424 )cc";
425 std::string Expected = R"cc(
426 namespace foo {
427 struct mystring {
428 char* c_str();
429 };
430 int f(mystring s) { return DISTINCT; }
431 } // namespace foo
432 int g(string s) { return REPLACED; }
433 )cc";
434
435 testRule(applyFirst({ruleStrlenSize(), ruleStrlenSizeDistinct()}), Input,
436 Expected);
437}
438
439// Change the order of the rules to get a different result.
440TEST_F(TransformerTest, OrderedRuleRelatedSwapped) {
441 std::string Input = R"cc(
442 namespace foo {
443 struct mystring {
444 char* c_str();
445 };
446 int f(mystring s) { return strlen(s.c_str()); }
447 } // namespace foo
448 int g(string s) { return strlen(s.c_str()); }
449 )cc";
450 std::string Expected = R"cc(
451 namespace foo {
452 struct mystring {
453 char* c_str();
454 };
455 int f(mystring s) { return DISTINCT; }
456 } // namespace foo
457 int g(string s) { return DISTINCT; }
458 )cc";
459
460 testRule(applyFirst({ruleStrlenSizeDistinct(), ruleStrlenSize()}), Input,
461 Expected);
462}
463
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000464//
465// Negative tests (where we expect no transformation to occur).
466//
467
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000468// Tests for a conflict in edits from a single match for a rule.
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000469TEST_F(TransformerTest, TextGeneratorFailure) {
470 std::string Input = "int conflictOneRule() { return 3 + 7; }";
471 // Try to change the whole binary-operator expression AND one its operands:
472 StringRef O = "O";
473 auto AlwaysFail = [](const ast_matchers::MatchFinder::MatchResult &)
474 -> llvm::Expected<std::string> {
475 return llvm::createStringError(llvm::errc::invalid_argument, "ERROR");
476 };
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000477 Transformer T(makeRule(binaryOperator().bind(O), change(node(O), AlwaysFail)),
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000478 consumer());
479 T.registerMatchers(&MatchFinder);
480 EXPECT_FALSE(rewrite(Input));
481 EXPECT_THAT(Changes, IsEmpty());
482 EXPECT_EQ(ErrorCount, 1);
483}
484
485// Tests for a conflict in edits from a single match for a rule.
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000486TEST_F(TransformerTest, OverlappingEditsInRule) {
487 std::string Input = "int conflictOneRule() { return 3 + 7; }";
488 // Try to change the whole binary-operator expression AND one its operands:
489 StringRef O = "O", L = "L";
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000490 Transformer T(makeRule(binaryOperator(hasLHS(expr().bind(L))).bind(O),
491 {change(node(O), text("DELETE_OP")),
492 change(node(L), text("DELETE_LHS"))}),
493 consumer());
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000494 T.registerMatchers(&MatchFinder);
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000495 EXPECT_FALSE(rewrite(Input));
496 EXPECT_THAT(Changes, IsEmpty());
497 EXPECT_EQ(ErrorCount, 1);
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000498}
499
500// Tests for a conflict in edits across multiple matches (of the same rule).
501TEST_F(TransformerTest, OverlappingEditsMultipleMatches) {
502 std::string Input = "int conflictOneRule() { return -7; }";
503 // Try to change the whole binary-operator expression AND one its operands:
504 StringRef E = "E";
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000505 Transformer T(makeRule(expr().bind(E), change(node(E), text("DELETE_EXPR"))),
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000506 consumer());
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000507 T.registerMatchers(&MatchFinder);
508 // The rewrite process fails because the changes conflict with each other...
509 EXPECT_FALSE(rewrite(Input));
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000510 // ... but two changes were produced.
511 EXPECT_EQ(Changes.size(), 2u);
512 EXPECT_EQ(ErrorCount, 0);
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000513}
514
515TEST_F(TransformerTest, ErrorOccurredMatchSkipped) {
516 // Syntax error in the function body:
517 std::string Input = "void errorOccurred() { 3 }";
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000518 Transformer T(makeRule(functionDecl(hasName("errorOccurred")),
Yitzhak Mandelbaum3ec50e22019-05-22 14:48:19 +0000519 change(text("DELETED;"))),
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000520 consumer());
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000521 T.registerMatchers(&MatchFinder);
522 // The rewrite process itself fails...
523 EXPECT_FALSE(rewrite(Input));
Yitzhak Mandelbaumaecc59c2019-04-30 16:48:33 +0000524 // ... and no changes or errors are produced in the process.
525 EXPECT_THAT(Changes, IsEmpty());
526 EXPECT_EQ(ErrorCount, 0);
Yitzhak Mandelbaumfa1552e2019-04-18 17:52:24 +0000527}
528
Yitzhak Mandelbaumfdd98782019-04-05 15:14:05 +0000529TEST_F(TransformerTest, NoTransformationInMacro) {
530 std::string Input = R"cc(
531#define MACRO(str) strlen((str).c_str())
532 int f(string s) { return MACRO(s); })cc";
533 testRule(ruleStrlenSize(), Input, Input);
534}
535
536// This test handles the corner case where a macro called within another macro
537// expands to matching code, but the matched code is an argument to the nested
538// macro. A simple check of isMacroArgExpansion() vs. isMacroBodyExpansion()
539// will get this wrong, and transform the code. This test verifies that no such
540// transformation occurs.
541TEST_F(TransformerTest, NoTransformationInNestedMacro) {
542 std::string Input = R"cc(
543#define NESTED(e) e
544#define MACRO(str) NESTED(strlen((str).c_str()))
545 int f(string s) { return MACRO(s); })cc";
546 testRule(ruleStrlenSize(), Input, Input);
547}
548} // namespace