diff --git a/clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp b/clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp --- a/clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp @@ -143,7 +143,7 @@ RecordedPreprocessor.Includes.all()) { if (Used.contains(&I) || !I.Resolved) continue; - if (RecordedPI.shouldKeep(I.Line)) + if (RecordedPI.shouldKeep(I.Line) || RecordedPI.shouldKeep(I.Resolved)) continue; // Check if main file is the public interface for a private header. If so // we shouldn't diagnose it as unused. diff --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp --- a/clang-tools-extra/clangd/IncludeCleaner.cpp +++ b/clang-tools-extra/clangd/IncludeCleaner.cpp @@ -67,9 +67,8 @@ return false; } -bool mayConsiderUnused( - const Inclusion &Inc, ParsedAST &AST, - const include_cleaner::PragmaIncludes *PI) { +bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST, + const include_cleaner::PragmaIncludes *PI) { // FIXME(kirillbobyrev): We currently do not support the umbrella headers. // System headers are likely to be standard library headers. // Until we have good support for umbrella headers, don't warn about them. @@ -81,7 +80,7 @@ AST.getIncludeStructure().getRealPath(HID)); assert(FE); if (PI) { - if (PI->shouldKeep(Inc.HashLine + 1)) + if (PI->shouldKeep(Inc.HashLine + 1) || PI->shouldKeep(*FE)) return false; // Check if main file is the public interface for a private header. If so we // shouldn't diagnose it as unused. diff --git a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h --- a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h +++ b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h @@ -59,9 +59,8 @@ /// Returns true if the given #include of the main-file should never be /// removed. - bool shouldKeep(unsigned HashLineNumber) const { - return ShouldKeep.contains(HashLineNumber); - } + bool shouldKeep(unsigned HashLineNumber) const; + bool shouldKeep(const FileEntry *FE) const; /// Returns the public mapping include for the given physical header file. /// Returns "" if there is none. @@ -113,6 +112,8 @@ /// Contains all non self-contained files detected during the parsing. llvm::DenseSet NonSelfContainedFiles; + // Files with an always_keep pragma. + llvm::DenseSet AlwaysKeep; /// Owns the strings. llvm::BumpPtrAllocator Arena; diff --git a/clang-tools-extra/include-cleaner/lib/Analysis.cpp b/clang-tools-extra/include-cleaner/lib/Analysis.cpp --- a/clang-tools-extra/include-cleaner/lib/Analysis.cpp +++ b/clang-tools-extra/include-cleaner/lib/Analysis.cpp @@ -91,7 +91,7 @@ HeaderFilter(I.Resolved->tryGetRealPathName())) continue; if (PI) { - if (PI->shouldKeep(I.Line)) + if (PI->shouldKeep(I.Line) || PI->shouldKeep(I.Resolved)) continue; // Check if main file is the public interface for a private header. If so // we shouldn't diagnose it as unused. diff --git a/clang-tools-extra/include-cleaner/lib/Record.cpp b/clang-tools-extra/include-cleaner/lib/Record.cpp --- a/clang-tools-extra/include-cleaner/lib/Record.cpp +++ b/clang-tools-extra/include-cleaner/lib/Record.cpp @@ -11,6 +11,9 @@ #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclGroup.h" +#include "clang/Basic/FileEntry.h" +#include "clang/Basic/LLVM.h" +#include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/Specifiers.h" #include "clang/Frontend/CompilerInstance.h" @@ -19,8 +22,19 @@ #include "clang/Lex/Preprocessor.h" #include "clang/Tooling/Inclusions/HeaderAnalysis.h" #include "clang/Tooling/Inclusions/StandardLibrary.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Allocator.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/StringSaver.h" +#include +#include #include +#include #include +#include namespace clang::include_cleaner { namespace { @@ -255,8 +269,9 @@ if (!Pragma) return false; + FileID CommentFID = SM.getFileID(Range.getBegin()); + auto *FE = SM.getFileEntryForID(CommentFID); if (Pragma->consume_front("private")) { - auto *FE = SM.getFileEntryForID(SM.getFileID(Range.getBegin())); if (!FE) return false; StringRef PublicHeader; @@ -269,7 +284,10 @@ Out->IWYUPublic.insert({FE->getLastRef().getUniqueID(), PublicHeader}); return false; } - FileID CommentFID = SM.getFileID(Range.getBegin()); + if (Pragma->consume_front("always_keep")) { + Out->AlwaysKeep.insert(FE->getUniqueID()); + return false; + } int CommentLine = SM.getLineNumber(SM.getFileID(Range.getBegin()), SM.getFileOffset(Range.getBegin())); // Record export pragma. @@ -394,6 +412,14 @@ return IWYUPublic.contains(FE->getUniqueID()); } +bool PragmaIncludes::shouldKeep(unsigned HashLineNumber) const { + return ShouldKeep.contains(HashLineNumber); +} + +bool PragmaIncludes::shouldKeep(const FileEntry *FE) const { + return AlwaysKeep.contains(FE->getUniqueID()); +} + namespace { template bool isImplicitTemplateSpecialization(const Decl *D) { if (const auto *TD = dyn_cast(D)) diff --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp --- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp @@ -502,5 +502,20 @@ EXPECT_FALSE(PI.isSelfContained(FM.getFile("unguarded.h").get())); } +TEST_F(PragmaIncludeTest, AlwaysKeep) { + Inputs.Code = R"cpp( + #include "always_keep.h" + #include "usual.h" + )cpp"; + Inputs.ExtraFiles["always_keep.h"] = R"cpp( + #pragma once + // IWYU pragma: always_keep + )cpp"; + Inputs.ExtraFiles["usual.h"] = "#pragma once"; + TestAST Processed = build(); + auto &FM = Processed.fileManager(); + EXPECT_TRUE(PI.shouldKeep(FM.getFile("always_keep.h").get())); + EXPECT_FALSE(PI.shouldKeep(FM.getFile("usual.h").get())); +} } // namespace } // namespace clang::include_cleaner