Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 1 | //===- 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 Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 10 | #include "clang/ASTMatchers/ASTMatchers.h" |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 11 | #include "clang/Tooling/Refactoring/RangeSelector.h" |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 12 | #include "clang/Tooling/Tooling.h" |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Errc.h" |
| 14 | #include "llvm/Support/Error.h" |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 15 | #include "gmock/gmock.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | using namespace tooling; |
| 20 | using namespace ast_matchers; |
| 21 | |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 22 | namespace { |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 23 | using ::testing::IsEmpty; |
| 24 | |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 25 | constexpr 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 Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 41 | class Logger {}; |
| 42 | void operator<<(Logger& l, string msg); |
| 43 | Logger& log(int level); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 44 | )cc"; |
| 45 | |
| 46 | static ast_matchers::internal::Matcher<clang::QualType> |
| 47 | isOrPointsTo(const clang::ast_matchers::DeclarationMatcher &TypeMatcher) { |
| 48 | return anyOf(hasDeclaration(TypeMatcher), pointsTo(TypeMatcher)); |
| 49 | } |
| 50 | |
| 51 | static 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 | |
| 64 | static 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). |
| 76 | class ClangRefactoringTestBase : public testing::Test { |
| 77 | protected: |
| 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 Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 91 | llvm::errs() << "Running tool failed.\n"; |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 92 | return None; |
| 93 | } |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 94 | if (ErrorCount != 0) { |
| 95 | llvm::errs() << "Generating changes failed.\n"; |
| 96 | return None; |
| 97 | } |
| 98 | auto ChangedCode = |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 99 | applyAtomicChanges("input.cc", Code, Changes, ApplyChangesSpec()); |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 100 | if (!ChangedCode) { |
| 101 | llvm::errs() << "Applying changes failed: " |
| 102 | << llvm::toString(ChangedCode.takeError()) << "\n"; |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 103 | return None; |
| 104 | } |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 105 | 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 Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Yitzhak Mandelbaum | 8369a9b | 2019-05-17 14:23:33 +0000 | [diff] [blame] | 119 | template <typename R> |
| 120 | void testRule(R Rule, StringRef Input, StringRef Expected) { |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 121 | Transformer T(std::move(Rule), consumer()); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 122 | T.registerMatchers(&MatchFinder); |
| 123 | compareSnippets(Expected, rewrite(Input)); |
| 124 | } |
| 125 | |
| 126 | clang::ast_matchers::MatchFinder MatchFinder; |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 127 | // Records whether any errors occurred in individual changes. |
| 128 | int ErrorCount = 0; |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 129 | AtomicChanges Changes; |
| 130 | |
| 131 | private: |
| 132 | FileContentMappings FileContents = {{"header.h", ""}}; |
| 133 | }; |
| 134 | |
| 135 | class TransformerTest : public ClangRefactoringTestBase { |
| 136 | protected: |
| 137 | TransformerTest() { appendToHeader(KHeaderContents); } |
| 138 | }; |
| 139 | |
| 140 | // Given string s, change strlen($s.c_str()) to $s.size(). |
| 141 | static RewriteRule ruleStrlenSize() { |
| 142 | StringRef StringExpr = "strexpr"; |
| 143 | auto StringType = namedDecl(hasAnyName("::basic_string", "::string")); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 144 | 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 Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 150 | change(text("REPLACED"))); |
Yitzhak Mandelbaum | 8369a9b | 2019-05-17 14:23:33 +0000 | [diff] [blame] | 151 | R.Cases[0].Explanation = text("Use size() method directly on string."); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 152 | return R; |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TEST_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. |
| 162 | TEST_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). |
| 168 | TEST_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. |
| 179 | TEST_F(TransformerTest, Flag) { |
| 180 | StringRef Flag = "flag"; |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 181 | RewriteRule Rule = makeRule( |
| 182 | cxxMemberCallExpr(on(expr(hasType(cxxRecordDecl( |
| 183 | hasName("proto::ProtoCommandLineFlag")))) |
| 184 | .bind(Flag)), |
| 185 | unless(callee(cxxMethodDecl(hasName("GetProto"))))), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 186 | change(node(Flag), text("EXPR"))); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 187 | |
| 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 | |
| 202 | TEST_F(TransformerTest, NodePartNameNamedDecl) { |
| 203 | StringRef Fun = "fun"; |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 204 | RewriteRule Rule = makeRule(functionDecl(hasName("bad")).bind(Fun), |
| 205 | change(name(Fun), text("good"))); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 206 | |
| 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 | |
| 219 | TEST_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 Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 236 | testRule(makeRule(declRefExpr(to(functionDecl(hasName("bad")))).bind(Ref), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 237 | change(name(Ref), text("good"))), |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 238 | Input, Expected); |
| 239 | } |
| 240 | |
| 241 | TEST_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 Mandelbaum | a5dadbe | 2019-04-30 17:24:36 +0000 | [diff] [blame] | 254 | Transformer T(makeRule(declRefExpr(to(functionDecl())).bind(Ref), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 255 | change(name(Ref), text("good"))), |
Yitzhak Mandelbaum | a5dadbe | 2019-04-30 17:24:36 +0000 | [diff] [blame] | 256 | consumer()); |
| 257 | T.registerMatchers(&MatchFinder); |
| 258 | EXPECT_FALSE(rewrite(Input)); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | TEST_F(TransformerTest, NodePartMember) { |
| 262 | StringRef E = "expr"; |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 263 | RewriteRule Rule = makeRule(memberExpr(member(hasName("bad"))).bind(E), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 264 | change(member(E), text("good"))); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 265 | |
| 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 | |
| 288 | TEST_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 Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 317 | testRule(makeRule(memberExpr().bind(E), change(member(E), text("good"))), |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 318 | Input, Expected); |
| 319 | } |
| 320 | |
| 321 | TEST_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 Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 348 | testRule(makeRule(memberExpr().bind(MemExpr), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 349 | change(member(MemExpr), text("good"))), |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 350 | Input, Expected); |
| 351 | } |
| 352 | |
| 353 | TEST_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 Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 372 | {change(node(C), text("true")), |
| 373 | change(statement(T), text("{ /* then */ }")), |
| 374 | change(statement(E), text("{ /* else */ }"))}), |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 375 | Input, Expected); |
| 376 | } |
| 377 | |
Yitzhak Mandelbaum | 8369a9b | 2019-05-17 14:23:33 +0000 | [diff] [blame] | 378 | TEST_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 Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 385 | change(node(Flag), text("PROTO"))); |
Yitzhak Mandelbaum | 8369a9b | 2019-05-17 14:23:33 +0000 | [diff] [blame] | 386 | |
| 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. |
| 405 | RewriteRule 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 Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 412 | change(text("DISTINCT"))); |
Yitzhak Mandelbaum | 8369a9b | 2019-05-17 14:23:33 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | TEST_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. |
| 440 | TEST_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 Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 464 | // |
| 465 | // Negative tests (where we expect no transformation to occur). |
| 466 | // |
| 467 | |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 468 | // Tests for a conflict in edits from a single match for a rule. |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 469 | TEST_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 Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 477 | Transformer T(makeRule(binaryOperator().bind(O), change(node(O), AlwaysFail)), |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 478 | 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 Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 486 | TEST_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 Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 490 | 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 Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 494 | T.registerMatchers(&MatchFinder); |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 495 | EXPECT_FALSE(rewrite(Input)); |
| 496 | EXPECT_THAT(Changes, IsEmpty()); |
| 497 | EXPECT_EQ(ErrorCount, 1); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // Tests for a conflict in edits across multiple matches (of the same rule). |
| 501 | TEST_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 Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 505 | Transformer T(makeRule(expr().bind(E), change(node(E), text("DELETE_EXPR"))), |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 506 | consumer()); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 507 | T.registerMatchers(&MatchFinder); |
| 508 | // The rewrite process fails because the changes conflict with each other... |
| 509 | EXPECT_FALSE(rewrite(Input)); |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 510 | // ... but two changes were produced. |
| 511 | EXPECT_EQ(Changes.size(), 2u); |
| 512 | EXPECT_EQ(ErrorCount, 0); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | TEST_F(TransformerTest, ErrorOccurredMatchSkipped) { |
| 516 | // Syntax error in the function body: |
| 517 | std::string Input = "void errorOccurred() { 3 }"; |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 518 | Transformer T(makeRule(functionDecl(hasName("errorOccurred")), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 519 | change(text("DELETED;"))), |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 520 | consumer()); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 521 | T.registerMatchers(&MatchFinder); |
| 522 | // The rewrite process itself fails... |
| 523 | EXPECT_FALSE(rewrite(Input)); |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 524 | // ... and no changes or errors are produced in the process. |
| 525 | EXPECT_THAT(Changes, IsEmpty()); |
| 526 | EXPECT_EQ(ErrorCount, 0); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 529 | TEST_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. |
| 541 | TEST_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 |