xref: /llvm-project/clang/unittests/Format/FormatTestSelective.cpp (revision 2ea34cdf2ba86cd129633f2a01fb695c79c0fe11)
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   EXPECT_EQ("namespace {\n"
392             "class C {\n"
393             "  int i;\n"
394             "};\n"
395             "} // namespace",
396             format("namespace {\n" // Format here.
397                    "  class C {\n"
398                    "    int i;\n"
399                    "  };\n"
400                    "}",
401                    1, 0));
402 }
403 
404 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
405   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
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                    8, 0)); // 8: position of "#define".
417   EXPECT_EQ("int  i;\n"
418             "#define A \\\n"
419             "  int i;  \\\n"
420             "  int j\n"
421             "int  k;",
422             format("int  i;\n"
423                    "#define A  \\\n"
424                    " int   i    ;  \\\n"
425                    " int   j\n"
426                    "int  k;",
427                    45, 0)); // 45: position of "j".
428 }
429 
430 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
431   EXPECT_EQ("{\n"
432             "{\n"
433             "a;\n"
434             "b;\n"
435             "}\n"
436             "}",
437             format("{\n"
438                    "{\n"
439                    "a;\n"
440                    "     b;\n"
441                    "}\n"
442                    "}",
443                    13, 2));
444   EXPECT_EQ("{\n"
445             "{\n"
446             "  a;\n"
447             "  b;\n"
448             "  c;\n"
449             " d;\n"
450             "}\n"
451             "}",
452             format("{\n"
453                    "{\n"
454                    "     a;\n"
455                    "   b;\n"
456                    "  c;\n"
457                    " d;\n"
458                    "}\n"
459                    "}",
460                    9, 2));
461   EXPECT_EQ("{\n"
462             "{\n"
463             "public:\n"
464             "  b;\n"
465             "}\n"
466             "}",
467             format("{\n"
468                    "{\n"
469                    "public:\n"
470                    "     b;\n"
471                    "}\n"
472                    "}",
473                    17, 2));
474   EXPECT_EQ("{\n"
475             "{\n"
476             "a;\n"
477             "}\n"
478             "{\n"
479             "  b; //\n"
480             "}\n"
481             "}",
482             format("{\n"
483                    "{\n"
484                    "a;\n"
485                    "}\n"
486                    "{\n"
487                    "           b; //\n"
488                    "}\n"
489                    "}",
490                    22, 2));
491   EXPECT_EQ("  {\n"
492             "    a; //\n"
493             "  }",
494             format("  {\n"
495                    "a; //\n"
496                    "  }",
497                    4, 2));
498   EXPECT_EQ("void f() {}\n"
499             "void g() {}",
500             format("void f() {}\n"
501                    "void g() {}",
502                    13, 0));
503   EXPECT_EQ("int a; // comment\n"
504             "       // line 2\n"
505             "int b;",
506             format("int a; // comment\n"
507                    "       // line 2\n"
508                    "  int b;",
509                    35, 0));
510 
511   EXPECT_EQ(" void f() {\n"
512             "#define A 1\n"
513             " }",
514             format(" void f() {\n"
515                    "     #define A 1\n" // Format this line.
516                    " }",
517                    20, 0));
518   EXPECT_EQ(" void f() {\n"
519             "    int i;\n"
520             "#define A \\\n"
521             "    int i;  \\\n"
522             "   int j;\n"
523             "    int k;\n"
524             " }",
525             format(" void f() {\n"
526                    "    int i;\n"
527                    "#define A \\\n"
528                    "    int i;  \\\n"
529                    "   int j;\n"
530                    "      int k;\n" // Format this line.
531                    " }",
532                    67, 0));
533 
534   Style.ColumnLimit = 11;
535   EXPECT_EQ("  int a;\n"
536             "  void\n"
537             "  ffffff() {\n"
538             "  }",
539             format("  int a;\n"
540                    "void ffffff() {}",
541                    11, 0));
542 
543   // https://github.com/llvm/llvm-project/issues/59178
544   Style = getMozillaStyle();
545   EXPECT_EQ("int a()\n"
546             "{\n"
547             "return 0;\n"
548             "}\n"
549             "int b()\n"
550             "{\n"
551             "  return 42;\n"
552             "}",
553             format("int a()\n"
554                    "{\n"
555                    "return 0;\n"
556                    "}\n"
557                    "int b()\n"
558                    "{\n"
559                    "return 42;\n" // Format this line only
560                    "}",
561                    32, 0));
562 }
563 
564 TEST_F(FormatTestSelective, UnderstandsTabs) {
565   Style.IndentWidth = 8;
566   Style.UseTab = FormatStyle::UT_Always;
567   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
568   EXPECT_EQ("void f() {\n"
569             "\tf();\n"
570             "\tg();\n"
571             "}",
572             format("void f() {\n"
573                    "\tf();\n"
574                    "\tg();\n"
575                    "}",
576                    0, 0));
577   EXPECT_EQ("void f() {\n"
578             "\tf();\n"
579             "\tg();\n"
580             "}",
581             format("void f() {\n"
582                    "\tf();\n"
583                    "\tg();\n"
584                    "}",
585                    16, 0));
586   EXPECT_EQ("void f() {\n"
587             "  \tf();\n"
588             "\tg();\n"
589             "}",
590             format("void f() {\n"
591                    "  \tf();\n"
592                    "  \tg();\n"
593                    "}",
594                    21, 0));
595 }
596 
597 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
598   EXPECT_EQ(
599       "void f() {\n"
600       "  if (a) {\n"
601       "    g();\n"
602       "    h();\n"
603       "  }\n"
604       "\n"
605       "void g() {\n"
606       "}",
607       format("void f() {\n"
608              "  if (a) {\n" // Assume this was added without the closing brace.
609              "  g();\n"
610              "  h();\n"
611              "}\n"
612              "\n"
613              "void g() {\n" // Make sure not to format this.
614              "}",
615              15, 0));
616 }
617 
618 TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
619   Style = getGoogleStyle(FormatStyle::LK_JavaScript);
620   EXPECT_EQ("var x = \"a\";\n"
621             "var x = 'a';\n"
622             "var x = \"a\";",
623             format("var x = \"a\";\n"
624                    "var x = \"a\";\n"
625                    "var x = \"a\";",
626                    20, 0));
627 }
628 
629 TEST_F(FormatTestSelective, KeepsIndentAfterCommentSectionImport) {
630   std::string Code = "#include <a> // line 1\n" // 23 chars long
631                      "             // line 2\n" // 23 chars long
632                      "\n"                       // this newline is char 47
633                      "int i;";                  // this line is not indented
634   EXPECT_EQ(Code, format(Code, 47, 1));
635 }
636 
637 TEST_F(FormatTestSelective, DontAssert) {
638   // https://llvm.org/PR53880
639   std::string Code = "void f() {\n"
640                      "  return a == 8 ? 32 : 16;\n"
641                      "}\n";
642   EXPECT_EQ(Code, format(Code, 40, 0));
643 
644   // https://llvm.org/PR56352
645   Style.CompactNamespaces = true;
646   Style.NamespaceIndentation = FormatStyle::NI_All;
647   Code = "\n"
648          "namespace ns1 { namespace ns2 {\n"
649          "}}";
650   EXPECT_EQ(Code, format(Code, 0, 0));
651 
652   // https://reviews.llvm.org/D151047#4369742
653   Style = getLLVMStyle();
654   Style.FixNamespaceComments = false;
655   Code = "namespace ns {\n"
656          "#define REF(alias) alias alias_var;\n"
657          "}"; // Format this line only
658   EXPECT_EQ(Code, format(Code, 51, 0));
659 }
660 
661 TEST_F(FormatTestSelective, FormatMacroRegardlessOfPreviousIndent) {
662   // clang-format currently does not (or should not) take into account the
663   // indent of previous unformatted lines when formatting a PP directive.
664   // Technically speaking, LevelIndentTracker::IndentForLevel is only for non-PP
665   // lines. So these tests here check that the indent of previous non-PP lines
666   // do not affect the formatting. If this requirement changes, the tests here
667   // need to be adapted.
668   Style = getLLVMStyle();
669 
670   const StringRef Code{"      class Foo {\n"
671                        "            void test() {\n"
672                        "    #ifdef 1\n"
673                        "                #define some\n" // format this line
674                        "         #endif\n"
675                        "    }};"};
676 
677   EXPECT_EQ(Style.IndentPPDirectives,
678             FormatStyle::PPDirectiveIndentStyle::PPDIS_None);
679   EXPECT_EQ("      class Foo {\n"
680             "            void test() {\n"
681             "    #ifdef 1\n"
682             "#define some\n" // Formatted line
683             "#endif\n"       // That this line is also formatted might be a bug.
684             "            }};", // Ditto: Bug?
685             format(Code, 57, 0));
686 
687   Style.IndentPPDirectives =
688       FormatStyle::PPDirectiveIndentStyle::PPDIS_BeforeHash;
689   EXPECT_EQ("      class Foo {\n"
690             "            void test() {\n"
691             "    #ifdef 1\n"
692             "  #define some\n" // Formatted line
693             "         #endif\n"
694             "    }};",
695             format(Code, 57, 0));
696 
697   Style.IndentPPDirectives =
698       FormatStyle::PPDirectiveIndentStyle::PPDIS_AfterHash;
699   EXPECT_EQ("      class Foo {\n"
700             "            void test() {\n"
701             "    #ifdef 1\n"
702             "#  define some\n" // Formatted line
703             "#endif\n" // That this line is also formatted might be a bug.
704             "    }};",
705             format(Code, 57, 0));
706 }
707 
708 } // end namespace
709 } // end namespace format
710 } // end namespace clang
711