Debian Codegraph @

llvm-project/clang/unittests/Tooling/HeaderIncludesTest.cpp

raw
1 //===- unittest/Tooling/CleanupTest.cpp - Include insertion/deletion 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 "clang/Tooling/Inclusions/HeaderIncludes.h"
10 #include "../Tooling/ReplacementTest.h"
11 #include "../Tooling/RewriterTestContext.h"
12 #include "clang/Format/Format.h"
13 #include "clang/Tooling/Core/Replacement.h"
14
15 #include "gtest/gtest.h"
16
17 namespace clang {
18 namespace tooling {
19 namespace {
20
21 class HeaderIncludesTest : public ::testing::Test {
22 protected:
23 std::string insert(llvm::StringRef Code, llvm::StringRef Header,
24 IncludeDirective Directive = IncludeDirective::Include) {
25 HeaderIncludes Includes(FileName, Code, Style);
26 assert(Header.startswith("\"") || Header.startswith("<"));
27 auto R =
28 Includes.insert(Header.trim("\"<>"), Header.startswith("<"), Directive);
29 if (!R)
30 return std::string(Code);
31 auto Result = applyAllReplacements(Code, Replacements(*R));
32 EXPECT_TRUE(static_cast<bool>(Result));
33 return *Result;
34 }
35
36 std::string remove(llvm::StringRef Code, llvm::StringRef Header) {
37 HeaderIncludes Includes(FileName, Code, Style);
38 assert(Header.startswith("\"") || Header.startswith("<"));
39 auto Replaces = Includes.remove(Header.trim("\"<>"), Header.startswith("<"));
40 auto Result = applyAllReplacements(Code, Replaces);
41 EXPECT_TRUE(static_cast<bool>(Result));
42 return *Result;
43 }
44
45 std::string FileName = "fix.cpp";
46 IncludeStyle Style = format::getLLVMStyle().IncludeStyle;
47 };
48
49 TEST_F(HeaderIncludesTest, NoExistingIncludeWithoutDefine) {
50 std::string Code = "int main() {}";
51 std::string Expected = "#include \"a.h\"\n"
52 "int main() {}";
53 EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
54 }
55
56 TEST_F(HeaderIncludesTest, RepeatedIncludes) {
57 std::string Code;
58 for (int i = 0; i < 100; ++i) {
59 Code += "#include \"a.h\"\n";
60 }
61 std::string Expected = Code + "#include \"a2.h\"\n";
62 EXPECT_EQ(Expected, insert(Code, "\"a2.h\""));
63 }
64
65 TEST_F(HeaderIncludesTest, InsertImportWithSameInclude) {
66 std::string Code = "#include \"a.h\"\n";
67 std::string Expected = Code + "#import \"a.h\"\n";
68 EXPECT_EQ(Expected, insert(Code, "\"a.h\"", IncludeDirective::Import));
69 }
70
71 TEST_F(HeaderIncludesTest, DontInsertAlreadyImported) {
72 std::string Code = "#import \"a.h\"\n";
73 EXPECT_EQ(Code, insert(Code, "\"a.h\"", IncludeDirective::Import));
74 }
75
76 TEST_F(HeaderIncludesTest, DeleteImportAndSameInclude) {
77 std::string Code = R"cpp(
78 #include <abc.h>
79 #import <abc.h>
80 int x;)cpp";
81 EXPECT_EQ("\nint x;", remove(Code, "<abc.h>"));
82 }
83
84 TEST_F(HeaderIncludesTest, NoExistingIncludeWithDefine) {
85 std::string Code = "#ifndef A_H\n"
86 "#define A_H\n"
87 "class A {};\n"
88 "#define MMM 123\n"
89 "#endif";
90 std::string Expected = "#ifndef A_H\n"
91 "#define A_H\n"
92 "#include \"b.h\"\n"
93 "class A {};\n"
94 "#define MMM 123\n"
95 "#endif";
96
97 EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
98 }
99
100 TEST_F(HeaderIncludesTest, InsertBeforeCategoryWithLowerPriority) {
101 std::string Code = "#ifndef A_H\n"
102 "#define A_H\n"
103 "\n"
104 "\n"
105 "\n"
106 "#include <vector>\n"
107 "class A {};\n"
108 "#define MMM 123\n"
109 "#endif";
110 std::string Expected = "#ifndef A_H\n"
111 "#define A_H\n"
112 "\n"
113 "\n"
114 "\n"
115 "#include \"a.h\"\n"
116 "#include <vector>\n"
117 "class A {};\n"
118 "#define MMM 123\n"
119 "#endif";
120
121 EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
122 }
123
124 TEST_F(HeaderIncludesTest, InsertAfterMainHeader) {
125 std::string Code = "#include \"fix.h\"\n"
126 "\n"
127 "int main() {}";
128 std::string Expected = "#include \"fix.h\"\n"
129 "#include <a>\n"
130 "\n"
131 "int main() {}";
132 Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
133 .IncludeStyle;
134 EXPECT_EQ(Expected, insert(Code, "<a>"));
135
136 FileName = "fix.cu.cpp";
137 EXPECT_EQ(Expected, insert(Code, "<a>"));
138
139 FileName = "fix_test.cu.cpp";
140 EXPECT_EQ(Expected, insert(Code, "<a>"));
141
142 FileName = "bar.cpp";
143 EXPECT_NE(Expected, insert(Code, "<a>")) << "Not main header";
144 }
145
146 TEST_F(HeaderIncludesTest, InsertBeforeSystemHeaderLLVM) {
147 std::string Code = "#include <memory>\n"
148 "\n"
149 "int main() {}";
150 std::string Expected = "#include \"z.h\"\n"
151 "#include <memory>\n"
152 "\n"
153 "int main() {}";
154 EXPECT_EQ(Expected, insert(Code, "\"z.h\""));
155 }
156
157 TEST_F(HeaderIncludesTest, InsertAfterSystemHeaderGoogle) {
158 std::string Code = "#include <memory>\n"
159 "\n"
160 "int main() {}";
161 std::string Expected = "#include <memory>\n"
162 "#include \"z.h\"\n"
163 "\n"
164 "int main() {}";
165 Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
166 .IncludeStyle;
167 EXPECT_EQ(Expected, insert(Code, "\"z.h\""));
168 }
169
170 TEST_F(HeaderIncludesTest, InsertOneIncludeLLVMStyle) {
171 std::string Code = "#include \"x/fix.h\"\n"
172 "#include \"a.h\"\n"
173 "#include \"b.h\"\n"
174 "#include \"clang/Format/Format.h\"\n"
175 "#include <memory>\n";
176 std::string Expected = "#include \"x/fix.h\"\n"
177 "#include \"a.h\"\n"
178 "#include \"b.h\"\n"
179 "#include \"clang/Format/Format.h\"\n"
180 "#include \"llvm/x/y.h\"\n"
181 "#include <memory>\n";
182 EXPECT_EQ(Expected, insert(Code, "\"llvm/x/y.h\""));
183 }
184
185 TEST_F(HeaderIncludesTest, InsertIntoBlockSorted) {
186 std::string Code = "#include \"x/fix.h\"\n"
187 "#include \"a.h\"\n"
188 "#include \"c.h\"\n"
189 "#include <memory>\n";
190 std::string Expected = "#include \"x/fix.h\"\n"
191 "#include \"a.h\"\n"
192 "#include \"b.h\"\n"
193 "#include \"c.h\"\n"
194 "#include <memory>\n";
195 EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
196 }
197
198 TEST_F(HeaderIncludesTest, InsertIntoFirstBlockOfSameKind) {
199 std::string Code = "#include \"x/fix.h\"\n"
200 "#include \"c.h\"\n"
201 "#include \"e.h\"\n"
202 "#include \"f.h\"\n"
203 "#include <memory>\n"
204 "#include <vector>\n"
205 "#include \"m.h\"\n"
206 "#include \"n.h\"\n";
207 std::string Expected = "#include \"x/fix.h\"\n"
208 "#include \"c.h\"\n"
209 "#include \"d.h\"\n"
210 "#include \"e.h\"\n"
211 "#include \"f.h\"\n"
212 "#include <memory>\n"
213 "#include <vector>\n"
214 "#include \"m.h\"\n"
215 "#include \"n.h\"\n";
216 EXPECT_EQ(Expected, insert(Code, "\"d.h\""));
217 }
218
219 TEST_F(HeaderIncludesTest, InsertIntoSystemBlockSorted) {
220 std::string Code = "#include \"x/fix.h\"\n"
221 "#include \"a.h\"\n"
222 "#include \"c.h\"\n"
223 "#include <a>\n"
224 "#include <z>\n";
225 std::string Expected = "#include \"x/fix.h\"\n"
226 "#include \"a.h\"\n"
227 "#include \"c.h\"\n"
228 "#include <a>\n"
229 "#include <vector>\n"
230 "#include <z>\n";
231 EXPECT_EQ(Expected, insert(Code, "<vector>"));
232 }
233
234 TEST_F(HeaderIncludesTest, InsertNewSystemIncludeGoogleStyle) {
235 std::string Code = "#include \"x/fix.h\"\n"
236 "\n"
237 "#include \"y/a.h\"\n"
238 "#include \"z/b.h\"\n";
239 // FIXME: inserting after the empty line following the main header might be
240 // preferred.
241 std::string Expected = "#include \"x/fix.h\"\n"
242 "#include <vector>\n"
243 "\n"
244 "#include \"y/a.h\"\n"
245 "#include \"z/b.h\"\n";
246 Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
247 .IncludeStyle;
248 EXPECT_EQ(Expected, insert(Code, "<vector>"));
249 }
250
251 TEST_F(HeaderIncludesTest, NotConfusedByDefine) {
252 std::string Code = "void f() {}\n"
253 "#define A \\\n"
254 " int i;";
255 std::string Expected = "#include <vector>\n"
256 "void f() {}\n"
257 "#define A \\\n"
258 " int i;";
259 EXPECT_EQ(Expected, insert(Code, "<vector>"));
260 }
261
262 TEST_F(HeaderIncludesTest, SkippedTopComment) {
263 std::string Code = "// comment\n"
264 "\n"
265 " // comment\n";
266 std::string Expected = "// comment\n"
267 "\n"
268 " // comment\n"
269 "#include <vector>\n";
270 EXPECT_EQ(Expected, insert(Code, "<vector>"));
271 }
272
273 TEST_F(HeaderIncludesTest, SkippedMixedComments) {
274 std::string Code = "// comment\n"
275 "// comment \\\n"
276 " comment continued\n"
277 "/*\n"
278 "* comment\n"
279 "*/\n";
280 std::string Expected = "// comment\n"
281 "// comment \\\n"
282 " comment continued\n"
283 "/*\n"
284 "* comment\n"
285 "*/\n"
286 "#include <vector>\n";
287 EXPECT_EQ(Expected, insert(Code, "<vector>"));
288 }
289
290 TEST_F(HeaderIncludesTest, MultipleBlockCommentsInOneLine) {
291 std::string Code = "/*\n"
292 "* comment\n"
293 "*/ /* comment\n"
294 "*/\n"
295 "\n\n"
296 "/* c1 */ /*c2 */\n";
297 std::string Expected = "/*\n"
298 "* comment\n"
299 "*/ /* comment\n"
300 "*/\n"
301 "\n\n"
302 "/* c1 */ /*c2 */\n"
303 "#include <vector>\n";
304 EXPECT_EQ(Expected, insert(Code, "<vector>"));
305 }
306
307 TEST_F(HeaderIncludesTest, CodeAfterComments) {
308 std::string Code = "/*\n"
309 "* comment\n"
310 "*/ /* comment\n"
311 "*/\n"
312 "\n\n"
313 "/* c1 */ /*c2 */\n"
314 "\n"
315 "int x;\n";
316 std::string Expected = "/*\n"
317 "* comment\n"
318 "*/ /* comment\n"
319 "*/\n"
320 "\n\n"
321 "/* c1 */ /*c2 */\n"
322 "\n"
323 "#include <vector>\n"
324 "int x;\n";
325 EXPECT_EQ(Expected, insert(Code, "<vector>"));
326 }
327
328 TEST_F(HeaderIncludesTest, FakeHeaderGuardIfDef) {
329 std::string Code = "// comment \n"
330 "#ifdef X\n"
331 "#define X\n";
332 std::string Expected = "// comment \n"
333 "#include <vector>\n"
334 "#ifdef X\n"
335 "#define X\n";
336 EXPECT_EQ(Expected, insert(Code, "<vector>"));
337 }
338
339 TEST_F(HeaderIncludesTest, RealHeaderGuardAfterComments) {
340 std::string Code = "// comment \n"
341 "#ifndef X\n"
342 "#define X\n"
343 "int x;\n"
344 "#define Y 1\n";
345 std::string Expected = "// comment \n"
346 "#ifndef X\n"
347 "#define X\n"
348 "#include <vector>\n"
349 "int x;\n"
350 "#define Y 1\n";
351 EXPECT_EQ(Expected, insert(Code, "<vector>"));
352 }
353
354 TEST_F(HeaderIncludesTest, PragmaOnce) {
355 std::string Code = "// comment \n"
356 "#pragma once\n"
357 "int x;\n";
358 std::string Expected = "// comment \n"
359 "#pragma once\n"
360 "#include <vector>\n"
361 "int x;\n";
362 EXPECT_EQ(Expected, insert(Code, "<vector>"));
363 }
364
365 TEST_F(HeaderIncludesTest, IfNDefWithNoDefine) {
366 std::string Code = "// comment \n"
367 "#ifndef X\n"
368 "int x;\n"
369 "#define Y 1\n";
370 std::string Expected = "// comment \n"
371 "#include <vector>\n"
372 "#ifndef X\n"
373 "int x;\n"
374 "#define Y 1\n";
375 EXPECT_EQ(Expected, insert(Code, "<vector>"));
376 }
377
378 TEST_F(HeaderIncludesTest, FakeHeaderGuard) {
379 std::string Code = "// comment \n"
380 "#ifndef X\n"
381 "#define 1\n";
382 std::string Expected = "// comment \n"
383 "#include <vector>\n"
384 "#ifndef X\n"
385 "#define 1\n";
386 EXPECT_EQ(Expected, insert(Code, "<vector>"));
387 }
388
389 TEST_F(HeaderIncludesTest, FakeHeaderGuardIfnDef) {
390 std::string Code = "#ifndef A_H\n"
391 "#define A_H 1\n"
392 "#endif";
393 std::string Expected = "#include \"b.h\"\n"
394 "#ifndef A_H\n"
395 "#define A_H 1\n"
396 "#endif";
397
398 EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
399 }
400
401 TEST_F(HeaderIncludesTest, HeaderGuardWithComment) {
402 std::string Code = "// comment \n"
403 "#ifndef X // comment\n"
404 "// comment\n"
405 "/* comment\n"
406 "*/\n"
407 "/* comment */ #define X\n"
408 "int x;\n"
409 "#define Y 1\n";
410 std::string Expected = "// comment \n"
411 "#ifndef X // comment\n"
412 "// comment\n"
413 "/* comment\n"
414 "*/\n"
415 "/* comment */ #define X\n"
416 "#include <vector>\n"
417 "int x;\n"
418 "#define Y 1\n";
419 EXPECT_EQ(Expected, insert(Code, "<vector>"));
420 }
421
422 TEST_F(HeaderIncludesTest, EmptyCode) {
423 std::string Code = "";
424 std::string Expected = "#include <vector>\n";
425 EXPECT_EQ(Expected, insert(Code, "<vector>"));
426 }
427
428 TEST_F(HeaderIncludesTest, NoNewLineAtTheEndOfCode) {
429 std::string Code = "#include <map>";
430 std::string Expected = "#include <map>\n#include <vector>\n";
431 EXPECT_EQ(Expected, insert(Code, "<vector>"));
432 }
433
434 TEST_F(HeaderIncludesTest, SkipExistingHeaders) {
435 std::string Code = "#include \"a.h\"\n"
436 "#include <vector>\n";
437 std::string Expected = "#include \"a.h\"\n"
438 "#include <vector>\n";
439 EXPECT_EQ(Expected, insert(Code, "<vector>"));
440 EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
441 }
442
443 TEST_F(HeaderIncludesTest, AddIncludesWithDifferentForms) {
444 std::string Code = "#include <vector>\n";
445 // FIXME: this might not be the best behavior.
446 std::string Expected = "#include \"vector\"\n"
447 "#include <vector>\n";
448 EXPECT_EQ(Expected, insert(Code, "\"vector\""));
449 }
450
451 TEST_F(HeaderIncludesTest, NoInsertionAfterCode) {
452 std::string Code = "#include \"a.h\"\n"
453 "void f() {}\n"
454 "#include \"b.h\"\n";
455 std::string Expected = "#include \"a.h\"\n"
456 "#include \"c.h\"\n"
457 "void f() {}\n"
458 "#include \"b.h\"\n";
459 EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
460 }
461
462 TEST_F(HeaderIncludesTest, NoInsertionInStringLiteral) {
463 std::string Code = "#include \"a.h\"\n"
464 "const char[] = R\"(\n"
465 "#include \"b.h\"\n"
466 ")\";\n";
467 std::string Expected = "#include \"a.h\"\n"
468 "#include \"c.h\"\n"
469 "const char[] = R\"(\n"
470 "#include \"b.h\"\n"
471 ")\";\n";
472 EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
473 }
474
475 TEST_F(HeaderIncludesTest, NoInsertionAfterOtherDirective) {
476 std::string Code = "#include \"a.h\"\n"
477 "#ifdef X\n"
478 "#include \"b.h\"\n"
479 "#endif\n";
480 std::string Expected = "#include \"a.h\"\n"
481 "#include \"c.h\"\n"
482 "#ifdef X\n"
483 "#include \"b.h\"\n"
484 "#endif\n";
485 EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
486 }
487
488 TEST_F(HeaderIncludesTest, CanInsertAfterLongSystemInclude) {
489 std::string Code = "#include \"a.h\"\n"
490 "// comment\n\n"
491 "#include <a/b/c/d/e.h>\n";
492 std::string Expected = "#include \"a.h\"\n"
493 "// comment\n\n"
494 "#include <a/b/c/d/e.h>\n"
495 "#include <x.h>\n";
496 EXPECT_EQ(Expected, insert(Code, "<x.h>"));
497 }
498
499 TEST_F(HeaderIncludesTest, CanInsertAfterComment) {
500 std::string Code = "#include \"a.h\"\n"
501 "// Comment\n"
502 "\n"
503 "/* Comment */\n"
504 "// Comment\n"
505 "\n"
506 "#include \"b.h\"\n";
507 std::string Expected = "#include \"a.h\"\n"
508 "// Comment\n"
509 "\n"
510 "/* Comment */\n"
511 "// Comment\n"
512 "\n"
513 "#include \"b.h\"\n"
514 "#include \"c.h\"\n";
515 EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
516 }
517
518 TEST_F(HeaderIncludesTest, LongCommentsInTheBeginningOfFile) {
519 std::string Code = "// Loooooooooooooooooooooooooong comment\n"
520 "// Loooooooooooooooooooooooooong comment\n"
521 "// Loooooooooooooooooooooooooong comment\n"
522 "#include <string>\n"
523 "#include <vector>\n"
524 "\n"
525 "#include \"a.h\"\n"
526 "#include \"b.h\"\n";
527 std::string Expected = "// Loooooooooooooooooooooooooong comment\n"
528 "// Loooooooooooooooooooooooooong comment\n"
529 "// Loooooooooooooooooooooooooong comment\n"
530 "#include <string>\n"
531 "#include <vector>\n"
532 "\n"
533 "#include \"a.h\"\n"
534 "#include \"b.h\"\n"
535 "#include \"third.h\"\n";
536 Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
537 .IncludeStyle;
538 EXPECT_EQ(Expected, insert(Code, "\"third.h\""));
539 }
540
541 TEST_F(HeaderIncludesTest, SimpleDeleteInclude) {
542 std::string Code = "#include \"abc.h\"\n"
543 "#include \"xyz.h\" // comment\n"
544 "int x;\n";
545 std::string Expected = "#include \"abc.h\"\n"
546 "int x;\n";
547 EXPECT_EQ(Expected, remove(Code, "\"xyz.h\""));
548 }
549
550 TEST_F(HeaderIncludesTest, DeleteQuotedOnly) {
551 std::string Code = "#include \"abc.h\"\n"
552 "#include <abc.h>\n"
553 "int x;\n";
554 std::string Expected = "#include <abc.h>\n"
555 "int x;\n";
556 EXPECT_EQ(Expected, remove(Code, "\"abc.h\""));
557 }
558
559 TEST_F(HeaderIncludesTest, DeleteAllCode) {
560 std::string Code = "#include \"xyz.h\"\n";
561 std::string Expected = "";
562 EXPECT_EQ(Expected, remove(Code, "\"xyz.h\""));
563 }
564
565 TEST_F(HeaderIncludesTest, DeleteOnlyIncludesWithSameQuote) {
566 std::string Code = "#include \"xyz.h\"\n"
567 "#include \"xyz\"\n"
568 "#include <xyz.h>\n";
569 std::string Expected = "#include \"xyz.h\"\n"
570 "#include \"xyz\"\n";
571 EXPECT_EQ(Expected, remove(Code, "<xyz.h>"));
572 }
573
574 TEST_F(HeaderIncludesTest, CanDeleteAfterCode) {
575 std::string Code = "#include \"a.h\"\n"
576 "void f() {}\n"
577 "#include \"b.h\"\n";
578 std::string Expected = "#include \"a.h\"\n"
579 "void f() {}\n";
580 EXPECT_EQ(Expected, remove(Code, "\"b.h\""));
581 }
582
583 } // namespace
584 } // namespace tooling
585 } // namespace clang