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

source code of clang-tools-extra/unittests/clang-tidy/ReadabilityModuleTest.cpp