xref: /llvm-project/clang/unittests/Format/SortIncludesTest.cpp (revision e8cc7490d23477233d21c72923a4f8ea43bfbbc0)
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, LeavesMainHeaderFirst) {
462   Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
463   EXPECT_EQ("#include \"llvm/a.h\"\n"
464             "#include \"b.h\"\n"
465             "#include \"c.h\"\n",
466             sort("#include \"llvm/a.h\"\n"
467                  "#include \"c.h\"\n"
468                  "#include \"b.h\"\n",
469                  "a.cc"));
470   EXPECT_EQ("#include \"llvm/a.h\"\n"
471             "#include \"b.h\"\n"
472             "#include \"c.h\"\n",
473             sort("#include \"llvm/a.h\"\n"
474                  "#include \"c.h\"\n"
475                  "#include \"b.h\"\n",
476                  "a_test.cc"));
477   EXPECT_EQ("#include \"llvm/input.h\"\n"
478             "#include \"b.h\"\n"
479             "#include \"c.h\"\n",
480             sort("#include \"llvm/input.h\"\n"
481                  "#include \"c.h\"\n"
482                  "#include \"b.h\"\n",
483                  "input.mm"));
484 
485   // Don't allow prefixes.
486   EXPECT_EQ("#include \"b.h\"\n"
487             "#include \"c.h\"\n"
488             "#include \"llvm/not_a.h\"\n",
489             sort("#include \"llvm/not_a.h\"\n"
490                  "#include \"c.h\"\n"
491                  "#include \"b.h\"\n",
492                  "a.cc"));
493 
494   // Don't do this for _main and other suffixes.
495   EXPECT_EQ("#include \"b.h\"\n"
496             "#include \"c.h\"\n"
497             "#include \"llvm/a.h\"\n",
498             sort("#include \"llvm/a.h\"\n"
499                  "#include \"c.h\"\n"
500                  "#include \"b.h\"\n",
501                  "a_main.cc"));
502 
503   // Don't do this in headers.
504   EXPECT_EQ("#include \"b.h\"\n"
505             "#include \"c.h\"\n"
506             "#include \"llvm/a.h\"\n",
507             sort("#include \"llvm/a.h\"\n"
508                  "#include \"c.h\"\n"
509                  "#include \"b.h\"\n",
510                  "a.h"));
511 
512   // Only do this in the first #include block.
513   EXPECT_EQ("#include <a>\n"
514             "\n"
515             "#include \"b.h\"\n"
516             "#include \"c.h\"\n"
517             "#include \"llvm/a.h\"\n",
518             sort("#include <a>\n"
519                  "\n"
520                  "#include \"llvm/a.h\"\n"
521                  "#include \"c.h\"\n"
522                  "#include \"b.h\"\n",
523                  "a.cc"));
524 
525   // Only recognize the first #include with a matching basename as main include.
526   EXPECT_EQ("#include \"a.h\"\n"
527             "#include \"b.h\"\n"
528             "#include \"c.h\"\n"
529             "#include \"llvm/a.h\"\n",
530             sort("#include \"b.h\"\n"
531                  "#include \"a.h\"\n"
532                  "#include \"c.h\"\n"
533                  "#include \"llvm/a.h\"\n",
534                  "a.cc"));
535 }
536 
537 TEST_F(SortIncludesTest, LeavesMainHeaderFirstInAdditionalExtensions) {
538   Style.IncludeIsMainRegex = "([-_](test|unittest))?|(Impl)?$";
539   EXPECT_EQ("#include \"b.h\"\n"
540             "#include \"c.h\"\n"
541             "#include \"llvm/a.h\"\n",
542             sort("#include \"llvm/a.h\"\n"
543                  "#include \"c.h\"\n"
544                  "#include \"b.h\"\n",
545                  "a_test.xxx"));
546   EXPECT_EQ("#include \"b.h\"\n"
547             "#include \"c.h\"\n"
548             "#include \"llvm/a.h\"\n",
549             sort("#include \"llvm/a.h\"\n"
550                  "#include \"c.h\"\n"
551                  "#include \"b.h\"\n",
552                  "aImpl.hpp"));
553 
554   // .cpp extension is considered "main" by default
555   EXPECT_EQ("#include \"llvm/a.h\"\n"
556             "#include \"b.h\"\n"
557             "#include \"c.h\"\n",
558             sort("#include \"llvm/a.h\"\n"
559                  "#include \"c.h\"\n"
560                  "#include \"b.h\"\n",
561                  "aImpl.cpp"));
562   EXPECT_EQ("#include \"llvm/a.h\"\n"
563             "#include \"b.h\"\n"
564             "#include \"c.h\"\n",
565             sort("#include \"llvm/a.h\"\n"
566                  "#include \"c.h\"\n"
567                  "#include \"b.h\"\n",
568                  "a_test.cpp"));
569 
570   // Allow additional filenames / extensions
571   Style.IncludeIsMainSourceRegex = "(Impl\\.hpp)|(\\.xxx)$";
572   EXPECT_EQ("#include \"llvm/a.h\"\n"
573             "#include \"b.h\"\n"
574             "#include \"c.h\"\n",
575             sort("#include \"llvm/a.h\"\n"
576                  "#include \"c.h\"\n"
577                  "#include \"b.h\"\n",
578                  "a_test.xxx"));
579   EXPECT_EQ("#include \"llvm/a.h\"\n"
580             "#include \"b.h\"\n"
581             "#include \"c.h\"\n",
582             sort("#include \"llvm/a.h\"\n"
583                  "#include \"c.h\"\n"
584                  "#include \"b.h\"\n",
585                  "aImpl.hpp"));
586 }
587 
588 TEST_F(SortIncludesTest, RecognizeMainHeaderInAllGroups) {
589   Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
590   Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
591 
592   EXPECT_EQ("#include \"c.h\"\n"
593             "#include \"a.h\"\n"
594             "#include \"b.h\"\n",
595             sort("#include \"b.h\"\n"
596                  "\n"
597                  "#include \"a.h\"\n"
598                  "#include \"c.h\"\n",
599                  "c.cc"));
600 }
601 
602 TEST_F(SortIncludesTest, MainHeaderIsSeparatedWhenRegroupping) {
603   Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
604   Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
605 
606   EXPECT_EQ("#include \"a.h\"\n"
607             "\n"
608             "#include \"b.h\"\n"
609             "#include \"c.h\"\n",
610             sort("#include \"b.h\"\n"
611                  "\n"
612                  "#include \"a.h\"\n"
613                  "#include \"c.h\"\n",
614                  "a.cc"));
615 }
616 
617 TEST_F(SortIncludesTest, SupportOptionalCaseSensitiveSorting) {
618   EXPECT_FALSE(FmtStyle.SortIncludes == FormatStyle::SI_CaseInsensitive);
619 
620   FmtStyle.SortIncludes = FormatStyle::SI_CaseInsensitive;
621 
622   EXPECT_EQ("#include \"A/B.h\"\n"
623             "#include \"A/b.h\"\n"
624             "#include \"a/b.h\"\n"
625             "#include \"B/A.h\"\n"
626             "#include \"B/a.h\"\n",
627             sort("#include \"B/a.h\"\n"
628                  "#include \"B/A.h\"\n"
629                  "#include \"A/B.h\"\n"
630                  "#include \"a/b.h\"\n"
631                  "#include \"A/b.h\"\n",
632                  "a.h"));
633 
634   Style.IncludeBlocks = clang::tooling::IncludeStyle::IBS_Regroup;
635   Style.IncludeCategories = {
636       {"^\"", 1, 0, false}, {"^<.*\\.h>$", 2, 0, false}, {"^<", 3, 0, false}};
637 
638   StringRef UnsortedCode = "#include \"qt.h\"\n"
639                            "#include <algorithm>\n"
640                            "#include <qtwhatever.h>\n"
641                            "#include <Qtwhatever.h>\n"
642                            "#include <Algorithm>\n"
643                            "#include \"vlib.h\"\n"
644                            "#include \"Vlib.h\"\n"
645                            "#include \"AST.h\"\n";
646 
647   EXPECT_EQ("#include \"AST.h\"\n"
648             "#include \"qt.h\"\n"
649             "#include \"Vlib.h\"\n"
650             "#include \"vlib.h\"\n"
651             "\n"
652             "#include <Qtwhatever.h>\n"
653             "#include <qtwhatever.h>\n"
654             "\n"
655             "#include <Algorithm>\n"
656             "#include <algorithm>\n",
657             sort(UnsortedCode));
658 }
659 
660 TEST_F(SortIncludesTest, SupportCaseInsensitiveMatching) {
661   // Setup an regex for main includes so we can cover those as well.
662   Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
663 
664   // Ensure both main header detection and grouping work in a case insensitive
665   // manner.
666   EXPECT_EQ("#include \"llvm/A.h\"\n"
667             "#include \"b.h\"\n"
668             "#include \"c.h\"\n"
669             "#include \"LLVM/z.h\"\n"
670             "#include \"llvm/X.h\"\n"
671             "#include \"GTest/GTest.h\"\n"
672             "#include \"gmock/gmock.h\"\n",
673             sort("#include \"c.h\"\n"
674                  "#include \"b.h\"\n"
675                  "#include \"GTest/GTest.h\"\n"
676                  "#include \"llvm/A.h\"\n"
677                  "#include \"gmock/gmock.h\"\n"
678                  "#include \"llvm/X.h\"\n"
679                  "#include \"LLVM/z.h\"\n",
680                  "a_TEST.cc"));
681 }
682 
683 TEST_F(SortIncludesTest, SupportOptionalCaseSensitiveMachting) {
684   Style.IncludeBlocks = clang::tooling::IncludeStyle::IBS_Regroup;
685   Style.IncludeCategories = {{"^\"", 1, 0, false},
686                              {"^<.*\\.h>$", 2, 0, false},
687                              {"^<Q[A-Z][^\\.]*>", 3, 0, false},
688                              {"^<Qt[^\\.]*>", 4, 0, false},
689                              {"^<", 5, 0, false}};
690 
691   StringRef UnsortedCode = "#include <QWidget>\n"
692                            "#include \"qt.h\"\n"
693                            "#include <algorithm>\n"
694                            "#include <windows.h>\n"
695                            "#include <QLabel>\n"
696                            "#include \"qa.h\"\n"
697                            "#include <queue>\n"
698                            "#include <qtwhatever.h>\n"
699                            "#include <QtGlobal>\n";
700 
701   EXPECT_EQ("#include \"qa.h\"\n"
702             "#include \"qt.h\"\n"
703             "\n"
704             "#include <qtwhatever.h>\n"
705             "#include <windows.h>\n"
706             "\n"
707             "#include <QLabel>\n"
708             "#include <QWidget>\n"
709             "#include <QtGlobal>\n"
710             "#include <queue>\n"
711             "\n"
712             "#include <algorithm>\n",
713             sort(UnsortedCode));
714 
715   Style.IncludeCategories[2].RegexIsCaseSensitive = true;
716   Style.IncludeCategories[3].RegexIsCaseSensitive = true;
717   EXPECT_EQ("#include \"qa.h\"\n"
718             "#include \"qt.h\"\n"
719             "\n"
720             "#include <qtwhatever.h>\n"
721             "#include <windows.h>\n"
722             "\n"
723             "#include <QLabel>\n"
724             "#include <QWidget>\n"
725             "\n"
726             "#include <QtGlobal>\n"
727             "\n"
728             "#include <algorithm>\n"
729             "#include <queue>\n",
730             sort(UnsortedCode));
731 }
732 
733 TEST_F(SortIncludesTest, NegativePriorities) {
734   Style.IncludeCategories = {{".*important_os_header.*", -1, 0, false},
735                              {".*", 1, 0, false}};
736   EXPECT_EQ("#include \"important_os_header.h\"\n"
737             "#include \"c_main.h\"\n"
738             "#include \"a_other.h\"\n",
739             sort("#include \"c_main.h\"\n"
740                  "#include \"a_other.h\"\n"
741                  "#include \"important_os_header.h\"\n",
742                  "c_main.cc"));
743 
744   // check stable when re-run
745   EXPECT_EQ("#include \"important_os_header.h\"\n"
746             "#include \"c_main.h\"\n"
747             "#include \"a_other.h\"\n",
748             sort("#include \"important_os_header.h\"\n"
749                  "#include \"c_main.h\"\n"
750                  "#include \"a_other.h\"\n",
751                  "c_main.cc", 0));
752 }
753 
754 TEST_F(SortIncludesTest, PriorityGroupsAreSeparatedWhenRegroupping) {
755   Style.IncludeCategories = {{".*important_os_header.*", -1, 0, false},
756                              {".*", 1, 0, false}};
757   Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
758 
759   EXPECT_EQ("#include \"important_os_header.h\"\n"
760             "\n"
761             "#include \"c_main.h\"\n"
762             "\n"
763             "#include \"a_other.h\"\n",
764             sort("#include \"c_main.h\"\n"
765                  "#include \"a_other.h\"\n"
766                  "#include \"important_os_header.h\"\n",
767                  "c_main.cc"));
768 
769   // check stable when re-run
770   EXPECT_EQ("#include \"important_os_header.h\"\n"
771             "\n"
772             "#include \"c_main.h\"\n"
773             "\n"
774             "#include \"a_other.h\"\n",
775             sort("#include \"important_os_header.h\"\n"
776                  "\n"
777                  "#include \"c_main.h\"\n"
778                  "\n"
779                  "#include \"a_other.h\"\n",
780                  "c_main.cc", 0));
781 }
782 
783 TEST_F(SortIncludesTest, CalculatesCorrectCursorPosition) {
784   std::string Code = "#include <ccc>\n"    // Start of line: 0
785                      "#include <bbbbbb>\n" // Start of line: 15
786                      "#include <a>\n";     // Start of line: 33
787   EXPECT_EQ(31u, newCursor(Code, 0));
788   EXPECT_EQ(13u, newCursor(Code, 15));
789   EXPECT_EQ(0u, newCursor(Code, 33));
790 
791   EXPECT_EQ(41u, newCursor(Code, 10));
792   EXPECT_EQ(23u, newCursor(Code, 25));
793   EXPECT_EQ(10u, newCursor(Code, 43));
794 }
795 
796 TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) {
797   Style.IncludeBlocks = Style.IBS_Regroup;
798   std::string Code = "#include \"b\"\n"      // Start of line: 0
799                      "\n"                    // Start of line: 13
800                      "#include \"aa\"\n"     // Start of line: 14
801                      "int i;";               // Start of line: 28
802   std::string Expected = "#include \"aa\"\n" // Start of line: 0
803                          "#include \"b\"\n"  // Start of line: 14
804                          "int i;";           // Start of line: 27
805   EXPECT_EQ(Expected, sort(Code));
806   EXPECT_EQ(12u, newCursor(Code, 26)); // Closing quote of "aa"
807   EXPECT_EQ(26u, newCursor(Code, 27)); // Newline after "aa"
808   EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
809 }
810 
811 TEST_F(SortIncludesTest, DeduplicateIncludes) {
812   EXPECT_EQ("#include <a>\n"
813             "#include <b>\n"
814             "#include <c>\n",
815             sort("#include <a>\n"
816                  "#include <b>\n"
817                  "#include <b>\n"
818                  "#include <b>\n"
819                  "#include <b>\n"
820                  "#include <c>\n"));
821 
822   Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
823   EXPECT_EQ("#include <a>\n"
824             "#include <b>\n"
825             "#include <c>\n",
826             sort("#include <a>\n"
827                  "#include <b>\n"
828                  "\n"
829                  "#include <b>\n"
830                  "\n"
831                  "#include <b>\n"
832                  "#include <c>\n"));
833 
834   Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
835   EXPECT_EQ("#include <a>\n"
836             "#include <b>\n"
837             "#include <c>\n",
838             sort("#include <a>\n"
839                  "#include <b>\n"
840                  "\n"
841                  "#include <b>\n"
842                  "\n"
843                  "#include <b>\n"
844                  "#include <c>\n"));
845 }
846 
847 TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
848   EXPECT_EQ("#include <a>\n"
849             "#include <b>\n"
850             "#include <c>\n",
851             sort("#include <b>\n"
852                  "#include <a>\n"
853                  "#include <b>\n"
854                  "#include <b>\n"
855                  "#include <c>\n"
856                  "#include <b>\n"));
857 
858   Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
859   EXPECT_EQ("#include <a>\n"
860             "#include <b>\n"
861             "#include <c>\n",
862             sort("#include <b>\n"
863                  "#include <a>\n"
864                  "\n"
865                  "#include <b>\n"
866                  "\n"
867                  "#include <c>\n"
868                  "#include <b>\n"));
869 
870   Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
871   EXPECT_EQ("#include <a>\n"
872             "#include <b>\n"
873             "#include <c>\n",
874             sort("#include <b>\n"
875                  "#include <a>\n"
876                  "\n"
877                  "#include <b>\n"
878                  "\n"
879                  "#include <c>\n"
880                  "#include <b>\n"));
881 }
882 
883 TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionAfterDeduplicate) {
884   std::string Code = "#include <b>\n"      // Start of line: 0
885                      "#include <a>\n"      // Start of line: 13
886                      "#include <b>\n"      // Start of line: 26
887                      "#include <b>\n"      // Start of line: 39
888                      "#include <c>\n"      // Start of line: 52
889                      "#include <b>\n";     // Start of line: 65
890   std::string Expected = "#include <a>\n"  // Start of line: 0
891                          "#include <b>\n"  // Start of line: 13
892                          "#include <c>\n"; // Start of line: 26
893   EXPECT_EQ(Expected, sort(Code));
894   // Cursor on 'i' in "#include <a>".
895   EXPECT_EQ(1u, newCursor(Code, 14));
896   // Cursor on 'b' in "#include <b>".
897   EXPECT_EQ(23u, newCursor(Code, 10));
898   EXPECT_EQ(23u, newCursor(Code, 36));
899   EXPECT_EQ(23u, newCursor(Code, 49));
900   EXPECT_EQ(23u, newCursor(Code, 36));
901   EXPECT_EQ(23u, newCursor(Code, 75));
902   // Cursor on '#' in "#include <c>".
903   EXPECT_EQ(26u, newCursor(Code, 52));
904 }
905 
906 TEST_F(SortIncludesTest, DeduplicateLocallyInEachBlock) {
907   EXPECT_EQ("#include <a>\n"
908             "#include <b>\n"
909             "\n"
910             "#include <b>\n"
911             "#include <c>\n",
912             sort("#include <a>\n"
913                  "#include <b>\n"
914                  "\n"
915                  "#include <c>\n"
916                  "#include <b>\n"
917                  "#include <b>\n"));
918 }
919 
920 TEST_F(SortIncludesTest, ValidAffactedRangesAfterDeduplicatingIncludes) {
921   std::string Code = "#include <a>\n"
922                      "#include <b>\n"
923                      "#include <a>\n"
924                      "#include <a>\n"
925                      "\n"
926                      "   int     x ;";
927   std::vector<tooling::Range> Ranges = {tooling::Range(0, 52)};
928   auto Replaces = sortIncludes(FmtStyle, Code, Ranges, "input.cpp");
929   Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
930   EXPECT_EQ(1u, Ranges.size());
931   EXPECT_EQ(0u, Ranges[0].getOffset());
932   EXPECT_EQ(26u, Ranges[0].getLength());
933 }
934 
935 TEST_F(SortIncludesTest, DoNotSortLikelyXml) {
936   EXPECT_EQ("<!--;\n"
937             "#include <b>\n"
938             "#include <a>\n"
939             "-->",
940             sort("<!--;\n"
941                  "#include <b>\n"
942                  "#include <a>\n"
943                  "-->",
944                  "input.h", 0));
945 }
946 
947 TEST_F(SortIncludesTest, DoNotOutputReplacementsForSortedBlocksWithRegrouping) {
948   Style.IncludeBlocks = Style.IBS_Regroup;
949   std::string Code = R"(
950 #include "b.h"
951 
952 #include <a.h>
953 )";
954   EXPECT_EQ(Code, sort(Code, "input.h", 0));
955 }
956 
957 TEST_F(SortIncludesTest,
958        DoNotOutputReplacementsForSortedBlocksWithRegroupingWindows) {
959   Style.IncludeBlocks = Style.IBS_Regroup;
960   std::string Code = "#include \"b.h\"\r\n"
961                      "\r\n"
962                      "#include <a.h>\r\n";
963   EXPECT_EQ(Code, sort(Code, "input.h", 0));
964 }
965 
966 TEST_F(SortIncludesTest, DoNotRegroupGroupsInGoogleObjCStyle) {
967   FmtStyle = getGoogleStyle(FormatStyle::LK_ObjC);
968 
969   EXPECT_EQ("#include <a.h>\n"
970             "#include <b.h>\n"
971             "#include \"a.h\"",
972             sort("#include <b.h>\n"
973                  "#include <a.h>\n"
974                  "#include \"a.h\""));
975 }
976 
977 TEST_F(SortIncludesTest, DoNotTreatPrecompiledHeadersAsFirstBlock) {
978   Style.IncludeBlocks = Style.IBS_Merge;
979   std::string Code = "#include \"d.h\"\r\n"
980                      "#include \"b.h\"\r\n"
981                      "#pragma hdrstop\r\n"
982                      "\r\n"
983                      "#include \"c.h\"\r\n"
984                      "#include \"a.h\"\r\n"
985                      "#include \"e.h\"\r\n";
986 
987   std::string Expected = "#include \"b.h\"\r\n"
988                          "#include \"d.h\"\r\n"
989                          "#pragma hdrstop\r\n"
990                          "\r\n"
991                          "#include \"e.h\"\r\n"
992                          "#include \"a.h\"\r\n"
993                          "#include \"c.h\"\r\n";
994 
995   EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
996 
997   Code = "#include \"d.h\"\n"
998          "#include \"b.h\"\n"
999          "#pragma hdrstop( \"c:\\projects\\include\\myinc.pch\" )\n"
1000          "\n"
1001          "#include \"c.h\"\n"
1002          "#include \"a.h\"\n"
1003          "#include \"e.h\"\n";
1004 
1005   Expected = "#include \"b.h\"\n"
1006              "#include \"d.h\"\n"
1007              "#pragma hdrstop(\"c:\\projects\\include\\myinc.pch\")\n"
1008              "\n"
1009              "#include \"e.h\"\n"
1010              "#include \"a.h\"\n"
1011              "#include \"c.h\"\n";
1012 
1013   EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
1014 }
1015 
1016 TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkMerge) {
1017   Style.IncludeBlocks = Style.IBS_Merge;
1018   std::string Code = "\xEF\xBB\xBF#include \"d.h\"\r\n"
1019                      "#include \"b.h\"\r\n"
1020                      "\r\n"
1021                      "#include \"c.h\"\r\n"
1022                      "#include \"a.h\"\r\n"
1023                      "#include \"e.h\"\r\n";
1024 
1025   std::string Expected = "\xEF\xBB\xBF#include \"e.h\"\r\n"
1026                          "#include \"a.h\"\r\n"
1027                          "#include \"b.h\"\r\n"
1028                          "#include \"c.h\"\r\n"
1029                          "#include \"d.h\"\r\n";
1030 
1031   EXPECT_EQ(Expected, sort(Code, "e.cpp", 1));
1032 }
1033 
1034 TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkPreserve) {
1035   Style.IncludeBlocks = Style.IBS_Preserve;
1036   std::string Code = "\xEF\xBB\xBF#include \"d.h\"\r\n"
1037                      "#include \"b.h\"\r\n"
1038                      "\r\n"
1039                      "#include \"c.h\"\r\n"
1040                      "#include \"a.h\"\r\n"
1041                      "#include \"e.h\"\r\n";
1042 
1043   std::string Expected = "\xEF\xBB\xBF#include \"b.h\"\r\n"
1044                          "#include \"d.h\"\r\n"
1045                          "\r\n"
1046                          "#include \"a.h\"\r\n"
1047                          "#include \"c.h\"\r\n"
1048                          "#include \"e.h\"\r\n";
1049 
1050   EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
1051 }
1052 
1053 TEST_F(SortIncludesTest, MergeLines) {
1054   Style.IncludeBlocks = Style.IBS_Merge;
1055   std::string Code = "#include \"c.h\"\r\n"
1056                      "#include \"b\\\r\n"
1057                      ".h\"\r\n"
1058                      "#include \"a.h\"\r\n";
1059 
1060   std::string Expected = "#include \"a.h\"\r\n"
1061                          "#include \"b\\\r\n"
1062                          ".h\"\r\n"
1063                          "#include \"c.h\"\r\n";
1064 
1065   EXPECT_EQ(Expected, sort(Code, "a.cpp", 1));
1066 }
1067 
1068 TEST_F(SortIncludesTest, DisableFormatDisablesIncludeSorting) {
1069   StringRef Sorted = "#include <a.h>\n"
1070                      "#include <b.h>\n";
1071   StringRef Unsorted = "#include <b.h>\n"
1072                        "#include <a.h>\n";
1073   EXPECT_EQ(Sorted, sort(Unsorted));
1074   FmtStyle.DisableFormat = true;
1075   EXPECT_EQ(Unsorted, sort(Unsorted, "input.cpp", 0));
1076 }
1077 
1078 TEST_F(SortIncludesTest, DisableRawStringLiteralSorting) {
1079 
1080   EXPECT_EQ("const char *t = R\"(\n"
1081             "#include <b.h>\n"
1082             "#include <a.h>\n"
1083             ")\";",
1084             sort("const char *t = R\"(\n"
1085                  "#include <b.h>\n"
1086                  "#include <a.h>\n"
1087                  ")\";",
1088                  "test.cxx", 0));
1089   EXPECT_EQ("const char *t = R\"x(\n"
1090             "#include <b.h>\n"
1091             "#include <a.h>\n"
1092             ")x\";",
1093             sort("const char *t = R\"x(\n"
1094                  "#include <b.h>\n"
1095                  "#include <a.h>\n"
1096                  ")x\";",
1097                  "test.cxx", 0));
1098   EXPECT_EQ("const char *t = R\"xyz(\n"
1099             "#include <b.h>\n"
1100             "#include <a.h>\n"
1101             ")xyz\";",
1102             sort("const char *t = R\"xyz(\n"
1103                  "#include <b.h>\n"
1104                  "#include <a.h>\n"
1105                  ")xyz\";",
1106                  "test.cxx", 0));
1107 
1108   EXPECT_EQ("#include <a.h>\n"
1109             "#include <b.h>\n"
1110             "const char *t = R\"(\n"
1111             "#include <b.h>\n"
1112             "#include <a.h>\n"
1113             ")\";\n"
1114             "#include <c.h>\n"
1115             "#include <d.h>\n"
1116             "const char *t = R\"x(\n"
1117             "#include <f.h>\n"
1118             "#include <e.h>\n"
1119             ")x\";\n"
1120             "#include <g.h>\n"
1121             "#include <h.h>\n"
1122             "const char *t = R\"xyz(\n"
1123             "#include <j.h>\n"
1124             "#include <i.h>\n"
1125             ")xyz\";\n"
1126             "#include <k.h>\n"
1127             "#include <l.h>",
1128             sort("#include <b.h>\n"
1129                  "#include <a.h>\n"
1130                  "const char *t = R\"(\n"
1131                  "#include <b.h>\n"
1132                  "#include <a.h>\n"
1133                  ")\";\n"
1134                  "#include <d.h>\n"
1135                  "#include <c.h>\n"
1136                  "const char *t = R\"x(\n"
1137                  "#include <f.h>\n"
1138                  "#include <e.h>\n"
1139                  ")x\";\n"
1140                  "#include <h.h>\n"
1141                  "#include <g.h>\n"
1142                  "const char *t = R\"xyz(\n"
1143                  "#include <j.h>\n"
1144                  "#include <i.h>\n"
1145                  ")xyz\";\n"
1146                  "#include <l.h>\n"
1147                  "#include <k.h>",
1148                  "test.cc", 4));
1149 
1150   EXPECT_EQ("const char *t = R\"AMZ029amz(\n"
1151             "#include <b.h>\n"
1152             "#include <a.h>\n"
1153             ")AMZ029amz\";",
1154             sort("const char *t = R\"AMZ029amz(\n"
1155                  "#include <b.h>\n"
1156                  "#include <a.h>\n"
1157                  ")AMZ029amz\";",
1158                  "test.cxx", 0));
1159 
1160   EXPECT_EQ("const char *t = R\"-AMZ029amz(\n"
1161             "#include <b.h>\n"
1162             "#include <a.h>\n"
1163             ")-AMZ029amz\";",
1164             sort("const char *t = R\"-AMZ029amz(\n"
1165                  "#include <b.h>\n"
1166                  "#include <a.h>\n"
1167                  ")-AMZ029amz\";",
1168                  "test.cxx", 0));
1169 
1170   EXPECT_EQ("const char *t = R\"AMZ029amz-(\n"
1171             "#include <b.h>\n"
1172             "#include <a.h>\n"
1173             ")AMZ029amz-\";",
1174             sort("const char *t = R\"AMZ029amz-(\n"
1175                  "#include <b.h>\n"
1176                  "#include <a.h>\n"
1177                  ")AMZ029amz-\";",
1178                  "test.cxx", 0));
1179 
1180   EXPECT_EQ("const char *t = R\"AM|029amz-(\n"
1181             "#include <b.h>\n"
1182             "#include <a.h>\n"
1183             ")AM|029amz-\";",
1184             sort("const char *t = R\"AM|029amz-(\n"
1185                  "#include <b.h>\n"
1186                  "#include <a.h>\n"
1187                  ")AM|029amz-\";",
1188                  "test.cxx", 0));
1189 
1190   EXPECT_EQ("const char *t = R\"AM[029amz-(\n"
1191             "#include <b.h>\n"
1192             "#include <a.h>\n"
1193             ")AM[029amz-\";",
1194             sort("const char *t = R\"AM[029amz-(\n"
1195                  "#include <b.h>\n"
1196                  "#include <a.h>\n"
1197                  ")AM[029amz-\";",
1198                  "test.cxx", 0));
1199 
1200   EXPECT_EQ("const char *t = R\"AM]029amz-(\n"
1201             "#include <b.h>\n"
1202             "#include <a.h>\n"
1203             ")AM]029amz-\";",
1204             sort("const char *t = R\"AM]029amz-(\n"
1205                  "#include <b.h>\n"
1206                  "#include <a.h>\n"
1207                  ")AM]029amz-\";",
1208                  "test.cxx", 0));
1209 
1210 #define X "AMZ029amz{}+!%*=_:;',.<>|/?#~-$"
1211 
1212   EXPECT_EQ("const char *t = R\"" X "(\n"
1213             "#include <b.h>\n"
1214             "#include <a.h>\n"
1215             ")" X "\";",
1216             sort("const char *t = R\"" X "(\n"
1217                  "#include <b.h>\n"
1218                  "#include <a.h>\n"
1219                  ")" X "\";",
1220                  "test.cxx", 0));
1221 
1222 #undef X
1223 }
1224 
1225 } // end namespace
1226 } // end namespace format
1227 } // end namespace clang
1228