[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / include-cleaner / unittests / RecordTest.cpp
blob36850731d5145399c00d339ef29c6045b81a8f0a
1 //===-- RecordTest.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 //===----------------------------------------------------------------------===//
9 #include "clang-include-cleaner/Record.h"
10 #include "clang-include-cleaner/Types.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/Basic/Diagnostic.h"
13 #include "clang/Basic/LLVM.h"
14 #include "clang/Basic/SourceLocation.h"
15 #include "clang/Frontend/CompilerInvocation.h"
16 #include "clang/Frontend/FrontendAction.h"
17 #include "clang/Frontend/FrontendActions.h"
18 #include "clang/Frontend/FrontendOptions.h"
19 #include "clang/Serialization/PCHContainerOperations.h"
20 #include "clang/Testing/TestAST.h"
21 #include "clang/Tooling/Inclusions/StandardLibrary.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/VirtualFileSystem.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Testing/Annotations/Annotations.h"
30 #include "gmock/gmock.h"
31 #include "gtest/gtest.h"
32 #include <cassert>
33 #include <memory>
34 #include <optional>
35 #include <utility>
37 namespace clang::include_cleaner {
38 namespace {
39 using testing::ElementsAreArray;
40 using testing::IsEmpty;
42 // Matches a Decl* if it is a NamedDecl with the given name.
43 MATCHER_P(named, N, "") {
44 if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(arg)) {
45 if (N == ND->getNameAsString())
46 return true;
48 std::string S;
49 llvm::raw_string_ostream OS(S);
50 arg->dump(OS);
51 *result_listener << S;
52 return false;
55 MATCHER_P(FileNamed, N, "") {
56 if (arg.getFileEntry().tryGetRealPathName() == N)
57 return true;
58 *result_listener << arg.getFileEntry().tryGetRealPathName().str();
59 return false;
62 class RecordASTTest : public ::testing::Test {
63 protected:
64 TestInputs Inputs;
65 RecordedAST Recorded;
67 RecordASTTest() {
68 struct RecordAction : public ASTFrontendAction {
69 RecordedAST &Out;
70 RecordAction(RecordedAST &Out) : Out(Out) {}
71 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
72 StringRef) override {
73 return Out.record();
76 Inputs.MakeAction = [this] {
77 return std::make_unique<RecordAction>(Recorded);
81 TestAST build() { return TestAST(Inputs); }
84 // Top-level decl from the main file is a root, nested ones aren't.
85 TEST_F(RecordASTTest, Namespace) {
86 Inputs.Code =
87 R"cpp(
88 namespace ns {
89 int x;
90 namespace {
91 int y;
94 )cpp";
95 auto AST = build();
96 EXPECT_THAT(Recorded.Roots, testing::ElementsAre(named("ns")));
99 // Decl in included file is not a root.
100 TEST_F(RecordASTTest, Inclusion) {
101 Inputs.ExtraFiles["header.h"] = "void headerFunc();";
102 Inputs.Code = R"cpp(
103 #include "header.h"
104 void mainFunc();
105 )cpp";
106 auto AST = build();
107 EXPECT_THAT(Recorded.Roots, testing::ElementsAre(named("mainFunc")));
110 // Decl from macro expanded into the main file is a root.
111 TEST_F(RecordASTTest, Macros) {
112 Inputs.ExtraFiles["header.h"] = "#define X void x();";
113 Inputs.Code = R"cpp(
114 #include "header.h"
116 )cpp";
117 auto AST = build();
118 EXPECT_THAT(Recorded.Roots, testing::ElementsAre(named("x")));
121 // Decl from template instantiation is filtered out from roots.
122 TEST_F(RecordASTTest, ImplicitTemplates) {
123 Inputs.ExtraFiles["dispatch.h"] = R"cpp(
124 struct A {
125 static constexpr int value = 1;
127 template <class Getter>
128 int dispatch() {
129 return Getter::template get<A>();
131 )cpp";
132 Inputs.Code = R"cpp(
133 #include "dispatch.h"
134 struct MyGetter {
135 template <class T> static int get() { return T::value; }
137 int v = dispatch<MyGetter>();
138 )cpp";
139 auto AST = build();
140 EXPECT_THAT(Recorded.Roots,
141 testing::ElementsAre(named("MyGetter"), named("v")));
144 class RecordPPTest : public ::testing::Test {
145 protected:
146 TestInputs Inputs;
147 RecordedPP Recorded;
149 RecordPPTest() {
150 struct RecordAction : public PreprocessOnlyAction {
151 RecordedPP &Out;
152 RecordAction(RecordedPP &Out) : Out(Out) {}
154 void ExecuteAction() override {
155 auto &PP = getCompilerInstance().getPreprocessor();
156 PP.addPPCallbacks(Out.record(PP));
157 PreprocessOnlyAction::ExecuteAction();
160 Inputs.MakeAction = [this] {
161 return std::make_unique<RecordAction>(Recorded);
165 TestAST build() { return TestAST(Inputs); }
168 // Matches an Include with a particular spelling.
169 MATCHER_P(spelled, S, "") { return arg.Spelled == S; }
171 TEST_F(RecordPPTest, CapturesIncludes) {
172 llvm::Annotations MainFile(R"cpp(
173 $H^#include "./header.h"
174 $M^#include <missing.h>
175 )cpp");
176 Inputs.Code = MainFile.code();
177 Inputs.ExtraFiles["header.h"] = "";
178 Inputs.ErrorOK = true; // missing header
179 auto AST = build();
181 ASSERT_THAT(
182 Recorded.Includes.all(),
183 testing::ElementsAre(spelled("./header.h"), spelled("missing.h")));
185 auto &H = Recorded.Includes.all().front();
186 EXPECT_EQ(H.Line, 2u);
187 EXPECT_EQ(H.HashLocation,
188 AST.sourceManager().getComposedLoc(
189 AST.sourceManager().getMainFileID(), MainFile.point("H")));
190 EXPECT_EQ(H.Resolved, *AST.fileManager().getOptionalFileRef("header.h"));
191 EXPECT_FALSE(H.Angled);
193 auto &M = Recorded.Includes.all().back();
194 EXPECT_EQ(M.Line, 3u);
195 EXPECT_EQ(M.HashLocation,
196 AST.sourceManager().getComposedLoc(
197 AST.sourceManager().getMainFileID(), MainFile.point("M")));
198 EXPECT_EQ(M.Resolved, std::nullopt);
199 EXPECT_TRUE(M.Angled);
202 TEST_F(RecordPPTest, CapturesMacroRefs) {
203 llvm::Annotations Header(R"cpp(
204 #define $def^X 1
206 // Refs, but not in main file.
207 #define Y X
208 int one = X;
209 )cpp");
210 llvm::Annotations MainFile(R"cpp(
211 #define EARLY X // not a ref, no definition
212 #include "header.h"
213 #define LATE ^X
214 #define LATE2 ^X // a ref even if not expanded
216 int uno = ^X;
217 int jeden = $exp^LATE; // a ref in LATE's expansion
219 #define IDENT(X) X // not a ref, shadowed
220 int eins = IDENT(^X);
222 #undef ^X
223 // Not refs, rather a new macro with the same name.
224 #define X 2
225 int two = X;
226 )cpp");
227 Inputs.Code = MainFile.code();
228 Inputs.ExtraFiles["header.h"] = Header.code();
229 auto AST = build();
230 const auto &SM = AST.sourceManager();
232 SourceLocation Def = SM.getComposedLoc(
233 SM.translateFile(AST.fileManager().getFile("header.h").get()),
234 Header.point("def"));
235 ASSERT_THAT(Recorded.MacroReferences, Not(IsEmpty()));
236 Symbol OrigX = Recorded.MacroReferences.front().Target;
237 EXPECT_EQ("X", OrigX.macro().Name->getName());
238 EXPECT_EQ(Def, OrigX.macro().Definition);
240 std::vector<unsigned> RefOffsets;
241 std::vector<unsigned> ExpOffsets; // Expansion locs of refs in macro locs.
242 for (const auto &Ref : Recorded.MacroReferences) {
243 if (Ref.Target == OrigX) {
244 auto [FID, Off] = SM.getDecomposedLoc(Ref.RefLocation);
245 if (FID == SM.getMainFileID()) {
246 RefOffsets.push_back(Off);
247 } else if (Ref.RefLocation.isMacroID() &&
248 SM.isWrittenInMainFile(SM.getExpansionLoc(Ref.RefLocation))) {
249 ExpOffsets.push_back(
250 SM.getDecomposedExpansionLoc(Ref.RefLocation).second);
251 } else {
252 ADD_FAILURE() << Ref.RefLocation.printToString(SM);
256 EXPECT_THAT(RefOffsets, ElementsAreArray(MainFile.points()));
257 EXPECT_THAT(ExpOffsets, ElementsAreArray(MainFile.points("exp")));
260 TEST_F(RecordPPTest, CapturesConditionalMacroRefs) {
261 llvm::Annotations MainFile(R"cpp(
262 #define X 1
264 #ifdef ^X
265 #endif
267 #if defined(^X)
268 #endif
270 #ifndef ^X
271 #endif
273 #ifdef Y
274 #elifdef ^X
275 #endif
277 #ifndef ^X
278 #elifndef ^X
279 #endif
280 )cpp");
282 Inputs.Code = MainFile.code();
283 Inputs.ExtraArgs.push_back("-std=c++2b");
284 auto AST = build();
286 std::vector<unsigned> RefOffsets;
287 SourceManager &SM = AST.sourceManager();
288 for (const auto &Ref : Recorded.MacroReferences) {
289 auto [FID, Off] = SM.getDecomposedLoc(Ref.RefLocation);
290 ASSERT_EQ(FID, SM.getMainFileID());
291 EXPECT_EQ(Ref.RT, RefType::Ambiguous);
292 EXPECT_EQ("X", Ref.Target.macro().Name->getName());
293 RefOffsets.push_back(Off);
295 EXPECT_THAT(RefOffsets, ElementsAreArray(MainFile.points()));
298 class PragmaIncludeTest : public ::testing::Test {
299 protected:
300 // We don't build an AST, we just run a preprocessor action!
301 TestInputs Inputs;
302 PragmaIncludes PI;
304 PragmaIncludeTest() {
305 Inputs.MakeAction = [this] {
306 struct Hook : public PreprocessOnlyAction {
307 public:
308 Hook(PragmaIncludes *Out) : Out(Out) {}
309 bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
310 Out->record(CI);
311 return true;
313 PragmaIncludes *Out;
315 return std::make_unique<Hook>(&PI);
319 TestAST build() { return TestAST(Inputs); }
321 void createEmptyFiles(llvm::ArrayRef<StringRef> FileNames) {
322 for (llvm::StringRef File : FileNames)
323 Inputs.ExtraFiles[File] = "#pragma once";
327 TEST_F(PragmaIncludeTest, IWYUKeep) {
328 Inputs.Code = R"cpp(
329 #include "keep1.h" // IWYU pragma: keep
330 #include "keep2.h" /* IWYU pragma: keep */
332 #include "export1.h" // IWYU pragma: export
333 // IWYU pragma: begin_exports
334 #include "export2.h"
335 #include "export3.h"
336 // IWYU pragma: end_exports
338 #include "normal.h"
340 // IWYU pragma: begin_keep
341 #include "keep3.h"
342 // IWYU pragma: end_keep
344 // IWYU pragma: begin_keep
345 #include "keep4.h"
346 // IWYU pragma: begin_keep
347 #include "keep5.h"
348 // IWYU pragma: end_keep
349 #include "keep6.h"
350 // IWYU pragma: end_keep
351 #include <vector>
352 #include <map> // IWYU pragma: keep
353 #include <set> // IWYU pragma: export
354 )cpp";
355 createEmptyFiles({"keep1.h", "keep2.h", "keep3.h", "keep4.h", "keep5.h",
356 "keep6.h", "export1.h", "export2.h", "export3.h",
357 "normal.h", "std/vector", "std/map", "std/set"});
359 Inputs.ExtraArgs.push_back("-isystemstd");
360 TestAST Processed = build();
361 auto &FM = Processed.fileManager();
363 EXPECT_FALSE(PI.shouldKeep(FM.getFile("normal.h").get()));
364 EXPECT_FALSE(PI.shouldKeep(FM.getFile("std/vector").get()));
366 // Keep
367 EXPECT_TRUE(PI.shouldKeep(FM.getFile("keep1.h").get()));
368 EXPECT_TRUE(PI.shouldKeep(FM.getFile("keep2.h").get()));
369 EXPECT_TRUE(PI.shouldKeep(FM.getFile("keep3.h").get()));
370 EXPECT_TRUE(PI.shouldKeep(FM.getFile("keep4.h").get()));
371 EXPECT_TRUE(PI.shouldKeep(FM.getFile("keep5.h").get()));
372 EXPECT_TRUE(PI.shouldKeep(FM.getFile("keep6.h").get()));
373 EXPECT_TRUE(PI.shouldKeep(FM.getFile("std/map").get()));
375 // Exports
376 EXPECT_TRUE(PI.shouldKeep(FM.getFile("export1.h").get()));
377 EXPECT_TRUE(PI.shouldKeep(FM.getFile("export2.h").get()));
378 EXPECT_TRUE(PI.shouldKeep(FM.getFile("export3.h").get()));
379 EXPECT_TRUE(PI.shouldKeep(FM.getFile("std/set").get()));
382 TEST_F(PragmaIncludeTest, IWYUPrivate) {
383 Inputs.Code = R"cpp(
384 #include "public.h"
385 )cpp";
386 Inputs.ExtraFiles["public.h"] = R"cpp(
387 #include "private.h"
388 #include "private2.h"
389 )cpp";
390 Inputs.ExtraFiles["private.h"] = R"cpp(
391 // IWYU pragma: private, include "public2.h"
392 )cpp";
393 Inputs.ExtraFiles["private2.h"] = R"cpp(
394 // IWYU pragma: private
395 )cpp";
396 TestAST Processed = build();
397 auto PrivateFE = Processed.fileManager().getFile("private.h");
398 assert(PrivateFE);
399 EXPECT_TRUE(PI.isPrivate(PrivateFE.get()));
400 EXPECT_EQ(PI.getPublic(PrivateFE.get()), "\"public2.h\"");
402 auto PublicFE = Processed.fileManager().getFile("public.h");
403 assert(PublicFE);
404 EXPECT_EQ(PI.getPublic(PublicFE.get()), ""); // no mapping.
405 EXPECT_FALSE(PI.isPrivate(PublicFE.get()));
407 auto Private2FE = Processed.fileManager().getFile("private2.h");
408 assert(Private2FE);
409 EXPECT_TRUE(PI.isPrivate(Private2FE.get()));
412 TEST_F(PragmaIncludeTest, IWYUExport) {
413 Inputs.Code = R"cpp(// Line 1
414 #include "export1.h"
415 #include "export2.h"
416 )cpp";
417 Inputs.ExtraFiles["export1.h"] = R"cpp(
418 #include "private.h" // IWYU pragma: export
419 )cpp";
420 Inputs.ExtraFiles["export2.h"] = R"cpp(
421 #include "export3.h"
422 )cpp";
423 Inputs.ExtraFiles["export3.h"] = R"cpp(
424 #include "private.h" // IWYU pragma: export
425 )cpp";
426 Inputs.ExtraFiles["private.h"] = "";
427 TestAST Processed = build();
428 const auto &SM = Processed.sourceManager();
429 auto &FM = Processed.fileManager();
431 EXPECT_THAT(PI.getExporters(FM.getFile("private.h").get(), FM),
432 testing::UnorderedElementsAre(FileNamed("export1.h"),
433 FileNamed("export3.h")));
435 EXPECT_TRUE(PI.getExporters(FM.getFile("export1.h").get(), FM).empty());
436 EXPECT_TRUE(PI.getExporters(FM.getFile("export2.h").get(), FM).empty());
437 EXPECT_TRUE(PI.getExporters(FM.getFile("export3.h").get(), FM).empty());
438 EXPECT_TRUE(
439 PI.getExporters(SM.getFileEntryForID(SM.getMainFileID()), FM).empty());
442 TEST_F(PragmaIncludeTest, IWYUExportForStandardHeaders) {
443 Inputs.Code = R"cpp(
444 #include "export.h"
445 )cpp";
446 Inputs.ExtraFiles["export.h"] = R"cpp(
447 #include <string> // IWYU pragma: export
448 )cpp";
449 Inputs.ExtraFiles["string"] = "";
450 Inputs.ExtraArgs = {"-isystem."};
451 TestAST Processed = build();
452 auto &FM = Processed.fileManager();
453 EXPECT_THAT(PI.getExporters(*tooling::stdlib::Header::named("<string>"), FM),
454 testing::UnorderedElementsAre(FileNamed("export.h")));
457 TEST_F(PragmaIncludeTest, IWYUExportBlock) {
458 Inputs.Code = R"cpp(// Line 1
459 #include "normal.h"
460 )cpp";
461 Inputs.ExtraFiles["normal.h"] = R"cpp(
462 #include "foo.h"
464 // IWYU pragma: begin_exports
465 #include "export1.h"
466 #include "private1.h"
467 // IWYU pragma: end_exports
468 )cpp";
469 Inputs.ExtraFiles["export1.h"] = R"cpp(
470 // IWYU pragma: begin_exports
471 #include "private1.h"
472 #include "private2.h"
473 // IWYU pragma: end_exports
475 #include "bar.h"
476 #include "private3.h" // IWYU pragma: export
477 )cpp";
478 createEmptyFiles(
479 {"private1.h", "private2.h", "private3.h", "foo.h", "bar.h"});
480 TestAST Processed = build();
481 auto &FM = Processed.fileManager();
483 EXPECT_THAT(PI.getExporters(FM.getFile("private1.h").get(), FM),
484 testing::UnorderedElementsAre(FileNamed("export1.h"),
485 FileNamed("normal.h")));
486 EXPECT_THAT(PI.getExporters(FM.getFile("private2.h").get(), FM),
487 testing::UnorderedElementsAre(FileNamed("export1.h")));
488 EXPECT_THAT(PI.getExporters(FM.getFile("private3.h").get(), FM),
489 testing::UnorderedElementsAre(FileNamed("export1.h")));
491 EXPECT_TRUE(PI.getExporters(FM.getFile("foo.h").get(), FM).empty());
492 EXPECT_TRUE(PI.getExporters(FM.getFile("bar.h").get(), FM).empty());
495 TEST_F(PragmaIncludeTest, SelfContained) {
496 Inputs.Code = R"cpp(
497 #include "guarded.h"
499 #include "unguarded.h"
500 )cpp";
501 Inputs.ExtraFiles["guarded.h"] = R"cpp(
502 #pragma once
503 )cpp";
504 Inputs.ExtraFiles["unguarded.h"] = "";
505 TestAST Processed = build();
506 auto &FM = Processed.fileManager();
507 EXPECT_TRUE(PI.isSelfContained(FM.getFile("guarded.h").get()));
508 EXPECT_FALSE(PI.isSelfContained(FM.getFile("unguarded.h").get()));
511 TEST_F(PragmaIncludeTest, AlwaysKeep) {
512 Inputs.Code = R"cpp(
513 #include "always_keep.h"
514 #include "usual.h"
515 )cpp";
516 Inputs.ExtraFiles["always_keep.h"] = R"cpp(
517 #pragma once
518 // IWYU pragma: always_keep
519 )cpp";
520 Inputs.ExtraFiles["usual.h"] = "#pragma once";
521 TestAST Processed = build();
522 auto &FM = Processed.fileManager();
523 EXPECT_TRUE(PI.shouldKeep(FM.getFile("always_keep.h").get()));
524 EXPECT_FALSE(PI.shouldKeep(FM.getFile("usual.h").get()));
527 TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
528 llvm::StringLiteral Filename = "test.cpp";
529 auto Code = R"cpp(#include "exporter.h")cpp";
530 Inputs.ExtraFiles["exporter.h"] = R"cpp(
531 #pragma once
532 #include "foo.h" // IWYU pragma: export
533 )cpp";
534 Inputs.ExtraFiles["foo.h"] = "";
536 auto Clang = std::make_unique<CompilerInstance>(
537 std::make_shared<PCHContainerOperations>());
538 Clang->createDiagnostics();
540 Clang->setInvocation(std::make_unique<CompilerInvocation>());
541 ASSERT_TRUE(CompilerInvocation::CreateFromArgs(
542 Clang->getInvocation(), {Filename.data()}, Clang->getDiagnostics(),
543 "clang"));
545 // Create unnamed memory buffers for all the files.
546 auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
547 VFS->addFile(Filename, /*ModificationTime=*/0,
548 llvm::MemoryBuffer::getMemBufferCopy(Code, /*BufferName=*/""));
549 for (const auto &Extra : Inputs.ExtraFiles)
550 VFS->addFile(Extra.getKey(), /*ModificationTime=*/0,
551 llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(),
552 /*BufferName=*/""));
553 auto *FM = Clang->createFileManager(VFS);
554 ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
555 EXPECT_THAT(
556 PI.getExporters(llvm::cantFail(FM->getFileRef("foo.h")), *FM),
557 testing::ElementsAre(llvm::cantFail(FM->getFileRef("exporter.h"))));
559 } // namespace
560 } // namespace clang::include_cleaner