Debian Codegraph @

llvm-project/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp

raw
1 //===--- IncludeCleanerTests.cpp --------------------------------*- C++ -*-===//
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 "Annotations.h"
10 #include "Config.h"
11 #include "IncludeCleaner.h"
12 #include "SourceCode.h"
13 #include "TestFS.h"
14 #include "TestTU.h"
15 #include "support/Context.h"
16 #include "llvm/ADT/ScopeExit.h"
17 #include "llvm/Testing/Support/SupportHelpers.h"
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20
21 namespace clang {
22 namespace clangd {
23 namespace {
24
25 using ::testing::ElementsAre;
26 using ::testing::ElementsAreArray;
27 using ::testing::IsEmpty;
28 using ::testing::Pointee;
29 using ::testing::UnorderedElementsAre;
30
31 std::string guard(llvm::StringRef Code) {
32 return "#pragma once\n" + Code.str();
33 }
34
35 TEST(IncludeCleaner, ReferencedLocations) {
36 struct TestCase {
37 std::string HeaderCode;
38 std::string MainCode;
39 };
40 TestCase Cases[] = {
41 // DeclRefExpr
42 {
43 "int ^x();",
44 "int y = x();",
45 },
46 // RecordDecl
47 {
48 "class ^X;",
49 "X *y;",
50 },
51 // When definition is available, we don't need to mark forward
52 // declarations as used.
53 {
54 "class ^X {}; class X;",
55 "X *y;",
56 },
57 // We already have forward declaration in the main file, the other
58 // non-definition declarations are not needed.
59 {
60 "class ^X {}; class X;",
61 "class X; X *y;",
62 },
63 // Nested class definition can occur outside of the parent class
64 // definition. Bar declaration should be visible to its definition but
65 // it will always be because we will mark Foo definition as used.
66 {
67 "class ^Foo { class Bar; };",
68 "class Foo::Bar {};",
69 },
70 // TypedefType and UsingDecls
71 {
72 "using ^Integer = int;",
73 "Integer x;",
74 },
75 {
76 "namespace ns { void ^foo(); void ^foo() {} }",
77 "using ns::foo;",
78 },
79 {
80 "namespace ns { void foo(); void foo() {}; }",
81 "using namespace ns;",
82 },
83 {
84 // Refs from UsingTypeLoc and implicit constructor!
85 "struct ^A {}; using B = A; using ^C = B;",
86 "C a;",
87 },
88 {"namespace ns { template<typename T> class A {}; } using ns::^A;",
89 "A<int>* a;"},
90 {"namespace ns { template<typename T> class A {}; } using ns::^A;",
91 R"cpp(
92 template <template <typename> class T> class X {};
93 X<A> x;
94 )cpp"},
95 {R"cpp(
96 namespace ns { template<typename T> class A {}; }
97 namespace absl {using ns::^A;}
98 )cpp",
99 R"cpp(
100 template <template <typename> class T> class X {};
101 X<absl::A> x;
102 )cpp"},
103 {R"cpp(
104 namespace ns { template<typename T> struct ^A { ^A(T); }; }
105 using ns::^A;
106 )cpp",
107 "A CATD(123);"},
108 {
109 "typedef bool ^Y; template <typename T> struct ^X {};",
110 "X<Y> x;",
111 },
112 {
113 // https://github.com/clangd/clangd/issues/1036
114 R"cpp(
115 struct ^Base { void ^base(); };
116 template <int> struct ^Derived : Base {};
117 )cpp",
118 R"cpp(
119 class Holder {
120 void foo() { Member.base(); }
121 Derived<0> Member;
122 };
123 )cpp",
124 },
125 {
126 "struct Foo; struct ^Foo{}; typedef Foo ^Bar;",
127 "Bar b;",
128 },
129 {
130 "namespace ns { class X; }; using ns::^X;",
131 "X *y;",
132 },
133 // MemberExpr
134 {
135 "struct ^X{int ^a;}; X ^foo();",
136 "int y = foo().a;",
137 },
138 // Expr (type is traversed)
139 {
140 "class ^X{}; X ^foo();",
141 "auto bar() { return foo(); }",
142 },
143 // Redecls
144 {
145 "void ^foo(); void ^foo() {} void ^foo();",
146 "void bar() { foo(); }",
147 },
148 // Constructor
149 {
150 "struct ^X { ^X(int) {} int ^foo(); };",
151 "auto x = X(42); auto y = x.foo();",
152 },
153 // Function
154 {
155 "void ^foo();",
156 "void foo() {}",
157 },
158 {
159 "void foo() {}",
160 "void foo();",
161 },
162 {
163 "inline void ^foo() {}",
164 "void bar() { foo(); }",
165 },
166 {
167 "int ^foo(char); int ^foo(float);",
168 "template<class T> int x = foo(T{});",
169 },
170 // Static function
171 {
172 "struct ^X { static bool ^foo(); }; bool X::^foo() {}",
173 "auto b = X::foo();",
174 },
175 // TemplateRecordDecl
176 {
177 "template <typename> class ^X;",
178 "X<int> *y;",
179 },
180 // Type name not spelled out in code
181 {
182 "class ^X{}; X ^getX();",
183 "auto x = getX();",
184 },
185 // Enums
186 {
187 "enum ^Color { ^Red = 42, Green = 9000};",
188 "int MyColor = Red;",
189 },
190 {
191 "struct ^X { enum ^Language { ^CXX = 42, Python = 9000}; };",
192 "int Lang = X::CXX;",
193 },
194 // Macros
195 {
196 "#define ^CONSTANT 42",
197 "int Foo = CONSTANT;",
198 },
199 {
200 "#define ^FOO x",
201 "#define BAR FOO",
202 },
203 {
204 "#define INNER 42\n"
205 "#define ^OUTER INNER",
206 "int answer = OUTER;",
207 },
208 {
209 "#define ^ANSWER 42\n"
210 "#define ^SQUARE(X) X * X",
211 "int sq = SQUARE(ANSWER);",
212 },
213 {
214 "#define ^FOO\n"
215 "#define ^BAR",
216 "#if 0\n"
217 "#if FOO\n"
218 "BAR\n"
219 "#endif\n"
220 "#endif",
221 },
222 // Misc
223 {
224 "enum class ^Color : int;",
225 "enum class Color : int {};",
226 },
227 {
228 "enum class Color : int {};",
229 "enum class Color : int;",
230 },
231 {
232 "enum class ^Color;",
233 "Color c;",
234 },
235 {
236 "enum class ^Color : int;",
237 "Color c;",
238 },
239 {
240 "enum class ^Color : char;",
241 "Color *c;",
242 },
243 {
244 "enum class ^Color : char {};",
245 "Color *c;",
246 },
247 };
248 for (const TestCase &T : Cases) {
249 TestTU TU;
250 TU.Code = T.MainCode;
251 Annotations Header(T.HeaderCode);
252 TU.HeaderCode = Header.code().str();
253 TU.ExtraArgs.push_back("-std=c++17");
254 auto AST = TU.build();
255
256 std::vector<Position> Points;
257 for (const auto &Loc : findReferencedLocations(AST).User) {
258 if (AST.getSourceManager().getBufferName(Loc).endswith(
259 TU.HeaderFilename)) {
260 Points.push_back(offsetToPosition(
261 TU.HeaderCode, AST.getSourceManager().getFileOffset(Loc)));
262 }
263 }
264 llvm::sort(Points);
265
266 EXPECT_EQ(Points, Header.points()) << T.HeaderCode << "\n---\n"
267 << T.MainCode;
268 }
269 }
270
271 TEST(IncludeCleaner, Stdlib) {
272 // Smoke tests only for finding used symbols/headers.
273 // Details of Decl -> stdlib::Symbol -> stdlib::Headers mapping tested there.
274 auto TU = TestTU::withHeaderCode(R"cpp(
275 namespace std { class error_code {}; }
276 class error_code {};
277 namespace nonstd { class error_code {}; }
278 )cpp");
279 struct {
280 llvm::StringRef Code;
281 std::vector<llvm::StringRef> Symbols;
282 std::vector<llvm::StringRef> Headers;
283 } Tests[] = {
284 {"std::error_code x;", {"std::error_code"}, {"<system_error>"}},
285 {"error_code x;", {}, {}},
286 {"nonstd::error_code x;", {}, {}},
287 };
288
289 for (const auto &Test : Tests) {
290 TU.Code = Test.Code.str();
291 ParsedAST AST = TU.build();
292 std::vector<tooling::stdlib::Symbol> WantSyms;
293 for (const auto &SymName : Test.Symbols) {
294 auto QName = splitQualifiedName(SymName);
295 auto Sym = tooling::stdlib::Symbol::named(QName.first, QName.second);
296 EXPECT_TRUE(Sym) << SymName;
297 WantSyms.push_back(*Sym);
298 }
299 std::vector<tooling::stdlib::Header> WantHeaders;
300 for (const auto &HeaderName : Test.Headers) {
301 auto Header = tooling::stdlib::Header::named(HeaderName);
302 EXPECT_TRUE(Header) << HeaderName;
303 WantHeaders.push_back(*Header);
304 }
305
306 ReferencedLocations Locs = findReferencedLocations(AST);
307 EXPECT_THAT(Locs.Stdlib, ElementsAreArray(WantSyms));
308 ReferencedFiles Files =
309 findReferencedFiles(Locs, AST.getIncludeStructure(),
310 AST.getCanonicalIncludes(), AST.getSourceManager());
311 EXPECT_THAT(Files.Stdlib, ElementsAreArray(WantHeaders));
312 }
313 }
314
315 MATCHER_P(writtenInclusion, Written, "") {
316 if (arg.Written != Written)
317 *result_listener << arg.Written;
318 return arg.Written == Written;
319 }
320
321 TEST(IncludeCleaner, StdlibUnused) {
322 setIncludeCleanerAnalyzesStdlib(true);
323 auto Cleanup =
324 llvm::make_scope_exit([] { setIncludeCleanerAnalyzesStdlib(false); });
325
326 auto TU = TestTU::withCode(R"cpp(
327 #include <list>
328 #include <queue>
329 std::list<int> x;
330 )cpp");
331 // Layout of std library impl is not relevant.
332 TU.AdditionalFiles["bits"] = R"cpp(
333 #pragma once
334 namespace std {
335 template <typename> class list {};
336 template <typename> class queue {};
337 }
338 )cpp";
339 TU.AdditionalFiles["list"] = "#include <bits>";
340 TU.AdditionalFiles["queue"] = "#include <bits>";
341 TU.ExtraArgs = {"-isystem", testRoot()};
342 auto AST = TU.build();
343 EXPECT_THAT(computeUnusedIncludes(AST),
344 ElementsAre(Pointee(writtenInclusion("<queue>"))));
345 EXPECT_THAT(computeUnusedIncludesExperimental(AST),
346 ElementsAre(Pointee(writtenInclusion("<queue>"))));
347 }
348
349 TEST(IncludeCleaner, GetUnusedHeaders) {
350 llvm::StringLiteral MainFile = R"cpp(
351 #include "a.h"
352 #include "b.h"
353 #include "dir/c.h"
354 #include "dir/unused.h"
355 #include "unguarded.h"
356 #include "unused.h"
357 #include <system_header.h>
358 void foo() {
359 a();
360 b();
361 c();
362 })cpp";
363 // Build expected ast with symbols coming from headers.
364 TestTU TU;
365 TU.Filename = "foo.cpp";
366 TU.AdditionalFiles["foo.h"] = guard("void foo();");
367 TU.AdditionalFiles["a.h"] = guard("void a();");
368 TU.AdditionalFiles["b.h"] = guard("void b();");
369 TU.AdditionalFiles["dir/c.h"] = guard("void c();");
370 TU.AdditionalFiles["unused.h"] = guard("void unused();");
371 TU.AdditionalFiles["dir/unused.h"] = guard("void dirUnused();");
372 TU.AdditionalFiles["system/system_header.h"] = guard("");
373 TU.AdditionalFiles["unguarded.h"] = "";
374 TU.ExtraArgs.push_back("-I" + testPath("dir"));
375 TU.ExtraArgs.push_back("-isystem" + testPath("system"));
376 TU.Code = MainFile.str();
377 ParsedAST AST = TU.build();
378 EXPECT_THAT(
379 computeUnusedIncludes(AST),
380 UnorderedElementsAre(Pointee(writtenInclusion("\"unused.h\"")),
381 Pointee(writtenInclusion("\"dir/unused.h\""))));
382 EXPECT_THAT(
383 computeUnusedIncludesExperimental(AST),
384 UnorderedElementsAre(Pointee(writtenInclusion("\"unused.h\"")),
385 Pointee(writtenInclusion("\"dir/unused.h\""))));
386 }
387
388 TEST(IncludeCleaner, VirtualBuffers) {
389 TestTU TU;
390 TU.Code = R"cpp(
391 #include "macros.h"
392
393 using flags::FLAGS_FOO;
394
395 // CLI will come from a define, __cplusplus is a built-in. In both cases, they
396 // come from non-existent files.
397 int y = CLI + __cplusplus;
398
399 int concat(a, b) = 42;
400 )cpp";
401 // The pasting operator in combination with DEFINE_FLAG will create
402 // ScratchBuffer with `flags::FLAGS_FOO` that will have FileID but not
403 // FileEntry.
404 TU.AdditionalFiles["macros.h"] = R"cpp(
405 #ifndef MACROS_H
406 #define MACROS_H
407
408 #define DEFINE_FLAG(X) \
409 namespace flags { \
410 int FLAGS_##X; \
411 } \
412
413 DEFINE_FLAG(FOO)
414
415 #define ab x
416 #define concat(x, y) x##y
417
418 #endif // MACROS_H
419 )cpp";
420 TU.ExtraArgs = {"-DCLI=42"};
421 ParsedAST AST = TU.build();
422 auto &SM = AST.getSourceManager();
423 auto &Includes = AST.getIncludeStructure();
424
425 auto ReferencedFiles = findReferencedFiles(
426 findReferencedLocations(AST), Includes, AST.getCanonicalIncludes(), SM);
427 llvm::StringSet<> ReferencedFileNames;
428 for (FileID FID : ReferencedFiles.User)
429 ReferencedFileNames.insert(
430 SM.getPresumedLoc(SM.getLocForStartOfFile(FID)).getFilename());
431 // Note we deduped the names as _number_ of <built-in>s is uninteresting.
432 EXPECT_THAT(ReferencedFileNames.keys(),
433 UnorderedElementsAre("<built-in>", "<scratch space>",
434 testPath("macros.h")));
435
436 // Should not crash due to FileIDs that are not headers.
437 auto ReferencedHeaders = translateToHeaderIDs(ReferencedFiles, Includes, SM);
438 std::vector<llvm::StringRef> ReferencedHeaderNames;
439 for (IncludeStructure::HeaderID HID : ReferencedHeaders)
440 ReferencedHeaderNames.push_back(Includes.getRealPath(HID));
441 // Non-header files are gone at this point.
442 EXPECT_THAT(ReferencedHeaderNames, ElementsAre(testPath("macros.h")));
443
444 // Sanity check.
445 EXPECT_THAT(
446 getUnused(AST, ReferencedHeaders, ReferencedFiles.SpelledUmbrellas),
447 IsEmpty());
448 }
449
450 TEST(IncludeCleaner, DistinctUnguardedInclusions) {
451 TestTU TU;
452 TU.Code = R"cpp(
453 #include "bar.h"
454 #include "foo.h"
455
456 int LocalFoo = foo::Variable;
457 )cpp";
458 TU.AdditionalFiles["foo.h"] = R"cpp(
459 #pragma once
460 namespace foo {
461 #include "unguarded.h"
462 }
463 )cpp";
464 TU.AdditionalFiles["bar.h"] = R"cpp(
465 #pragma once
466 namespace bar {
467 #include "unguarded.h"
468 }
469 )cpp";
470 TU.AdditionalFiles["unguarded.h"] = R"cpp(
471 constexpr int Variable = 42;
472 )cpp";
473
474 ParsedAST AST = TU.build();
475
476 auto ReferencedFiles = findReferencedFiles(
477 findReferencedLocations(AST), AST.getIncludeStructure(),
478 AST.getCanonicalIncludes(), AST.getSourceManager());
479 llvm::StringSet<> ReferencedFileNames;
480 auto &SM = AST.getSourceManager();
481 for (FileID FID : ReferencedFiles.User)
482 ReferencedFileNames.insert(
483 SM.getPresumedLoc(SM.getLocForStartOfFile(FID)).getFilename());
484 // Note that we have uplifted the referenced files from non self-contained
485 // headers to header-guarded ones.
486 EXPECT_THAT(ReferencedFileNames.keys(),
487 UnorderedElementsAre(testPath("foo.h")));
488 }
489
490 TEST(IncludeCleaner, NonSelfContainedHeaders) {
491 TestTU TU;
492 TU.Code = R"cpp(
493 #include "foo.h"
494
495 int LocalFoo = Variable;
496 )cpp";
497 TU.AdditionalFiles["foo.h"] = R"cpp(
498 #pragma once
499 #include "indirection.h"
500 )cpp";
501 TU.AdditionalFiles["indirection.h"] = R"cpp(
502 #include "unguarded.h"
503 )cpp";
504 TU.AdditionalFiles["unguarded.h"] = R"cpp(
505 constexpr int Variable = 42;
506 )cpp";
507
508 ParsedAST AST = TU.build();
509
510 auto ReferencedFiles = findReferencedFiles(
511 findReferencedLocations(AST), AST.getIncludeStructure(),
512 AST.getCanonicalIncludes(), AST.getSourceManager());
513 llvm::StringSet<> ReferencedFileNames;
514 auto &SM = AST.getSourceManager();
515 for (FileID FID : ReferencedFiles.User)
516 ReferencedFileNames.insert(
517 SM.getPresumedLoc(SM.getLocForStartOfFile(FID)).getFilename());
518 // Note that we have uplifted the referenced files from non self-contained
519 // headers to header-guarded ones.
520 EXPECT_THAT(ReferencedFileNames.keys(),
521 UnorderedElementsAre(testPath("foo.h")));
522 }
523
524 TEST(IncludeCleaner, IWYUPragmas) {
525 TestTU TU;
526 TU.Code = R"cpp(
527 #include "behind_keep.h" // IWYU pragma: keep
528 #include "exported.h" // IWYU pragma: export
529 #include "public.h"
530
531 void bar() { foo(); }
532 )cpp";
533 TU.AdditionalFiles["behind_keep.h"] = guard("");
534 TU.AdditionalFiles["exported.h"] = guard("");
535 TU.AdditionalFiles["public.h"] = guard("#include \"private.h\"");
536 TU.AdditionalFiles["private.h"] = guard(R"cpp(
537 // IWYU pragma: private, include "public.h"
538 void foo() {}
539 )cpp");
540 Config Cfg;
541 Cfg.Diagnostics.UnusedIncludes = Config::Experiment;
542 WithContextValue Ctx(Config::Key, std::move(Cfg));
543 ParsedAST AST = TU.build();
544
545 auto ReferencedFiles = findReferencedFiles(
546 findReferencedLocations(AST), AST.getIncludeStructure(),
547 AST.getCanonicalIncludes(), AST.getSourceManager());
548 EXPECT_EQ(ReferencedFiles.SpelledUmbrellas.size(), 1u);
549 EXPECT_EQ(ReferencedFiles.SpelledUmbrellas.begin()->getKey(), "\"public.h\"");
550 EXPECT_EQ(ReferencedFiles.User.size(), 2u);
551 EXPECT_TRUE(
552 ReferencedFiles.User.contains(AST.getSourceManager().getMainFileID()));
553 EXPECT_TRUE(
554 ReferencedFiles.User.contains(AST.getSourceManager().getMainFileID()));
555 EXPECT_THAT(AST.getDiagnostics(), llvm::ValueIs(IsEmpty()));
556 EXPECT_THAT(computeUnusedIncludes(AST), IsEmpty());
557 EXPECT_THAT(computeUnusedIncludesExperimental(AST), IsEmpty());
558 }
559
560 TEST(IncludeCleaner, RecursiveInclusion) {
561 TestTU TU;
562 TU.Code = R"cpp(
563 #include "foo.h"
564
565 void baz() {
566 foo();
567 }
568 )cpp";
569 TU.AdditionalFiles["foo.h"] = R"cpp(
570 #ifndef FOO_H
571 #define FOO_H
572
573 void foo() {}
574
575 #include "bar.h"
576
577 #endif
578 )cpp";
579 TU.AdditionalFiles["bar.h"] = guard(R"cpp(
580 #include "foo.h"
581 )cpp");
582 ParsedAST AST = TU.build();
583
584 EXPECT_THAT(AST.getDiagnostics(), llvm::ValueIs(IsEmpty()));
585 EXPECT_THAT(computeUnusedIncludes(AST), IsEmpty());
586 EXPECT_THAT(computeUnusedIncludesExperimental(AST), IsEmpty());
587 }
588
589 TEST(IncludeCleaner, IWYUPragmaExport) {
590 TestTU TU;
591 TU.Code = R"cpp(
592 #include "foo.h"
593 )cpp";
594 TU.AdditionalFiles["foo.h"] = R"cpp(
595 #ifndef FOO_H
596 #define FOO_H
597
598 #include "bar.h" // IWYU pragma: export
599
600 #endif
601 )cpp";
602 TU.AdditionalFiles["bar.h"] = guard(R"cpp(
603 void bar() {}
604 )cpp");
605 ParsedAST AST = TU.build();
606
607 EXPECT_THAT(AST.getDiagnostics(), llvm::ValueIs(IsEmpty()));
608 // FIXME: This is not correct: foo.h is unused but is not diagnosed as such
609 // because we ignore headers with IWYU export pragmas for now.
610 EXPECT_THAT(computeUnusedIncludes(AST), IsEmpty());
611 EXPECT_THAT(computeUnusedIncludesExperimental(AST), IsEmpty());
612 }
613
614 TEST(IncludeCleaner, NoDiagsForObjC) {
615 TestTU TU;
616 TU.Code = R"cpp(
617 #include "foo.h"
618
619 void bar() {}
620 )cpp";
621 TU.AdditionalFiles["foo.h"] = R"cpp(
622 #ifndef FOO_H
623 #define FOO_H
624
625 #endif
626 )cpp";
627 TU.ExtraArgs.emplace_back("-xobjective-c");
628
629 Config Cfg;
630 Cfg.Diagnostics.UnusedIncludes = Config::Strict;
631 WithContextValue Ctx(Config::Key, std::move(Cfg));
632 ParsedAST AST = TU.build();
633 EXPECT_THAT(AST.getDiagnostics(), llvm::ValueIs(IsEmpty()));
634 }
635 } // namespace
636 } // namespace clangd
637 } // namespace clang