Debian Codegraph @

clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp

raw
1 //===--- AnalysisTest.cpp -------------------------------------------------===//
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-include-cleaner/Analysis.h"
10 #include "AnalysisInternal.h"
11 #include "TypesInternal.h"
12 #include "clang-include-cleaner/Record.h"
13 #include "clang-include-cleaner/Types.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/DeclBase.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Format/Format.h"
21 #include "clang/Frontend/FrontendActions.h"
22 #include "clang/Testing/TestAST.h"
23 #include "clang/Tooling/Inclusions/StandardLibrary.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/Support/ScopedPrinter.h"
28 #include "llvm/Testing/Annotations/Annotations.h"
29 #include "gmock/gmock.h"
30 #include "gtest/gtest.h"
31 #include <cstddef>
32 #include <map>
33 #include <memory>
34 #include <string>
35 #include <vector>
36
37 namespace clang::include_cleaner {
38 namespace {
39 using testing::AllOf;
40 using testing::Contains;
41 using testing::ElementsAre;
42 using testing::Pair;
43 using testing::UnorderedElementsAre;
44
45 std::string guard(llvm::StringRef Code) {
46 return "#pragma once\n" + Code.str();
47 }
48
49 class WalkUsedTest : public testing::Test {
50 protected:
51 TestInputs Inputs;
52 PragmaIncludes PI;
53 WalkUsedTest() {
54 Inputs.MakeAction = [this] {
55 struct Hook : public SyntaxOnlyAction {
56 public:
57 Hook(PragmaIncludes *Out) : Out(Out) {}
58 bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
59 Out->record(CI);
60 return true;
61 }
62
63 PragmaIncludes *Out;
64 };
65 return std::make_unique<Hook>(&PI);
66 };
67 }
68
69 std::multimap<size_t, std::vector<Header>>
70 offsetToProviders(TestAST &AST,
71 llvm::ArrayRef<SymbolReference> MacroRefs = {}) {
72 const auto &SM = AST.sourceManager();
73 llvm::SmallVector<Decl *> TopLevelDecls;
74 for (Decl *D : AST.context().getTranslationUnitDecl()->decls()) {
75 if (!SM.isWrittenInMainFile(SM.getExpansionLoc(D->getLocation())))
76 continue;
77 TopLevelDecls.emplace_back(D);
78 }
79 std::multimap<size_t, std::vector<Header>> OffsetToProviders;
80 walkUsed(TopLevelDecls, MacroRefs, &PI, AST.preprocessor(),
81 [&](const SymbolReference &Ref, llvm::ArrayRef<Header> Providers) {
82 auto [FID, Offset] = SM.getDecomposedLoc(Ref.RefLocation);
83 if (FID != SM.getMainFileID())
84 ADD_FAILURE() << "Reference outside of the main file!";
85 OffsetToProviders.emplace(Offset, Providers.vec());
86 });
87 return OffsetToProviders;
88 }
89 };
90
91 TEST_F(WalkUsedTest, Basic) {
92 llvm::Annotations Code(R"cpp(
93 #include "header.h"
94 #include "private.h"
95
96 // No reference reported for the Parameter "p".
97 void $bar^bar($private^Private p) {
98 $foo^foo();
99 std::$vector^vector $vconstructor^$v^v;
100 $builtin^__builtin_popcount(1);
101 std::$move^move(3);
102 }
103 )cpp");
104 Inputs.Code = Code.code();
105 Inputs.ExtraFiles["header.h"] = guard(R"cpp(
106 void foo();
107 namespace std { class vector {}; int&& move(int&&); }
108 )cpp");
109 Inputs.ExtraFiles["private.h"] = guard(R"cpp(
110 // IWYU pragma: private, include "path/public.h"
111 class Private {};
112 )cpp");
113
114 TestAST AST(Inputs);
115 auto &SM = AST.sourceManager();
116 auto HeaderFile = Header(*AST.fileManager().getOptionalFileRef("header.h"));
117 auto PrivateFile = Header(*AST.fileManager().getOptionalFileRef("private.h"));
118 auto PublicFile = Header("\"path/public.h\"");
119 auto MainFile = Header(*SM.getFileEntryRefForID(SM.getMainFileID()));
120 auto VectorSTL = Header(*tooling::stdlib::Header::named("<vector>"));
121 auto UtilitySTL = Header(*tooling::stdlib::Header::named("<utility>"));
122 EXPECT_THAT(
123 offsetToProviders(AST),
124 UnorderedElementsAre(
125 Pair(Code.point("bar"), UnorderedElementsAre(MainFile)),
126 Pair(Code.point("private"),
127 UnorderedElementsAre(PublicFile, PrivateFile)),
128 Pair(Code.point("foo"), UnorderedElementsAre(HeaderFile)),
129 Pair(Code.point("vector"), UnorderedElementsAre(VectorSTL)),
130 Pair(Code.point("vconstructor"), UnorderedElementsAre(VectorSTL)),
131 Pair(Code.point("v"), UnorderedElementsAre(MainFile)),
132 Pair(Code.point("builtin"), testing::IsEmpty()),
133 Pair(Code.point("move"), UnorderedElementsAre(UtilitySTL))));
134 }
135
136 TEST_F(WalkUsedTest, MultipleProviders) {
137 llvm::Annotations Code(R"cpp(
138 #include "header1.h"
139 #include "header2.h"
140 void foo();
141
142 void bar() {
143 $foo^foo();
144 }
145 )cpp");
146 Inputs.Code = Code.code();
147 Inputs.ExtraFiles["header1.h"] = guard(R"cpp(
148 void foo();
149 )cpp");
150 Inputs.ExtraFiles["header2.h"] = guard(R"cpp(
151 void foo();
152 )cpp");
153
154 TestAST AST(Inputs);
155 auto &SM = AST.sourceManager();
156 auto HeaderFile1 = Header(*AST.fileManager().getOptionalFileRef("header1.h"));
157 auto HeaderFile2 = Header(*AST.fileManager().getOptionalFileRef("header2.h"));
158 auto MainFile = Header(*SM.getFileEntryRefForID(SM.getMainFileID()));
159 EXPECT_THAT(
160 offsetToProviders(AST),
161 Contains(Pair(Code.point("foo"),
162 UnorderedElementsAre(HeaderFile1, HeaderFile2, MainFile))));
163 }
164
165 TEST_F(WalkUsedTest, MacroRefs) {
166 llvm::Annotations Code(R"cpp(
167 #include "hdr.h"
168 int $3^x = $1^ANSWER;
169 int $4^y = $2^ANSWER;
170 )cpp");
171 llvm::Annotations Hdr(guard("#define ^ANSWER 42"));
172 Inputs.Code = Code.code();
173 Inputs.ExtraFiles["hdr.h"] = Hdr.code();
174 TestAST AST(Inputs);
175 auto &SM = AST.sourceManager();
176 auto &PP = AST.preprocessor();
177 auto HdrFile = *SM.getFileManager().getOptionalFileRef("hdr.h");
178 auto MainFile = Header(*SM.getFileEntryRefForID(SM.getMainFileID()));
179
180 auto HdrID = SM.translateFile(HdrFile);
181
182 Symbol Answer1 = Macro{PP.getIdentifierInfo("ANSWER"),
183 SM.getComposedLoc(HdrID, Hdr.point())};
184 Symbol Answer2 = Macro{PP.getIdentifierInfo("ANSWER"),
185 SM.getComposedLoc(HdrID, Hdr.point())};
186 EXPECT_THAT(
187 offsetToProviders(
188 AST,
189 {SymbolReference{
190 Answer1, SM.getComposedLoc(SM.getMainFileID(), Code.point("1")),
191 RefType::Explicit},
192 SymbolReference{
193 Answer2, SM.getComposedLoc(SM.getMainFileID(), Code.point("2")),
194 RefType::Explicit}}),
195 UnorderedElementsAre(
196 Pair(Code.point("1"), UnorderedElementsAre(HdrFile)),
197 Pair(Code.point("2"), UnorderedElementsAre(HdrFile)),
198 Pair(Code.point("3"), UnorderedElementsAre(MainFile)),
199 Pair(Code.point("4"), UnorderedElementsAre(MainFile))));
200 }
201
202 class AnalyzeTest : public testing::Test {
203 protected:
204 TestInputs Inputs;
205 PragmaIncludes PI;
206 RecordedPP PP;
207 AnalyzeTest() {
208 Inputs.MakeAction = [this] {
209 struct Hook : public SyntaxOnlyAction {
210 public:
211 Hook(RecordedPP &PP, PragmaIncludes &PI) : PP(PP), PI(PI) {}
212 bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
213 CI.getPreprocessor().addPPCallbacks(PP.record(CI.getPreprocessor()));
214 PI.record(CI);
215 return true;
216 }
217
218 RecordedPP &PP;
219 PragmaIncludes &PI;
220 };
221 return std::make_unique<Hook>(PP, PI);
222 };
223 }
224 };
225
226 TEST_F(AnalyzeTest, Basic) {
227 Inputs.Code = R"cpp(
228 #include "a.h"
229 #include "b.h"
230 #include "keep.h" // IWYU pragma: keep
231
232 int x = a + c;
233 )cpp";
234 Inputs.ExtraFiles["a.h"] = guard("int a;");
235 Inputs.ExtraFiles["b.h"] = guard(R"cpp(
236 #include "c.h"
237 int b;
238 )cpp");
239 Inputs.ExtraFiles["c.h"] = guard("int c;");
240 Inputs.ExtraFiles["keep.h"] = guard("");
241 TestAST AST(Inputs);
242 auto Decls = AST.context().getTranslationUnitDecl()->decls();
243 auto Results =
244 analyze(std::vector<Decl *>{Decls.begin(), Decls.end()},
245 PP.MacroReferences, PP.Includes, &PI, AST.preprocessor());
246
247 const Include *B = PP.Includes.atLine(3);
248 ASSERT_EQ(B->Spelled, "b.h");
249 EXPECT_THAT(Results.Missing, ElementsAre("\"c.h\""));
250 EXPECT_THAT(Results.Unused, ElementsAre(B));
251 }
252
253 TEST_F(AnalyzeTest, PrivateUsedInPublic) {
254 // Check that umbrella header uses private include.
255 Inputs.Code = R"cpp(#include "private.h")cpp";
256 Inputs.ExtraFiles["private.h"] =
257 guard("// IWYU pragma: private, include \"public.h\"");
258 Inputs.FileName = "public.h";
259 TestAST AST(Inputs);
260 EXPECT_FALSE(PP.Includes.all().empty());
261 auto Results = analyze({}, {}, PP.Includes, &PI, AST.preprocessor());
262 EXPECT_THAT(Results.Unused, testing::IsEmpty());
263 }
264
265 TEST_F(AnalyzeTest, NoCrashWhenUnresolved) {
266 // Check that umbrella header uses private include.
267 Inputs.Code = R"cpp(#include "not_found.h")cpp";
268 Inputs.ErrorOK = true;
269 TestAST AST(Inputs);
270 EXPECT_FALSE(PP.Includes.all().empty());
271 auto Results = analyze({}, {}, PP.Includes, &PI, AST.preprocessor());
272 EXPECT_THAT(Results.Unused, testing::IsEmpty());
273 }
274
275 TEST_F(AnalyzeTest, ResourceDirIsIgnored) {
276 Inputs.ExtraArgs.push_back("-resource-dir");
277 Inputs.ExtraArgs.push_back("resources");
278 Inputs.ExtraArgs.push_back("-internal-isystem");
279 Inputs.ExtraArgs.push_back("resources/include");
280 Inputs.Code = R"cpp(
281 #include <amintrin.h>
282 #include <imintrin.h>
283 void baz() {
284 bar();
285 }
286 )cpp";
287 Inputs.ExtraFiles["resources/include/amintrin.h"] = guard("");
288 Inputs.ExtraFiles["resources/include/emintrin.h"] = guard(R"cpp(
289 void bar();
290 )cpp");
291 Inputs.ExtraFiles["resources/include/imintrin.h"] = guard(R"cpp(
292 #include <emintrin.h>
293 )cpp");
294 TestAST AST(Inputs);
295 auto Results = analyze({}, {}, PP.Includes, &PI, AST.preprocessor());
296 EXPECT_THAT(Results.Unused, testing::IsEmpty());
297 EXPECT_THAT(Results.Missing, testing::IsEmpty());
298 }
299
300 TEST_F(AnalyzeTest, DifferentHeaderSameSpelling) {
301 Inputs.ExtraArgs.push_back("-Ifoo");
302 Inputs.ExtraArgs.push_back("-Ifoo_inner");
303 // `foo` is declared in foo_inner/foo.h, but there's no way to spell it
304 // directly. Make sure we don't generate unusued/missing include findings in
305 // such cases.
306 Inputs.Code = R"cpp(
307 #include <foo.h>
308 void baz() {
309 foo();
310 }
311 )cpp";
312 Inputs.ExtraFiles["foo/foo.h"] = guard("#include_next <foo.h>");
313 Inputs.ExtraFiles["foo_inner/foo.h"] = guard(R"cpp(
314 void foo();
315 )cpp");
316 TestAST AST(Inputs);
317 std::vector<Decl *> DeclsInTU;
318 for (auto *D : AST.context().getTranslationUnitDecl()->decls())
319 DeclsInTU.push_back(D);
320 auto Results = analyze(DeclsInTU, {}, PP.Includes, &PI, AST.preprocessor());
321 EXPECT_THAT(Results.Unused, testing::IsEmpty());
322 EXPECT_THAT(Results.Missing, testing::IsEmpty());
323 }
324
325 TEST(FixIncludes, Basic) {
326 llvm::StringRef Code = R"cpp(#include "d.h"
327 #include "a.h"
328 #include "b.h"
329 #include <c.h>
330 )cpp";
331
332 Includes Inc;
333 Include I;
334 I.Spelled = "a.h";
335 I.Line = 2;
336 Inc.add(I);
337 I.Spelled = "b.h";
338 I.Line = 3;
339 Inc.add(I);
340 I.Spelled = "c.h";
341 I.Line = 4;
342 I.Angled = true;
343 Inc.add(I);
344
345 AnalysisResults Results;
346 Results.Missing.push_back("\"aa.h\"");
347 Results.Missing.push_back("\"ab.h\"");
348 Results.Missing.push_back("<e.h>");
349 Results.Unused.push_back(Inc.atLine(3));
350 Results.Unused.push_back(Inc.atLine(4));
351
352 EXPECT_EQ(fixIncludes(Results, "d.cc", Code, format::getLLVMStyle()),
353 R"cpp(#include "d.h"
354 #include "a.h"
355 #include "aa.h"
356 #include "ab.h"
357 #include <e.h>
358 )cpp");
359
360 Results = {};
361 Results.Missing.push_back("\"d.h\"");
362 Code = R"cpp(#include "a.h")cpp";
363 EXPECT_EQ(fixIncludes(Results, "d.cc", Code, format::getLLVMStyle()),
364 R"cpp(#include "d.h"
365 #include "a.h")cpp");
366 }
367
368 MATCHER_P3(expandedAt, FileID, Offset, SM, "") {
369 auto [ExpanedFileID, ExpandedOffset] = SM->getDecomposedExpansionLoc(arg);
370 return ExpanedFileID == FileID && ExpandedOffset == Offset;
371 }
372 MATCHER_P3(spelledAt, FileID, Offset, SM, "") {
373 auto [SpelledFileID, SpelledOffset] = SM->getDecomposedSpellingLoc(arg);
374 return SpelledFileID == FileID && SpelledOffset == Offset;
375 }
376 TEST(WalkUsed, FilterRefsNotSpelledInMainFile) {
377 // Each test is expected to have a single expected ref of `target` symbol
378 // (or have none).
379 // The location in the reported ref is a macro location. $expand points to
380 // the macro location, and $spell points to the spelled location.
381 struct {
382 llvm::StringRef Header;
383 llvm::StringRef Main;
384 } TestCases[] = {
385 // Tests for decl references.
386 {
387 /*Header=*/"int target();",
388 R"cpp(
389 #define CALL_FUNC $spell^target()
390
391 int b = $expand^CALL_FUNC;
392 )cpp",
393 },
394 {/*Header=*/R"cpp(
395 int target();
396 #define CALL_FUNC target()
397 )cpp",
398 // No ref of `target` being reported, as it is not spelled in main file.
399 "int a = CALL_FUNC;"},
400 {
401 /*Header=*/R"cpp(
402 int target();
403 #define PLUS_ONE(X) X() + 1
404 )cpp",
405 R"cpp(
406 int a = $expand^PLUS_ONE($spell^target);
407 )cpp",
408 },
409 {
410 /*Header=*/R"cpp(
411 int target();
412 #define PLUS_ONE(X) X() + 1
413 )cpp",
414 R"cpp(
415 int a = $expand^PLUS_ONE($spell^target);
416 )cpp",
417 },
418 // Tests for macro references
419 {/*Header=*/"#define target 1",
420 R"cpp(
421 #define USE_target $spell^target
422 int b = $expand^USE_target;
423 )cpp"},
424 {/*Header=*/R"cpp(
425 #define target 1
426 #define USE_target target
427 )cpp",
428 // No ref of `target` being reported, it is not spelled in main file.
429 R"cpp(
430 int a = USE_target;
431 )cpp"},
432 };
433
434 for (const auto &T : TestCases) {
435 llvm::Annotations Main(T.Main);
436 TestInputs Inputs(Main.code());
437 Inputs.ExtraFiles["header.h"] = guard(T.Header);
438 RecordedPP Recorded;
439 Inputs.MakeAction = [&]() {
440 struct RecordAction : public SyntaxOnlyAction {
441 RecordedPP &Out;
442 RecordAction(RecordedPP &Out) : Out(Out) {}
443 bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
444 auto &PP = CI.getPreprocessor();
445 PP.addPPCallbacks(Out.record(PP));
446 return true;
447 }
448 };
449 return std::make_unique<RecordAction>(Recorded);
450 };
451 Inputs.ExtraArgs.push_back("-include");
452 Inputs.ExtraArgs.push_back("header.h");
453 TestAST AST(Inputs);
454 llvm::SmallVector<Decl *> TopLevelDecls;
455 for (Decl *D : AST.context().getTranslationUnitDecl()->decls())
456 TopLevelDecls.emplace_back(D);
457 auto &SM = AST.sourceManager();
458
459 SourceLocation RefLoc;
460 walkUsed(TopLevelDecls, Recorded.MacroReferences,
461 /*PragmaIncludes=*/nullptr, AST.preprocessor(),
462 [&](const SymbolReference &Ref, llvm::ArrayRef<Header>) {
463 if (!Ref.RefLocation.isMacroID())
464 return;
465 if (llvm::to_string(Ref.Target) == "target") {
466 ASSERT_TRUE(RefLoc.isInvalid())
467 << "Expected only one 'target' ref loc per testcase";
468 RefLoc = Ref.RefLocation;
469 }
470 });
471 FileID MainFID = SM.getMainFileID();
472 if (RefLoc.isValid()) {
473 EXPECT_THAT(RefLoc, AllOf(expandedAt(MainFID, Main.point("expand"), &SM),
474 spelledAt(MainFID, Main.point("spell"), &SM)))
475 << T.Main.str();
476 } else {
477 EXPECT_THAT(Main.points(), testing::IsEmpty());
478 }
479 }
480 }
481
482 struct Tag {
483 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Tag &T) {
484 return OS << "Anon Tag";
485 }
486 };
487 TEST(Hints, Ordering) {
488 auto Hinted = [](Hints Hints) {
489 return clang::include_cleaner::Hinted<Tag>({}, Hints);
490 };
491 EXPECT_LT(Hinted(Hints::None), Hinted(Hints::CompleteSymbol));
492 EXPECT_LT(Hinted(Hints::CompleteSymbol), Hinted(Hints::PublicHeader));
493 EXPECT_LT(Hinted(Hints::PreferredHeader), Hinted(Hints::PublicHeader));
494 EXPECT_LT(Hinted(Hints::CompleteSymbol | Hints::PreferredHeader),
495 Hinted(Hints::PublicHeader));
496 }
497
498 // Test ast traversal & redecl selection end-to-end for templates, as explicit
499 // instantiations/specializations are not redecls of the primary template. We
500 // need to make sure we're selecting the right ones.
501 TEST_F(WalkUsedTest, TemplateDecls) {
502 llvm::Annotations Code(R"cpp(
503 #include "fwd.h"
504 #include "def.h"
505 #include "partial.h"
506 template <> struct $exp_spec^Foo<char> {};
507 template struct $exp^Foo<int>;
508 $full^Foo<int> x;
509 $implicit^Foo<bool> y;
510 $partial^Foo<int*> z;
511 )cpp");
512 Inputs.Code = Code.code();
513 Inputs.ExtraFiles["fwd.h"] = guard("template<typename> struct Foo;");
514 Inputs.ExtraFiles["def.h"] = guard("template<typename> struct Foo {};");
515 Inputs.ExtraFiles["partial.h"] =
516 guard("template<typename T> struct Foo<T*> {};");
517 TestAST AST(Inputs);
518 auto &SM = AST.sourceManager();
519 auto Fwd = *SM.getFileManager().getOptionalFileRef("fwd.h");
520 auto Def = *SM.getFileManager().getOptionalFileRef("def.h");
521 auto Partial = *SM.getFileManager().getOptionalFileRef("partial.h");
522
523 EXPECT_THAT(
524 offsetToProviders(AST),
525 AllOf(Contains(
526 Pair(Code.point("exp_spec"), UnorderedElementsAre(Fwd, Def))),
527 Contains(Pair(Code.point("exp"), UnorderedElementsAre(Fwd, Def))),
528 Contains(Pair(Code.point("full"), UnorderedElementsAre(Fwd, Def))),
529 Contains(
530 Pair(Code.point("implicit"), UnorderedElementsAre(Fwd, Def))),
531 Contains(
532 Pair(Code.point("partial"), UnorderedElementsAre(Partial)))));
533 }
534
535 TEST_F(WalkUsedTest, IgnoresIdentityMacros) {
536 llvm::Annotations Code(R"cpp(
537 #include "header.h"
538 void $bar^bar() {
539 $stdin^stdin();
540 }
541 )cpp");
542 Inputs.Code = Code.code();
543 Inputs.ExtraFiles["header.h"] = guard(R"cpp(
544 #include "inner.h"
545 void stdin();
546 )cpp");
547 Inputs.ExtraFiles["inner.h"] = guard(R"cpp(
548 #define stdin stdin
549 )cpp");
550
551 TestAST AST(Inputs);
552 auto &SM = AST.sourceManager();
553 auto MainFile = Header(*SM.getFileEntryRefForID(SM.getMainFileID()));
554 EXPECT_THAT(offsetToProviders(AST),
555 UnorderedElementsAre(
556 // FIXME: we should have a reference from stdin to header.h
557 Pair(Code.point("bar"), UnorderedElementsAre(MainFile))));
558 }
559 } // namespace
560 } // namespace clang::include_cleaner