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