diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -243,6 +243,10 @@ void findHover(PathRef File, Position Pos, Callback> CB); + /// Get information about unused includes. + void findUnusedIncludes(PathRef File, + Callback> CB); + /// Get information about type hierarchy for a given position. void typeHierarchy(PathRef File, Position Pos, int Resolve, TypeHierarchyDirection Direction, diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -14,6 +14,7 @@ #include "FindSymbols.h" #include "Format.h" #include "HeaderSourceSwitch.h" +#include "IncludeCleaner.h" #include "InlayHints.h" #include "ParsedAST.h" #include "Preamble.h" @@ -715,6 +716,19 @@ WorkScheduler->runWithAST("Hover", File, std::move(Action), Transient); } +void ClangdServer::findUnusedIncludes( + PathRef File, Callback> CB) { + auto Action = [File = File.str(), CB = std::move(CB)]( + llvm::Expected InpAST) mutable { + if (!InpAST) + return CB(InpAST.takeError()); + CB(clangd::computeUnusedIncludes(InpAST->AST)); + }; + + WorkScheduler->runWithAST("UnusedIncludes", File, std::move(Action), + Transient); +} + void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve, TypeHierarchyDirection Direction, Callback> CB) {