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, CalculatesCorrectCursorPositionWithRegrouping) { 894 Style.IncludeBlocks = Style.IBS_Regroup; 895 std::string Code = "#include \"b\"\n" // Start of line: 0 896 "\n" // Start of line: 13 897 "#include \"aa\"\n" // Start of line: 14 898 "int i;"; // Start of line: 28 899 std::string Expected = "#include \"aa\"\n" // Start of line: 0 900 "#include \"b\"\n" // Start of line: 14 901 "int i;"; // Start of line: 27 902 EXPECT_EQ(Expected, sort(Code)); 903 EXPECT_EQ(12u, newCursor(Code, 26)); // Closing quote of "aa" 904 EXPECT_EQ(26u, newCursor(Code, 27)); // Newline after "aa" 905 EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line 906 } 907 908 TEST_F(SortIncludesTest, DeduplicateIncludes) { 909 EXPECT_EQ("#include <a>\n" 910 "#include <b>\n" 911 "#include <c>\n", 912 sort("#include <a>\n" 913 "#include <b>\n" 914 "#include <b>\n" 915 "#include <b>\n" 916 "#include <b>\n" 917 "#include <c>\n")); 918 919 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge; 920 EXPECT_EQ("#include <a>\n" 921 "#include <b>\n" 922 "#include <c>\n", 923 sort("#include <a>\n" 924 "#include <b>\n" 925 "\n" 926 "#include <b>\n" 927 "\n" 928 "#include <b>\n" 929 "#include <c>\n")); 930 931 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 932 EXPECT_EQ("#include <a>\n" 933 "#include <b>\n" 934 "#include <c>\n", 935 sort("#include <a>\n" 936 "#include <b>\n" 937 "\n" 938 "#include <b>\n" 939 "\n" 940 "#include <b>\n" 941 "#include <c>\n")); 942 } 943 944 TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) { 945 EXPECT_EQ("#include <a>\n" 946 "#include <b>\n" 947 "#include <c>\n", 948 sort("#include <b>\n" 949 "#include <a>\n" 950 "#include <b>\n" 951 "#include <b>\n" 952 "#include <c>\n" 953 "#include <b>\n")); 954 955 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge; 956 EXPECT_EQ("#include <a>\n" 957 "#include <b>\n" 958 "#include <c>\n", 959 sort("#include <b>\n" 960 "#include <a>\n" 961 "\n" 962 "#include <b>\n" 963 "\n" 964 "#include <c>\n" 965 "#include <b>\n")); 966 967 Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; 968 EXPECT_EQ("#include <a>\n" 969 "#include <b>\n" 970 "#include <c>\n", 971 sort("#include <b>\n" 972 "#include <a>\n" 973 "\n" 974 "#include <b>\n" 975 "\n" 976 "#include <c>\n" 977 "#include <b>\n")); 978 } 979 980 TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionAfterDeduplicate) { 981 std::string Code = "#include <b>\n" // Start of line: 0 982 "#include <a>\n" // Start of line: 13 983 "#include <b>\n" // Start of line: 26 984 "#include <b>\n" // Start of line: 39 985 "#include <c>\n" // Start of line: 52 986 "#include <b>\n"; // Start of line: 65 987 std::string Expected = "#include <a>\n" // Start of line: 0 988 "#include <b>\n" // Start of line: 13 989 "#include <c>\n"; // Start of line: 26 990 EXPECT_EQ(Expected, sort(Code)); 991 // Cursor on 'i' in "#include <a>". 992 EXPECT_EQ(1u, newCursor(Code, 14)); 993 // Cursor on 'b' in "#include <b>". 994 EXPECT_EQ(23u, newCursor(Code, 10)); 995 EXPECT_EQ(23u, newCursor(Code, 36)); 996 EXPECT_EQ(23u, newCursor(Code, 49)); 997 EXPECT_EQ(23u, newCursor(Code, 36)); 998 EXPECT_EQ(23u, newCursor(Code, 75)); 999 // Cursor on '#' in "#include <c>". 1000 EXPECT_EQ(26u, newCursor(Code, 52)); 1001 } 1002 1003 TEST_F(SortIncludesTest, DeduplicateLocallyInEachBlock) { 1004 EXPECT_EQ("#include <a>\n" 1005 "#include <b>\n" 1006 "\n" 1007 "#include <b>\n" 1008 "#include <c>\n", 1009 sort("#include <a>\n" 1010 "#include <b>\n" 1011 "\n" 1012 "#include <c>\n" 1013 "#include <b>\n" 1014 "#include <b>\n")); 1015 } 1016 1017 TEST_F(SortIncludesTest, ValidAffactedRangesAfterDeduplicatingIncludes) { 1018 std::string Code = "#include <a>\n" 1019 "#include <b>\n" 1020 "#include <a>\n" 1021 "#include <a>\n" 1022 "\n" 1023 " int x ;"; 1024 std::vector<tooling::Range> Ranges = {tooling::Range(0, 52)}; 1025 auto Replaces = sortIncludes(FmtStyle, Code, Ranges, "input.cpp"); 1026 Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); 1027 EXPECT_EQ(1u, Ranges.size()); 1028 EXPECT_EQ(0u, Ranges[0].getOffset()); 1029 EXPECT_EQ(26u, Ranges[0].getLength()); 1030 } 1031 1032 TEST_F(SortIncludesTest, DoNotSortLikelyXml) { 1033 EXPECT_EQ("<!--;\n" 1034 "#include <b>\n" 1035 "#include <a>\n" 1036 "-->", 1037 sort("<!--;\n" 1038 "#include <b>\n" 1039 "#include <a>\n" 1040 "-->", 1041 "input.h", 0)); 1042 } 1043 1044 TEST_F(SortIncludesTest, DoNotOutputReplacementsForSortedBlocksWithRegrouping) { 1045 Style.IncludeBlocks = Style.IBS_Regroup; 1046 std::string Code = R"( 1047 #include "b.h" 1048 1049 #include <a.h> 1050 )"; 1051 EXPECT_EQ(Code, sort(Code, "input.h", 0)); 1052 } 1053 1054 TEST_F(SortIncludesTest, 1055 DoNotOutputReplacementsForSortedBlocksWithRegroupingWindows) { 1056 Style.IncludeBlocks = Style.IBS_Regroup; 1057 std::string Code = "#include \"b.h\"\r\n" 1058 "\r\n" 1059 "#include <a.h>\r\n"; 1060 EXPECT_EQ(Code, sort(Code, "input.h", 0)); 1061 } 1062 1063 TEST_F(SortIncludesTest, DoNotRegroupGroupsInGoogleObjCStyle) { 1064 FmtStyle = getGoogleStyle(FormatStyle::LK_ObjC); 1065 1066 EXPECT_EQ("#include <a.h>\n" 1067 "#include <b.h>\n" 1068 "#include \"a.h\"", 1069 sort("#include <b.h>\n" 1070 "#include <a.h>\n" 1071 "#include \"a.h\"")); 1072 } 1073 1074 TEST_F(SortIncludesTest, DoNotTreatPrecompiledHeadersAsFirstBlock) { 1075 Style.IncludeBlocks = Style.IBS_Merge; 1076 std::string Code = "#include \"d.h\"\r\n" 1077 "#include \"b.h\"\r\n" 1078 "#pragma hdrstop\r\n" 1079 "\r\n" 1080 "#include \"c.h\"\r\n" 1081 "#include \"a.h\"\r\n" 1082 "#include \"e.h\"\r\n"; 1083 1084 std::string Expected = "#include \"b.h\"\r\n" 1085 "#include \"d.h\"\r\n" 1086 "#pragma hdrstop\r\n" 1087 "\r\n" 1088 "#include \"e.h\"\r\n" 1089 "#include \"a.h\"\r\n" 1090 "#include \"c.h\"\r\n"; 1091 1092 EXPECT_EQ(Expected, sort(Code, "e.cpp", 2)); 1093 1094 Code = "#include \"d.h\"\n" 1095 "#include \"b.h\"\n" 1096 "#pragma hdrstop( \"c:\\projects\\include\\myinc.pch\" )\n" 1097 "\n" 1098 "#include \"c.h\"\n" 1099 "#include \"a.h\"\n" 1100 "#include \"e.h\"\n"; 1101 1102 Expected = "#include \"b.h\"\n" 1103 "#include \"d.h\"\n" 1104 "#pragma hdrstop(\"c:\\projects\\include\\myinc.pch\")\n" 1105 "\n" 1106 "#include \"e.h\"\n" 1107 "#include \"a.h\"\n" 1108 "#include \"c.h\"\n"; 1109 1110 EXPECT_EQ(Expected, sort(Code, "e.cpp", 2)); 1111 } 1112 1113 TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkMerge) { 1114 Style.IncludeBlocks = Style.IBS_Merge; 1115 std::string Code = "\xEF\xBB\xBF#include \"d.h\"\r\n" 1116 "#include \"b.h\"\r\n" 1117 "\r\n" 1118 "#include \"c.h\"\r\n" 1119 "#include \"a.h\"\r\n" 1120 "#include \"e.h\"\r\n"; 1121 1122 std::string Expected = "\xEF\xBB\xBF#include \"e.h\"\r\n" 1123 "#include \"a.h\"\r\n" 1124 "#include \"b.h\"\r\n" 1125 "#include \"c.h\"\r\n" 1126 "#include \"d.h\"\r\n"; 1127 1128 EXPECT_EQ(Expected, sort(Code, "e.cpp", 1)); 1129 } 1130 1131 TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkPreserve) { 1132 Style.IncludeBlocks = Style.IBS_Preserve; 1133 std::string Code = "\xEF\xBB\xBF#include \"d.h\"\r\n" 1134 "#include \"b.h\"\r\n" 1135 "\r\n" 1136 "#include \"c.h\"\r\n" 1137 "#include \"a.h\"\r\n" 1138 "#include \"e.h\"\r\n"; 1139 1140 std::string Expected = "\xEF\xBB\xBF#include \"b.h\"\r\n" 1141 "#include \"d.h\"\r\n" 1142 "\r\n" 1143 "#include \"a.h\"\r\n" 1144 "#include \"c.h\"\r\n" 1145 "#include \"e.h\"\r\n"; 1146 1147 EXPECT_EQ(Expected, sort(Code, "e.cpp", 2)); 1148 } 1149 1150 TEST_F(SortIncludesTest, MergeLines) { 1151 Style.IncludeBlocks = Style.IBS_Merge; 1152 std::string Code = "#include \"c.h\"\r\n" 1153 "#include \"b\\\r\n" 1154 ".h\"\r\n" 1155 "#include \"a.h\"\r\n"; 1156 1157 std::string Expected = "#include \"a.h\"\r\n" 1158 "#include \"b\\\r\n" 1159 ".h\"\r\n" 1160 "#include \"c.h\"\r\n"; 1161 1162 EXPECT_EQ(Expected, sort(Code, "a.cpp", 1)); 1163 } 1164 1165 TEST_F(SortIncludesTest, DisableFormatDisablesIncludeSorting) { 1166 StringRef Sorted = "#include <a.h>\n" 1167 "#include <b.h>\n"; 1168 StringRef Unsorted = "#include <b.h>\n" 1169 "#include <a.h>\n"; 1170 EXPECT_EQ(Sorted, sort(Unsorted)); 1171 FmtStyle.DisableFormat = true; 1172 EXPECT_EQ(Unsorted, sort(Unsorted, "input.cpp", 0)); 1173 } 1174 1175 TEST_F(SortIncludesTest, DisableRawStringLiteralSorting) { 1176 1177 EXPECT_EQ("const char *t = R\"(\n" 1178 "#include <b.h>\n" 1179 "#include <a.h>\n" 1180 ")\";", 1181 sort("const char *t = R\"(\n" 1182 "#include <b.h>\n" 1183 "#include <a.h>\n" 1184 ")\";", 1185 "test.cxx", 0)); 1186 EXPECT_EQ("const char *t = R\"x(\n" 1187 "#include <b.h>\n" 1188 "#include <a.h>\n" 1189 ")x\";", 1190 sort("const char *t = R\"x(\n" 1191 "#include <b.h>\n" 1192 "#include <a.h>\n" 1193 ")x\";", 1194 "test.cxx", 0)); 1195 EXPECT_EQ("const char *t = R\"xyz(\n" 1196 "#include <b.h>\n" 1197 "#include <a.h>\n" 1198 ")xyz\";", 1199 sort("const char *t = R\"xyz(\n" 1200 "#include <b.h>\n" 1201 "#include <a.h>\n" 1202 ")xyz\";", 1203 "test.cxx", 0)); 1204 1205 EXPECT_EQ("#include <a.h>\n" 1206 "#include <b.h>\n" 1207 "const char *t = R\"(\n" 1208 "#include <b.h>\n" 1209 "#include <a.h>\n" 1210 ")\";\n" 1211 "#include <c.h>\n" 1212 "#include <d.h>\n" 1213 "const char *t = R\"x(\n" 1214 "#include <f.h>\n" 1215 "#include <e.h>\n" 1216 ")x\";\n" 1217 "#include <g.h>\n" 1218 "#include <h.h>\n" 1219 "const char *t = R\"xyz(\n" 1220 "#include <j.h>\n" 1221 "#include <i.h>\n" 1222 ")xyz\";\n" 1223 "#include <k.h>\n" 1224 "#include <l.h>", 1225 sort("#include <b.h>\n" 1226 "#include <a.h>\n" 1227 "const char *t = R\"(\n" 1228 "#include <b.h>\n" 1229 "#include <a.h>\n" 1230 ")\";\n" 1231 "#include <d.h>\n" 1232 "#include <c.h>\n" 1233 "const char *t = R\"x(\n" 1234 "#include <f.h>\n" 1235 "#include <e.h>\n" 1236 ")x\";\n" 1237 "#include <h.h>\n" 1238 "#include <g.h>\n" 1239 "const char *t = R\"xyz(\n" 1240 "#include <j.h>\n" 1241 "#include <i.h>\n" 1242 ")xyz\";\n" 1243 "#include <l.h>\n" 1244 "#include <k.h>", 1245 "test.cc", 4)); 1246 1247 EXPECT_EQ("const char *t = R\"AMZ029amz(\n" 1248 "#include <b.h>\n" 1249 "#include <a.h>\n" 1250 ")AMZ029amz\";", 1251 sort("const char *t = R\"AMZ029amz(\n" 1252 "#include <b.h>\n" 1253 "#include <a.h>\n" 1254 ")AMZ029amz\";", 1255 "test.cxx", 0)); 1256 1257 EXPECT_EQ("const char *t = R\"-AMZ029amz(\n" 1258 "#include <b.h>\n" 1259 "#include <a.h>\n" 1260 ")-AMZ029amz\";", 1261 sort("const char *t = R\"-AMZ029amz(\n" 1262 "#include <b.h>\n" 1263 "#include <a.h>\n" 1264 ")-AMZ029amz\";", 1265 "test.cxx", 0)); 1266 1267 EXPECT_EQ("const char *t = R\"AMZ029amz-(\n" 1268 "#include <b.h>\n" 1269 "#include <a.h>\n" 1270 ")AMZ029amz-\";", 1271 sort("const char *t = R\"AMZ029amz-(\n" 1272 "#include <b.h>\n" 1273 "#include <a.h>\n" 1274 ")AMZ029amz-\";", 1275 "test.cxx", 0)); 1276 1277 EXPECT_EQ("const char *t = R\"AM|029amz-(\n" 1278 "#include <b.h>\n" 1279 "#include <a.h>\n" 1280 ")AM|029amz-\";", 1281 sort("const char *t = R\"AM|029amz-(\n" 1282 "#include <b.h>\n" 1283 "#include <a.h>\n" 1284 ")AM|029amz-\";", 1285 "test.cxx", 0)); 1286 1287 EXPECT_EQ("const char *t = R\"AM[029amz-(\n" 1288 "#include <b.h>\n" 1289 "#include <a.h>\n" 1290 ")AM[029amz-\";", 1291 sort("const char *t = R\"AM[029amz-(\n" 1292 "#include <b.h>\n" 1293 "#include <a.h>\n" 1294 ")AM[029amz-\";", 1295 "test.cxx", 0)); 1296 1297 EXPECT_EQ("const char *t = R\"AM]029amz-(\n" 1298 "#include <b.h>\n" 1299 "#include <a.h>\n" 1300 ")AM]029amz-\";", 1301 sort("const char *t = R\"AM]029amz-(\n" 1302 "#include <b.h>\n" 1303 "#include <a.h>\n" 1304 ")AM]029amz-\";", 1305 "test.cxx", 0)); 1306 1307 #define X "AMZ029amz{}+!%*=_:;',.<>|/?#~-$" 1308 1309 EXPECT_EQ("const char *t = R\"" X "(\n" 1310 "#include <b.h>\n" 1311 "#include <a.h>\n" 1312 ")" X "\";", 1313 sort("const char *t = R\"" X "(\n" 1314 "#include <b.h>\n" 1315 "#include <a.h>\n" 1316 ")" X "\";", 1317 "test.cxx", 0)); 1318 1319 #undef X 1320 } 1321 1322 } // end namespace 1323 } // end namespace format 1324 } // end namespace clang 1325