xref: /llvm-project/clang/unittests/Format/FormatTestSelective.cpp (revision 91834227a3d58ecdb9e3f7300f587ab45c32ba06)
1 //===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 namespace {
20 
21 class FormatTestSelective : public ::testing::Test {
22 protected:
23   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) {
24     DEBUG(llvm::errs() << "---\n");
25     DEBUG(llvm::errs() << Code << "\n\n");
26     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27     bool IncompleteFormat = false;
28     tooling::Replacements Replaces =
29         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
30     EXPECT_FALSE(IncompleteFormat) << Code << "\n\n";
31     auto Result = applyAllReplacements(Code, Replaces);
32     EXPECT_TRUE(static_cast<bool>(Result));
33     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
34     return *Result;
35   }
36 
37   FormatStyle Style = getLLVMStyle();
38 };
39 
40 TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
41   EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0));
42   EXPECT_EQ("int a;", format("int a;         ", 0, 0));
43   EXPECT_EQ("int a;\n", format("int a;  \n   \n   \n ", 0, 0));
44   EXPECT_EQ("int a;\nint b;    ", format("int a;  \nint b;    ", 0, 0));
45 }
46 
47 TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
48   EXPECT_EQ("{int b;\n"
49             "  int a;\n"
50             "}",
51             format("{int b;\n  int  a;}", 8, 0));
52   EXPECT_EQ("{\n"
53             "  int b;\n"
54             "  int  a;}",
55             format("{int b;\n  int  a;}", 7, 0));
56 
57   Style.ColumnLimit = 12;
58   EXPECT_EQ("#define A  \\\n"
59             "  int a;   \\\n"
60             "  int b;",
61             format("#define A  \\\n"
62                    "  int a;   \\\n"
63                    "    int b;",
64                    26, 0));
65   EXPECT_EQ("#define A  \\\n"
66             "  int a;   \\\n"
67             "  int b;",
68             format("#define A  \\\n"
69                    "  int a;   \\\n"
70                    "  int b;",
71                    25, 0));
72 }
73 
74 TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
75   EXPECT_EQ("int  b;\n\nint a;", format("int  b;\n\nint a;", 8, 0));
76   EXPECT_EQ("int b;\n\nint a;", format("int  b;\n\nint a;", 7, 0));
77 
78   // This might not strictly be correct, but is likely good in all practical
79   // cases.
80   EXPECT_EQ("int b;\nint a;", format("int  b;int a;", 7, 0));
81 }
82 
83 TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
84   EXPECT_EQ("int  a;\n\n int b;", format("int  a;\n  \n\n int b;", 8, 0));
85   EXPECT_EQ("int  a;\n\n int b;", format("int  a;\n  \n\n int b;", 9, 0));
86 }
87 
88 TEST_F(FormatTestSelective, ReformatsMovedLines) {
89   EXPECT_EQ(
90       "template <typename T> T *getFETokenInfo() const {\n"
91       "  return static_cast<T *>(FETokenInfo);\n"
92       "}\n"
93       "int  a; // <- Should not be formatted",
94       format(
95           "template<typename T>\n"
96           "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
97           "int  a; // <- Should not be formatted",
98           9, 5));
99 }
100 
101 TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
102   Style.AllowShortIfStatementsOnASingleLine = true;
103   EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
104   EXPECT_EQ("if (a) return; // comment",
105             format("if(a)\nreturn; // comment", 20, 1));
106 }
107 
108 TEST_F(FormatTestSelective, FormatsCommentsLocally) {
109   EXPECT_EQ("int a;    // comment\n"
110             "int    b; // comment",
111             format("int   a; // comment\n"
112                    "int    b; // comment",
113                    0, 0));
114   EXPECT_EQ("int a; // comment\n"
115             "       // line 2\n"
116             "int b;",
117             format("int   a; // comment\n"
118                    "            // line 2\n"
119                    "int b;",
120                    28, 0));
121   EXPECT_EQ("int   a; // comment\n"
122             "// comment 2\n"
123             "int b;",
124             format("int   a; // comment\n"
125                    "// comment 2\n"
126                    "int b;", 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, IndividualStatementsOfNestedBlocks) {
181   EXPECT_EQ("DEBUG({\n"
182             "  int i;\n"
183             "  int        j;\n"
184             "});",
185             format("DEBUG(   {\n"
186                    "  int        i;\n"
187                    "  int        j;\n"
188                    "}   )  ;",
189                    20, 1));
190   EXPECT_EQ("DEBUG(   {\n"
191             "  int        i;\n"
192             "  int j;\n"
193             "}   )  ;",
194             format("DEBUG(   {\n"
195                    "  int        i;\n"
196                    "  int        j;\n"
197                    "}   )  ;",
198                    41, 1));
199   EXPECT_EQ("DEBUG(   {\n"
200             "    int        i;\n"
201             "    int j;\n"
202             "}   )  ;",
203             format("DEBUG(   {\n"
204                    "    int        i;\n"
205                    "    int        j;\n"
206                    "}   )  ;",
207                    41, 1));
208   EXPECT_EQ("DEBUG({\n"
209             "  int i;\n"
210             "  int j;\n"
211             "});",
212             format("DEBUG(   {\n"
213                    "    int        i;\n"
214                    "    int        j;\n"
215                    "}   )  ;",
216                    20, 1));
217 
218   EXPECT_EQ("Debug({\n"
219             "        if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
220             "          return;\n"
221             "      },\n"
222             "      a);",
223             format("Debug({\n"
224                    "        if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
225                    "             return;\n"
226                    "      },\n"
227                    "      a);",
228                    50, 1));
229   EXPECT_EQ("DEBUG({\n"
230             "  DEBUG({\n"
231             "    int a;\n"
232             "    int b;\n"
233             "  }) ;\n"
234             "});",
235             format("DEBUG({\n"
236                    "  DEBUG({\n"
237                    "    int a;\n"
238                    "    int    b;\n" // Format this line only.
239                    "  }) ;\n"        // Don't touch this line.
240                    "});",
241                    35, 0));
242   EXPECT_EQ("DEBUG({\n"
243             "  int a; //\n"
244             "});",
245             format("DEBUG({\n"
246                    "    int a; //\n"
247                    "});",
248                    0, 0));
249   EXPECT_EQ("someFunction(\n"
250             "    [] {\n"
251             "      // Only with this comment.\n"
252             "      int i; // invoke formatting here.\n"
253             "    }, // force line break\n"
254             "    aaa);",
255             format("someFunction(\n"
256                    "    [] {\n"
257                    "      // Only with this comment.\n"
258                    "      int   i; // invoke formatting here.\n"
259                    "    }, // force line break\n"
260                    "    aaa);",
261                    63, 1));
262 
263   EXPECT_EQ("int longlongname; // comment\n"
264             "int x = f({\n"
265             "  int x; // comment\n"
266             "  int y; // comment\n"
267             "});",
268             format("int longlongname; // comment\n"
269                    "int x = f({\n"
270                    "  int x; // comment\n"
271                    "  int y; // comment\n"
272                    "});",
273                    65, 0));
274   EXPECT_EQ("int s = f({\n"
275             "  class X {\n"
276             "  public:\n"
277             "    void f();\n"
278             "  };\n"
279             "});",
280             format("int s = f({\n"
281                    "  class X {\n"
282                    "    public:\n"
283                    "    void f();\n"
284                    "  };\n"
285                    "});",
286                    0, 0));
287   EXPECT_EQ("SomeFunction(\n"
288             "    [] {\n"
289             "      int i;\n"
290             "      return i;\n" // Format this line.
291             "    },\n"
292             "    [] {\n"
293             "       return 2;\n" // Don't fix this.
294             "    });",
295             format("SomeFunction(\n"
296                    "    [] {\n"
297                    "      int i;\n"
298                    "       return i;\n" // Format this line.
299                    "    },\n"
300                    "    [] {\n"
301                    "       return 2;\n" // Don't fix this.
302                    "    });",
303                    40, 0));
304 }
305 
306 TEST_F(FormatTestSelective, WrongIndent) {
307   EXPECT_EQ("namespace {\n"
308             "int i;\n"
309             "int j;\n"
310             "}",
311             format("namespace {\n"
312                    "  int i;\n" // Format here.
313                    "  int j;\n"
314                    "}",
315                    15, 0));
316   EXPECT_EQ("namespace {\n"
317             "  int i;\n"
318             "  int j;\n"
319             "}",
320             format("namespace {\n"
321                    "  int i;\n"
322                    "  int j;\n" // Format here.
323                    "}",
324                    24, 0));
325 }
326 
327 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
328   Style.AlignEscapedNewlinesLeft = true;
329   EXPECT_EQ("int  i;\n"
330             "#define A \\\n"
331             "  int i;  \\\n"
332             "  int j\n"
333             "int  k;",
334             format("int  i;\n"
335                    "#define A  \\\n"
336                    " int   i    ;  \\\n"
337                    " int   j\n"
338                    "int  k;",
339                    8, 0)); // 8: position of "#define".
340   EXPECT_EQ("int  i;\n"
341             "#define A \\\n"
342             "  int i;  \\\n"
343             "  int j\n"
344             "int  k;",
345             format("int  i;\n"
346                    "#define A  \\\n"
347                    " int   i    ;  \\\n"
348                    " int   j\n"
349                    "int  k;",
350                    45, 0)); // 45: position of "j".
351 }
352 
353 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
354   EXPECT_EQ("{\n"
355             "{\n"
356             "a;\n"
357             "b;\n"
358             "}\n"
359             "}",
360             format("{\n"
361                    "{\n"
362                    "a;\n"
363                    "     b;\n"
364                    "}\n"
365                    "}",
366                    13, 2));
367   EXPECT_EQ("{\n"
368             "{\n"
369             "  a;\n"
370             "  b;\n"
371             "  c;\n"
372             " d;\n"
373             "}\n"
374             "}",
375             format("{\n"
376                    "{\n"
377                    "     a;\n"
378                    "   b;\n"
379                    "  c;\n"
380                    " d;\n"
381                    "}\n"
382                    "}",
383                    9, 2));
384   EXPECT_EQ("{\n"
385             "{\n"
386             "public:\n"
387             "  b;\n"
388             "}\n"
389             "}",
390             format("{\n"
391                    "{\n"
392                    "public:\n"
393                    "     b;\n"
394                    "}\n"
395                    "}",
396                    17, 2));
397   EXPECT_EQ("{\n"
398             "{\n"
399             "a;\n"
400             "}\n"
401             "{\n"
402             "  b; //\n"
403             "}\n"
404             "}",
405             format("{\n"
406                    "{\n"
407                    "a;\n"
408                    "}\n"
409                    "{\n"
410                    "           b; //\n"
411                    "}\n"
412                    "}",
413                    22, 2));
414   EXPECT_EQ("  {\n"
415             "    a; //\n"
416             "  }",
417             format("  {\n"
418                    "a; //\n"
419                    "  }",
420                    4, 2));
421   EXPECT_EQ("void f() {}\n"
422             "void g() {}",
423             format("void f() {}\n"
424                    "void g() {}",
425                    13, 0));
426   EXPECT_EQ("int a; // comment\n"
427             "       // line 2\n"
428             "int b;",
429             format("int a; // comment\n"
430                    "       // line 2\n"
431                    "  int b;",
432                    35, 0));
433 
434   EXPECT_EQ(" void f() {\n"
435             "#define A 1\n"
436             " }",
437             format(" void f() {\n"
438                    "     #define A 1\n" // Format this line.
439                    " }",
440                    20, 0));
441   EXPECT_EQ(" void f() {\n"
442             "    int i;\n"
443             "#define A \\\n"
444             "    int i;  \\\n"
445             "   int j;\n"
446             "    int k;\n"
447             " }",
448             format(" void f() {\n"
449                    "    int i;\n"
450                    "#define A \\\n"
451                    "    int i;  \\\n"
452                    "   int j;\n"
453                    "      int k;\n" // Format this line.
454                    " }",
455                    67, 0));
456 
457   Style.ColumnLimit = 11;
458   EXPECT_EQ("  int a;\n"
459             "  void\n"
460             "  ffffff() {\n"
461             "  }",
462             format("  int a;\n"
463                    "void ffffff() {}",
464                    11, 0));
465 }
466 
467 TEST_F(FormatTestSelective, UnderstandsTabs) {
468   Style.IndentWidth = 8;
469   Style.UseTab = FormatStyle::UT_Always;
470   Style.AlignEscapedNewlinesLeft = true;
471   EXPECT_EQ("void f() {\n"
472             "\tf();\n"
473             "\tg();\n"
474             "}",
475             format("void f() {\n"
476                    "\tf();\n"
477                    "\tg();\n"
478                    "}",
479                    0, 0));
480   EXPECT_EQ("void f() {\n"
481             "\tf();\n"
482             "\tg();\n"
483             "}",
484             format("void f() {\n"
485                    "\tf();\n"
486                    "\tg();\n"
487                    "}",
488                    16, 0));
489   EXPECT_EQ("void f() {\n"
490             "  \tf();\n"
491             "\tg();\n"
492             "}",
493             format("void f() {\n"
494                    "  \tf();\n"
495                    "  \tg();\n"
496                    "}",
497                    21, 0));
498 }
499 
500 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
501   EXPECT_EQ(
502       "void f() {\n"
503       "  if (a) {\n"
504       "    g();\n"
505       "    h();\n"
506       "}\n"
507       "\n"
508       "void g() {\n"
509       "}",
510       format("void f() {\n"
511              "  if (a) {\n" // Assume this was added without the closing brace.
512              "  g();\n"
513              "  h();\n"
514              "}\n"
515              "\n"
516              "void g() {\n" // Make sure not to format this.
517              "}",
518              15, 0));
519 }
520 
521 TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
522   Style = getGoogleStyle(FormatStyle::LK_JavaScript);
523   EXPECT_EQ(
524       "var x = \"a\";\n"
525       "var x = 'a';\n"
526       "var x = \"a\";",
527       format("var x = \"a\";\n"
528              "var x = \"a\";\n"
529              "var x = \"a\";",
530              20, 0));
531 }
532 
533 } // end namespace
534 } // end namespace format
535 } // end namespace clang
536