diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -18,6 +18,7 @@ #include "Selection.h" #include "SourceCode.h" #include "clang-include-cleaner/Analysis.h" +#include "clang-include-cleaner/IncludeSpeller.h" #include "clang-include-cleaner/Types.h" #include "index/SymbolCollector.h" #include "support/Logger.h" @@ -1223,7 +1224,9 @@ // on local variables, etc. return; - HI.Provider = spellHeader(AST, SM.getFileEntryForID(SM.getMainFileID()), H); + HI.Provider = include_cleaner::spellHeader( + {H, AST.getPreprocessor().getHeaderSearchInfo(), + SM.getFileEntryForID(SM.getMainFileID())}); } // FIXME: similar functions are present in FindHeaders.cpp (symbolName) diff --git a/clang-tools-extra/clangd/IncludeCleaner.h b/clang-tools-extra/clangd/IncludeCleaner.h --- a/clang-tools-extra/clangd/IncludeCleaner.h +++ b/clang-tools-extra/clangd/IncludeCleaner.h @@ -74,11 +74,6 @@ convertIncludes(const SourceManager &SM, const llvm::ArrayRef Includes); -/// Determines the header spelling of an include-cleaner header -/// representation. The spelling contains the ""<> characters. -std::string spellHeader(ParsedAST &AST, const FileEntry *MainFile, - include_cleaner::Header Provider); - std::vector collectMacroReferences(ParsedAST &AST); 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 @@ -198,8 +198,9 @@ continue; } - std::string Spelling = - spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front()); + std::string Spelling = include_cleaner::spellHeader( + {SymbolWithMissingInclude.Providers.front(), + AST.getPreprocessor().getHeaderSearchInfo(), MainFile}); llvm::StringRef HeaderRef{Spelling}; bool Angled = HeaderRef.starts_with("<"); @@ -334,22 +335,6 @@ return ConvertedIncludes; } -std::string spellHeader(ParsedAST &AST, const FileEntry *MainFile, - include_cleaner::Header Provider) { - if (Provider.kind() == include_cleaner::Header::Physical) { - if (auto CanonicalPath = - getCanonicalPath(Provider.physical()->getLastRef(), - AST.getSourceManager().getFileManager())) { - std::string SpelledHeader = - llvm::cantFail(URI::includeSpelling(URI::create(*CanonicalPath))); - if (!SpelledHeader.empty()) - return SpelledHeader; - } - } - return include_cleaner::spellHeader( - {Provider, AST.getPreprocessor().getHeaderSearchInfo(), MainFile}); -} - std::vector getUnused(ParsedAST &AST, const llvm::DenseSet &ReferencedFiles, @@ -459,7 +444,8 @@ return {std::move(UnusedIncludes), std::move(MissingIncludes)}; } -std::optional removeAllUnusedIncludes(llvm::ArrayRef UnusedIncludes) { +std::optional +removeAllUnusedIncludes(llvm::ArrayRef UnusedIncludes) { if (UnusedIncludes.empty()) return std::nullopt; @@ -468,8 +454,8 @@ for (const auto &Diag : UnusedIncludes) { assert(Diag.Fixes.size() == 1 && "Expected exactly one fix."); RemoveAll.Edits.insert(RemoveAll.Edits.end(), - Diag.Fixes.front().Edits.begin(), - Diag.Fixes.front().Edits.end()); + Diag.Fixes.front().Edits.begin(), + Diag.Fixes.front().Edits.end()); } // TODO(hokein): emit a suitable text for the label. @@ -497,7 +483,7 @@ llvm::StringMap Edits; for (const auto &Diag : MissingIncludeDiags) { assert(Diag.Fixes.size() == 1 && "Expected exactly one fix."); - for (const auto& Edit : Diag.Fixes.front().Edits) { + for (const auto &Edit : Diag.Fixes.front().Edits) { Edits.try_emplace(Edit.newText, Edit); } } @@ -517,7 +503,7 @@ } return AddAllMissing; } -Fix fixAll(const Fix& RemoveAllUnused, const Fix& AddAllMissing) { +Fix fixAll(const Fix &RemoveAllUnused, const Fix &AddAllMissing) { Fix FixAll; FixAll.Message = "fix all includes"; @@ -526,22 +512,23 @@ for (const auto &F : AddAllMissing.Edits) FixAll.Edits.push_back(F); - for (const auto& A : RemoveAllUnused.Annotations) + for (const auto &A : RemoveAllUnused.Annotations) FixAll.Annotations.push_back(A); - for (const auto& A : AddAllMissing.Annotations) + for (const auto &A : AddAllMissing.Annotations) FixAll.Annotations.push_back(A); return FixAll; } -std::vector generateIncludeCleanerDiagnostic( - ParsedAST &AST, const IncludeCleanerFindings &Findings, - llvm::StringRef Code) { +std::vector +generateIncludeCleanerDiagnostic(ParsedAST &AST, + const IncludeCleanerFindings &Findings, + llvm::StringRef Code) { std::vector UnusedIncludes = generateUnusedIncludeDiagnostics( AST.tuPath(), Findings.UnusedIncludes, Code); std::optional RemoveAllUnused = removeAllUnusedIncludes(UnusedIncludes); - std::vector MissingIncludeDiags = generateMissingIncludeDiagnostics( - AST, Findings.MissingIncludes, Code); + std::vector MissingIncludeDiags = + generateMissingIncludeDiagnostics(AST, Findings.MissingIncludes, Code); std::optional AddAllMissing = addAllMissingIncludes(MissingIncludeDiags); std::optional FixAll; @@ -549,26 +536,23 @@ FixAll = fixAll(*RemoveAllUnused, *AddAllMissing); auto AddBatchFix = [](const std::optional &F, clang::clangd::Diag *Out) { - if (!F) return; + if (!F) + return; Out->Fixes.push_back(*F); }; for (auto &Diag : MissingIncludeDiags) { - AddBatchFix(MissingIncludeDiags.size() > 1 - ? AddAllMissing - : std::nullopt, + AddBatchFix(MissingIncludeDiags.size() > 1 ? AddAllMissing : std::nullopt, &Diag); AddBatchFix(FixAll, &Diag); } for (auto &Diag : UnusedIncludes) { - AddBatchFix(UnusedIncludes.size() > 1 ? RemoveAllUnused - : std::nullopt, + AddBatchFix(UnusedIncludes.size() > 1 ? RemoveAllUnused : std::nullopt, &Diag); AddBatchFix(FixAll, &Diag); } auto Result = std::move(MissingIncludeDiags); - llvm::move(UnusedIncludes, - std::back_inserter(Result)); + llvm::move(UnusedIncludes, std::back_inserter(Result)); return Result; }