xref: /llvm-project/clang/unittests/Format/FormatTestSelective.cpp (revision 1c58208d899285318c89e069268145c85ec33368)
1 //===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FormatTestUtils.h"
10 #include "clang/Format/Format.h"
11 #include "llvm/Support/Debug.h"
12 #include "gtest/gtest.h"
13 
14 #define DEBUG_TYPE "format-test"
15 
16 namespace clang {
17 namespace format {
18 namespace {
19 
20 class FormatTestSelective : public testing::Test {
21 protected:
22   std::string format(StringRef Code, unsigned Offset, unsigned Length) {
23     LLVM_DEBUG(llvm::errs() << "---\n");
24     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
25     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
26     FormattingAttemptStatus Status;
27     tooling::Replacements Replaces =
28         reformat(Style, Code, Ranges, "<stdin>", &Status);
29     EXPECT_TRUE(Status.FormatComplete) << Code << "\n\n";
30     auto Result = applyAllReplacements(Code, Replaces);
31     EXPECT_TRUE(static_cast<bool>(Result));
32     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
33     return *Result;
34   }
35 
36   FormatStyle Style = getLLVMStyle();
37 };
38 
39 TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
40   EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0));
41   EXPECT_EQ("int a;", format("int a;         ", 0, 0));
42   EXPECT_EQ("int a;\n", format("int a;  \n   \n   \n ", 0, 0));
43   EXPECT_EQ("int a;\nint b;    ", format("int a;  \nint b;    ", 0, 0));
44 }
45 
46 TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
47   EXPECT_EQ("{int b;\n"
48             "  int a;\n"
49             "}",
50             format("{int b;\n  int  a;}", 8, 0));
51   EXPECT_EQ("{\n"
52             "  int b;\n"
53             "  int  a;}",
54             format("{int b;\n  int  a;}", 7, 0));
55 
56   Style.ColumnLimit = 12;
57   EXPECT_EQ("#define A  \\\n"
58             "  int a;   \\\n"
59             "  int b;",
60             format("#define A  \\\n"
61                    "  int a;   \\\n"
62                    "    int b;",
63                    26, 0));
64   EXPECT_EQ("#define A  \\\n"
65             "  int a;   \\\n"
66             "  int b;",
67             format("#define A  \\\n"
68                    "  int a;   \\\n"
69                    "  int b;",
70                    25, 0));
71 }
72 
73 TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
74   EXPECT_EQ("int  b;\n\nint a;", format("int  b;\n\nint a;", 8, 0));
75   EXPECT_EQ("int b;\n\nint a;", format("int  b;\n\nint a;", 7, 0));
76 
77   // This might not strictly be correct, but is likely good in all practical
78   // cases.
79   EXPECT_EQ("int b;\nint a;", format("int  b;int a;", 7, 0));
80 }
81 
82 TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
83   EXPECT_EQ("int  a;\n\n int b;", format("int  a;\n  \n\n int b;", 8, 0));
84   EXPECT_EQ("int  a;\n\n int b;", format("int  a;\n  \n\n int b;", 9, 0));
85 }
86 
87 TEST_F(FormatTestSelective, ReformatsMovedLines) {
88   EXPECT_EQ(
89       "template <typename T> T *getFETokenInfo() const {\n"
90       "  return static_cast<T *>(FETokenInfo);\n"
91       "}\n"
92       "int  a; // <- Should not be formatted",
93       format(
94           "template<typename T>\n"
95           "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
96           "int  a; // <- Should not be formatted",
97           9, 5));
98 }
99 
100 TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
101   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
102   EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
103   EXPECT_EQ("if (a) return; // comment",
104             format("if(a)\nreturn; // comment", 20, 1));
105 }
106 
107 TEST_F(FormatTestSelective, FormatsCommentsLocally) {
108   EXPECT_EQ("int a;    // comment\n"
109             "int    b; // comment",
110             format("int   a; // comment\n"
111                    "int    b; // comment",
112                    0, 0));
113   EXPECT_EQ("int a; // comment\n"
114             "       // line 2\n"
115             "int b;",
116             format("int   a; // comment\n"
117                    "            // line 2\n"
118                    "int b;",
119                    28, 0));
120   EXPECT_EQ("int   a; // comment\n"
121             "// comment 2\n"
122             "int b;",
123             format("int   a; // comment\n"
124                    "// comment 2\n"
125                    "int b;",
126                    28, 0));
127   EXPECT_EQ("int aaaaaa; // comment\n"
128             "int b;\n"
129             "int c; // unrelated comment",
130             format("int aaaaaa; // comment\n"
131                    "int b;\n"
132                    "int   c; // unrelated comment",
133                    31, 0));
134 
135   EXPECT_EQ("int a; // This\n"
136             "       // is\n"
137             "       // a",
138             format("int a;      // This\n"
139                    "            // is\n"
140                    "            // a",
141                    0, 0));
142   EXPECT_EQ("int a; // This\n"
143             "       // is\n"
144             "       // a\n"
145             "// This is b\n"
146             "int b;",
147             format("int a; // This\n"
148                    "     // is\n"
149                    "     // a\n"
150                    "// This is b\n"
151                    "int b;",
152                    0, 0));
153   EXPECT_EQ("int a; // This\n"
154             "       // is\n"
155             "       // a\n"
156             "\n"
157             "//This is unrelated",
158             format("int a; // This\n"
159                    "     // is\n"
160                    "     // a\n"
161                    "\n"
162                    "//This is unrelated",
163                    0, 0));
164   EXPECT_EQ("int a;\n"
165             "// This is\n"
166             "// not formatted.   ",
167             format("int a;\n"
168                    "// This is\n"
169                    "// not formatted.   ",
170                    0, 0));
171   EXPECT_EQ("int x;  // Format this line.\n"
172             "int xx; //\n"
173             "int xxxxx; //",
174             format("int x; // Format this line.\n"
175                    "int xx; //\n"
176                    "int xxxxx; //",
177                    0, 0));
178 }
179 
180 TEST_F(FormatTestSelective, ContinueReindenting) {
181   // When we change an indent, we continue formatting as long as following
182   // lines are not indented correctly.
183   EXPECT_EQ("int   i;\n"
184             "int b;\n"
185             "int c;\n"
186             "int d;\n"
187             "int e;\n"
188             "  int f;",
189             format("int   i;\n"
190                    "  int b;\n"
191                    " int   c;\n"
192                    "  int d;\n"
193                    "int e;\n"
194                    "  int f;",
195                    11, 0));
196 }
197 
198 TEST_F(FormatTestSelective, ReindentClosingBrace) {
199   EXPECT_EQ("int   i;\n"
200             "int f() {\n"
201             "  int a;\n"
202             "  int b;\n"
203             "}\n"
204             " int c;",
205             format("int   i;\n"
206                    "  int f(){\n"
207                    "int a;\n"
208                    "int b;\n"
209                    "  }\n"
210                    " int c;",
211                    11, 0));
212   EXPECT_EQ("void f() {\n"
213             "  if (foo) {\n"
214             "    b();\n"
215             "  } else {\n"
216             "    c();\n"
217             "  }\n"
218             "int d;\n"
219             "}",
220             format("void f() {\n"
221                    "  if (foo) {\n"
222                    "b();\n"
223                    "}else{\n"
224                    "c();\n"
225                    "}\n"
226                    "int d;\n"
227                    "}",
228                    13, 0));
229   EXPECT_EQ("int i = []() {\n"
230             "  class C {\n"
231             "    int a;\n"
232             "    int b;\n"
233             "  };\n"
234             "  int c;\n"
235             "};",
236             format("int i = []() {\n"
237                    "  class C{\n"
238                    "int a;\n"
239                    "int b;\n"
240                    "};\n"
241                    "int c;\n"
242                    "  };",
243                    17, 0));
244 }
245 
246 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
247   EXPECT_EQ("DEBUG({\n"
248             "  int i;\n"
249             "  int        j;\n"
250             "});",
251             format("DEBUG(   {\n"
252                    "  int        i;\n"
253                    "  int        j;\n"
254                    "}   )  ;",
255                    20, 1));
256   EXPECT_EQ("DEBUG(   {\n"
257             "  int        i;\n"
258             "  int j;\n"
259             "}   )  ;",
260             format("DEBUG(   {\n"
261                    "  int        i;\n"
262                    "  int        j;\n"
263                    "}   )  ;",
264                    41, 1));
265   EXPECT_EQ("DEBUG(   {\n"
266             "    int        i;\n"
267             "    int j;\n"
268             "}   )  ;",
269             format("DEBUG(   {\n"
270                    "    int        i;\n"
271                    "    int        j;\n"
272                    "}   )  ;",
273                    41, 1));
274   EXPECT_EQ("DEBUG({\n"
275             "  int i;\n"
276             "  int j;\n"
277             "});",
278             format("DEBUG(   {\n"
279                    "    int        i;\n"
280                    "    int        j;\n"
281                    "}   )  ;",
282                    20, 1));
283 
284   EXPECT_EQ("Debug({\n"
285             "        if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
286             "          return;\n"
287             "      },\n"
288             "      a);",
289             format("Debug({\n"
290                    "        if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
291                    "             return;\n"
292                    "      },\n"
293                    "      a);",
294                    50, 1));
295   EXPECT_EQ("DEBUG({\n"
296             "  DEBUG({\n"
297             "    int a;\n"
298             "    int b;\n"
299             "  }) ;\n"
300             "});",
301             format("DEBUG({\n"
302                    "  DEBUG({\n"
303                    "    int a;\n"
304                    "    int    b;\n" // Format this line only.
305                    "  }) ;\n"        // Don't touch this line.
306                    "});",
307                    35, 0));
308   EXPECT_EQ("DEBUG({\n"
309             "  int a; //\n"
310             "});",
311             format("DEBUG({\n"
312                    "    int a; //\n"
313                    "});",
314                    0, 0));
315   EXPECT_EQ("someFunction(\n"
316             "    [] {\n"
317             "      // Only with this comment.\n"
318             "      int i; // invoke formatting here.\n"
319             "    }, // force line break\n"
320             "    aaa);",
321             format("someFunction(\n"
322                    "    [] {\n"
323                    "      // Only with this comment.\n"
324                    "      int   i; // invoke formatting here.\n"
325                    "    }, // force line break\n"
326                    "    aaa);",
327                    63, 1));
328 
329   EXPECT_EQ("int longlongname; // comment\n"
330             "int x = f({\n"
331             "  int x; // comment\n"
332             "  int y; // comment\n"
333             "});",
334             format("int longlongname; // comment\n"
335                    "int x = f({\n"
336                    "  int x; // comment\n"
337                    "  int y; // comment\n"
338                    "});",
339                    65, 0));
340   EXPECT_EQ("int s = f({\n"
341             "  class X {\n"
342             "  public:\n"
343             "    void f();\n"
344             "  };\n"
345             "});",
346             format("int s = f({\n"
347                    "  class X {\n"
348                    "    public:\n"
349                    "    void f();\n"
350                    "  };\n"
351                    "});",
352                    0, 0));
353   EXPECT_EQ("SomeFunction(\n"
354             "    [] {\n"
355             "      int i;\n"
356             "      return i;\n" // Format this line.
357             "    },\n"
358             "    [] {\n"
359             "       return 2;\n" // Don't fix this.
360             "    });",
361             format("SomeFunction(\n"
362                    "    [] {\n"
363                    "      int i;\n"
364                    "       return i;\n" // Format this line.
365                    "    },\n"
366                    "    [] {\n"
367                    "       return 2;\n" // Don't fix this.
368                    "    });",
369                    40, 0));
370 }
371 
372 TEST_F(FormatTestSelective, WrongIndent) {
373   EXPECT_EQ("namespace {\n"
374             "int i;\n"
375             "int j;\n"
376             "}",
377             format("namespace {\n"
378                    "  int i;\n" // Format here.
379                    "  int j;\n"
380                    "}",
381                    15, 0));
382   EXPECT_EQ("namespace {\n"
383             "  int i;\n"
384             "  int j;\n"
385             "}",
386             format("namespace {\n"
387                    "  int i;\n"
388                    "  int j;\n" // Format here.
389                    "}",
390                    24, 0));
391 }
392 
393 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
394   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
395   EXPECT_EQ("int  i;\n"
396             "#define A \\\n"
397             "  int i;  \\\n"
398             "  int j\n"
399             "int  k;",
400             format("int  i;\n"
401                    "#define A  \\\n"
402                    " int   i    ;  \\\n"
403                    " int   j\n"
404                    "int  k;",
405                    8, 0)); // 8: position of "#define".
406   EXPECT_EQ("int  i;\n"
407             "#define A \\\n"
408             "  int i;  \\\n"
409             "  int j\n"
410             "int  k;",
411             format("int  i;\n"
412                    "#define A  \\\n"
413                    " int   i    ;  \\\n"
414                    " int   j\n"
415                    "int  k;",
416                    45, 0)); // 45: position of "j".
417 }
418 
419 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
420   EXPECT_EQ("{\n"
421             "{\n"
422             "a;\n"
423             "b;\n"
424             "}\n"
425             "}",
426             format("{\n"
427                    "{\n"
428                    "a;\n"
429                    "     b;\n"
430                    "}\n"
431                    "}",
432                    13, 2));
433   EXPECT_EQ("{\n"
434             "{\n"
435             "  a;\n"
436             "  b;\n"
437             "  c;\n"
438             " d;\n"
439             "}\n"
440             "}",
441             format("{\n"
442                    "{\n"
443                    "     a;\n"
444                    "   b;\n"
445                    "  c;\n"
446                    " d;\n"
447                    "}\n"
448                    "}",
449                    9, 2));
450   EXPECT_EQ("{\n"
451             "{\n"
452             "public:\n"
453             "  b;\n"
454             "}\n"
455             "}",
456             format("{\n"
457                    "{\n"
458                    "public:\n"
459                    "     b;\n"
460                    "}\n"
461                    "}",
462                    17, 2));
463   EXPECT_EQ("{\n"
464             "{\n"
465             "a;\n"
466             "}\n"
467             "{\n"
468             "  b; //\n"
469             "}\n"
470             "}",
471             format("{\n"
472                    "{\n"
473                    "a;\n"
474                    "}\n"
475                    "{\n"
476                    "           b; //\n"
477                    "}\n"
478                    "}",
479                    22, 2));
480   EXPECT_EQ("  {\n"
481             "    a; //\n"
482             "  }",
483             format("  {\n"
484                    "a; //\n"
485                    "  }",
486                    4, 2));
487   EXPECT_EQ("void f() {}\n"
488             "void g() {}",
489             format("void f() {}\n"
490                    "void g() {}",
491                    13, 0));
492   EXPECT_EQ("int a; // comment\n"
493             "       // line 2\n"
494             "int b;",
495             format("int a; // comment\n"
496                    "       // line 2\n"
497                    "  int b;",
498                    35, 0));
499 
500   EXPECT_EQ(" void f() {\n"
501             "#define A 1\n"
502             " }",
503             format(" void f() {\n"
504                    "     #define A 1\n" // Format this line.
505                    " }",
506                    20, 0));
507   EXPECT_EQ(" void f() {\n"
508             "    int i;\n"
509             "#define A \\\n"
510             "    int i;  \\\n"
511             "   int j;\n"
512             "    int k;\n"
513             " }",
514             format(" void f() {\n"
515                    "    int i;\n"
516                    "#define A \\\n"
517                    "    int i;  \\\n"
518                    "   int j;\n"
519                    "      int k;\n" // Format this line.
520                    " }",
521                    67, 0));
522 
523   Style.ColumnLimit = 11;
524   EXPECT_EQ("  int a;\n"
525             "  void\n"
526             "  ffffff() {\n"
527             "  }",
528             format("  int a;\n"
529                    "void ffffff() {}",
530                    11, 0));
531 
532   // https://github.com/llvm/llvm-project/issues/59178
533   Style = getMozillaStyle();
534   EXPECT_EQ("int a()\n"
535             "{\n"
536             "return 0;\n"
537             "}\n"
538             "int b()\n"
539             "{\n"
540             "  return 42;\n"
541             "}",
542             format("int a()\n"
543                    "{\n"
544                    "return 0;\n"
545                    "}\n"
546                    "int b()\n"
547                    "{\n"
548                    "return 42;\n" // Format this line only
549                    "}",
550                    32, 0));
551 }
552 
553 TEST_F(FormatTestSelective, UnderstandsTabs) {
554   Style.IndentWidth = 8;
555   Style.UseTab = FormatStyle::UT_Always;
556   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
557   EXPECT_EQ("void f() {\n"
558             "\tf();\n"
559             "\tg();\n"
560             "}",
561             format("void f() {\n"
562                    "\tf();\n"
563                    "\tg();\n"
564                    "}",
565                    0, 0));
566   EXPECT_EQ("void f() {\n"
567             "\tf();\n"
568             "\tg();\n"
569             "}",
570             format("void f() {\n"
571                    "\tf();\n"
572                    "\tg();\n"
573                    "}",
574                    16, 0));
575   EXPECT_EQ("void f() {\n"
576             "  \tf();\n"
577             "\tg();\n"
578             "}",
579             format("void f() {\n"
580                    "  \tf();\n"
581                    "  \tg();\n"
582                    "}",
583                    21, 0));
584 }
585 
586 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
587   EXPECT_EQ(
588       "void f() {\n"
589       "  if (a) {\n"
590       "    g();\n"
591       "    h();\n"
592       "  }\n"
593       "\n"
594       "void g() {\n"
595       "}",
596       format("void f() {\n"
597              "  if (a) {\n" // Assume this was added without the closing brace.
598              "  g();\n"
599              "  h();\n"
600              "}\n"
601              "\n"
602              "void g() {\n" // Make sure not to format this.
603              "}",
604              15, 0));
605 }
606 
607 TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
608   Style = getGoogleStyle(FormatStyle::LK_JavaScript);
609   EXPECT_EQ("var x = \"a\";\n"
610             "var x = 'a';\n"
611             "var x = \"a\";",
612             format("var x = \"a\";\n"
613                    "var x = \"a\";\n"
614                    "var x = \"a\";",
615                    20, 0));
616 }
617 
618 TEST_F(FormatTestSelective, KeepsIndentAfterCommentSectionImport) {
619   std::string Code = "#include <a> // line 1\n" // 23 chars long
620                      "             // line 2\n" // 23 chars long
621                      "\n"                       // this newline is char 47
622                      "int i;";                  // this line is not indented
623   EXPECT_EQ(Code, format(Code, 47, 1));
624 }
625 
626 TEST_F(FormatTestSelective, DontAssert) {
627   // https://llvm.org/PR53880
628   std::string Code = "void f() {\n"
629                      "  return a == 8 ? 32 : 16;\n"
630                      "}\n";
631   EXPECT_EQ(Code, format(Code, 40, 0));
632 
633   // https://llvm.org/PR56352
634   Style.CompactNamespaces = true;
635   Style.NamespaceIndentation = FormatStyle::NI_All;
636   Code = "\n"
637          "namespace ns1 { namespace ns2 {\n"
638          "}}";
639   EXPECT_EQ(Code, format(Code, 0, 0));
640 
641   // https://reviews.llvm.org/D151047#4369742
642   Style = getLLVMStyle();
643   Style.FixNamespaceComments = false;
644   Code = "namespace ns {\n"
645          "#define REF(alias) alias alias_var;\n"
646          "}"; // Format this line only
647   EXPECT_EQ(Code, format(Code, 51, 0));
648 }
649 
650 TEST_F(FormatTestSelective, FormatMacroRegardlessOfPreviousIndent) {
651   // clang-format currently does not (or should not) take into account the
652   // indent of previous unformatted lines when formatting a PP directive.
653   // Technically speaking, LevelIndentTracker::IndentForLevel is only for non-PP
654   // lines. So these tests here check that the indent of previous non-PP lines
655   // do not affect the formatting. If this requirement changes, the tests here
656   // need to be adapted.
657   Style = getLLVMStyle();
658 
659   const StringRef Code{"      class Foo {\n"
660                        "            void test() {\n"
661                        "    #ifdef 1\n"
662                        "                #define some\n" // format this line
663                        "         #endif\n"
664                        "    }};"};
665 
666   EXPECT_EQ(Style.IndentPPDirectives,
667             FormatStyle::PPDirectiveIndentStyle::PPDIS_None);
668   EXPECT_EQ("      class Foo {\n"
669             "            void test() {\n"
670             "    #ifdef 1\n"
671             "#define some\n" // Formatted line
672             "#endif\n"       // That this line is also formatted might be a bug.
673             "            }};", // Ditto: Bug?
674             format(Code, 57, 0));
675 
676   Style.IndentPPDirectives =
677       FormatStyle::PPDirectiveIndentStyle::PPDIS_BeforeHash;
678   EXPECT_EQ("      class Foo {\n"
679             "            void test() {\n"
680             "    #ifdef 1\n"
681             "  #define some\n" // Formatted line
682             "         #endif\n"
683             "    }};",
684             format(Code, 57, 0));
685 
686   Style.IndentPPDirectives =
687       FormatStyle::PPDirectiveIndentStyle::PPDIS_AfterHash;
688   EXPECT_EQ("      class Foo {\n"
689             "            void test() {\n"
690             "    #ifdef 1\n"
691             "#  define some\n" // Formatted line
692             "#endif\n" // That this line is also formatted might be a bug.
693             "    }};",
694             format(Code, 57, 0));
695 }
696 
697 } // end namespace
698 } // end namespace format
699 } // end namespace clang
700