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 @@ -11,6 +11,7 @@ #include "Diagnostics.h" #include "Headers.h" #include "ParsedAST.h" +#include "Preamble.h" #include "Protocol.h" #include "SourceCode.h" #include "URI.h" @@ -112,12 +113,12 @@ return false; // Check if main file is the public interface for a private header. If so we // shouldn't diagnose it as unused. - if(auto PHeader = PI->getPublic(*FE); !PHeader.empty()) { + if (auto PHeader = PI->getPublic(*FE); !PHeader.empty()) { PHeader = PHeader.trim("<>\""); // Since most private -> public mappings happen in a verbatim way, we // check textually here. This might go wrong in presence of symlinks or // header mappings. But that's not different than rest of the places. - if(AST.tuPath().endswith(PHeader)) + if (AST.tuPath().endswith(PHeader)) return false; } } @@ -360,6 +361,7 @@ include_cleaner::Includes ConvertedIncludes = convertIncludes(SM, Includes.MainFileIncludes); const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()); + auto *PreamblePatch = PreamblePatch::getPatchEntry(AST.tuPath(), SM); std::vector Macros = collectMacroReferences(AST); @@ -374,7 +376,7 @@ bool Satisfied = false; for (const auto &H : Providers) { if (H.kind() == include_cleaner::Header::Physical && - H.physical() == MainFile) { + (H.physical() == MainFile || H.physical() == PreamblePatch)) { Satisfied = true; continue; } diff --git a/clang-tools-extra/clangd/Preamble.h b/clang-tools-extra/clangd/Preamble.h --- a/clang-tools-extra/clangd/Preamble.h +++ b/clang-tools-extra/clangd/Preamble.h @@ -30,6 +30,7 @@ #include "clang-include-cleaner/Record.h" #include "index/CanonicalIncludes.h" #include "support/Path.h" +#include "clang/Basic/SourceManager.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/PrecompiledPreamble.h" #include "clang/Lex/Lexer.h" @@ -37,8 +38,11 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" +#include +#include #include #include +#include #include namespace clang { @@ -135,6 +139,10 @@ static PreamblePatch createMacroPatch(llvm::StringRef FileName, const ParseInputs &Modified, const PreambleData &Baseline); + /// Returns the FileEntry for the preamble patch of MainFilePath in SM, if + /// any. + static const FileEntry *getPatchEntry(llvm::StringRef MainFilePath, + const SourceManager &SM); /// Adjusts CI (which compiles the modified inputs) to be used with the /// baseline preamble. This is done by inserting an artifical include to the diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp --- a/clang-tools-extra/clangd/Preamble.cpp +++ b/clang-tools-extra/clangd/Preamble.cpp @@ -738,6 +738,14 @@ return PatchedDiags; } +static std::string getPatchName(llvm::StringRef FileName) { + // This shouldn't coincide with any real file name. + llvm::SmallString<128> PatchName; + llvm::sys::path::append(PatchName, llvm::sys::path::parent_path(FileName), + PreamblePatch::HeaderName); + return PatchName.str().str(); +} + PreamblePatch PreamblePatch::create(llvm::StringRef FileName, const ParseInputs &Modified, const PreambleData &Baseline, @@ -776,11 +784,7 @@ PreamblePatch PP; PP.Baseline = &Baseline; - // This shouldn't coincide with any real file name. - llvm::SmallString<128> PatchName; - llvm::sys::path::append(PatchName, llvm::sys::path::parent_path(FileName), - PreamblePatch::HeaderName); - PP.PatchFileName = PatchName.str().str(); + PP.PatchFileName = getPatchName(FileName); PP.ModifiedBounds = ModifiedScan->Bounds; llvm::raw_string_ostream Patch(PP.PatchContents); @@ -921,5 +925,13 @@ return Baseline->Macros; return PatchedMacros; } + +const FileEntry *PreamblePatch::getPatchEntry(llvm::StringRef MainFilePath, + const SourceManager &SM) { + auto PatchFilePath = getPatchName(MainFilePath); + if (auto File = SM.getFileManager().getFile(PatchFilePath)) + return *File; + return nullptr; +} } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/unittests/PreambleTests.cpp b/clang-tools-extra/clangd/unittests/PreambleTests.cpp --- a/clang-tools-extra/clangd/unittests/PreambleTests.cpp +++ b/clang-tools-extra/clangd/unittests/PreambleTests.cpp @@ -666,6 +666,7 @@ Config Cfg; Cfg.Diagnostics.AllowStalePreamble = true; Cfg.Diagnostics.UnusedIncludes = Config::IncludesPolicy::Strict; + Cfg.Diagnostics.MissingIncludes = Config::IncludesPolicy::Strict; WithContextValue WithCfg(Config::Key, std::move(Cfg)); llvm::StringMap AdditionalFiles; @@ -699,6 +700,8 @@ { Annotations Code("#define [[FOO]] 1\n"); // Check ranges for notes. + // This also makes sure we don't generate missing-include diagnostics + // because macros are redefined in preamble-patch. Annotations NewCode(R"(#define BARXYZ 1 #define $foo1[[FOO]] 1 void foo(); @@ -866,6 +869,27 @@ } } +TEST(PreamblePatch, PatchFileEntry) { + Annotations Code(R"cpp(#define FOO)cpp"); + Annotations NewCode(R"cpp( +#define BAR +#define FOO)cpp"); + { + auto AST = createPatchedAST(Code.code(), Code.code()); + EXPECT_EQ( + PreamblePatch::getPatchEntry(AST->tuPath(), AST->getSourceManager()), + nullptr); + } + { + auto AST = createPatchedAST(Code.code(), NewCode.code()); + auto *FE = + PreamblePatch::getPatchEntry(AST->tuPath(), AST->getSourceManager()); + ASSERT_NE(FE, nullptr); + EXPECT_THAT(FE->getName().str(), + testing::EndsWith(PreamblePatch::HeaderName.str())); + } +} + } // namespace } // namespace clangd } // namespace clang