xref: /llvm-project/clang/unittests/Format/FormatTestSelective.cpp (revision f67c32466d782824c67a3f31196eb31d22faa7fa)
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     std::string Result = applyAllReplacements(Code, Replaces);
32     EXPECT_NE("", 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 aaaaaa; // comment\n"
122             "int b;\n"
123             "int c; // unrelated comment",
124             format("int aaaaaa; // comment\n"
125                    "int b;\n"
126                    "int   c; // unrelated comment",
127                    31, 0));
128 
129   EXPECT_EQ("int a; // This\n"
130             "       // is\n"
131             "       // a",
132             format("int a;      // This\n"
133                    "            // is\n"
134                    "            // a",
135                    0, 0));
136   EXPECT_EQ("int a; // This\n"
137             "       // is\n"
138             "       // a\n"
139             "// This is b\n"
140             "int b;",
141             format("int a; // This\n"
142                    "     // is\n"
143                    "     // a\n"
144                    "// This is b\n"
145                    "int b;",
146                    0, 0));
147   EXPECT_EQ("int a; // This\n"
148             "       // is\n"
149             "       // a\n"
150             "\n"
151             "//This is unrelated",
152             format("int a; // This\n"
153                    "     // is\n"
154                    "     // a\n"
155                    "\n"
156                    "//This is unrelated",
157                    0, 0));
158   EXPECT_EQ("int a;\n"
159             "// This is\n"
160             "// not formatted.   ",
161             format("int a;\n"
162                    "// This is\n"
163                    "// not formatted.   ",
164                    0, 0));
165 }
166 
167 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
168   EXPECT_EQ("DEBUG({\n"
169             "  int i;\n"
170             "  int        j;\n"
171             "});",
172             format("DEBUG(   {\n"
173                    "  int        i;\n"
174                    "  int        j;\n"
175                    "}   )  ;",
176                    20, 1));
177   EXPECT_EQ("DEBUG(   {\n"
178             "  int        i;\n"
179             "  int j;\n"
180             "}   )  ;",
181             format("DEBUG(   {\n"
182                    "  int        i;\n"
183                    "  int        j;\n"
184                    "}   )  ;",
185                    41, 1));
186   EXPECT_EQ("DEBUG(   {\n"
187             "    int        i;\n"
188             "    int j;\n"
189             "}   )  ;",
190             format("DEBUG(   {\n"
191                    "    int        i;\n"
192                    "    int        j;\n"
193                    "}   )  ;",
194                    41, 1));
195   EXPECT_EQ("DEBUG({\n"
196             "  int i;\n"
197             "  int j;\n"
198             "});",
199             format("DEBUG(   {\n"
200                    "    int        i;\n"
201                    "    int        j;\n"
202                    "}   )  ;",
203                    20, 1));
204 
205   EXPECT_EQ("Debug({\n"
206             "        if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
207             "          return;\n"
208             "      },\n"
209             "      a);",
210             format("Debug({\n"
211                    "        if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
212                    "             return;\n"
213                    "      },\n"
214                    "      a);",
215                    50, 1));
216   EXPECT_EQ("DEBUG({\n"
217             "  DEBUG({\n"
218             "    int a;\n"
219             "    int b;\n"
220             "  }) ;\n"
221             "});",
222             format("DEBUG({\n"
223                    "  DEBUG({\n"
224                    "    int a;\n"
225                    "    int    b;\n" // Format this line only.
226                    "  }) ;\n"        // Don't touch this line.
227                    "});",
228                    35, 0));
229   EXPECT_EQ("DEBUG({\n"
230             "  int a; //\n"
231             "});",
232             format("DEBUG({\n"
233                    "    int a; //\n"
234                    "});",
235                    0, 0));
236   EXPECT_EQ("someFunction(\n"
237             "    [] {\n"
238             "      // Only with this comment.\n"
239             "      int i; // invoke formatting here.\n"
240             "    }, // force line break\n"
241             "    aaa);",
242             format("someFunction(\n"
243                    "    [] {\n"
244                    "      // Only with this comment.\n"
245                    "      int   i; // invoke formatting here.\n"
246                    "    }, // force line break\n"
247                    "    aaa);",
248                    63, 1));
249 
250   EXPECT_EQ("int longlongname; // comment\n"
251             "int x = f({\n"
252             "  int x; // comment\n"
253             "  int y; // comment\n"
254             "});",
255             format("int longlongname; // comment\n"
256                    "int x = f({\n"
257                    "  int x; // comment\n"
258                    "  int y; // comment\n"
259                    "});",
260                    65, 0));
261   EXPECT_EQ("int s = f({\n"
262             "  class X {\n"
263             "  public:\n"
264             "    void f();\n"
265             "  };\n"
266             "});",
267             format("int s = f({\n"
268                    "  class X {\n"
269                    "    public:\n"
270                    "    void f();\n"
271                    "  };\n"
272                    "});",
273                    0, 0));
274 }
275 
276 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
277   Style.AlignEscapedNewlinesLeft = true;
278   EXPECT_EQ("int  i;\n"
279             "#define A \\\n"
280             "  int i;  \\\n"
281             "  int j\n"
282             "int  k;",
283             format("int  i;\n"
284                    "#define A  \\\n"
285                    " int   i    ;  \\\n"
286                    " int   j\n"
287                    "int  k;",
288                    8, 0)); // 8: position of "#define".
289   EXPECT_EQ("int  i;\n"
290             "#define A \\\n"
291             "  int i;  \\\n"
292             "  int j\n"
293             "int  k;",
294             format("int  i;\n"
295                    "#define A  \\\n"
296                    " int   i    ;  \\\n"
297                    " int   j\n"
298                    "int  k;",
299                    45, 0)); // 45: position of "j".
300 }
301 
302 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
303   EXPECT_EQ("{\n"
304             "{\n"
305             "a;\n"
306             "b;\n"
307             "}\n"
308             "}",
309             format("{\n"
310                    "{\n"
311                    "a;\n"
312                    "     b;\n"
313                    "}\n"
314                    "}",
315                    13, 2));
316   EXPECT_EQ("{\n"
317             "{\n"
318             "  a;\n"
319             "  b;\n"
320             "  c;\n"
321             " d;\n"
322             "}\n"
323             "}",
324             format("{\n"
325                    "{\n"
326                    "     a;\n"
327                    "   b;\n"
328                    "  c;\n"
329                    " d;\n"
330                    "}\n"
331                    "}",
332                    9, 2));
333   EXPECT_EQ("{\n"
334             "{\n"
335             "public:\n"
336             "  b;\n"
337             "}\n"
338             "}",
339             format("{\n"
340                    "{\n"
341                    "public:\n"
342                    "     b;\n"
343                    "}\n"
344                    "}",
345                    17, 2));
346   EXPECT_EQ("{\n"
347             "{\n"
348             "a;\n"
349             "}\n"
350             "{\n"
351             "  b; //\n"
352             "}\n"
353             "}",
354             format("{\n"
355                    "{\n"
356                    "a;\n"
357                    "}\n"
358                    "{\n"
359                    "           b; //\n"
360                    "}\n"
361                    "}",
362                    22, 2));
363   EXPECT_EQ("  {\n"
364             "    a; //\n"
365             "  }",
366             format("  {\n"
367                    "a; //\n"
368                    "  }",
369                    4, 2));
370   EXPECT_EQ("void f() {}\n"
371             "void g() {}",
372             format("void f() {}\n"
373                    "void g() {}",
374                    13, 0));
375   EXPECT_EQ("int a; // comment\n"
376             "       // line 2\n"
377             "int b;",
378             format("int a; // comment\n"
379                    "       // line 2\n"
380                    "  int b;",
381                    35, 0));
382 
383   EXPECT_EQ(" void f() {\n"
384             "#define A 1\n"
385             " }",
386             format(" void f() {\n"
387                    "     #define A 1\n" // Format this line.
388                    " }",
389                    20, 0));
390   EXPECT_EQ(" void f() {\n"
391             "    int i;\n"
392             "#define A \\\n"
393             "    int i;  \\\n"
394             "   int j;\n"
395             "    int k;\n"
396             " }",
397             format(" void f() {\n"
398                    "    int i;\n"
399                    "#define A \\\n"
400                    "    int i;  \\\n"
401                    "   int j;\n"
402                    "      int k;\n" // Format this line.
403                    " }",
404                    67, 0));
405 
406   Style.ColumnLimit = 11;
407   EXPECT_EQ("  int a;\n"
408             "  void\n"
409             "  ffffff() {\n"
410             "  }",
411             format("  int a;\n"
412                    "void ffffff() {}",
413                    11, 0));
414 }
415 
416 TEST_F(FormatTestSelective, UnderstandsTabs) {
417   Style.IndentWidth = 8;
418   Style.UseTab = FormatStyle::UT_Always;
419   Style.AlignEscapedNewlinesLeft = true;
420   EXPECT_EQ("void f() {\n"
421             "\tf();\n"
422             "\tg();\n"
423             "}",
424             format("void f() {\n"
425                    "\tf();\n"
426                    "\tg();\n"
427                    "}",
428                    0, 0));
429   EXPECT_EQ("void f() {\n"
430             "\tf();\n"
431             "\tg();\n"
432             "}",
433             format("void f() {\n"
434                    "\tf();\n"
435                    "\tg();\n"
436                    "}",
437                    16, 0));
438   EXPECT_EQ("void f() {\n"
439             "  \tf();\n"
440             "\tg();\n"
441             "}",
442             format("void f() {\n"
443                    "  \tf();\n"
444                    "  \tg();\n"
445                    "}",
446                    21, 0));
447 }
448 
449 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
450   EXPECT_EQ(
451       "void f() {\n"
452       "  if (a) {\n"
453       "    g();\n"
454       "    h();\n"
455       "}\n"
456       "\n"
457       "void g() {\n"
458       "}",
459       format("void f() {\n"
460              "  if (a) {\n" // Assume this was added without the closing brace.
461              "  g();\n"
462              "  h();\n"
463              "}\n"
464              "\n"
465              "void g() {\n" // Make sure not to format this.
466              "}",
467              15, 0));
468 }
469 
470 } // end namespace
471 } // end namespace format
472 } // end namespace clang
473