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 @@ -50,6 +50,9 @@ /// - don't attempt to describe where symbols were referenced from in /// ambiguous cases (e.g. implicitly used symbols, multiple declarations) /// - err on the side of reporting all possible locations +ReferencedLocations findReferencedLocations(const SourceManager &SM, + ASTContext &Ctx, Preprocessor &PP, + const syntax::TokenBuffer &Tokens); ReferencedLocations findReferencedLocations(ParsedAST &AST); struct ReferencedFiles { @@ -60,6 +63,9 @@ /// Retrieves IDs of all files containing SourceLocations from \p Locs. /// The output only includes things SourceManager sees as files (not macro IDs). /// This can include , etc that are not true files. +ReferencedFiles +findReferencedFiles(const ReferencedLocations &Locs, const SourceManager &SM, + std::function HeaderResponsible); ReferencedFiles findReferencedFiles(const ReferencedLocations &Locs, const IncludeStructure &Includes, const SourceManager &SM); @@ -81,8 +87,8 @@ std::vector issueUnusedIncludesDiagnostics(ParsedAST &AST, llvm::StringRef Code); -/// Affects whether standard library includes should be considered for removal. -/// This is off by default for now due to implementation limitations: +/// Affects whether standard library includes should be considered for +/// removal. This is off by default for now due to implementation limitations: /// - macros are not tracked /// - symbol names without a unique associated header are not tracked /// - references to std-namespaced C types are not properly tracked: 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 @@ -14,6 +14,7 @@ #include "SourceCode.h" #include "support/Logger.h" #include "support/Trace.h" +#include "clang/AST/ASTContext.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/SourceLocation.h" @@ -23,6 +24,7 @@ #include "clang/Tooling/Syntax/Tokens.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Path.h" +#include namespace clang { namespace clangd { @@ -207,10 +209,10 @@ // Finds locations of macros referenced from within the main file. That includes // references that were not yet expanded, e.g `BAR` in `#define FOO BAR`. -void findReferencedMacros(ParsedAST &AST, ReferencedLocations &Result) { +void findReferencedMacros(const SourceManager &SM, Preprocessor &PP, + const syntax::TokenBuffer &Tokens, + ReferencedLocations &Result) { trace::Span Tracer("IncludeCleaner::findReferencedMacros"); - auto &SM = AST.getSourceManager(); - auto &PP = AST.getPreprocessor(); // FIXME(kirillbobyrev): The macros from the main file are collected in // ParsedAST's MainFileMacros. However, we can't use it here because it // doesn't handle macro references that were not expanded, e.g. in macro @@ -220,8 +222,7 @@ // this mechanism (as opposed to iterating through all tokens) will improve // the performance of findReferencedMacros and also improve other features // relying on MainFileMacros. - for (const syntax::Token &Tok : - AST.getTokens().spelledTokens(SM.getMainFileID())) { + for (const syntax::Token &Tok : Tokens.spelledTokens(SM.getMainFileID())) { auto Macro = locateMacroAt(Tok, PP); if (!Macro) continue; @@ -287,18 +288,25 @@ } // namespace -ReferencedLocations findReferencedLocations(ParsedAST &AST) { +ReferencedLocations findReferencedLocations(const SourceManager &SM, + ASTContext &Ctx, Preprocessor &PP, + const syntax::TokenBuffer &Tokens) { trace::Span Tracer("IncludeCleaner::findReferencedLocations"); ReferencedLocations Result; - ReferencedLocationCrawler Crawler(Result, AST.getSourceManager()); - Crawler.TraverseAST(AST.getASTContext()); - findReferencedMacros(AST, Result); + ReferencedLocationCrawler Crawler(Result, SM); + Crawler.TraverseAST(Ctx); + findReferencedMacros(SM, PP, Tokens, Result); return Result; } -ReferencedFiles findReferencedFiles(const ReferencedLocations &Locs, - const IncludeStructure &Includes, - const SourceManager &SM) { +ReferencedLocations findReferencedLocations(ParsedAST &AST) { + return findReferencedLocations(AST.getSourceManager(), AST.getASTContext(), + AST.getPreprocessor(), AST.getTokens()); +} + +ReferencedFiles +findReferencedFiles(const ReferencedLocations &Locs, const SourceManager &SM, + std::function HeaderResponsible) { std::vector Sorted{Locs.User.begin(), Locs.User.end()}; llvm::sort(Sorted); // Group by FileID. ReferencedFilesBuilder Builder(SM); @@ -318,7 +326,7 @@ // HeaderIDs, as each inclusion of a non-self-contained file is distinct. llvm::DenseSet UserFiles; for (FileID ID : Builder.Files) - UserFiles.insert(headerResponsible(ID, SM, Includes)); + UserFiles.insert(HeaderResponsible(ID)); llvm::DenseSet StdlibFiles; for (const auto &Symbol : Locs.Stdlib) @@ -328,6 +336,14 @@ return {std::move(UserFiles), std::move(StdlibFiles)}; } +ReferencedFiles findReferencedFiles(const ReferencedLocations &Locs, + const IncludeStructure &Includes, + const SourceManager &SM) { + return findReferencedFiles(Locs, SM, [&SM, &Includes](FileID ID) { + return headerResponsible(ID, SM, Includes); + }); +} + std::vector getUnused(ParsedAST &AST, const llvm::DenseSet &ReferencedFiles) {