diff --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp --- a/clang/unittests/Tooling/TransformerTest.cpp +++ b/clang/unittests/Tooling/TransformerTest.cpp @@ -571,6 +571,54 @@ testRule(Rule, Input, Expected); } +// Demonstrates bug in applyFirst caused by +// https://reviews.llvm.org/D72534. This passes before and fails after. +TEST_F(TransformerTest, OrderedRuleUnrelatedIgnored) { + std::string Input = R"cc( + void f1(); + int f2(); + void call_f1() { f1(); } + float call_f2() { return f2(); } + )cc"; + std::string Expected = R"cc( + void f1(); + int f2(); + void call_f1() { REPLACE_F1; } + float call_f2() { return REPLACE_F2; } + )cc"; + + RewriteRule ReplaceF1 = + makeRule(callExpr(callee(functionDecl(hasName("f1")))), + changeTo(cat("REPLACE_F1"))); + RewriteRule ReplaceF2 = makeRule(castExpr(hasSourceExpression(callExpr())), + changeTo(cat("REPLACE_F2"))); + testRule(applyFirst({ReplaceF1, ReplaceF2}), Input, Expected); +} + +// BAD FIX: This attempt by the API client to fix the problem fails. +TEST_F(TransformerTest, OrderedRuleUnrelatedIgnoredBadFix) { + std::string Input = R"cc( + void f1(); + int f2(); + void call_f1() { f1(); } + float call_f2() { return f2(); } + )cc"; + std::string Expected = R"cc( + void f1(); + int f2(); + void call_f1() { REPLACE_F1; } + float call_f2() { return REPLACE_F2; } + )cc"; + + RewriteRule ReplaceF1 = + makeRule(callExpr(callee(functionDecl(hasName("f1")))), + changeTo(cat("REPLACE_F1"))); + RewriteRule ReplaceF2 = makeRule( + traverse(clang::TK_AsIs, castExpr(hasSourceExpression(callExpr()))), + changeTo(cat("REPLACE_F2"))); + testRule(applyFirst({ReplaceF1, ReplaceF2}), Input, Expected); +} + // // Negative tests (where we expect no transformation to occur). //