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 | fab7205 | 2019-05-24 15:11:45 +0000 | [diff] [blame] | 150 | change(text("REPLACED")), text("Use size() method directly on string.")); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 151 | return R; |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST_F(TransformerTest, StrlenSize) { |
| 155 | std::string Input = "int f(string s) { return strlen(s.c_str()); }"; |
| 156 | std::string Expected = "int f(string s) { return REPLACED; }"; |
| 157 | testRule(ruleStrlenSize(), Input, Expected); |
| 158 | } |
| 159 | |
| 160 | // Tests that no change is applied when a match is not expected. |
| 161 | TEST_F(TransformerTest, NoMatch) { |
| 162 | std::string Input = "int f(string s) { return s.size(); }"; |
| 163 | testRule(ruleStrlenSize(), Input, Input); |
| 164 | } |
| 165 | |
| 166 | // Tests that expressions in macro arguments are rewritten (when applicable). |
| 167 | TEST_F(TransformerTest, StrlenSizeMacro) { |
| 168 | std::string Input = R"cc( |
| 169 | #define ID(e) e |
| 170 | int f(string s) { return ID(strlen(s.c_str())); })cc"; |
| 171 | std::string Expected = R"cc( |
| 172 | #define ID(e) e |
| 173 | int f(string s) { return ID(REPLACED); })cc"; |
| 174 | testRule(ruleStrlenSize(), Input, Expected); |
| 175 | } |
| 176 | |
| 177 | // Tests replacing an expression. |
| 178 | TEST_F(TransformerTest, Flag) { |
| 179 | StringRef Flag = "flag"; |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 180 | RewriteRule Rule = makeRule( |
| 181 | cxxMemberCallExpr(on(expr(hasType(cxxRecordDecl( |
| 182 | hasName("proto::ProtoCommandLineFlag")))) |
| 183 | .bind(Flag)), |
| 184 | unless(callee(cxxMethodDecl(hasName("GetProto"))))), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 185 | change(node(Flag), text("EXPR"))); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 186 | |
| 187 | std::string Input = R"cc( |
| 188 | proto::ProtoCommandLineFlag flag; |
| 189 | int x = flag.foo(); |
| 190 | int y = flag.GetProto().foo(); |
| 191 | )cc"; |
| 192 | std::string Expected = R"cc( |
| 193 | proto::ProtoCommandLineFlag flag; |
| 194 | int x = EXPR.foo(); |
| 195 | int y = flag.GetProto().foo(); |
| 196 | )cc"; |
| 197 | |
| 198 | testRule(std::move(Rule), Input, Expected); |
| 199 | } |
| 200 | |
Yitzhak Mandelbaum | 727bdcb | 2019-07-02 13:11:04 +0000 | [diff] [blame^] | 201 | TEST_F(TransformerTest, AddIncludeQuoted) { |
| 202 | RewriteRule Rule = makeRule(callExpr(callee(functionDecl(hasName("f")))), |
| 203 | change(text("other()"))); |
| 204 | addInclude(Rule, "clang/OtherLib.h"); |
| 205 | |
| 206 | std::string Input = R"cc( |
| 207 | int f(int x); |
| 208 | int h(int x) { return f(x); } |
| 209 | )cc"; |
| 210 | std::string Expected = R"cc(#include "clang/OtherLib.h" |
| 211 | |
| 212 | int f(int x); |
| 213 | int h(int x) { return other(); } |
| 214 | )cc"; |
| 215 | |
| 216 | testRule(Rule, Input, Expected); |
| 217 | } |
| 218 | |
| 219 | TEST_F(TransformerTest, AddIncludeAngled) { |
| 220 | RewriteRule Rule = makeRule(callExpr(callee(functionDecl(hasName("f")))), |
| 221 | change(text("other()"))); |
| 222 | addInclude(Rule, "clang/OtherLib.h", IncludeFormat::Angled); |
| 223 | |
| 224 | std::string Input = R"cc( |
| 225 | int f(int x); |
| 226 | int h(int x) { return f(x); } |
| 227 | )cc"; |
| 228 | std::string Expected = R"cc(#include <clang/OtherLib.h> |
| 229 | |
| 230 | int f(int x); |
| 231 | int h(int x) { return other(); } |
| 232 | )cc"; |
| 233 | |
| 234 | testRule(Rule, Input, Expected); |
| 235 | } |
| 236 | |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 237 | TEST_F(TransformerTest, NodePartNameNamedDecl) { |
| 238 | StringRef Fun = "fun"; |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 239 | RewriteRule Rule = makeRule(functionDecl(hasName("bad")).bind(Fun), |
| 240 | change(name(Fun), text("good"))); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 241 | |
| 242 | std::string Input = R"cc( |
| 243 | int bad(int x); |
| 244 | int bad(int x) { return x * x; } |
| 245 | )cc"; |
| 246 | std::string Expected = R"cc( |
| 247 | int good(int x); |
| 248 | int good(int x) { return x * x; } |
| 249 | )cc"; |
| 250 | |
| 251 | testRule(Rule, Input, Expected); |
| 252 | } |
| 253 | |
| 254 | TEST_F(TransformerTest, NodePartNameDeclRef) { |
| 255 | std::string Input = R"cc( |
| 256 | template <typename T> |
| 257 | T bad(T x) { |
| 258 | return x; |
| 259 | } |
| 260 | int neutral(int x) { return bad<int>(x) * x; } |
| 261 | )cc"; |
| 262 | std::string Expected = R"cc( |
| 263 | template <typename T> |
| 264 | T bad(T x) { |
| 265 | return x; |
| 266 | } |
| 267 | int neutral(int x) { return good<int>(x) * x; } |
| 268 | )cc"; |
| 269 | |
| 270 | StringRef Ref = "ref"; |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 271 | testRule(makeRule(declRefExpr(to(functionDecl(hasName("bad")))).bind(Ref), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 272 | change(name(Ref), text("good"))), |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 273 | Input, Expected); |
| 274 | } |
| 275 | |
| 276 | TEST_F(TransformerTest, NodePartNameDeclRefFailure) { |
| 277 | std::string Input = R"cc( |
| 278 | struct Y { |
| 279 | int operator*(); |
| 280 | }; |
| 281 | int neutral(int x) { |
| 282 | Y y; |
| 283 | int (Y::*ptr)() = &Y::operator*; |
| 284 | return *y + x; |
| 285 | } |
| 286 | )cc"; |
| 287 | |
| 288 | StringRef Ref = "ref"; |
Yitzhak Mandelbaum | a5dadbe | 2019-04-30 17:24:36 +0000 | [diff] [blame] | 289 | Transformer T(makeRule(declRefExpr(to(functionDecl())).bind(Ref), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 290 | change(name(Ref), text("good"))), |
Yitzhak Mandelbaum | a5dadbe | 2019-04-30 17:24:36 +0000 | [diff] [blame] | 291 | consumer()); |
| 292 | T.registerMatchers(&MatchFinder); |
| 293 | EXPECT_FALSE(rewrite(Input)); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | TEST_F(TransformerTest, NodePartMember) { |
| 297 | StringRef E = "expr"; |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 298 | RewriteRule Rule = makeRule(memberExpr(member(hasName("bad"))).bind(E), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 299 | change(member(E), text("good"))); |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 300 | |
| 301 | std::string Input = R"cc( |
| 302 | struct S { |
| 303 | int bad; |
| 304 | }; |
| 305 | int g() { |
| 306 | S s; |
| 307 | return s.bad; |
| 308 | } |
| 309 | )cc"; |
| 310 | std::string Expected = R"cc( |
| 311 | struct S { |
| 312 | int bad; |
| 313 | }; |
| 314 | int g() { |
| 315 | S s; |
| 316 | return s.good; |
| 317 | } |
| 318 | )cc"; |
| 319 | |
| 320 | testRule(Rule, Input, Expected); |
| 321 | } |
| 322 | |
| 323 | TEST_F(TransformerTest, NodePartMemberQualified) { |
| 324 | std::string Input = R"cc( |
| 325 | struct S { |
| 326 | int bad; |
| 327 | int good; |
| 328 | }; |
| 329 | struct T : public S { |
| 330 | int bad; |
| 331 | }; |
| 332 | int g() { |
| 333 | T t; |
| 334 | return t.S::bad; |
| 335 | } |
| 336 | )cc"; |
| 337 | std::string Expected = R"cc( |
| 338 | struct S { |
| 339 | int bad; |
| 340 | int good; |
| 341 | }; |
| 342 | struct T : public S { |
| 343 | int bad; |
| 344 | }; |
| 345 | int g() { |
| 346 | T t; |
| 347 | return t.S::good; |
| 348 | } |
| 349 | )cc"; |
| 350 | |
| 351 | StringRef E = "expr"; |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 352 | testRule(makeRule(memberExpr().bind(E), change(member(E), text("good"))), |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 353 | Input, Expected); |
| 354 | } |
| 355 | |
| 356 | TEST_F(TransformerTest, NodePartMemberMultiToken) { |
| 357 | std::string Input = R"cc( |
| 358 | struct Y { |
| 359 | int operator*(); |
| 360 | int good(); |
| 361 | template <typename T> void foo(T t); |
| 362 | }; |
| 363 | int neutral(int x) { |
| 364 | Y y; |
| 365 | y.template foo<int>(3); |
| 366 | return y.operator *(); |
| 367 | } |
| 368 | )cc"; |
| 369 | std::string Expected = R"cc( |
| 370 | struct Y { |
| 371 | int operator*(); |
| 372 | int good(); |
| 373 | template <typename T> void foo(T t); |
| 374 | }; |
| 375 | int neutral(int x) { |
| 376 | Y y; |
| 377 | y.template good<int>(3); |
| 378 | return y.good(); |
| 379 | } |
| 380 | )cc"; |
| 381 | |
| 382 | StringRef MemExpr = "member"; |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 383 | testRule(makeRule(memberExpr().bind(MemExpr), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 384 | change(member(MemExpr), text("good"))), |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 385 | Input, Expected); |
| 386 | } |
| 387 | |
Yitzhak Mandelbaum | 2e4a628 | 2019-06-06 14:20:29 +0000 | [diff] [blame] | 388 | TEST_F(TransformerTest, InsertBeforeEdit) { |
| 389 | std::string Input = R"cc( |
| 390 | int f() { |
| 391 | return 7; |
| 392 | } |
| 393 | )cc"; |
| 394 | std::string Expected = R"cc( |
| 395 | int f() { |
| 396 | int y = 3; |
| 397 | return 7; |
| 398 | } |
| 399 | )cc"; |
| 400 | |
| 401 | StringRef Ret = "return"; |
| 402 | testRule(makeRule(returnStmt().bind(Ret), |
| 403 | insertBefore(statement(Ret), text("int y = 3;"))), |
| 404 | Input, Expected); |
| 405 | } |
| 406 | |
| 407 | TEST_F(TransformerTest, InsertAfterEdit) { |
| 408 | std::string Input = R"cc( |
| 409 | int f() { |
| 410 | int x = 5; |
| 411 | return 7; |
| 412 | } |
| 413 | )cc"; |
| 414 | std::string Expected = R"cc( |
| 415 | int f() { |
| 416 | int x = 5; |
| 417 | int y = 3; |
| 418 | return 7; |
| 419 | } |
| 420 | )cc"; |
| 421 | |
| 422 | StringRef Decl = "decl"; |
| 423 | testRule(makeRule(declStmt().bind(Decl), |
| 424 | insertAfter(statement(Decl), text("int y = 3;"))), |
| 425 | Input, Expected); |
| 426 | } |
| 427 | |
| 428 | TEST_F(TransformerTest, RemoveEdit) { |
| 429 | std::string Input = R"cc( |
| 430 | int f() { |
| 431 | int x = 5; |
| 432 | return 7; |
| 433 | } |
| 434 | )cc"; |
| 435 | std::string Expected = R"cc( |
| 436 | int f() { |
| 437 | return 7; |
| 438 | } |
| 439 | )cc"; |
| 440 | |
| 441 | StringRef Decl = "decl"; |
| 442 | testRule(makeRule(declStmt().bind(Decl), remove(statement(Decl))), Input, |
| 443 | Expected); |
| 444 | } |
| 445 | |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 446 | TEST_F(TransformerTest, MultiChange) { |
| 447 | std::string Input = R"cc( |
| 448 | void foo() { |
| 449 | if (10 > 1.0) |
| 450 | log(1) << "oh no!"; |
| 451 | else |
| 452 | log(0) << "ok"; |
| 453 | } |
| 454 | )cc"; |
| 455 | std::string Expected = R"( |
| 456 | void foo() { |
| 457 | if (true) { /* then */ } |
| 458 | else { /* else */ } |
| 459 | } |
| 460 | )"; |
| 461 | |
| 462 | StringRef C = "C", T = "T", E = "E"; |
| 463 | testRule(makeRule(ifStmt(hasCondition(expr().bind(C)), |
| 464 | hasThen(stmt().bind(T)), hasElse(stmt().bind(E))), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 465 | {change(node(C), text("true")), |
| 466 | change(statement(T), text("{ /* then */ }")), |
| 467 | change(statement(E), text("{ /* else */ }"))}), |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 468 | Input, Expected); |
| 469 | } |
| 470 | |
Yitzhak Mandelbaum | 8369a9b | 2019-05-17 14:23:33 +0000 | [diff] [blame] | 471 | TEST_F(TransformerTest, OrderedRuleUnrelated) { |
| 472 | StringRef Flag = "flag"; |
| 473 | RewriteRule FlagRule = makeRule( |
| 474 | cxxMemberCallExpr(on(expr(hasType(cxxRecordDecl( |
| 475 | hasName("proto::ProtoCommandLineFlag")))) |
| 476 | .bind(Flag)), |
| 477 | unless(callee(cxxMethodDecl(hasName("GetProto"))))), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 478 | change(node(Flag), text("PROTO"))); |
Yitzhak Mandelbaum | 8369a9b | 2019-05-17 14:23:33 +0000 | [diff] [blame] | 479 | |
| 480 | std::string Input = R"cc( |
| 481 | proto::ProtoCommandLineFlag flag; |
| 482 | int x = flag.foo(); |
| 483 | int y = flag.GetProto().foo(); |
| 484 | int f(string s) { return strlen(s.c_str()); } |
| 485 | )cc"; |
| 486 | std::string Expected = R"cc( |
| 487 | proto::ProtoCommandLineFlag flag; |
| 488 | int x = PROTO.foo(); |
| 489 | int y = flag.GetProto().foo(); |
| 490 | int f(string s) { return REPLACED; } |
| 491 | )cc"; |
| 492 | |
| 493 | testRule(applyFirst({ruleStrlenSize(), FlagRule}), Input, Expected); |
| 494 | } |
| 495 | |
| 496 | // Version of ruleStrlenSizeAny that inserts a method with a different name than |
| 497 | // ruleStrlenSize, so we can tell their effect apart. |
| 498 | RewriteRule ruleStrlenSizeDistinct() { |
| 499 | StringRef S; |
| 500 | return makeRule( |
| 501 | callExpr(callee(functionDecl(hasName("strlen"))), |
| 502 | hasArgument(0, cxxMemberCallExpr( |
| 503 | on(expr().bind(S)), |
| 504 | callee(cxxMethodDecl(hasName("c_str")))))), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 505 | change(text("DISTINCT"))); |
Yitzhak Mandelbaum | 8369a9b | 2019-05-17 14:23:33 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | TEST_F(TransformerTest, OrderedRuleRelated) { |
| 509 | std::string Input = R"cc( |
| 510 | namespace foo { |
| 511 | struct mystring { |
| 512 | char* c_str(); |
| 513 | }; |
| 514 | int f(mystring s) { return strlen(s.c_str()); } |
| 515 | } // namespace foo |
| 516 | int g(string s) { return strlen(s.c_str()); } |
| 517 | )cc"; |
| 518 | std::string Expected = R"cc( |
| 519 | namespace foo { |
| 520 | struct mystring { |
| 521 | char* c_str(); |
| 522 | }; |
| 523 | int f(mystring s) { return DISTINCT; } |
| 524 | } // namespace foo |
| 525 | int g(string s) { return REPLACED; } |
| 526 | )cc"; |
| 527 | |
| 528 | testRule(applyFirst({ruleStrlenSize(), ruleStrlenSizeDistinct()}), Input, |
| 529 | Expected); |
| 530 | } |
| 531 | |
| 532 | // Change the order of the rules to get a different result. |
| 533 | TEST_F(TransformerTest, OrderedRuleRelatedSwapped) { |
| 534 | std::string Input = R"cc( |
| 535 | namespace foo { |
| 536 | struct mystring { |
| 537 | char* c_str(); |
| 538 | }; |
| 539 | int f(mystring s) { return strlen(s.c_str()); } |
| 540 | } // namespace foo |
| 541 | int g(string s) { return strlen(s.c_str()); } |
| 542 | )cc"; |
| 543 | std::string Expected = R"cc( |
| 544 | namespace foo { |
| 545 | struct mystring { |
| 546 | char* c_str(); |
| 547 | }; |
| 548 | int f(mystring s) { return DISTINCT; } |
| 549 | } // namespace foo |
| 550 | int g(string s) { return DISTINCT; } |
| 551 | )cc"; |
| 552 | |
| 553 | testRule(applyFirst({ruleStrlenSizeDistinct(), ruleStrlenSize()}), Input, |
| 554 | Expected); |
| 555 | } |
| 556 | |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 557 | // |
| 558 | // Negative tests (where we expect no transformation to occur). |
| 559 | // |
| 560 | |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 561 | // 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] | 562 | TEST_F(TransformerTest, TextGeneratorFailure) { |
| 563 | std::string Input = "int conflictOneRule() { return 3 + 7; }"; |
| 564 | // Try to change the whole binary-operator expression AND one its operands: |
| 565 | StringRef O = "O"; |
| 566 | auto AlwaysFail = [](const ast_matchers::MatchFinder::MatchResult &) |
| 567 | -> llvm::Expected<std::string> { |
| 568 | return llvm::createStringError(llvm::errc::invalid_argument, "ERROR"); |
| 569 | }; |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 570 | Transformer T(makeRule(binaryOperator().bind(O), change(node(O), AlwaysFail)), |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 571 | consumer()); |
| 572 | T.registerMatchers(&MatchFinder); |
| 573 | EXPECT_FALSE(rewrite(Input)); |
| 574 | EXPECT_THAT(Changes, IsEmpty()); |
| 575 | EXPECT_EQ(ErrorCount, 1); |
| 576 | } |
| 577 | |
| 578 | // 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] | 579 | TEST_F(TransformerTest, OverlappingEditsInRule) { |
| 580 | std::string Input = "int conflictOneRule() { return 3 + 7; }"; |
| 581 | // Try to change the whole binary-operator expression AND one its operands: |
| 582 | StringRef O = "O", L = "L"; |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 583 | Transformer T(makeRule(binaryOperator(hasLHS(expr().bind(L))).bind(O), |
| 584 | {change(node(O), text("DELETE_OP")), |
| 585 | change(node(L), text("DELETE_LHS"))}), |
| 586 | consumer()); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 587 | T.registerMatchers(&MatchFinder); |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 588 | EXPECT_FALSE(rewrite(Input)); |
| 589 | EXPECT_THAT(Changes, IsEmpty()); |
| 590 | EXPECT_EQ(ErrorCount, 1); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // Tests for a conflict in edits across multiple matches (of the same rule). |
| 594 | TEST_F(TransformerTest, OverlappingEditsMultipleMatches) { |
| 595 | std::string Input = "int conflictOneRule() { return -7; }"; |
| 596 | // Try to change the whole binary-operator expression AND one its operands: |
| 597 | StringRef E = "E"; |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 598 | Transformer T(makeRule(expr().bind(E), change(node(E), text("DELETE_EXPR"))), |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 599 | consumer()); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 600 | T.registerMatchers(&MatchFinder); |
| 601 | // The rewrite process fails because the changes conflict with each other... |
| 602 | EXPECT_FALSE(rewrite(Input)); |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 603 | // ... but two changes were produced. |
| 604 | EXPECT_EQ(Changes.size(), 2u); |
| 605 | EXPECT_EQ(ErrorCount, 0); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | TEST_F(TransformerTest, ErrorOccurredMatchSkipped) { |
| 609 | // Syntax error in the function body: |
| 610 | std::string Input = "void errorOccurred() { 3 }"; |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 611 | Transformer T(makeRule(functionDecl(hasName("errorOccurred")), |
Yitzhak Mandelbaum | 3ec50e2 | 2019-05-22 14:48:19 +0000 | [diff] [blame] | 612 | change(text("DELETED;"))), |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 613 | consumer()); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 614 | T.registerMatchers(&MatchFinder); |
| 615 | // The rewrite process itself fails... |
| 616 | EXPECT_FALSE(rewrite(Input)); |
Yitzhak Mandelbaum | aecc59c | 2019-04-30 16:48:33 +0000 | [diff] [blame] | 617 | // ... and no changes or errors are produced in the process. |
| 618 | EXPECT_THAT(Changes, IsEmpty()); |
| 619 | EXPECT_EQ(ErrorCount, 0); |
Yitzhak Mandelbaum | fa1552e | 2019-04-18 17:52:24 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Yitzhak Mandelbaum | fdd9878 | 2019-04-05 15:14:05 +0000 | [diff] [blame] | 622 | TEST_F(TransformerTest, NoTransformationInMacro) { |
| 623 | std::string Input = R"cc( |
| 624 | #define MACRO(str) strlen((str).c_str()) |
| 625 | int f(string s) { return MACRO(s); })cc"; |
| 626 | testRule(ruleStrlenSize(), Input, Input); |
| 627 | } |
| 628 | |
| 629 | // This test handles the corner case where a macro called within another macro |
| 630 | // expands to matching code, but the matched code is an argument to the nested |
| 631 | // macro. A simple check of isMacroArgExpansion() vs. isMacroBodyExpansion() |
| 632 | // will get this wrong, and transform the code. This test verifies that no such |
| 633 | // transformation occurs. |
| 634 | TEST_F(TransformerTest, NoTransformationInNestedMacro) { |
| 635 | std::string Input = R"cc( |
| 636 | #define NESTED(e) e |
| 637 | #define MACRO(str) NESTED(strlen((str).c_str())) |
| 638 | int f(string s) { return MACRO(s); })cc"; |
| 639 | testRule(ruleStrlenSize(), Input, Input); |
| 640 | } |
| 641 | } // namespace |