1 #include "ClangTidyTest.h"
2 #include "readability/BracesAroundStatementsCheck.h"
3 #include "readability/NamespaceCommentCheck.h"
4 #include "gtest/gtest.h"
5
6 namespace clang {
7 namespace tidy {
8 namespace test {
9
10 using readability::BracesAroundStatementsCheck;
11 using readability::NamespaceCommentCheck;
12
TEST(NamespaceCommentCheckTest,Basic)13 TEST(NamespaceCommentCheckTest, Basic) {
14 EXPECT_EQ("namespace i {\n} // namespace i",
15 runCheckOnCode<NamespaceCommentCheck>("namespace i {\n}"));
16 EXPECT_EQ("namespace {\n} // namespace",
17 runCheckOnCode<NamespaceCommentCheck>("namespace {\n}"));
18 EXPECT_EQ("namespace i { namespace j {\n} // namespace j\n } // namespace i",
19 runCheckOnCode<NamespaceCommentCheck>(
20 "namespace i { namespace j {\n} }"));
21 }
22
TEST(NamespaceCommentCheckTest,SingleLineNamespaces)23 TEST(NamespaceCommentCheckTest, SingleLineNamespaces) {
24 EXPECT_EQ(
25 "namespace i { namespace j { } }",
26 runCheckOnCode<NamespaceCommentCheck>("namespace i { namespace j { } }"));
27 }
28
TEST(NamespaceCommentCheckTest,CheckExistingComments)29 TEST(NamespaceCommentCheckTest, CheckExistingComments) {
30 EXPECT_EQ("namespace i { namespace j {\n"
31 "} /* namespace j */ } // namespace i\n"
32 " /* random comment */",
33 runCheckOnCode<NamespaceCommentCheck>(
34 "namespace i { namespace j {\n"
35 "} /* namespace j */ } /* random comment */"));
36 EXPECT_EQ("namespace {\n"
37 "} // namespace",
38 runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
39 "} // namespace"));
40 EXPECT_EQ("namespace {\n"
41 "} //namespace",
42 runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
43 "} //namespace"));
44 EXPECT_EQ("namespace {\n"
45 "} // anonymous namespace",
46 runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
47 "} // anonymous namespace"));
48 EXPECT_EQ("namespace {\n"
49 "} // Anonymous namespace.",
50 runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
51 "} // Anonymous namespace."));
52 EXPECT_EQ(
53 "namespace q {\n"
54 "} // namespace q",
55 runCheckOnCode<NamespaceCommentCheck>("namespace q {\n"
56 "} // anonymous namespace q"));
57 EXPECT_EQ(
58 "namespace My_NameSpace123 {\n"
59 "} // namespace My_NameSpace123",
60 runCheckOnCode<NamespaceCommentCheck>("namespace My_NameSpace123 {\n"
61 "} // namespace My_NameSpace123"));
62 EXPECT_EQ(
63 "namespace My_NameSpace123 {\n"
64 "} //namespace My_NameSpace123",
65 runCheckOnCode<NamespaceCommentCheck>("namespace My_NameSpace123 {\n"
66 "} //namespace My_NameSpace123"));
67 EXPECT_EQ("namespace My_NameSpace123 {\n"
68 "} // end namespace My_NameSpace123",
69 runCheckOnCode<NamespaceCommentCheck>(
70 "namespace My_NameSpace123 {\n"
71 "} // end namespace My_NameSpace123"));
72 // Understand comments only on the same line.
73 EXPECT_EQ("namespace {\n"
74 "} // namespace\n"
75 "// namespace",
76 runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
77 "}\n"
78 "// namespace"));
79 }
80
TEST(NamespaceCommentCheckTest,FixWrongComments)81 TEST(NamespaceCommentCheckTest, FixWrongComments) {
82 EXPECT_EQ("namespace i { namespace jJ0_ {\n"
83 "} // namespace jJ0_\n"
84 " } // namespace i\n"
85 " /* random comment */",
86 runCheckOnCode<NamespaceCommentCheck>(
87 "namespace i { namespace jJ0_ {\n"
88 "} /* namespace qqq */ } /* random comment */"));
89 EXPECT_EQ("namespace {\n"
90 "} // namespace",
91 runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
92 "} // namespace asdf"));
93 // Remove unknown line comments. These are likely to be an unrecognized form
94 // of a namespace ending comment.
95 EXPECT_EQ("namespace {\n"
96 "} // namespace",
97 runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
98 "} // random text"));
99 }
100
TEST(BracesAroundStatementsCheckTest,IfWithComments)101 TEST(BracesAroundStatementsCheckTest, IfWithComments) {
102 EXPECT_EQ("int main() {\n"
103 " if (false /*dummy token*/) {\n"
104 " // comment\n"
105 " return -1; /**/\n"
106 "}\n"
107 " if (false) {\n"
108 " return -1; // comment\n"
109 "}\n"
110 " if (false) {\n"
111 " return -1; \n"
112 "}/* multi-line \n comment */\n"
113 "}",
114 runCheckOnCode<BracesAroundStatementsCheck>(
115 "int main() {\n"
116 " if (false /*dummy token*/)\n"
117 " // comment\n"
118 " return -1; /**/\n"
119 " if (false)\n"
120 " return -1; // comment\n"
121 " if (false)\n"
122 " return -1; /* multi-line \n comment */\n"
123 "}"));
124 EXPECT_EQ("int main() {\n"
125 " if (false /*dummy token*/) {\n"
126 " // comment\n"
127 " return -1 /**/ ;\n"
128 "}\n"
129 "}",
130 runCheckOnCode<BracesAroundStatementsCheck>(
131 "int main() {\n"
132 " if (false /*dummy token*/)\n"
133 " // comment\n"
134 " return -1 /**/ ;\n"
135 "}"));
136 }
137
TEST(BracesAroundStatementsCheckTest,If)138 TEST(BracesAroundStatementsCheckTest, If) {
139 EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
140 " if (false) {\n"
141 " return -1;\n"
142 " }\n"
143 "}");
144 EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
145 " if (auto Cond = false) {\n"
146 " return -1;\n"
147 " }\n"
148 "}");
149 EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
150 " if (false) {\n"
151 " return -1;\n"
152 " } else {\n"
153 " return -2;\n"
154 " }\n"
155 "}");
156 EXPECT_EQ("int main() {\n"
157 " if (false) {\n"
158 " return -1;\n"
159 "}\n"
160 "}",
161 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
162 " if (false)\n"
163 " return -1;\n"
164 "}"));
165 EXPECT_EQ("int main() {\n"
166 " if (auto Cond = false /**/ ) {\n"
167 " return -1;\n"
168 "}\n"
169 "}",
170 runCheckOnCode<BracesAroundStatementsCheck>(
171 "int main() {\n"
172 " if (auto Cond = false /**/ )\n"
173 " return -1;\n"
174 "}"));
175 // FIXME: Consider adding braces before EMPTY_MACRO and after the statement.
176 EXPECT_NO_CHANGES(BracesAroundStatementsCheck,
177 "#define EMPTY_MACRO\n"
178 "int main() {\n"
179 " if (auto Cond = false EMPTY_MACRO /**/ ) EMPTY_MACRO\n"
180 " return -1;\n"
181 "}");
182 EXPECT_EQ("int main() {\n"
183 " if (true) { return -1/**/ ;\n"
184 "}\n"
185 "}",
186 runCheckOnCode<BracesAroundStatementsCheck>(
187 "int main() {\n"
188 " if (true) return -1/**/ ;\n"
189 "}"));
190 EXPECT_EQ("int main() {\n"
191 " if (false) {\n"
192 " return -1;\n"
193 " } else {\n"
194 " return -2;\n"
195 "}\n"
196 "}",
197 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
198 " if (false)\n"
199 " return -1;\n"
200 " else\n"
201 " return -2;\n"
202 "}"));
203 EXPECT_EQ("int main() {\n"
204 " if (false) {\n"
205 " return -1;\n"
206 " } else if (1 == 2) {\n"
207 " return -2;\n"
208 " } else {\n"
209 " return -3;\n"
210 "}\n"
211 "}",
212 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
213 " if (false)\n"
214 " return -1;\n"
215 " else if (1 == 2)\n"
216 " return -2;\n"
217 " else\n"
218 " return -3;\n"
219 "}"));
220 EXPECT_EQ("int main() {\n"
221 " if (false) {\n"
222 " return -1;\n"
223 " } else if (1 == 2) {\n"
224 " return -2;\n"
225 " } else {\n"
226 " return -3;\n"
227 "}\n"
228 "}",
229 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
230 " if (false)\n"
231 " return -1;\n"
232 " else if (1 == 2) {\n"
233 " return -2;\n"
234 " } else\n"
235 " return -3;\n"
236 "}"));
237 }
238
TEST(BracesAroundStatementsCheckTest,IfElseWithShortStatements)239 TEST(BracesAroundStatementsCheckTest, IfElseWithShortStatements) {
240 ClangTidyOptions Options;
241 Options.CheckOptions["test-check-0.ShortStatementLines"] = "1";
242
243 EXPECT_EQ("int main() {\n"
244 " if (true) return 1;\n"
245 " if (false) { return -1;\n"
246 " } else if (1 == 2) { return -2;\n"
247 " } else { return -3;\n"
248 "}\n"
249 "}",
250 runCheckOnCode<BracesAroundStatementsCheck>(
251 "int main() {\n"
252 " if (true) return 1;\n"
253 " if (false) return -1;\n"
254 " else if (1 == 2) return -2;\n"
255 " else return -3;\n"
256 "}",
257 nullptr, "input.cc", None, Options));
258
259 // If the last else is an else-if, we also force it.
260 EXPECT_EQ("int main() {\n"
261 " if (false) { return -1;\n"
262 " } else if (1 == 2) { return -2;\n"
263 "}\n"
264 "}",
265 runCheckOnCode<BracesAroundStatementsCheck>(
266 "int main() {\n"
267 " if (false) return -1;\n"
268 " else if (1 == 2) return -2;\n"
269 "}",
270 nullptr, "input.cc", None, Options));
271 }
272
TEST(BracesAroundStatementsCheckTest,For)273 TEST(BracesAroundStatementsCheckTest, For) {
274 EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
275 " for (;;) {\n"
276 " ;\n"
277 " }\n"
278 "}");
279 EXPECT_EQ("int main() {\n"
280 " for (;;) {\n"
281 " ;\n"
282 "}\n"
283 "}",
284 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
285 " for (;;)\n"
286 " ;\n"
287 "}"));
288 EXPECT_EQ("int main() {\n"
289 " for (;;) {\n"
290 " /**/ ;\n"
291 "}\n"
292 "}",
293 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
294 " for (;;)\n"
295 " /**/ ;\n"
296 "}"));
297 EXPECT_EQ("int main() {\n"
298 " for (;;) {\n"
299 " return -1 /**/ ;\n"
300 "}\n"
301 "}",
302 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
303 " for (;;)\n"
304 " return -1 /**/ ;\n"
305 "}"));
306 }
307
TEST(BracesAroundStatementsCheckTest,ForRange)308 TEST(BracesAroundStatementsCheckTest, ForRange) {
309 EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
310 " int arr[4];\n"
311 " for (int i : arr) {\n"
312 " ;\n"
313 " }\n"
314 "}");
315 EXPECT_EQ("int main() {\n"
316 " int arr[4];\n"
317 " for (int i : arr) {\n"
318 " ;\n"
319 "}\n"
320 " for (int i : arr) {\n"
321 " return -1 ;\n"
322 "}\n"
323 "}",
324 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
325 " int arr[4];\n"
326 " for (int i : arr)\n"
327 " ;\n"
328 " for (int i : arr)\n"
329 " return -1 ;\n"
330 "}"));
331 }
332
TEST(BracesAroundStatementsCheckTest,DoWhile)333 TEST(BracesAroundStatementsCheckTest, DoWhile) {
334 EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
335 " do {\n"
336 " ;\n"
337 " } while (false);\n"
338 "}");
339 EXPECT_EQ("int main() {\n"
340 " do {\n"
341 " ;\n"
342 " } while (false);\n"
343 "}",
344 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
345 " do\n"
346 " ;\n"
347 " while (false);\n"
348 "}"));
349 }
350
TEST(BracesAroundStatementsCheckTest,While)351 TEST(BracesAroundStatementsCheckTest, While) {
352 EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
353 " while (false) {\n"
354 " ;\n"
355 " }\n"
356 "}");
357 EXPECT_EQ("int main() {\n"
358 " while (false) {\n"
359 " ;\n"
360 "}\n"
361 "}",
362 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
363 " while (false)\n"
364 " ;\n"
365 "}"));
366 EXPECT_EQ("int main() {\n"
367 " while (auto Cond = false) {\n"
368 " ;\n"
369 "}\n"
370 "}",
371 runCheckOnCode<BracesAroundStatementsCheck>(
372 "int main() {\n"
373 " while (auto Cond = false)\n"
374 " ;\n"
375 "}"));
376 EXPECT_EQ("int main() {\n"
377 " while (false /*dummy token*/) {\n"
378 " ;\n"
379 "}\n"
380 "}",
381 runCheckOnCode<BracesAroundStatementsCheck>(
382 "int main() {\n"
383 " while (false /*dummy token*/)\n"
384 " ;\n"
385 "}"));
386 EXPECT_EQ("int main() {\n"
387 " while (false) {\n"
388 " break;\n"
389 "}\n"
390 "}",
391 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
392 " while (false)\n"
393 " break;\n"
394 "}"));
395 EXPECT_EQ("int main() {\n"
396 " while (false) {\n"
397 " break /**/;\n"
398 "}\n"
399 "}",
400 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
401 " while (false)\n"
402 " break /**/;\n"
403 "}"));
404 EXPECT_EQ("int main() {\n"
405 " while (false) {\n"
406 " /**/;\n"
407 "}\n"
408 "}",
409 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
410 " while (false)\n"
411 " /**/;\n"
412 "}"));
413 }
414
TEST(BracesAroundStatementsCheckTest,Nested)415 TEST(BracesAroundStatementsCheckTest, Nested) {
416 EXPECT_EQ("int main() {\n"
417 " do { if (true) {}} while (false);\n"
418 "}",
419 runCheckOnCode<BracesAroundStatementsCheck>(
420 "int main() {\n"
421 " do if (true) {}while (false);\n"
422 "}"));
423 EXPECT_EQ("int main() {\n"
424 " do { if (true) {}} while (false);\n"
425 "}",
426 runCheckOnCode<BracesAroundStatementsCheck>(
427 "int main() {\n"
428 " do if (true) {}while (false);\n"
429 "}"));
430 EXPECT_EQ(
431 "int main() {\n"
432 " if (true) {\n"
433 " // comment\n"
434 " if (false) {\n"
435 " // comment\n"
436 " /**/ ; // comment\n"
437 " }\n"
438 "}\n"
439 "}",
440 runCheckOnCode<BracesAroundStatementsCheck>("int main() {\n"
441 " if (true)\n"
442 " // comment\n"
443 " if (false) {\n"
444 " // comment\n"
445 " /**/ ; // comment\n"
446 " }\n"
447 "}"));
448 }
449
TEST(BracesAroundStatementsCheckTest,Macros)450 TEST(BracesAroundStatementsCheckTest, Macros) {
451 EXPECT_NO_CHANGES(BracesAroundStatementsCheck,
452 "#define IF(COND) if (COND) return -1;\n"
453 "int main() {\n"
454 " IF(false)\n"
455 "}");
456 EXPECT_NO_CHANGES(BracesAroundStatementsCheck,
457 "#define FOR(COND) for (COND) return -1;\n"
458 "int main() {\n"
459 " FOR(;;)\n"
460 "}");
461 EXPECT_EQ("#define DO_IT ++i\n"
462 "int i = 0;\n"
463 "int main() {\n"
464 " if (false) {\n"
465 " DO_IT;\n"
466 " } else if (1 == 2) {\n"
467 " DO_IT;\n"
468 " } else {\n"
469 " DO_IT;\n"
470 "}\n"
471 "}",
472 runCheckOnCode<BracesAroundStatementsCheck>("#define DO_IT ++i\n"
473 "int i = 0;\n"
474 "int main() {\n"
475 " if (false)\n"
476 " DO_IT;\n"
477 " else if (1 == 2)\n"
478 " DO_IT;\n"
479 " else\n"
480 " DO_IT;\n"
481 "}"));
482 }
483
484 #define EXPECT_NO_CHANGES_WITH_OPTS(Check, Opts, Code) \
485 EXPECT_EQ(Code, runCheckOnCode<Check>(Code, nullptr, "input.cc", None, Opts))
TEST(BracesAroundStatementsCheckTest,ImplicitCastInReturn)486 TEST(BracesAroundStatementsCheckTest, ImplicitCastInReturn) {
487 ClangTidyOptions Opts;
488 Opts.CheckOptions["test-check-0.ShortStatementLines"] = "1";
489
490 StringRef Input = "const char *f() {\n"
491 " if (true) return \"\";\n"
492 " return \"abc\";\n"
493 "}\n";
494 EXPECT_NO_CHANGES_WITH_OPTS(BracesAroundStatementsCheck, Opts, Input);
495 EXPECT_EQ("const char *f() {\n"
496 " if (true) { return \"\";\n"
497 "}\n"
498 " return \"abc\";\n"
499 "}\n",
500 runCheckOnCode<BracesAroundStatementsCheck>(Input));
501 }
502
503 } // namespace test
504 } // namespace tidy
505 } // namespace clang
506