1 //===- unittest/Format/SortIncludesTest.cpp - Include sort 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/ADT/None.h" 12 #include "llvm/ADT/StringRef.h" 13 #include "llvm/Support/Debug.h" 14 #include "gtest/gtest.h" 15 16 #define DEBUG_TYPE "format-test" 17 18 namespace clang { 19 namespace format { 20 namespace { 21 22 class SortIncludesTest : public ::testing::Test { 23 protected: 24 std::vector<tooling::Range> GetCodeRange(StringRef Code) { 25 return std::vector<tooling::Range>(1, tooling::Range(0, Code.size())); 26 } 27 28 std::string sort(StringRef Code, std::vector<tooling::Range> Ranges, 29 StringRef FileName = "input.cc", 30 unsigned ExpectedNumRanges = 1) { 31 auto Replaces = sortIncludes(FmtStyle, Code, Ranges, FileName); 32 Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); 33 EXPECT_EQ(ExpectedNumRanges, Replaces.size()); 34 auto Sorted = applyAllReplacements(Code, Replaces); 35 EXPECT_TRUE(static_cast<bool>(Sorted)); 36 auto Result = applyAllReplacements( 37 *Sorted, reformat(FmtStyle, *Sorted, Ranges, FileName)); 38 EXPECT_TRUE(static_cast<bool>(Result)); 39 return *Result; 40 } 41 42 std::string sort(StringRef Code, StringRef FileName = "input.cpp", 43 unsigned ExpectedNumRanges = 1) { 44 return sort(Code, GetCodeRange(Code), FileName, ExpectedNumRanges); 45 } 46 47 unsigned newCursor(llvm::StringRef Code, unsigned Cursor) { 48 sortIncludes(FmtStyle, Code, GetCodeRange(Code), "input.cpp", &Cursor); 49 return Cursor; 50 } 51 52 FormatStyle FmtStyle = getLLVMStyle(); 53 tooling::IncludeStyle &Style = FmtStyle.IncludeStyle; 54 }; 55 56 TEST_F(SortIncludesTest, BasicSorting) { 57 EXPECT_EQ("#include \"a.h\"\n" 58 "#include \"b.h\"\n" 59 "#include \"c.h\"\n", 60 sort("#include \"a.h\"\n" 61 "#include \"c.h\"\n" 62 "#include \"b.h\"\n")); 63 64 EXPECT_EQ("// comment\n" 65 "#include <a>\n" 66 "#include <b>\n", 67 sort("// comment\n" 68 "#include <b>\n" 69 "#include <a>\n", 70 {tooling::Range(25, 1)})); 71 } 72 73 TEST_F(SortIncludesTest, TrailingComments) { 74 EXPECT_EQ("#include \"a.h\"\n" 75 "#include \"b.h\" /* long\n" 76 " * long\n" 77 " * comment*/\n" 78 "#include \"c.h\"\n" 79 "#include \"d.h\"\n", 80 sort("#include \"a.h\"\n" 81 "#include \"c.h\"\n" 82 "#include \"b.h\" /* long\n" 83 " * long\n" 84 " * comment*/\n" 85 "#include \"d.h\"\n")); 86 } 87 88 TEST_F(SortIncludesTest, SortedIncludesUsingSortPriorityAttribute) { 89 FmtStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 90 FmtStyle.IncludeStyle.IncludeCategories = { 91 {"^<sys/param\\.h>", 1, 0, false}, 92 {"^<sys/types\\.h>", 1, 1, false}, 93 {"^<sys.*/", 1, 2, false}, 94 {"^<uvm/", 2, 3, false}, 95 {"^<machine/", 3, 4, false}, 96 {"^<dev/", 4, 5, false}, 97 {"^<net.*/", 5, 6, false}, 98 {"^<protocols/", 5, 7, false}, 99 {"^<(fs|miscfs|msdosfs|nfs|ntfs|ufs)/", 6, 8, false}, 100 {"^<(x86|amd64|i386|xen)/", 7, 8, false}, 101 {"<path", 9, 11, false}, 102 {"^<[^/].*\\.h>", 8, 10, false}, 103 {"^\".*\\.h\"", 10, 12, false}}; 104 EXPECT_EQ("#include <sys/param.h>\n" 105 "#include <sys/types.h>\n" 106 "#include <sys/ioctl.h>\n" 107 "#include <sys/socket.h>\n" 108 "#include <sys/stat.h>\n" 109 "#include <sys/wait.h>\n" 110 "\n" 111 "#include <net/if.h>\n" 112 "#include <net/if_dl.h>\n" 113 "#include <net/route.h>\n" 114 "#include <netinet/in.h>\n" 115 "#include <protocols/rwhod.h>\n" 116 "\n" 117 "#include <assert.h>\n" 118 "#include <errno.h>\n" 119 "#include <inttypes.h>\n" 120 "#include <stdio.h>\n" 121 "#include <stdlib.h>\n" 122 "\n" 123 "#include <paths.h>\n" 124 "\n" 125 "#include \"pathnames.h\"\n", 126 sort("#include <sys/param.h>\n" 127 "#include <sys/types.h>\n" 128 "#include <sys/ioctl.h>\n" 129 "#include <net/if_dl.h>\n" 130 "#include <net/route.h>\n" 131 "#include <netinet/in.h>\n" 132 "#include <sys/socket.h>\n" 133 "#include <sys/stat.h>\n" 134 "#include <sys/wait.h>\n" 135 "#include <net/if.h>\n" 136 "#include <protocols/rwhod.h>\n" 137 "#include <assert.h>\n" 138 "#include <paths.h>\n" 139 "#include \"pathnames.h\"\n" 140 "#include <errno.h>\n" 141 "#include <inttypes.h>\n" 142 "#include <stdio.h>\n" 143 "#include <stdlib.h>\n")); 144 } 145 TEST_F(SortIncludesTest, SortPriorityNotDefined) { 146 FmtStyle = getLLVMStyle(); 147 EXPECT_EQ("#include \"FormatTestUtils.h\"\n" 148 "#include \"clang/Format/Format.h\"\n" 149 "#include \"llvm/ADT/None.h\"\n" 150 "#include \"llvm/Support/Debug.h\"\n" 151 "#include \"gtest/gtest.h\"\n", 152 sort("#include \"clang/Format/Format.h\"\n" 153 "#include \"llvm/ADT/None.h\"\n" 154 "#include \"FormatTestUtils.h\"\n" 155 "#include \"gtest/gtest.h\"\n" 156 "#include \"llvm/Support/Debug.h\"\n")); 157 } 158 159 TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) { 160 // Identical #includes have led to a failure with an unstable sort. 161 std::string Code = "#include <a>\n" 162 "#include <b>\n" 163 "#include <c>\n" 164 "#include <d>\n" 165 "#include <e>\n" 166 "#include <f>\n"; 167 EXPECT_TRUE(sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a.cc").empty()); 168 } 169 170 TEST_F(SortIncludesTest, MainFileHeader) { 171 std::string Code = "#include <string>\n" 172 "\n" 173 "#include \"a/extra_action.proto.h\"\n"; 174 FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp); 175 EXPECT_TRUE( 176 sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a/extra_action.cc") 177 .empty()); 178 179 EXPECT_EQ("#include \"foo.bar.h\"\n" 180 "\n" 181 "#include \"a.h\"\n", 182 sort("#include \"a.h\"\n" 183 "#include \"foo.bar.h\"\n", 184 "foo.bar.cc")); 185 } 186 187 TEST_F(SortIncludesTest, SortedIncludesInMultipleBlocksAreMerged) { 188 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge; 189 EXPECT_EQ("#include \"a.h\"\n" 190 "#include \"b.h\"\n" 191 "#include \"c.h\"\n", 192 sort("#include \"a.h\"\n" 193 "#include \"c.h\"\n" 194 "\n" 195 "\n" 196 "#include \"b.h\"\n")); 197 198 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 199 EXPECT_EQ("#include \"a.h\"\n" 200 "#include \"b.h\"\n" 201 "#include \"c.h\"\n", 202 sort("#include \"a.h\"\n" 203 "#include \"c.h\"\n" 204 "\n" 205 "\n" 206 "#include \"b.h\"\n")); 207 } 208 209 TEST_F(SortIncludesTest, SupportClangFormatOff) { 210 EXPECT_EQ("#include <a>\n" 211 "#include <b>\n" 212 "#include <c>\n" 213 "// clang-format off\n" 214 "#include <b>\n" 215 "#include <a>\n" 216 "#include <c>\n" 217 "// clang-format on\n", 218 sort("#include <b>\n" 219 "#include <a>\n" 220 "#include <c>\n" 221 "// clang-format off\n" 222 "#include <b>\n" 223 "#include <a>\n" 224 "#include <c>\n" 225 "// clang-format on\n")); 226 227 Style.IncludeBlocks = Style.IBS_Merge; 228 std::string Code = "// clang-format off\r\n" 229 "#include \"d.h\"\r\n" 230 "#include \"b.h\"\r\n" 231 "// clang-format on\r\n" 232 "\r\n" 233 "#include \"c.h\"\r\n" 234 "#include \"a.h\"\r\n" 235 "#include \"e.h\"\r\n"; 236 237 std::string Expected = "// clang-format off\r\n" 238 "#include \"d.h\"\r\n" 239 "#include \"b.h\"\r\n" 240 "// clang-format on\r\n" 241 "\r\n" 242 "#include \"e.h\"\r\n" 243 "#include \"a.h\"\r\n" 244 "#include \"c.h\"\r\n"; 245 246 EXPECT_EQ(Expected, sort(Code, "e.cpp", 1)); 247 } 248 249 TEST_F(SortIncludesTest, SupportClangFormatOffCStyle) { 250 EXPECT_EQ("#include <a>\n" 251 "#include <b>\n" 252 "#include <c>\n" 253 "/* clang-format off */\n" 254 "#include <b>\n" 255 "#include <a>\n" 256 "#include <c>\n" 257 "/* clang-format on */\n", 258 sort("#include <b>\n" 259 "#include <a>\n" 260 "#include <c>\n" 261 "/* clang-format off */\n" 262 "#include <b>\n" 263 "#include <a>\n" 264 "#include <c>\n" 265 "/* clang-format on */\n")); 266 267 // Not really turning it off 268 EXPECT_EQ("#include <a>\n" 269 "#include <b>\n" 270 "#include <c>\n" 271 "/* clang-format offically */\n" 272 "#include <a>\n" 273 "#include <b>\n" 274 "#include <c>\n" 275 "/* clang-format onwards */\n", 276 sort("#include <b>\n" 277 "#include <a>\n" 278 "#include <c>\n" 279 "/* clang-format offically */\n" 280 "#include <b>\n" 281 "#include <a>\n" 282 "#include <c>\n" 283 "/* clang-format onwards */\n", 284 "input.h", 2)); 285 } 286 287 TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) { 288 FmtStyle.SortIncludes = FormatStyle::SI_Never; 289 EXPECT_EQ("#include \"a.h\"\n" 290 "#include \"c.h\"\n" 291 "#include \"b.h\"\n", 292 sort("#include \"a.h\"\n" 293 "#include \"c.h\"\n" 294 "#include \"b.h\"\n", 295 "input.h", 0)); 296 } 297 298 TEST_F(SortIncludesTest, MixIncludeAndImport) { 299 EXPECT_EQ("#include \"a.h\"\n" 300 "#import \"b.h\"\n" 301 "#include \"c.h\"\n", 302 sort("#include \"a.h\"\n" 303 "#include \"c.h\"\n" 304 "#import \"b.h\"\n")); 305 } 306 307 TEST_F(SortIncludesTest, FixTrailingComments) { 308 EXPECT_EQ("#include \"a.h\" // comment\n" 309 "#include \"bb.h\" // comment\n" 310 "#include \"ccc.h\"\n", 311 sort("#include \"a.h\" // comment\n" 312 "#include \"ccc.h\"\n" 313 "#include \"bb.h\" // comment\n")); 314 } 315 316 TEST_F(SortIncludesTest, LeadingWhitespace) { 317 EXPECT_EQ("#include \"a.h\"\n" 318 "#include \"b.h\"\n" 319 "#include \"c.h\"\n", 320 sort(" #include \"a.h\"\n" 321 " #include \"c.h\"\n" 322 " #include \"b.h\"\n")); 323 EXPECT_EQ("#include \"a.h\"\n" 324 "#include \"b.h\"\n" 325 "#include \"c.h\"\n", 326 sort("# include \"a.h\"\n" 327 "# include \"c.h\"\n" 328 "# include \"b.h\"\n")); 329 EXPECT_EQ("#include \"a.h\"\n", sort("#include \"a.h\"\n" 330 " #include \"a.h\"\n")); 331 } 332 333 TEST_F(SortIncludesTest, TrailingWhitespace) { 334 EXPECT_EQ("#include \"a.h\"\n" 335 "#include \"b.h\"\n" 336 "#include \"c.h\"\n", 337 sort("#include \"a.h\" \n" 338 "#include \"c.h\" \n" 339 "#include \"b.h\" \n")); 340 EXPECT_EQ("#include \"a.h\"\n", sort("#include \"a.h\"\n" 341 "#include \"a.h\" \n")); 342 } 343 344 TEST_F(SortIncludesTest, GreaterInComment) { 345 EXPECT_EQ("#include \"a.h\"\n" 346 "#include \"b.h\" // >\n" 347 "#include \"c.h\"\n", 348 sort("#include \"a.h\"\n" 349 "#include \"c.h\"\n" 350 "#include \"b.h\" // >\n")); 351 } 352 353 TEST_F(SortIncludesTest, SortsLocallyInEachBlock) { 354 EXPECT_EQ("#include \"a.h\"\n" 355 "#include \"c.h\"\n" 356 "\n" 357 "#include \"b.h\"\n", 358 sort("#include \"a.h\"\n" 359 "#include \"c.h\"\n" 360 "\n" 361 "#include \"b.h\"\n", 362 "input.h", 0)); 363 } 364 365 TEST_F(SortIncludesTest, SortsAllBlocksWhenMerging) { 366 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge; 367 EXPECT_EQ("#include \"a.h\"\n" 368 "#include \"b.h\"\n" 369 "#include \"c.h\"\n", 370 sort("#include \"a.h\"\n" 371 "#include \"c.h\"\n" 372 "\n" 373 "#include \"b.h\"\n")); 374 } 375 376 TEST_F(SortIncludesTest, CommentsAlwaysSeparateGroups) { 377 EXPECT_EQ("#include \"a.h\"\n" 378 "#include \"c.h\"\n" 379 "// comment\n" 380 "#include \"b.h\"\n", 381 sort("#include \"c.h\"\n" 382 "#include \"a.h\"\n" 383 "// comment\n" 384 "#include \"b.h\"\n")); 385 386 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge; 387 EXPECT_EQ("#include \"a.h\"\n" 388 "#include \"c.h\"\n" 389 "// comment\n" 390 "#include \"b.h\"\n", 391 sort("#include \"c.h\"\n" 392 "#include \"a.h\"\n" 393 "// comment\n" 394 "#include \"b.h\"\n")); 395 396 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 397 EXPECT_EQ("#include \"a.h\"\n" 398 "#include \"c.h\"\n" 399 "// comment\n" 400 "#include \"b.h\"\n", 401 sort("#include \"c.h\"\n" 402 "#include \"a.h\"\n" 403 "// comment\n" 404 "#include \"b.h\"\n")); 405 } 406 407 TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) { 408 EXPECT_EQ("#include \"a.h\"\n" 409 "#include \"c.h\"\n" 410 "#include <array>\n" 411 "#include <b.h>\n" 412 "#include <d.h>\n" 413 "#include <vector>\n", 414 sort("#include <vector>\n" 415 "#include <d.h>\n" 416 "#include <array>\n" 417 "#include <b.h>\n" 418 "#include \"c.h\"\n" 419 "#include \"a.h\"\n")); 420 421 FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp); 422 EXPECT_EQ("#include <b.h>\n" 423 "#include <d.h>\n" 424 "\n" 425 "#include <array>\n" 426 "#include <vector>\n" 427 "\n" 428 "#include \"a.h\"\n" 429 "#include \"c.h\"\n", 430 sort("#include <vector>\n" 431 "#include <d.h>\n" 432 "#include <array>\n" 433 "#include <b.h>\n" 434 "#include \"c.h\"\n" 435 "#include \"a.h\"\n")); 436 } 437 438 TEST_F(SortIncludesTest, RegroupsAngledIncludesInSeparateBlocks) { 439 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 440 EXPECT_EQ("#include \"a.h\"\n" 441 "#include \"c.h\"\n" 442 "\n" 443 "#include <b.h>\n" 444 "#include <d.h>\n", 445 sort("#include <d.h>\n" 446 "#include <b.h>\n" 447 "#include \"c.h\"\n" 448 "#include \"a.h\"\n")); 449 } 450 451 TEST_F(SortIncludesTest, HandlesMultilineIncludes) { 452 EXPECT_EQ("#include \"a.h\"\n" 453 "#include \"b.h\"\n" 454 "#include \"c.h\"\n", 455 sort("#include \"a.h\"\n" 456 "#include \\\n" 457 "\"c.h\"\n" 458 "#include \"b.h\"\n")); 459 } 460 461 TEST_F(SortIncludesTest, SupportAtImportLines) { 462 // Test from https://github.com/llvm/llvm-project/issues/38995 463 EXPECT_EQ("#import \"a.h\"\n" 464 "#import \"b.h\"\n" 465 "#import \"c.h\"\n" 466 "#import <d/e.h>\n" 467 "@import Foundation;\n", 468 sort("#import \"b.h\"\n" 469 "#import \"c.h\"\n" 470 "#import <d/e.h>\n" 471 "@import Foundation;\n" 472 "#import \"a.h\"\n")); 473 474 // Slightly more complicated test that shows sorting in each priorities still 475 // works. 476 EXPECT_EQ("#import \"a.h\"\n" 477 "#import \"b.h\"\n" 478 "#import \"c.h\"\n" 479 "#import <d/e.h>\n" 480 "@import Base;\n" 481 "@import Foundation;\n" 482 "@import base;\n" 483 "@import foundation;\n", 484 sort("#import \"b.h\"\n" 485 "#import \"c.h\"\n" 486 "@import Base;\n" 487 "#import <d/e.h>\n" 488 "@import foundation;\n" 489 "@import Foundation;\n" 490 "@import base;\n" 491 "#import \"a.h\"\n")); 492 493 // Test that shows main headers in two groups are still found and sorting 494 // still works. The @import's are kept in their respective group but are 495 // put at the end of each group. 496 FmtStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Preserve; 497 EXPECT_EQ("#import \"foo.hpp\"\n" 498 "#import \"b.h\"\n" 499 "#import \"c.h\"\n" 500 "#import <d/e.h>\n" 501 "@import Base;\n" 502 "@import Foundation;\n" 503 "@import foundation;\n" 504 "\n" 505 "#import \"foo.h\"\n" 506 "#include \"foobar\"\n" 507 "#import <f/g.h>\n" 508 "@import AAAA;\n" 509 "@import aaaa;\n", 510 sort("#import \"b.h\"\n" 511 "@import Foundation;\n" 512 "@import foundation;\n" 513 "#import \"c.h\"\n" 514 "#import <d/e.h>\n" 515 "@import Base;\n" 516 "#import \"foo.hpp\"\n" 517 "\n" 518 "@import aaaa;\n" 519 "#import <f/g.h>\n" 520 "@import AAAA;\n" 521 "#include \"foobar\"\n" 522 "#import \"foo.h\"\n", 523 "foo.c", 2)); 524 525 // Regrouping and putting @import's in the very last group 526 FmtStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 527 EXPECT_EQ("#import \"foo.hpp\"\n" 528 "\n" 529 "#import \"b.h\"\n" 530 "#import \"c.h\"\n" 531 "#import \"foo.h\"\n" 532 "#include \"foobar\"\n" 533 "\n" 534 "#import <d/e.h>\n" 535 "#import <f/g.h>\n" 536 "\n" 537 "@import AAAA;\n" 538 "@import Base;\n" 539 "@import Foundation;\n" 540 "@import aaaa;\n" 541 "@import foundation;\n", 542 sort("#import \"b.h\"\n" 543 "@import Foundation;\n" 544 "@import foundation;\n" 545 "#import \"c.h\"\n" 546 "#import <d/e.h>\n" 547 "@import Base;\n" 548 "#import \"foo.hpp\"\n" 549 "\n" 550 "@import aaaa;\n" 551 "#import <f/g.h>\n" 552 "@import AAAA;\n" 553 "#include \"foobar\"\n" 554 "#import \"foo.h\"\n", 555 "foo.c")); 556 } 557 558 TEST_F(SortIncludesTest, LeavesMainHeaderFirst) { 559 Style.IncludeIsMainRegex = "([-_](test|unittest))?$"; 560 EXPECT_EQ("#include \"llvm/a.h\"\n" 561 "#include \"b.h\"\n" 562 "#include \"c.h\"\n", 563 sort("#include \"llvm/a.h\"\n" 564 "#include \"c.h\"\n" 565 "#include \"b.h\"\n", 566 "a.cc")); 567 EXPECT_EQ("#include \"llvm/a.h\"\n" 568 "#include \"b.h\"\n" 569 "#include \"c.h\"\n", 570 sort("#include \"llvm/a.h\"\n" 571 "#include \"c.h\"\n" 572 "#include \"b.h\"\n", 573 "a_test.cc")); 574 EXPECT_EQ("#include \"llvm/input.h\"\n" 575 "#include \"b.h\"\n" 576 "#include \"c.h\"\n", 577 sort("#include \"llvm/input.h\"\n" 578 "#include \"c.h\"\n" 579 "#include \"b.h\"\n", 580 "input.mm")); 581 582 // Don't allow prefixes. 583 EXPECT_EQ("#include \"b.h\"\n" 584 "#include \"c.h\"\n" 585 "#include \"llvm/not_a.h\"\n", 586 sort("#include \"llvm/not_a.h\"\n" 587 "#include \"c.h\"\n" 588 "#include \"b.h\"\n", 589 "a.cc")); 590 591 // Don't do this for _main and other suffixes. 592 EXPECT_EQ("#include \"b.h\"\n" 593 "#include \"c.h\"\n" 594 "#include \"llvm/a.h\"\n", 595 sort("#include \"llvm/a.h\"\n" 596 "#include \"c.h\"\n" 597 "#include \"b.h\"\n", 598 "a_main.cc")); 599 600 // Don't do this in headers. 601 EXPECT_EQ("#include \"b.h\"\n" 602 "#include \"c.h\"\n" 603 "#include \"llvm/a.h\"\n", 604 sort("#include \"llvm/a.h\"\n" 605 "#include \"c.h\"\n" 606 "#include \"b.h\"\n", 607 "a.h")); 608 609 // Only do this in the first #include block. 610 EXPECT_EQ("#include <a>\n" 611 "\n" 612 "#include \"b.h\"\n" 613 "#include \"c.h\"\n" 614 "#include \"llvm/a.h\"\n", 615 sort("#include <a>\n" 616 "\n" 617 "#include \"llvm/a.h\"\n" 618 "#include \"c.h\"\n" 619 "#include \"b.h\"\n", 620 "a.cc")); 621 622 // Only recognize the first #include with a matching basename as main include. 623 EXPECT_EQ("#include \"a.h\"\n" 624 "#include \"b.h\"\n" 625 "#include \"c.h\"\n" 626 "#include \"llvm/a.h\"\n", 627 sort("#include \"b.h\"\n" 628 "#include \"a.h\"\n" 629 "#include \"c.h\"\n" 630 "#include \"llvm/a.h\"\n", 631 "a.cc")); 632 } 633 634 TEST_F(SortIncludesTest, LeavesMainHeaderFirstInAdditionalExtensions) { 635 Style.IncludeIsMainRegex = "([-_](test|unittest))?|(Impl)?$"; 636 EXPECT_EQ("#include \"b.h\"\n" 637 "#include \"c.h\"\n" 638 "#include \"llvm/a.h\"\n", 639 sort("#include \"llvm/a.h\"\n" 640 "#include \"c.h\"\n" 641 "#include \"b.h\"\n", 642 "a_test.xxx")); 643 EXPECT_EQ("#include \"b.h\"\n" 644 "#include \"c.h\"\n" 645 "#include \"llvm/a.h\"\n", 646 sort("#include \"llvm/a.h\"\n" 647 "#include \"c.h\"\n" 648 "#include \"b.h\"\n", 649 "aImpl.hpp")); 650 651 // .cpp extension is considered "main" by default 652 EXPECT_EQ("#include \"llvm/a.h\"\n" 653 "#include \"b.h\"\n" 654 "#include \"c.h\"\n", 655 sort("#include \"llvm/a.h\"\n" 656 "#include \"c.h\"\n" 657 "#include \"b.h\"\n", 658 "aImpl.cpp")); 659 EXPECT_EQ("#include \"llvm/a.h\"\n" 660 "#include \"b.h\"\n" 661 "#include \"c.h\"\n", 662 sort("#include \"llvm/a.h\"\n" 663 "#include \"c.h\"\n" 664 "#include \"b.h\"\n", 665 "a_test.cpp")); 666 667 // Allow additional filenames / extensions 668 Style.IncludeIsMainSourceRegex = "(Impl\\.hpp)|(\\.xxx)$"; 669 EXPECT_EQ("#include \"llvm/a.h\"\n" 670 "#include \"b.h\"\n" 671 "#include \"c.h\"\n", 672 sort("#include \"llvm/a.h\"\n" 673 "#include \"c.h\"\n" 674 "#include \"b.h\"\n", 675 "a_test.xxx")); 676 EXPECT_EQ("#include \"llvm/a.h\"\n" 677 "#include \"b.h\"\n" 678 "#include \"c.h\"\n", 679 sort("#include \"llvm/a.h\"\n" 680 "#include \"c.h\"\n" 681 "#include \"b.h\"\n", 682 "aImpl.hpp")); 683 } 684 685 TEST_F(SortIncludesTest, RecognizeMainHeaderInAllGroups) { 686 Style.IncludeIsMainRegex = "([-_](test|unittest))?$"; 687 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge; 688 689 EXPECT_EQ("#include \"c.h\"\n" 690 "#include \"a.h\"\n" 691 "#include \"b.h\"\n", 692 sort("#include \"b.h\"\n" 693 "\n" 694 "#include \"a.h\"\n" 695 "#include \"c.h\"\n", 696 "c.cc")); 697 } 698 699 TEST_F(SortIncludesTest, MainHeaderIsSeparatedWhenRegroupping) { 700 Style.IncludeIsMainRegex = "([-_](test|unittest))?$"; 701 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 702 703 EXPECT_EQ("#include \"a.h\"\n" 704 "\n" 705 "#include \"b.h\"\n" 706 "#include \"c.h\"\n", 707 sort("#include \"b.h\"\n" 708 "\n" 709 "#include \"a.h\"\n" 710 "#include \"c.h\"\n", 711 "a.cc")); 712 } 713 714 TEST_F(SortIncludesTest, SupportOptionalCaseSensitiveSorting) { 715 EXPECT_FALSE(FmtStyle.SortIncludes == FormatStyle::SI_CaseInsensitive); 716 717 FmtStyle.SortIncludes = FormatStyle::SI_CaseInsensitive; 718 719 EXPECT_EQ("#include \"A/B.h\"\n" 720 "#include \"A/b.h\"\n" 721 "#include \"a/b.h\"\n" 722 "#include \"B/A.h\"\n" 723 "#include \"B/a.h\"\n", 724 sort("#include \"B/a.h\"\n" 725 "#include \"B/A.h\"\n" 726 "#include \"A/B.h\"\n" 727 "#include \"a/b.h\"\n" 728 "#include \"A/b.h\"\n", 729 "a.h")); 730 731 Style.IncludeBlocks = clang::tooling::IncludeStyle::IBS_Regroup; 732 Style.IncludeCategories = { 733 {"^\"", 1, 0, false}, {"^<.*\\.h>$", 2, 0, false}, {"^<", 3, 0, false}}; 734 735 StringRef UnsortedCode = "#include \"qt.h\"\n" 736 "#include <algorithm>\n" 737 "#include <qtwhatever.h>\n" 738 "#include <Qtwhatever.h>\n" 739 "#include <Algorithm>\n" 740 "#include \"vlib.h\"\n" 741 "#include \"Vlib.h\"\n" 742 "#include \"AST.h\"\n"; 743 744 EXPECT_EQ("#include \"AST.h\"\n" 745 "#include \"qt.h\"\n" 746 "#include \"Vlib.h\"\n" 747 "#include \"vlib.h\"\n" 748 "\n" 749 "#include <Qtwhatever.h>\n" 750 "#include <qtwhatever.h>\n" 751 "\n" 752 "#include <Algorithm>\n" 753 "#include <algorithm>\n", 754 sort(UnsortedCode)); 755 } 756 757 TEST_F(SortIncludesTest, SupportCaseInsensitiveMatching) { 758 // Setup an regex for main includes so we can cover those as well. 759 Style.IncludeIsMainRegex = "([-_](test|unittest))?$"; 760 761 // Ensure both main header detection and grouping work in a case insensitive 762 // manner. 763 EXPECT_EQ("#include \"llvm/A.h\"\n" 764 "#include \"b.h\"\n" 765 "#include \"c.h\"\n" 766 "#include \"LLVM/z.h\"\n" 767 "#include \"llvm/X.h\"\n" 768 "#include \"GTest/GTest.h\"\n" 769 "#include \"gmock/gmock.h\"\n", 770 sort("#include \"c.h\"\n" 771 "#include \"b.h\"\n" 772 "#include \"GTest/GTest.h\"\n" 773 "#include \"llvm/A.h\"\n" 774 "#include \"gmock/gmock.h\"\n" 775 "#include \"llvm/X.h\"\n" 776 "#include \"LLVM/z.h\"\n", 777 "a_TEST.cc")); 778 } 779 780 TEST_F(SortIncludesTest, SupportOptionalCaseSensitiveMachting) { 781 Style.IncludeBlocks = clang::tooling::IncludeStyle::IBS_Regroup; 782 Style.IncludeCategories = {{"^\"", 1, 0, false}, 783 {"^<.*\\.h>$", 2, 0, false}, 784 {"^<Q[A-Z][^\\.]*>", 3, 0, false}, 785 {"^<Qt[^\\.]*>", 4, 0, false}, 786 {"^<", 5, 0, false}}; 787 788 StringRef UnsortedCode = "#include <QWidget>\n" 789 "#include \"qt.h\"\n" 790 "#include <algorithm>\n" 791 "#include <windows.h>\n" 792 "#include <QLabel>\n" 793 "#include \"qa.h\"\n" 794 "#include <queue>\n" 795 "#include <qtwhatever.h>\n" 796 "#include <QtGlobal>\n"; 797 798 EXPECT_EQ("#include \"qa.h\"\n" 799 "#include \"qt.h\"\n" 800 "\n" 801 "#include <qtwhatever.h>\n" 802 "#include <windows.h>\n" 803 "\n" 804 "#include <QLabel>\n" 805 "#include <QWidget>\n" 806 "#include <QtGlobal>\n" 807 "#include <queue>\n" 808 "\n" 809 "#include <algorithm>\n", 810 sort(UnsortedCode)); 811 812 Style.IncludeCategories[2].RegexIsCaseSensitive = true; 813 Style.IncludeCategories[3].RegexIsCaseSensitive = true; 814 EXPECT_EQ("#include \"qa.h\"\n" 815 "#include \"qt.h\"\n" 816 "\n" 817 "#include <qtwhatever.h>\n" 818 "#include <windows.h>\n" 819 "\n" 820 "#include <QLabel>\n" 821 "#include <QWidget>\n" 822 "\n" 823 "#include <QtGlobal>\n" 824 "\n" 825 "#include <algorithm>\n" 826 "#include <queue>\n", 827 sort(UnsortedCode)); 828 } 829 830 TEST_F(SortIncludesTest, NegativePriorities) { 831 Style.IncludeCategories = {{".*important_os_header.*", -1, 0, false}, 832 {".*", 1, 0, false}}; 833 EXPECT_EQ("#include \"important_os_header.h\"\n" 834 "#include \"c_main.h\"\n" 835 "#include \"a_other.h\"\n", 836 sort("#include \"c_main.h\"\n" 837 "#include \"a_other.h\"\n" 838 "#include \"important_os_header.h\"\n", 839 "c_main.cc")); 840 841 // check stable when re-run 842 EXPECT_EQ("#include \"important_os_header.h\"\n" 843 "#include \"c_main.h\"\n" 844 "#include \"a_other.h\"\n", 845 sort("#include \"important_os_header.h\"\n" 846 "#include \"c_main.h\"\n" 847 "#include \"a_other.h\"\n", 848 "c_main.cc", 0)); 849 } 850 851 TEST_F(SortIncludesTest, PriorityGroupsAreSeparatedWhenRegroupping) { 852 Style.IncludeCategories = {{".*important_os_header.*", -1, 0, false}, 853 {".*", 1, 0, false}}; 854 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 855 856 EXPECT_EQ("#include \"important_os_header.h\"\n" 857 "\n" 858 "#include \"c_main.h\"\n" 859 "\n" 860 "#include \"a_other.h\"\n", 861 sort("#include \"c_main.h\"\n" 862 "#include \"a_other.h\"\n" 863 "#include \"important_os_header.h\"\n", 864 "c_main.cc")); 865 866 // check stable when re-run 867 EXPECT_EQ("#include \"important_os_header.h\"\n" 868 "\n" 869 "#include \"c_main.h\"\n" 870 "\n" 871 "#include \"a_other.h\"\n", 872 sort("#include \"important_os_header.h\"\n" 873 "\n" 874 "#include \"c_main.h\"\n" 875 "\n" 876 "#include \"a_other.h\"\n", 877 "c_main.cc", 0)); 878 } 879 880 TEST_F(SortIncludesTest, CalculatesCorrectCursorPosition) { 881 std::string Code = "#include <ccc>\n" // Start of line: 0 882 "#include <bbbbbb>\n" // Start of line: 15 883 "#include <a>\n"; // Start of line: 33 884 EXPECT_EQ(31u, newCursor(Code, 0)); 885 EXPECT_EQ(13u, newCursor(Code, 15)); 886 EXPECT_EQ(0u, newCursor(Code, 33)); 887 888 EXPECT_EQ(41u, newCursor(Code, 10)); 889 EXPECT_EQ(23u, newCursor(Code, 25)); 890 EXPECT_EQ(10u, newCursor(Code, 43)); 891 } 892 893 TEST_F(SortIncludesTest, DeduplicateIncludes) { 894 EXPECT_EQ("#include <a>\n" 895 "#include <b>\n" 896 "#include <c>\n", 897 sort("#include <a>\n" 898 "#include <b>\n" 899 "#include <b>\n" 900 "#include <b>\n" 901 "#include <b>\n" 902 "#include <c>\n")); 903 904 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge; 905 EXPECT_EQ("#include <a>\n" 906 "#include <b>\n" 907 "#include <c>\n", 908 sort("#include <a>\n" 909 "#include <b>\n" 910 "\n" 911 "#include <b>\n" 912 "\n" 913 "#include <b>\n" 914 "#include <c>\n")); 915 916 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 917 EXPECT_EQ("#include <a>\n" 918 "#include <b>\n" 919 "#include <c>\n", 920 sort("#include <a>\n" 921 "#include <b>\n" 922 "\n" 923 "#include <b>\n" 924 "\n" 925 "#include <b>\n" 926 "#include <c>\n")); 927 } 928 929 TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) { 930 EXPECT_EQ("#include <a>\n" 931 "#include <b>\n" 932 "#include <c>\n", 933 sort("#include <b>\n" 934 "#include <a>\n" 935 "#include <b>\n" 936 "#include <b>\n" 937 "#include <c>\n" 938 "#include <b>\n")); 939 940 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge; 941 EXPECT_EQ("#include <a>\n" 942 "#include <b>\n" 943 "#include <c>\n", 944 sort("#include <b>\n" 945 "#include <a>\n" 946 "\n" 947 "#include <b>\n" 948 "\n" 949 "#include <c>\n" 950 "#include <b>\n")); 951 952 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 953 EXPECT_EQ("#include <a>\n" 954 "#include <b>\n" 955 "#include <c>\n", 956 sort("#include <b>\n" 957 "#include <a>\n" 958 "\n" 959 "#include <b>\n" 960 "\n" 961 "#include <c>\n" 962 "#include <b>\n")); 963 } 964 965 TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionAfterDeduplicate) { 966 std::string Code = "#include <b>\n" // Start of line: 0 967 "#include <a>\n" // Start of line: 13 968 "#include <b>\n" // Start of line: 26 969 "#include <b>\n" // Start of line: 39 970 "#include <c>\n" // Start of line: 52 971 "#include <b>\n"; // Start of line: 65 972 std::string Expected = "#include <a>\n" // Start of line: 0 973 "#include <b>\n" // Start of line: 13 974 "#include <c>\n"; // Start of line: 26 975 EXPECT_EQ(Expected, sort(Code)); 976 // Cursor on 'i' in "#include <a>". 977 EXPECT_EQ(1u, newCursor(Code, 14)); 978 // Cursor on 'b' in "#include <b>". 979 EXPECT_EQ(23u, newCursor(Code, 10)); 980 EXPECT_EQ(23u, newCursor(Code, 36)); 981 EXPECT_EQ(23u, newCursor(Code, 49)); 982 EXPECT_EQ(23u, newCursor(Code, 36)); 983 EXPECT_EQ(23u, newCursor(Code, 75)); 984 // Cursor on '#' in "#include <c>". 985 EXPECT_EQ(26u, newCursor(Code, 52)); 986 } 987 988 TEST_F(SortIncludesTest, DeduplicateLocallyInEachBlock) { 989 EXPECT_EQ("#include <a>\n" 990 "#include <b>\n" 991 "\n" 992 "#include <b>\n" 993 "#include <c>\n", 994 sort("#include <a>\n" 995 "#include <b>\n" 996 "\n" 997 "#include <c>\n" 998 "#include <b>\n" 999 "#include <b>\n")); 1000 } 1001 1002 TEST_F(SortIncludesTest, ValidAffactedRangesAfterDeduplicatingIncludes) { 1003 std::string Code = "#include <a>\n" 1004 "#include <b>\n" 1005 "#include <a>\n" 1006 "#include <a>\n" 1007 "\n" 1008 " int x ;"; 1009 std::vector<tooling::Range> Ranges = {tooling::Range(0, 52)}; 1010 auto Replaces = sortIncludes(FmtStyle, Code, Ranges, "input.cpp"); 1011 Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); 1012 EXPECT_EQ(1u, Ranges.size()); 1013 EXPECT_EQ(0u, Ranges[0].getOffset()); 1014 EXPECT_EQ(26u, Ranges[0].getLength()); 1015 } 1016 1017 TEST_F(SortIncludesTest, DoNotSortLikelyXml) { 1018 EXPECT_EQ("<!--;\n" 1019 "#include <b>\n" 1020 "#include <a>\n" 1021 "-->", 1022 sort("<!--;\n" 1023 "#include <b>\n" 1024 "#include <a>\n" 1025 "-->", 1026 "input.h", 0)); 1027 } 1028 1029 TEST_F(SortIncludesTest, DoNotOutputReplacementsForSortedBlocksWithRegrouping) { 1030 Style.IncludeBlocks = Style.IBS_Regroup; 1031 std::string Code = R"( 1032 #include "b.h" 1033 1034 #include <a.h> 1035 )"; 1036 EXPECT_EQ(Code, sort(Code, "input.h", 0)); 1037 } 1038 1039 TEST_F(SortIncludesTest, 1040 DoNotOutputReplacementsForSortedBlocksWithRegroupingWindows) { 1041 Style.IncludeBlocks = Style.IBS_Regroup; 1042 std::string Code = "#include \"b.h\"\r\n" 1043 "\r\n" 1044 "#include <a.h>\r\n"; 1045 EXPECT_EQ(Code, sort(Code, "input.h", 0)); 1046 } 1047 1048 TEST_F(SortIncludesTest, DoNotRegroupGroupsInGoogleObjCStyle) { 1049 FmtStyle = getGoogleStyle(FormatStyle::LK_ObjC); 1050 1051 EXPECT_EQ("#include <a.h>\n" 1052 "#include <b.h>\n" 1053 "#include \"a.h\"", 1054 sort("#include <b.h>\n" 1055 "#include <a.h>\n" 1056 "#include \"a.h\"")); 1057 } 1058 1059 TEST_F(SortIncludesTest, DoNotTreatPrecompiledHeadersAsFirstBlock) { 1060 Style.IncludeBlocks = Style.IBS_Merge; 1061 std::string Code = "#include \"d.h\"\r\n" 1062 "#include \"b.h\"\r\n" 1063 "#pragma hdrstop\r\n" 1064 "\r\n" 1065 "#include \"c.h\"\r\n" 1066 "#include \"a.h\"\r\n" 1067 "#include \"e.h\"\r\n"; 1068 1069 std::string Expected = "#include \"b.h\"\r\n" 1070 "#include \"d.h\"\r\n" 1071 "#pragma hdrstop\r\n" 1072 "\r\n" 1073 "#include \"e.h\"\r\n" 1074 "#include \"a.h\"\r\n" 1075 "#include \"c.h\"\r\n"; 1076 1077 EXPECT_EQ(Expected, sort(Code, "e.cpp", 2)); 1078 1079 Code = "#include \"d.h\"\n" 1080 "#include \"b.h\"\n" 1081 "#pragma hdrstop( \"c:\\projects\\include\\myinc.pch\" )\n" 1082 "\n" 1083 "#include \"c.h\"\n" 1084 "#include \"a.h\"\n" 1085 "#include \"e.h\"\n"; 1086 1087 Expected = "#include \"b.h\"\n" 1088 "#include \"d.h\"\n" 1089 "#pragma hdrstop(\"c:\\projects\\include\\myinc.pch\")\n" 1090 "\n" 1091 "#include \"e.h\"\n" 1092 "#include \"a.h\"\n" 1093 "#include \"c.h\"\n"; 1094 1095 EXPECT_EQ(Expected, sort(Code, "e.cpp", 2)); 1096 } 1097 1098 TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkMerge) { 1099 Style.IncludeBlocks = Style.IBS_Merge; 1100 std::string Code = "\xEF\xBB\xBF#include \"d.h\"\r\n" 1101 "#include \"b.h\"\r\n" 1102 "\r\n" 1103 "#include \"c.h\"\r\n" 1104 "#include \"a.h\"\r\n" 1105 "#include \"e.h\"\r\n"; 1106 1107 std::string Expected = "\xEF\xBB\xBF#include \"e.h\"\r\n" 1108 "#include \"a.h\"\r\n" 1109 "#include \"b.h\"\r\n" 1110 "#include \"c.h\"\r\n" 1111 "#include \"d.h\"\r\n"; 1112 1113 EXPECT_EQ(Expected, sort(Code, "e.cpp", 1)); 1114 } 1115 1116 TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkPreserve) { 1117 Style.IncludeBlocks = Style.IBS_Preserve; 1118 std::string Code = "\xEF\xBB\xBF#include \"d.h\"\r\n" 1119 "#include \"b.h\"\r\n" 1120 "\r\n" 1121 "#include \"c.h\"\r\n" 1122 "#include \"a.h\"\r\n" 1123 "#include \"e.h\"\r\n"; 1124 1125 std::string Expected = "\xEF\xBB\xBF#include \"b.h\"\r\n" 1126 "#include \"d.h\"\r\n" 1127 "\r\n" 1128 "#include \"a.h\"\r\n" 1129 "#include \"c.h\"\r\n" 1130 "#include \"e.h\"\r\n"; 1131 1132 EXPECT_EQ(Expected, sort(Code, "e.cpp", 2)); 1133 } 1134 1135 TEST_F(SortIncludesTest, MergeLines) { 1136 Style.IncludeBlocks = Style.IBS_Merge; 1137 std::string Code = "#include \"c.h\"\r\n" 1138 "#include \"b\\\r\n" 1139 ".h\"\r\n" 1140 "#include \"a.h\"\r\n"; 1141 1142 std::string Expected = "#include \"a.h\"\r\n" 1143 "#include \"b\\\r\n" 1144 ".h\"\r\n" 1145 "#include \"c.h\"\r\n"; 1146 1147 EXPECT_EQ(Expected, sort(Code, "a.cpp", 1)); 1148 } 1149 1150 TEST_F(SortIncludesTest, DisableFormatDisablesIncludeSorting) { 1151 StringRef Sorted = "#include <a.h>\n" 1152 "#include <b.h>\n"; 1153 StringRef Unsorted = "#include <b.h>\n" 1154 "#include <a.h>\n"; 1155 EXPECT_EQ(Sorted, sort(Unsorted)); 1156 FmtStyle.DisableFormat = true; 1157 EXPECT_EQ(Unsorted, sort(Unsorted, "input.cpp", 0)); 1158 } 1159 1160 TEST_F(SortIncludesTest, DisableRawStringLiteralSorting) { 1161 1162 EXPECT_EQ("const char *t = R\"(\n" 1163 "#include <b.h>\n" 1164 "#include <a.h>\n" 1165 ")\";", 1166 sort("const char *t = R\"(\n" 1167 "#include <b.h>\n" 1168 "#include <a.h>\n" 1169 ")\";", 1170 "test.cxx", 0)); 1171 EXPECT_EQ("const char *t = R\"x(\n" 1172 "#include <b.h>\n" 1173 "#include <a.h>\n" 1174 ")x\";", 1175 sort("const char *t = R\"x(\n" 1176 "#include <b.h>\n" 1177 "#include <a.h>\n" 1178 ")x\";", 1179 "test.cxx", 0)); 1180 EXPECT_EQ("const char *t = R\"xyz(\n" 1181 "#include <b.h>\n" 1182 "#include <a.h>\n" 1183 ")xyz\";", 1184 sort("const char *t = R\"xyz(\n" 1185 "#include <b.h>\n" 1186 "#include <a.h>\n" 1187 ")xyz\";", 1188 "test.cxx", 0)); 1189 1190 EXPECT_EQ("#include <a.h>\n" 1191 "#include <b.h>\n" 1192 "const char *t = R\"(\n" 1193 "#include <b.h>\n" 1194 "#include <a.h>\n" 1195 ")\";\n" 1196 "#include <c.h>\n" 1197 "#include <d.h>\n" 1198 "const char *t = R\"x(\n" 1199 "#include <f.h>\n" 1200 "#include <e.h>\n" 1201 ")x\";\n" 1202 "#include <g.h>\n" 1203 "#include <h.h>\n" 1204 "const char *t = R\"xyz(\n" 1205 "#include <j.h>\n" 1206 "#include <i.h>\n" 1207 ")xyz\";\n" 1208 "#include <k.h>\n" 1209 "#include <l.h>", 1210 sort("#include <b.h>\n" 1211 "#include <a.h>\n" 1212 "const char *t = R\"(\n" 1213 "#include <b.h>\n" 1214 "#include <a.h>\n" 1215 ")\";\n" 1216 "#include <d.h>\n" 1217 "#include <c.h>\n" 1218 "const char *t = R\"x(\n" 1219 "#include <f.h>\n" 1220 "#include <e.h>\n" 1221 ")x\";\n" 1222 "#include <h.h>\n" 1223 "#include <g.h>\n" 1224 "const char *t = R\"xyz(\n" 1225 "#include <j.h>\n" 1226 "#include <i.h>\n" 1227 ")xyz\";\n" 1228 "#include <l.h>\n" 1229 "#include <k.h>", 1230 "test.cc", 4)); 1231 1232 EXPECT_EQ("const char *t = R\"AMZ029amz(\n" 1233 "#include <b.h>\n" 1234 "#include <a.h>\n" 1235 ")AMZ029amz\";", 1236 sort("const char *t = R\"AMZ029amz(\n" 1237 "#include <b.h>\n" 1238 "#include <a.h>\n" 1239 ")AMZ029amz\";", 1240 "test.cxx", 0)); 1241 1242 EXPECT_EQ("const char *t = R\"-AMZ029amz(\n" 1243 "#include <b.h>\n" 1244 "#include <a.h>\n" 1245 ")-AMZ029amz\";", 1246 sort("const char *t = R\"-AMZ029amz(\n" 1247 "#include <b.h>\n" 1248 "#include <a.h>\n" 1249 ")-AMZ029amz\";", 1250 "test.cxx", 0)); 1251 1252 EXPECT_EQ("const char *t = R\"AMZ029amz-(\n" 1253 "#include <b.h>\n" 1254 "#include <a.h>\n" 1255 ")AMZ029amz-\";", 1256 sort("const char *t = R\"AMZ029amz-(\n" 1257 "#include <b.h>\n" 1258 "#include <a.h>\n" 1259 ")AMZ029amz-\";", 1260 "test.cxx", 0)); 1261 1262 EXPECT_EQ("const char *t = R\"AM|029amz-(\n" 1263 "#include <b.h>\n" 1264 "#include <a.h>\n" 1265 ")AM|029amz-\";", 1266 sort("const char *t = R\"AM|029amz-(\n" 1267 "#include <b.h>\n" 1268 "#include <a.h>\n" 1269 ")AM|029amz-\";", 1270 "test.cxx", 0)); 1271 1272 EXPECT_EQ("const char *t = R\"AM[029amz-(\n" 1273 "#include <b.h>\n" 1274 "#include <a.h>\n" 1275 ")AM[029amz-\";", 1276 sort("const char *t = R\"AM[029amz-(\n" 1277 "#include <b.h>\n" 1278 "#include <a.h>\n" 1279 ")AM[029amz-\";", 1280 "test.cxx", 0)); 1281 1282 EXPECT_EQ("const char *t = R\"AM]029amz-(\n" 1283 "#include <b.h>\n" 1284 "#include <a.h>\n" 1285 ")AM]029amz-\";", 1286 sort("const char *t = R\"AM]029amz-(\n" 1287 "#include <b.h>\n" 1288 "#include <a.h>\n" 1289 ")AM]029amz-\";", 1290 "test.cxx", 0)); 1291 1292 #define X "AMZ029amz{}+!%*=_:;',.<>|/?#~-$" 1293 1294 EXPECT_EQ("const char *t = R\"" X "(\n" 1295 "#include <b.h>\n" 1296 "#include <a.h>\n" 1297 ")" X "\";", 1298 sort("const char *t = R\"" X "(\n" 1299 "#include <b.h>\n" 1300 "#include <a.h>\n" 1301 ")" X "\";", 1302 "test.cxx", 0)); 1303 1304 #undef X 1305 } 1306 1307 } // end namespace 1308 } // end namespace format 1309 } // end namespace clang 1310