1 //===- unittest/Format/FormatTestSelective.cpp - Formatting 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 "FormatTestUtils.h" 10 #include "clang/Format/Format.h" 11 #include "llvm/Support/Debug.h" 12 #include "gtest/gtest.h" 13 14 #define DEBUG_TYPE "format-test" 15 16 namespace clang { 17 namespace format { 18 namespace { 19 20 class FormatTestSelective : public ::testing::Test { 21 protected: 22 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) { 23 LLVM_DEBUG(llvm::errs() << "---\n"); 24 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 25 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 26 FormattingAttemptStatus Status; 27 tooling::Replacements Replaces = 28 reformat(Style, Code, Ranges, "<stdin>", &Status); 29 EXPECT_TRUE(Status.FormatComplete) << Code << "\n\n"; 30 auto Result = applyAllReplacements(Code, Replaces); 31 EXPECT_TRUE(static_cast<bool>(Result)); 32 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 33 return *Result; 34 } 35 36 FormatStyle Style = getLLVMStyle(); 37 }; 38 39 TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) { 40 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0)); 41 EXPECT_EQ("int a;", format("int a; ", 0, 0)); 42 EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0)); 43 EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0)); 44 } 45 46 TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) { 47 EXPECT_EQ("{int b;\n" 48 " int a;\n" 49 "}", 50 format("{int b;\n int a;}", 8, 0)); 51 EXPECT_EQ("{\n" 52 " int b;\n" 53 " int a;}", 54 format("{int b;\n int a;}", 7, 0)); 55 56 Style.ColumnLimit = 12; 57 EXPECT_EQ("#define A \\\n" 58 " int a; \\\n" 59 " int b;", 60 format("#define A \\\n" 61 " int a; \\\n" 62 " int b;", 63 26, 0)); 64 EXPECT_EQ("#define A \\\n" 65 " int a; \\\n" 66 " int b;", 67 format("#define A \\\n" 68 " int a; \\\n" 69 " int b;", 70 25, 0)); 71 } 72 73 TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) { 74 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0)); 75 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0)); 76 77 // This might not strictly be correct, but is likely good in all practical 78 // cases. 79 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0)); 80 } 81 82 TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) { 83 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0)); 84 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0)); 85 } 86 87 TEST_F(FormatTestSelective, ReformatsMovedLines) { 88 EXPECT_EQ( 89 "template <typename T> T *getFETokenInfo() const {\n" 90 " return static_cast<T *>(FETokenInfo);\n" 91 "}\n" 92 "int a; // <- Should not be formatted", 93 format( 94 "template<typename T>\n" 95 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" 96 "int a; // <- Should not be formatted", 97 9, 5)); 98 } 99 100 TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) { 101 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 102 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1)); 103 EXPECT_EQ("if (a) return; // comment", 104 format("if(a)\nreturn; // comment", 20, 1)); 105 } 106 107 TEST_F(FormatTestSelective, FormatsCommentsLocally) { 108 EXPECT_EQ("int a; // comment\n" 109 "int b; // comment", 110 format("int a; // comment\n" 111 "int b; // comment", 112 0, 0)); 113 EXPECT_EQ("int a; // comment\n" 114 " // line 2\n" 115 "int b;", 116 format("int a; // comment\n" 117 " // line 2\n" 118 "int b;", 119 28, 0)); 120 EXPECT_EQ("int a; // comment\n" 121 "// comment 2\n" 122 "int b;", 123 format("int a; // comment\n" 124 "// comment 2\n" 125 "int b;", 28, 0)); 126 EXPECT_EQ("int aaaaaa; // comment\n" 127 "int b;\n" 128 "int c; // unrelated comment", 129 format("int aaaaaa; // comment\n" 130 "int b;\n" 131 "int c; // unrelated comment", 132 31, 0)); 133 134 EXPECT_EQ("int a; // This\n" 135 " // is\n" 136 " // a", 137 format("int a; // This\n" 138 " // is\n" 139 " // a", 140 0, 0)); 141 EXPECT_EQ("int a; // This\n" 142 " // is\n" 143 " // a\n" 144 "// This is b\n" 145 "int b;", 146 format("int a; // This\n" 147 " // is\n" 148 " // a\n" 149 "// This is b\n" 150 "int b;", 151 0, 0)); 152 EXPECT_EQ("int a; // This\n" 153 " // is\n" 154 " // a\n" 155 "\n" 156 "//This is unrelated", 157 format("int a; // This\n" 158 " // is\n" 159 " // a\n" 160 "\n" 161 "//This is unrelated", 162 0, 0)); 163 EXPECT_EQ("int a;\n" 164 "// This is\n" 165 "// not formatted. ", 166 format("int a;\n" 167 "// This is\n" 168 "// not formatted. ", 169 0, 0)); 170 EXPECT_EQ("int x; // Format this line.\n" 171 "int xx; //\n" 172 "int xxxxx; //", 173 format("int x; // Format this line.\n" 174 "int xx; //\n" 175 "int xxxxx; //", 176 0, 0)); 177 } 178 179 TEST_F(FormatTestSelective, ContinueReindenting) { 180 // When we change an indent, we continue formatting as long as following 181 // lines are not indented correctly. 182 EXPECT_EQ("int i;\n" 183 "int b;\n" 184 "int c;\n" 185 "int d;\n" 186 "int e;\n" 187 " int f;\n", 188 format("int i;\n" 189 " int b;\n" 190 " int c;\n" 191 " int d;\n" 192 "int e;\n" 193 " int f;\n", 194 11, 0)); 195 } 196 197 TEST_F(FormatTestSelective, ReindentClosingBrace) { 198 EXPECT_EQ("int i;\n" 199 "int f() {\n" 200 " int a;\n" 201 " int b;\n" 202 "}\n" 203 " int c;\n", 204 format("int i;\n" 205 " int f(){\n" 206 "int a;\n" 207 "int b;\n" 208 " }\n" 209 " int c;\n", 210 11, 0)); 211 EXPECT_EQ("void f() {\n" 212 " if (foo) {\n" 213 " b();\n" 214 " } else {\n" 215 " c();\n" 216 " }\n" 217 "int d;\n" 218 "}\n", 219 format("void f() {\n" 220 " if (foo) {\n" 221 "b();\n" 222 "}else{\n" 223 "c();\n" 224 "}\n" 225 "int d;\n" 226 "}\n", 227 13, 0)); 228 EXPECT_EQ("int i = []() {\n" 229 " class C {\n" 230 " int a;\n" 231 " int b;\n" 232 " };\n" 233 " int c;\n" 234 "};\n", 235 format("int i = []() {\n" 236 " class C{\n" 237 "int a;\n" 238 "int b;\n" 239 "};\n" 240 "int c;\n" 241 " };\n", 242 17, 0)); 243 } 244 245 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) { 246 EXPECT_EQ("DEBUG({\n" 247 " int i;\n" 248 " int j;\n" 249 "});", 250 format("DEBUG( {\n" 251 " int i;\n" 252 " int j;\n" 253 "} ) ;", 254 20, 1)); 255 EXPECT_EQ("DEBUG( {\n" 256 " int i;\n" 257 " int j;\n" 258 "} ) ;", 259 format("DEBUG( {\n" 260 " int i;\n" 261 " int j;\n" 262 "} ) ;", 263 41, 1)); 264 EXPECT_EQ("DEBUG( {\n" 265 " int i;\n" 266 " int j;\n" 267 "} ) ;", 268 format("DEBUG( {\n" 269 " int i;\n" 270 " int j;\n" 271 "} ) ;", 272 41, 1)); 273 EXPECT_EQ("DEBUG({\n" 274 " int i;\n" 275 " int j;\n" 276 "});", 277 format("DEBUG( {\n" 278 " int i;\n" 279 " int j;\n" 280 "} ) ;", 281 20, 1)); 282 283 EXPECT_EQ("Debug({\n" 284 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 285 " return;\n" 286 " },\n" 287 " a);", 288 format("Debug({\n" 289 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 290 " return;\n" 291 " },\n" 292 " a);", 293 50, 1)); 294 EXPECT_EQ("DEBUG({\n" 295 " DEBUG({\n" 296 " int a;\n" 297 " int b;\n" 298 " }) ;\n" 299 "});", 300 format("DEBUG({\n" 301 " DEBUG({\n" 302 " int a;\n" 303 " int b;\n" // Format this line only. 304 " }) ;\n" // Don't touch this line. 305 "});", 306 35, 0)); 307 EXPECT_EQ("DEBUG({\n" 308 " int a; //\n" 309 "});", 310 format("DEBUG({\n" 311 " int a; //\n" 312 "});", 313 0, 0)); 314 EXPECT_EQ("someFunction(\n" 315 " [] {\n" 316 " // Only with this comment.\n" 317 " int i; // invoke formatting here.\n" 318 " }, // force line break\n" 319 " aaa);", 320 format("someFunction(\n" 321 " [] {\n" 322 " // Only with this comment.\n" 323 " int i; // invoke formatting here.\n" 324 " }, // force line break\n" 325 " aaa);", 326 63, 1)); 327 328 EXPECT_EQ("int longlongname; // comment\n" 329 "int x = f({\n" 330 " int x; // comment\n" 331 " int y; // comment\n" 332 "});", 333 format("int longlongname; // comment\n" 334 "int x = f({\n" 335 " int x; // comment\n" 336 " int y; // comment\n" 337 "});", 338 65, 0)); 339 EXPECT_EQ("int s = f({\n" 340 " class X {\n" 341 " public:\n" 342 " void f();\n" 343 " };\n" 344 "});", 345 format("int s = f({\n" 346 " class X {\n" 347 " public:\n" 348 " void f();\n" 349 " };\n" 350 "});", 351 0, 0)); 352 EXPECT_EQ("SomeFunction(\n" 353 " [] {\n" 354 " int i;\n" 355 " return i;\n" // Format this line. 356 " },\n" 357 " [] {\n" 358 " return 2;\n" // Don't fix this. 359 " });", 360 format("SomeFunction(\n" 361 " [] {\n" 362 " int i;\n" 363 " return i;\n" // Format this line. 364 " },\n" 365 " [] {\n" 366 " return 2;\n" // Don't fix this. 367 " });", 368 40, 0)); 369 } 370 371 TEST_F(FormatTestSelective, WrongIndent) { 372 EXPECT_EQ("namespace {\n" 373 "int i;\n" 374 "int j;\n" 375 "}", 376 format("namespace {\n" 377 " int i;\n" // Format here. 378 " int j;\n" 379 "}", 380 15, 0)); 381 EXPECT_EQ("namespace {\n" 382 " int i;\n" 383 " int j;\n" 384 "}", 385 format("namespace {\n" 386 " int i;\n" 387 " int j;\n" // Format here. 388 "}", 389 24, 0)); 390 } 391 392 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) { 393 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 394 EXPECT_EQ("int i;\n" 395 "#define A \\\n" 396 " int i; \\\n" 397 " int j\n" 398 "int k;", 399 format("int i;\n" 400 "#define A \\\n" 401 " int i ; \\\n" 402 " int j\n" 403 "int k;", 404 8, 0)); // 8: position of "#define". 405 EXPECT_EQ("int i;\n" 406 "#define A \\\n" 407 " int i; \\\n" 408 " int j\n" 409 "int k;", 410 format("int i;\n" 411 "#define A \\\n" 412 " int i ; \\\n" 413 " int j\n" 414 "int k;", 415 45, 0)); // 45: position of "j". 416 } 417 418 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) { 419 EXPECT_EQ("{\n" 420 "{\n" 421 "a;\n" 422 "b;\n" 423 "}\n" 424 "}", 425 format("{\n" 426 "{\n" 427 "a;\n" 428 " b;\n" 429 "}\n" 430 "}", 431 13, 2)); 432 EXPECT_EQ("{\n" 433 "{\n" 434 " a;\n" 435 " b;\n" 436 " c;\n" 437 " d;\n" 438 "}\n" 439 "}", 440 format("{\n" 441 "{\n" 442 " a;\n" 443 " b;\n" 444 " c;\n" 445 " d;\n" 446 "}\n" 447 "}", 448 9, 2)); 449 EXPECT_EQ("{\n" 450 "{\n" 451 "public:\n" 452 " b;\n" 453 "}\n" 454 "}", 455 format("{\n" 456 "{\n" 457 "public:\n" 458 " b;\n" 459 "}\n" 460 "}", 461 17, 2)); 462 EXPECT_EQ("{\n" 463 "{\n" 464 "a;\n" 465 "}\n" 466 "{\n" 467 " b; //\n" 468 "}\n" 469 "}", 470 format("{\n" 471 "{\n" 472 "a;\n" 473 "}\n" 474 "{\n" 475 " b; //\n" 476 "}\n" 477 "}", 478 22, 2)); 479 EXPECT_EQ(" {\n" 480 " a; //\n" 481 " }", 482 format(" {\n" 483 "a; //\n" 484 " }", 485 4, 2)); 486 EXPECT_EQ("void f() {}\n" 487 "void g() {}", 488 format("void f() {}\n" 489 "void g() {}", 490 13, 0)); 491 EXPECT_EQ("int a; // comment\n" 492 " // line 2\n" 493 "int b;", 494 format("int a; // comment\n" 495 " // line 2\n" 496 " int b;", 497 35, 0)); 498 499 EXPECT_EQ(" void f() {\n" 500 "#define A 1\n" 501 " }", 502 format(" void f() {\n" 503 " #define A 1\n" // Format this line. 504 " }", 505 20, 0)); 506 EXPECT_EQ(" void f() {\n" 507 " int i;\n" 508 "#define A \\\n" 509 " int i; \\\n" 510 " int j;\n" 511 " int k;\n" 512 " }", 513 format(" void f() {\n" 514 " int i;\n" 515 "#define A \\\n" 516 " int i; \\\n" 517 " int j;\n" 518 " int k;\n" // Format this line. 519 " }", 520 67, 0)); 521 522 Style.ColumnLimit = 11; 523 EXPECT_EQ(" int a;\n" 524 " void\n" 525 " ffffff() {\n" 526 " }", 527 format(" int a;\n" 528 "void ffffff() {}", 529 11, 0)); 530 } 531 532 TEST_F(FormatTestSelective, UnderstandsTabs) { 533 Style.IndentWidth = 8; 534 Style.UseTab = FormatStyle::UT_Always; 535 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 536 EXPECT_EQ("void f() {\n" 537 "\tf();\n" 538 "\tg();\n" 539 "}", 540 format("void f() {\n" 541 "\tf();\n" 542 "\tg();\n" 543 "}", 544 0, 0)); 545 EXPECT_EQ("void f() {\n" 546 "\tf();\n" 547 "\tg();\n" 548 "}", 549 format("void f() {\n" 550 "\tf();\n" 551 "\tg();\n" 552 "}", 553 16, 0)); 554 EXPECT_EQ("void f() {\n" 555 " \tf();\n" 556 "\tg();\n" 557 "}", 558 format("void f() {\n" 559 " \tf();\n" 560 " \tg();\n" 561 "}", 562 21, 0)); 563 } 564 565 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) { 566 EXPECT_EQ( 567 "void f() {\n" 568 " if (a) {\n" 569 " g();\n" 570 " h();\n" 571 " }\n" 572 "\n" 573 "void g() {\n" 574 "}", 575 format("void f() {\n" 576 " if (a) {\n" // Assume this was added without the closing brace. 577 " g();\n" 578 " h();\n" 579 "}\n" 580 "\n" 581 "void g() {\n" // Make sure not to format this. 582 "}", 583 15, 0)); 584 } 585 586 TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) { 587 Style = getGoogleStyle(FormatStyle::LK_JavaScript); 588 EXPECT_EQ( 589 "var x = \"a\";\n" 590 "var x = 'a';\n" 591 "var x = \"a\";", 592 format("var x = \"a\";\n" 593 "var x = \"a\";\n" 594 "var x = \"a\";", 595 20, 0)); 596 } 597 598 TEST_F(FormatTestSelective, KeepsIndentAfterCommentSectionImport) { 599 std::string Code = "#include <a> // line 1\n" // 23 chars long 600 " // line 2\n" // 23 chars long 601 "\n" // this newline is char 47 602 "int i;"; // this line is not indented 603 EXPECT_EQ(Code, format(Code, 47, 1)); 604 } 605 606 } // end namespace 607 } // end namespace format 608 } // end namespace clang 609