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;", 126 28, 0)); 127 EXPECT_EQ("int aaaaaa; // comment\n" 128 "int b;\n" 129 "int c; // unrelated comment", 130 format("int aaaaaa; // comment\n" 131 "int b;\n" 132 "int c; // unrelated comment", 133 31, 0)); 134 135 EXPECT_EQ("int a; // This\n" 136 " // is\n" 137 " // a", 138 format("int a; // This\n" 139 " // is\n" 140 " // a", 141 0, 0)); 142 EXPECT_EQ("int a; // This\n" 143 " // is\n" 144 " // a\n" 145 "// This is b\n" 146 "int b;", 147 format("int a; // This\n" 148 " // is\n" 149 " // a\n" 150 "// This is b\n" 151 "int b;", 152 0, 0)); 153 EXPECT_EQ("int a; // This\n" 154 " // is\n" 155 " // a\n" 156 "\n" 157 "//This is unrelated", 158 format("int a; // This\n" 159 " // is\n" 160 " // a\n" 161 "\n" 162 "//This is unrelated", 163 0, 0)); 164 EXPECT_EQ("int a;\n" 165 "// This is\n" 166 "// not formatted. ", 167 format("int a;\n" 168 "// This is\n" 169 "// not formatted. ", 170 0, 0)); 171 EXPECT_EQ("int x; // Format this line.\n" 172 "int xx; //\n" 173 "int xxxxx; //", 174 format("int x; // Format this line.\n" 175 "int xx; //\n" 176 "int xxxxx; //", 177 0, 0)); 178 } 179 180 TEST_F(FormatTestSelective, ContinueReindenting) { 181 // When we change an indent, we continue formatting as long as following 182 // lines are not indented correctly. 183 EXPECT_EQ("int i;\n" 184 "int b;\n" 185 "int c;\n" 186 "int d;\n" 187 "int e;\n" 188 " int f;\n", 189 format("int i;\n" 190 " int b;\n" 191 " int c;\n" 192 " int d;\n" 193 "int e;\n" 194 " int f;\n", 195 11, 0)); 196 } 197 198 TEST_F(FormatTestSelective, ReindentClosingBrace) { 199 EXPECT_EQ("int i;\n" 200 "int f() {\n" 201 " int a;\n" 202 " int b;\n" 203 "}\n" 204 " int c;\n", 205 format("int i;\n" 206 " int f(){\n" 207 "int a;\n" 208 "int b;\n" 209 " }\n" 210 " int c;\n", 211 11, 0)); 212 EXPECT_EQ("void f() {\n" 213 " if (foo) {\n" 214 " b();\n" 215 " } else {\n" 216 " c();\n" 217 " }\n" 218 "int d;\n" 219 "}\n", 220 format("void f() {\n" 221 " if (foo) {\n" 222 "b();\n" 223 "}else{\n" 224 "c();\n" 225 "}\n" 226 "int d;\n" 227 "}\n", 228 13, 0)); 229 EXPECT_EQ("int i = []() {\n" 230 " class C {\n" 231 " int a;\n" 232 " int b;\n" 233 " };\n" 234 " int c;\n" 235 "};\n", 236 format("int i = []() {\n" 237 " class C{\n" 238 "int a;\n" 239 "int b;\n" 240 "};\n" 241 "int c;\n" 242 " };\n", 243 17, 0)); 244 } 245 246 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) { 247 EXPECT_EQ("DEBUG({\n" 248 " int i;\n" 249 " int j;\n" 250 "});", 251 format("DEBUG( {\n" 252 " int i;\n" 253 " int j;\n" 254 "} ) ;", 255 20, 1)); 256 EXPECT_EQ("DEBUG( {\n" 257 " int i;\n" 258 " int j;\n" 259 "} ) ;", 260 format("DEBUG( {\n" 261 " int i;\n" 262 " int j;\n" 263 "} ) ;", 264 41, 1)); 265 EXPECT_EQ("DEBUG( {\n" 266 " int i;\n" 267 " int j;\n" 268 "} ) ;", 269 format("DEBUG( {\n" 270 " int i;\n" 271 " int j;\n" 272 "} ) ;", 273 41, 1)); 274 EXPECT_EQ("DEBUG({\n" 275 " int i;\n" 276 " int j;\n" 277 "});", 278 format("DEBUG( {\n" 279 " int i;\n" 280 " int j;\n" 281 "} ) ;", 282 20, 1)); 283 284 EXPECT_EQ("Debug({\n" 285 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 286 " return;\n" 287 " },\n" 288 " a);", 289 format("Debug({\n" 290 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 291 " return;\n" 292 " },\n" 293 " a);", 294 50, 1)); 295 EXPECT_EQ("DEBUG({\n" 296 " DEBUG({\n" 297 " int a;\n" 298 " int b;\n" 299 " }) ;\n" 300 "});", 301 format("DEBUG({\n" 302 " DEBUG({\n" 303 " int a;\n" 304 " int b;\n" // Format this line only. 305 " }) ;\n" // Don't touch this line. 306 "});", 307 35, 0)); 308 EXPECT_EQ("DEBUG({\n" 309 " int a; //\n" 310 "});", 311 format("DEBUG({\n" 312 " int a; //\n" 313 "});", 314 0, 0)); 315 EXPECT_EQ("someFunction(\n" 316 " [] {\n" 317 " // Only with this comment.\n" 318 " int i; // invoke formatting here.\n" 319 " }, // force line break\n" 320 " aaa);", 321 format("someFunction(\n" 322 " [] {\n" 323 " // Only with this comment.\n" 324 " int i; // invoke formatting here.\n" 325 " }, // force line break\n" 326 " aaa);", 327 63, 1)); 328 329 EXPECT_EQ("int longlongname; // comment\n" 330 "int x = f({\n" 331 " int x; // comment\n" 332 " int y; // comment\n" 333 "});", 334 format("int longlongname; // comment\n" 335 "int x = f({\n" 336 " int x; // comment\n" 337 " int y; // comment\n" 338 "});", 339 65, 0)); 340 EXPECT_EQ("int s = f({\n" 341 " class X {\n" 342 " public:\n" 343 " void f();\n" 344 " };\n" 345 "});", 346 format("int s = f({\n" 347 " class X {\n" 348 " public:\n" 349 " void f();\n" 350 " };\n" 351 "});", 352 0, 0)); 353 EXPECT_EQ("SomeFunction(\n" 354 " [] {\n" 355 " int i;\n" 356 " return i;\n" // Format this line. 357 " },\n" 358 " [] {\n" 359 " return 2;\n" // Don't fix this. 360 " });", 361 format("SomeFunction(\n" 362 " [] {\n" 363 " int i;\n" 364 " return i;\n" // Format this line. 365 " },\n" 366 " [] {\n" 367 " return 2;\n" // Don't fix this. 368 " });", 369 40, 0)); 370 } 371 372 TEST_F(FormatTestSelective, WrongIndent) { 373 EXPECT_EQ("namespace {\n" 374 "int i;\n" 375 "int j;\n" 376 "}", 377 format("namespace {\n" 378 " int i;\n" // Format here. 379 " int j;\n" 380 "}", 381 15, 0)); 382 EXPECT_EQ("namespace {\n" 383 " int i;\n" 384 " int j;\n" 385 "}", 386 format("namespace {\n" 387 " int i;\n" 388 " int j;\n" // Format here. 389 "}", 390 24, 0)); 391 } 392 393 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) { 394 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 395 EXPECT_EQ("int i;\n" 396 "#define A \\\n" 397 " int i; \\\n" 398 " int j\n" 399 "int k;", 400 format("int i;\n" 401 "#define A \\\n" 402 " int i ; \\\n" 403 " int j\n" 404 "int k;", 405 8, 0)); // 8: position of "#define". 406 EXPECT_EQ("int i;\n" 407 "#define A \\\n" 408 " int i; \\\n" 409 " int j\n" 410 "int k;", 411 format("int i;\n" 412 "#define A \\\n" 413 " int i ; \\\n" 414 " int j\n" 415 "int k;", 416 45, 0)); // 45: position of "j". 417 } 418 419 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) { 420 EXPECT_EQ("{\n" 421 "{\n" 422 "a;\n" 423 "b;\n" 424 "}\n" 425 "}", 426 format("{\n" 427 "{\n" 428 "a;\n" 429 " b;\n" 430 "}\n" 431 "}", 432 13, 2)); 433 EXPECT_EQ("{\n" 434 "{\n" 435 " a;\n" 436 " b;\n" 437 " c;\n" 438 " d;\n" 439 "}\n" 440 "}", 441 format("{\n" 442 "{\n" 443 " a;\n" 444 " b;\n" 445 " c;\n" 446 " d;\n" 447 "}\n" 448 "}", 449 9, 2)); 450 EXPECT_EQ("{\n" 451 "{\n" 452 "public:\n" 453 " b;\n" 454 "}\n" 455 "}", 456 format("{\n" 457 "{\n" 458 "public:\n" 459 " b;\n" 460 "}\n" 461 "}", 462 17, 2)); 463 EXPECT_EQ("{\n" 464 "{\n" 465 "a;\n" 466 "}\n" 467 "{\n" 468 " b; //\n" 469 "}\n" 470 "}", 471 format("{\n" 472 "{\n" 473 "a;\n" 474 "}\n" 475 "{\n" 476 " b; //\n" 477 "}\n" 478 "}", 479 22, 2)); 480 EXPECT_EQ(" {\n" 481 " a; //\n" 482 " }", 483 format(" {\n" 484 "a; //\n" 485 " }", 486 4, 2)); 487 EXPECT_EQ("void f() {}\n" 488 "void g() {}", 489 format("void f() {}\n" 490 "void g() {}", 491 13, 0)); 492 EXPECT_EQ("int a; // comment\n" 493 " // line 2\n" 494 "int b;", 495 format("int a; // comment\n" 496 " // line 2\n" 497 " int b;", 498 35, 0)); 499 500 EXPECT_EQ(" void f() {\n" 501 "#define A 1\n" 502 " }", 503 format(" void f() {\n" 504 " #define A 1\n" // Format this line. 505 " }", 506 20, 0)); 507 EXPECT_EQ(" void f() {\n" 508 " int i;\n" 509 "#define A \\\n" 510 " int i; \\\n" 511 " int j;\n" 512 " int k;\n" 513 " }", 514 format(" void f() {\n" 515 " int i;\n" 516 "#define A \\\n" 517 " int i; \\\n" 518 " int j;\n" 519 " int k;\n" // Format this line. 520 " }", 521 67, 0)); 522 523 Style.ColumnLimit = 11; 524 EXPECT_EQ(" int a;\n" 525 " void\n" 526 " ffffff() {\n" 527 " }", 528 format(" int a;\n" 529 "void ffffff() {}", 530 11, 0)); 531 } 532 533 TEST_F(FormatTestSelective, UnderstandsTabs) { 534 Style.IndentWidth = 8; 535 Style.UseTab = FormatStyle::UT_Always; 536 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 537 EXPECT_EQ("void f() {\n" 538 "\tf();\n" 539 "\tg();\n" 540 "}", 541 format("void f() {\n" 542 "\tf();\n" 543 "\tg();\n" 544 "}", 545 0, 0)); 546 EXPECT_EQ("void f() {\n" 547 "\tf();\n" 548 "\tg();\n" 549 "}", 550 format("void f() {\n" 551 "\tf();\n" 552 "\tg();\n" 553 "}", 554 16, 0)); 555 EXPECT_EQ("void f() {\n" 556 " \tf();\n" 557 "\tg();\n" 558 "}", 559 format("void f() {\n" 560 " \tf();\n" 561 " \tg();\n" 562 "}", 563 21, 0)); 564 } 565 566 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) { 567 EXPECT_EQ( 568 "void f() {\n" 569 " if (a) {\n" 570 " g();\n" 571 " h();\n" 572 " }\n" 573 "\n" 574 "void g() {\n" 575 "}", 576 format("void f() {\n" 577 " if (a) {\n" // Assume this was added without the closing brace. 578 " g();\n" 579 " h();\n" 580 "}\n" 581 "\n" 582 "void g() {\n" // Make sure not to format this. 583 "}", 584 15, 0)); 585 } 586 587 TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) { 588 Style = getGoogleStyle(FormatStyle::LK_JavaScript); 589 EXPECT_EQ("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