1 //===- unittest/Format/CleanupTest.cpp - Code cleanup unit tests ----------===// 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/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "../Tooling/RewriterTestContext.h" 13 #include "clang/Tooling/Core/Replacement.h" 14 15 #include "gtest/gtest.h" 16 17 using clang::tooling::ReplacementTest; 18 using clang::tooling::toReplacements; 19 20 namespace clang { 21 namespace format { 22 namespace { 23 24 class CleanupTest : public ::testing::Test { 25 protected: 26 std::string cleanup(llvm::StringRef Code, 27 const std::vector<tooling::Range> &Ranges, 28 const FormatStyle &Style = getLLVMStyle()) { 29 tooling::Replacements Replaces = format::cleanup(Style, Code, Ranges); 30 31 auto Result = applyAllReplacements(Code, Replaces); 32 EXPECT_TRUE(static_cast<bool>(Result)); 33 return *Result; 34 } 35 36 // Returns code after cleanup around \p Offsets. 37 std::string cleanupAroundOffsets(llvm::ArrayRef<unsigned> Offsets, 38 llvm::StringRef Code, 39 const FormatStyle &Style = getLLVMStyle()) { 40 std::vector<tooling::Range> Ranges; 41 for (auto Offset : Offsets) 42 Ranges.push_back(tooling::Range(Offset, 0)); 43 return cleanup(Code, Ranges, Style); 44 } 45 }; 46 47 TEST_F(CleanupTest, DeleteEmptyNamespaces) { 48 std::string Code = "namespace A {\n" 49 "namespace B {\n" 50 "} // namespace B\n" 51 "} // namespace A\n\n" 52 "namespace C {\n" 53 "namespace D { int i; }\n" 54 "inline namespace E { namespace { } }\n" 55 "}"; 56 std::string Expected = "\n\n\n\n\nnamespace C {\n" 57 "namespace D { int i; }\n \n" 58 "}"; 59 EXPECT_EQ(Expected, cleanupAroundOffsets({28, 91, 132}, Code)); 60 } 61 62 TEST_F(CleanupTest, NamespaceWithSyntaxError) { 63 std::string Code = "namespace A {\n" 64 "namespace B {\n" // missing r_brace 65 "} // namespace A\n\n" 66 "namespace C {\n" 67 "namespace D int i; }\n" 68 "inline namespace E { namespace { } }\n" 69 "}"; 70 std::string Expected = "namespace A {\n" 71 "\n\n\nnamespace C {\n" 72 "namespace D int i; }\n \n" 73 "}"; 74 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 75 EXPECT_EQ(Expected, cleanup(Code, Ranges)); 76 } 77 78 TEST_F(CleanupTest, EmptyNamespaceNotAffected) { 79 std::string Code = "namespace A {\n\n" 80 "namespace {\n\n}}"; 81 // Even though the namespaces are empty, but the inner most empty namespace 82 // block is not affected by the changed ranges. 83 std::string Expected = "namespace A {\n\n" 84 "namespace {\n\n}}"; 85 // Set the changed range to be the second "\n". 86 EXPECT_EQ(Expected, cleanupAroundOffsets({14}, Code)); 87 } 88 89 TEST_F(CleanupTest, EmptyNamespaceWithCommentsNoBreakBeforeBrace) { 90 std::string Code = "namespace A {\n" 91 "namespace B {\n" 92 "// Yo\n" 93 "} // namespace B\n" 94 "} // namespace A\n" 95 "namespace C { // Yo\n" 96 "}"; 97 std::string Expected = "\n\n\n\n\n\n"; 98 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 99 std::string Result = cleanup(Code, Ranges); 100 EXPECT_EQ(Expected, Result); 101 } 102 103 TEST_F(CleanupTest, EmptyNamespaceWithCommentsBreakBeforeBrace) { 104 std::string Code = "namespace A\n" 105 "/* Yo */ {\n" 106 "namespace B\n" 107 "{\n" 108 "// Yo\n" 109 "} // namespace B\n" 110 "} // namespace A\n" 111 "namespace C\n" 112 "{ // Yo\n" 113 "}\n"; 114 std::string Expected = "\n\n\n\n\n\n\n\n\n\n"; 115 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 116 FormatStyle Style = getLLVMStyle(); 117 Style.BraceWrapping.AfterNamespace = true; 118 std::string Result = cleanup(Code, Ranges, Style); 119 EXPECT_EQ(Expected, Result); 120 } 121 122 TEST_F(CleanupTest, EmptyNamespaceAroundConditionalCompilation) { 123 std::string Code = "#ifdef A\n" 124 "int a;\n" 125 "int b;\n" 126 "#else\n" 127 "#endif\n" 128 "namespace {}"; 129 std::string Expected = "#ifdef A\n" 130 "int a;\n" 131 "int b;\n" 132 "#else\n" 133 "#endif\n"; 134 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 135 FormatStyle Style = getLLVMStyle(); 136 std::string Result = cleanup(Code, Ranges, Style); 137 EXPECT_EQ(Expected, Result); 138 } 139 140 TEST_F(CleanupTest, CtorInitializationSimpleRedundantComma) { 141 std::string Code = "class A {\nA() : , {} };"; 142 std::string Expected = "class A {\nA() {} };"; 143 EXPECT_EQ(Expected, cleanupAroundOffsets({17, 19}, Code)); 144 145 Code = "class A {\nA() : x(1), {} };"; 146 Expected = "class A {\nA() : x(1) {} };"; 147 EXPECT_EQ(Expected, cleanupAroundOffsets({23}, Code)); 148 149 Code = "class A {\nA() :,,,,{} };"; 150 Expected = "class A {\nA() {} };"; 151 EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code)); 152 } 153 154 // regression test for bug 39310 155 TEST_F(CleanupTest, CtorInitializationSimpleRedundantCommaInFunctionTryBlock) { 156 std::string Code = "class A {\nA() try : , {} };"; 157 std::string Expected = "class A {\nA() try {} };"; 158 EXPECT_EQ(Expected, cleanupAroundOffsets({21, 23}, Code)); 159 160 Code = "class A {\nA() try : x(1), {} };"; 161 Expected = "class A {\nA() try : x(1) {} };"; 162 EXPECT_EQ(Expected, cleanupAroundOffsets({27}, Code)); 163 164 Code = "class A {\nA() try :,,,,{} };"; 165 Expected = "class A {\nA() try {} };"; 166 EXPECT_EQ(Expected, cleanupAroundOffsets({19}, Code)); 167 168 Code = "class A {\nA() try : x(1),,, {} };"; 169 Expected = "class A {\nA() try : x(1) {} };"; 170 EXPECT_EQ(Expected, cleanupAroundOffsets({27}, Code)); 171 172 // Do not remove every comma following a colon as it simply doesn't make 173 // sense in some situations. 174 Code = "try : , {}"; 175 Expected = "try : , {}"; 176 EXPECT_EQ(Expected, cleanupAroundOffsets({8}, Code)); 177 } 178 179 TEST_F(CleanupTest, CtorInitializationSimpleRedundantColon) { 180 std::string Code = "class A {\nA() : =default; };"; 181 std::string Expected = "class A {\nA() =default; };"; 182 EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code)); 183 184 Code = "class A {\nA() : , =default; };"; 185 Expected = "class A {\nA() =default; };"; 186 EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code)); 187 } 188 189 TEST_F(CleanupTest, ListRedundantComma) { 190 std::string Code = "void f() { std::vector<int> v = {1,2,,,3,{4,5}}; }"; 191 std::string Expected = "void f() { std::vector<int> v = {1,2,3,{4,5}}; }"; 192 EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code)); 193 194 Code = "int main() { f(1,,2,3,,4);}"; 195 Expected = "int main() { f(1,2,3,4);}"; 196 EXPECT_EQ(Expected, cleanupAroundOffsets({17, 22}, Code)); 197 } 198 199 TEST_F(CleanupTest, NoCleanupsForJavaScript) { 200 std::string Code = "function f() { var x = [a, b, , c]; }"; 201 std::string Expected = "function f() { var x = [a, b, , c]; }"; 202 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript); 203 204 EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code, Style)); 205 } 206 207 TEST_F(CleanupTest, TrailingCommaInParens) { 208 std::string Code = "int main() { f(,1,,2,3,f(1,2,),4,,);}"; 209 std::string Expected = "int main() { f(1,2,3,f(1,2),4);}"; 210 EXPECT_EQ(Expected, cleanupAroundOffsets({15, 18, 29, 33}, Code)); 211 212 // Lambda contents are also checked for trailing commas. 213 Code = "int main() { [](){f(,1,,2,3,f(1,2,),4,,);}();}"; 214 Expected = "int main() { [](){f(1,2,3,f(1,2),4);}();}"; 215 EXPECT_EQ(Expected, cleanupAroundOffsets({20, 23, 34, 38}, Code)); 216 } 217 218 TEST_F(CleanupTest, TrailingCommaInBraces) { 219 // Trailing comma is allowed in brace list. 220 // If there was trailing comma in the original code, then trailing comma is 221 // preserved. In this example, element between the last two commas is deleted 222 // causing the second-last comma to be redundant. 223 std::string Code = "void f() { std::vector<int> v = {1,2,3,,}; }"; 224 std::string Expected = "void f() { std::vector<int> v = {1,2,3,}; }"; 225 EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code)); 226 227 // If there was no trailing comma in the original code, then trailing comma 228 // introduced by replacements should be cleaned up. In this example, the 229 // element after the last comma is deleted causing the last comma to be 230 // redundant. 231 Code = "void f() { std::vector<int> v = {1,2,3,}; }"; 232 // FIXME: redundant trailing comma should be removed. 233 Expected = "void f() { std::vector<int> v = {1,2,3,}; }"; 234 EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code)); 235 236 // Still no trailing comma in the original code, but two elements are deleted, 237 // which makes it seems like there was trailing comma. 238 Code = "void f() { std::vector<int> v = {1, 2, 3, , }; }"; 239 // FIXME: redundant trailing comma should also be removed. 240 Expected = "void f() { std::vector<int> v = {1, 2, 3, }; }"; 241 EXPECT_EQ(Expected, cleanupAroundOffsets({42, 44}, Code)); 242 } 243 244 TEST_F(CleanupTest, CtorInitializationBracesInParens) { 245 std::string Code = "class A {\nA() : x({1}),, {} };"; 246 std::string Expected = "class A {\nA() : x({1}) {} };"; 247 EXPECT_EQ(Expected, cleanupAroundOffsets({24, 26}, Code)); 248 } 249 250 TEST_F(CleanupTest, RedundantCommaNotInAffectedRanges) { 251 std::string Code = 252 "class A {\nA() : x({1}), /* comment */, { int x = 0; } };"; 253 std::string Expected = 254 "class A {\nA() : x({1}), /* comment */, { int x = 0; } };"; 255 // Set the affected range to be "int x = 0", which does not intercept the 256 // constructor initialization list. 257 std::vector<tooling::Range> Ranges(1, tooling::Range(42, 9)); 258 std::string Result = cleanup(Code, Ranges); 259 EXPECT_EQ(Expected, Result); 260 261 Code = "class A {\nA() : x(1), {} };"; 262 Expected = "class A {\nA() : x(1), {} };"; 263 // No range. Fixer should do nothing. 264 Ranges.clear(); 265 Result = cleanup(Code, Ranges); 266 EXPECT_EQ(Expected, Result); 267 } 268 269 TEST_F(CleanupTest, RemoveCommentsAroundDeleteCode) { 270 std::string Code = 271 "class A {\nA() : x({1}), /* comment */, /* comment */ {} };"; 272 std::string Expected = "class A {\nA() : x({1}) {} };"; 273 EXPECT_EQ(Expected, cleanupAroundOffsets({25, 40}, Code)); 274 275 Code = "class A {\nA() : x({1}), // comment\n {} };"; 276 Expected = "class A {\nA() : x({1})\n {} };"; 277 EXPECT_EQ(Expected, cleanupAroundOffsets({25}, Code)); 278 279 Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };"; 280 Expected = "class A {\nA() : x({1}), y(1){} };"; 281 EXPECT_EQ(Expected, cleanupAroundOffsets({38}, Code)); 282 283 Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };"; 284 Expected = "class A {\nA() : x({1}), \n y(1){} };"; 285 EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code)); 286 287 Code = "class A {\nA() : , // comment\n y(1),{} };"; 288 Expected = "class A {\nA() : // comment\n y(1){} };"; 289 EXPECT_EQ(Expected, cleanupAroundOffsets({17}, Code)); 290 291 Code = "class A {\nA() // comment\n : ,,{} };"; 292 Expected = "class A {\nA() // comment\n {} };"; 293 EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code)); 294 295 Code = "class A {\nA() // comment\n : ,,=default; };"; 296 Expected = "class A {\nA() // comment\n =default; };"; 297 EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code)); 298 } 299 300 TEST_F(CleanupTest, CtorInitializerInNamespace) { 301 std::string Code = "namespace A {\n" 302 "namespace B {\n" // missing r_brace 303 "} // namespace A\n\n" 304 "namespace C {\n" 305 "class A { A() : x(0),, {} };\n" 306 "inline namespace E { namespace { } }\n" 307 "}"; 308 std::string Expected = "namespace A {\n" 309 "\n\n\nnamespace C {\n" 310 "class A { A() : x(0) {} };\n \n" 311 "}"; 312 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 313 std::string Result = cleanup(Code, Ranges); 314 EXPECT_EQ(Expected, Result); 315 } 316 317 class CleanUpReplacementsTest : public ReplacementTest { 318 protected: 319 tooling::Replacement createReplacement(unsigned Offset, unsigned Length, 320 StringRef Text) { 321 return tooling::Replacement(FileName, Offset, Length, Text); 322 } 323 324 tooling::Replacement createInsertion(StringRef IncludeDirective) { 325 return createReplacement(UINT_MAX, 0, IncludeDirective); 326 } 327 328 tooling::Replacement createDeletion(StringRef HeaderName) { 329 return createReplacement(UINT_MAX, 1, HeaderName); 330 } 331 332 inline std::string apply(StringRef Code, 333 const tooling::Replacements &Replaces) { 334 auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style); 335 EXPECT_TRUE(static_cast<bool>(CleanReplaces)) 336 << llvm::toString(CleanReplaces.takeError()) << "\n"; 337 auto Result = applyAllReplacements(Code, *CleanReplaces); 338 EXPECT_TRUE(static_cast<bool>(Result)); 339 return *Result; 340 } 341 342 inline std::string formatAndApply(StringRef Code, 343 const tooling::Replacements &Replaces) { 344 auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style); 345 EXPECT_TRUE(static_cast<bool>(CleanReplaces)) 346 << llvm::toString(CleanReplaces.takeError()) << "\n"; 347 auto FormattedReplaces = formatReplacements(Code, *CleanReplaces, Style); 348 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 349 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 350 auto Result = applyAllReplacements(Code, *FormattedReplaces); 351 EXPECT_TRUE(static_cast<bool>(Result)); 352 return *Result; 353 } 354 355 int getOffset(StringRef Code, int Line, int Column) { 356 RewriterTestContext Context; 357 FileID ID = Context.createInMemoryFile(FileName, Code); 358 auto DecomposedLocation = 359 Context.Sources.getDecomposedLoc(Context.getLocation(ID, Line, Column)); 360 return DecomposedLocation.second; 361 } 362 363 const std::string FileName = "fix.cpp"; 364 FormatStyle Style = getLLVMStyle(); 365 }; 366 367 TEST_F(CleanUpReplacementsTest, FixOnlyAffectedCodeAfterReplacements) { 368 std::string Code = "namespace A {\n" 369 "namespace B {\n" 370 " int x;\n" 371 "} // namespace B\n" 372 "} // namespace A\n" 373 "\n" 374 "namespace C {\n" 375 "namespace D { int i; }\n" 376 "inline namespace E { namespace { int y; } }\n" 377 "int x= 0;" 378 "}"; 379 std::string Expected = "\n\nnamespace C {\n" 380 "namespace D { int i; }\n\n" 381 "int x= 0;" 382 "}"; 383 tooling::Replacements Replaces = 384 toReplacements({createReplacement(getOffset(Code, 3, 3), 6, ""), 385 createReplacement(getOffset(Code, 9, 34), 6, "")}); 386 387 EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); 388 } 389 390 TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesLLVMStyle) { 391 std::string Code = "#include \"x/fix.h\"\n" 392 "#include \"a.h\"\n" 393 "#include \"b.h\"\n" 394 "#include \"z.h\"\n" 395 "#include \"clang/Format/Format.h\"\n" 396 "#include <memory>\n"; 397 std::string Expected = "#include \"x/fix.h\"\n" 398 "#include \"a.h\"\n" 399 "#include \"b.h\"\n" 400 "#include \"new/new.h\"\n" 401 "#include \"z.h\"\n" 402 "#include \"clang/Format/Format.h\"\n" 403 "#include <list>\n" 404 "#include <memory>\n"; 405 tooling::Replacements Replaces = 406 toReplacements({createInsertion("#include <list>"), 407 createInsertion("#include \"new/new.h\"")}); 408 EXPECT_EQ(Expected, apply(Code, Replaces)); 409 } 410 411 TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesGoogleStyle) { 412 std::string Code = "#include \"x/fix.h\"\n" 413 "\n" 414 "#include <vector>\n" 415 "\n" 416 "#include \"y/a.h\"\n" 417 "#include \"z/b.h\"\n"; 418 std::string Expected = "#include \"x/fix.h\"\n" 419 "\n" 420 "#include <list>\n" 421 "#include <vector>\n" 422 "\n" 423 "#include \"x/x.h\"\n" 424 "#include \"y/a.h\"\n" 425 "#include \"z/b.h\"\n"; 426 tooling::Replacements Replaces = 427 toReplacements({createInsertion("#include <list>"), 428 createInsertion("#include \"x/x.h\"")}); 429 Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp); 430 EXPECT_EQ(Expected, apply(Code, Replaces)); 431 } 432 433 TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortLLVM) { 434 std::string Code = "\nint x;"; 435 std::string Expected = "\n#include \"fix.h\"\n" 436 "#include \"a.h\"\n" 437 "#include \"b.h\"\n" 438 "#include \"c.h\"\n" 439 "#include <list>\n" 440 "#include <vector>\n" 441 "int x;"; 442 tooling::Replacements Replaces = toReplacements( 443 {createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""), 444 createInsertion("#include \"b.h\""), 445 createInsertion("#include <vector>"), createInsertion("#include <list>"), 446 createInsertion("#include \"fix.h\"")}); 447 EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); 448 } 449 450 TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortGoogle) { 451 std::string Code = "\nint x;"; 452 std::string Expected = "\n#include \"fix.h\"\n" 453 "\n" 454 "#include <list>\n" 455 "#include <vector>\n" 456 "\n" 457 "#include \"a.h\"\n" 458 "#include \"b.h\"\n" 459 "#include \"c.h\"\n" 460 "int x;"; 461 tooling::Replacements Replaces = toReplacements( 462 {createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""), 463 createInsertion("#include \"b.h\""), 464 createInsertion("#include <vector>"), createInsertion("#include <list>"), 465 createInsertion("#include \"fix.h\"")}); 466 Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp); 467 EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); 468 } 469 470 TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) { 471 std::string Code = "#include <map>"; 472 // FIXME: a better behavior is to only append on newline to Code, but this 473 // case should be rare in practice. 474 std::string Expected = 475 "#include <map>\n#include <string>\n\n#include <vector>\n"; 476 tooling::Replacements Replaces = 477 toReplacements({createInsertion("#include <string>"), 478 createInsertion("#include <vector>")}); 479 EXPECT_EQ(Expected, apply(Code, Replaces)); 480 } 481 482 TEST_F(CleanUpReplacementsTest, FormatCorrectLineWhenHeadersAreInserted) { 483 std::string Code = "\n" 484 "int x;\n" 485 "int a;\n" 486 "int a;\n" 487 "int a;"; 488 489 std::string Expected = "\n#include \"x.h\"\n" 490 "#include \"y.h\"\n" 491 "#include \"clang/x/x.h\"\n" 492 "#include <list>\n" 493 "#include <vector>\n" 494 "int x;\n" 495 "int a;\n" 496 "int b;\n" 497 "int a;"; 498 tooling::Replacements Replaces = toReplacements( 499 {createReplacement(getOffset(Code, 4, 8), 1, "b"), 500 createInsertion("#include <vector>"), createInsertion("#include <list>"), 501 createInsertion("#include \"clang/x/x.h\""), 502 createInsertion("#include \"y.h\""), 503 createInsertion("#include \"x.h\"")}); 504 EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); 505 } 506 507 TEST_F(CleanUpReplacementsTest, SimpleDeleteIncludes) { 508 std::string Code = "#include \"abc.h\"\n" 509 "#include \"xyz.h\" // comment\n" 510 "#include \"xyz\"\n" 511 "int x;\n"; 512 std::string Expected = "#include \"xyz\"\n" 513 "int x;\n"; 514 tooling::Replacements Replaces = 515 toReplacements({createDeletion("abc.h"), createDeletion("xyz.h")}); 516 EXPECT_EQ(Expected, apply(Code, Replaces)); 517 } 518 519 TEST_F(CleanUpReplacementsTest, InsertionAndDeleteHeader) { 520 std::string Code = "#include \"a.h\"\n" 521 "\n" 522 "#include <vector>\n"; 523 std::string Expected = "#include \"a.h\"\n" 524 "\n" 525 "#include <map>\n"; 526 tooling::Replacements Replaces = toReplacements( 527 {createDeletion("<vector>"), createInsertion("#include <map>")}); 528 EXPECT_EQ(Expected, apply(Code, Replaces)); 529 } 530 531 } // end namespace 532 } // end namespace format 533 } // end namespace clang 534