Skip to content

Commit bd514d4

Browse files
[clangd] Store documentation when indexing standard library
Fixes clangd/clangd#2344
1 parent fff8f03 commit bd514d4

File tree

9 files changed

+69
-18
lines changed

9 files changed

+69
-18
lines changed

clang-tools-extra/clangd/index/Background.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ llvm::Error BackgroundIndex::index(tooling::CompileCommand Cmd) {
308308
IndexOpts.CollectMainFileRefs = true;
309309

310310
IndexFileIn Index;
311-
auto Action = createStaticIndexingAction(
311+
auto Action = createIndexingAction(
312312
IndexOpts, [&](SymbolSlab S) { Index.Symbols = std::move(S); },
313313
[&](RefSlab R) { Index.Refs = std::move(R); },
314314
[&](RelationSlab R) { Index.Relations = std::move(R); },

clang-tools-extra/clangd/index/IndexAction.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,13 @@ class IndexAction : public ASTFrontendAction {
209209

210210
} // namespace
211211

212-
std::unique_ptr<FrontendAction> createStaticIndexingAction(
213-
SymbolCollector::Options Opts,
214-
std::function<void(SymbolSlab)> SymbolsCallback,
215-
std::function<void(RefSlab)> RefsCallback,
216-
std::function<void(RelationSlab)> RelationsCallback,
217-
std::function<void(IncludeGraph)> IncludeGraphCallback) {
212+
std::unique_ptr<FrontendAction>
213+
createIndexingAction(SymbolCollector::Options Opts,
214+
std::function<void(SymbolSlab)> SymbolsCallback,
215+
std::function<void(RefSlab)> RefsCallback,
216+
std::function<void(RelationSlab)> RelationsCallback,
217+
std::function<void(IncludeGraph)> IncludeGraphCallback,
218+
IndexActionKind Kind) {
218219
index::IndexingOptions IndexOpts;
219220
IndexOpts.SystemSymbolFilter =
220221
index::IndexingOptions::SystemSymbolFilterKind::All;
@@ -223,7 +224,7 @@ std::unique_ptr<FrontendAction> createStaticIndexingAction(
223224
Opts.CollectIncludePath = true;
224225
if (Opts.Origin == SymbolOrigin::Unknown)
225226
Opts.Origin = SymbolOrigin::Static;
226-
Opts.StoreAllDocumentation = false;
227+
Opts.StoreAllDocumentation = (Kind == IndexActionKind::Stdlib);
227228
if (RefsCallback != nullptr) {
228229
Opts.RefFilter = RefKind::All;
229230
Opts.RefsInHeaders = true;

clang-tools-extra/clangd/index/IndexAction.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
namespace clang {
1616
namespace clangd {
1717

18+
enum class IndexActionKind { Static, Stdlib };
19+
1820
// Creates an action that indexes translation units and delivers the results
1921
// for SymbolsCallback (each slab corresponds to one TU).
2022
//
@@ -23,12 +25,13 @@ namespace clangd {
2325
// - references are always counted
2426
// - all references are collected (if RefsCallback is non-null)
2527
// - the symbol origin is set to Static if not specified by caller
26-
std::unique_ptr<FrontendAction> createStaticIndexingAction(
27-
SymbolCollector::Options Opts,
28-
std::function<void(SymbolSlab)> SymbolsCallback,
29-
std::function<void(RefSlab)> RefsCallback,
30-
std::function<void(RelationSlab)> RelationsCallback,
31-
std::function<void(IncludeGraph)> IncludeGraphCallback);
28+
std::unique_ptr<FrontendAction>
29+
createIndexingAction(SymbolCollector::Options Opts,
30+
std::function<void(SymbolSlab)> SymbolsCallback,
31+
std::function<void(RefSlab)> RefsCallback,
32+
std::function<void(RelationSlab)> RelationsCallback,
33+
std::function<void(IncludeGraph)> IncludeGraphCallback,
34+
IndexActionKind Kind = IndexActionKind::Static);
3235

3336
} // namespace clangd
3437
} // namespace clang

clang-tools-extra/clangd/index/StdLib.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ SymbolSlab indexStandardLibrary(llvm::StringRef HeaderSources,
235235
// everything first.
236236

237237
// Refs, relations, include graph in the stdlib mostly aren't useful.
238-
auto Action = createStaticIndexingAction(
238+
auto Action = createIndexingAction(
239239
IndexOpts, [&](SymbolSlab S) { Symbols = std::move(S); }, nullptr,
240-
nullptr, nullptr);
240+
nullptr, nullptr, IndexActionKind::Stdlib);
241241

242242
if (!Action->BeginSourceFile(*Clang, Input)) {
243243
elog("Standard Library Index: BeginSourceFile() failed");

clang-tools-extra/clangd/indexer/IndexerMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class IndexActionFactory : public tooling::FrontendActionFactory {
6565
std::lock_guard<std::mutex> Lock(FilesMu);
6666
return Files.insert(*AbsPath).second; // Skip already processed files.
6767
};
68-
return createStaticIndexingAction(
68+
return createIndexingAction(
6969
Opts,
7070
[&](SymbolSlab S) {
7171
// Merge as we go.

clang-tools-extra/clangd/unittests/IndexActionTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class IndexActionTest : public ::testing::Test {
8585
llvm::IntrusiveRefCntPtr<FileManager> Files(
8686
new FileManager(FileSystemOptions(), InMemoryFileSystem));
8787

88-
auto Action = createStaticIndexingAction(
88+
auto Action = createIndexingAction(
8989
Opts, [&](SymbolSlab S) { IndexFile.Symbols = std::move(S); },
9090
[&](RefSlab R) { IndexFile.Refs = std::move(R); },
9191
[&](RelationSlab R) { IndexFile.Relations = std::move(R); },

clang-tools-extra/clangd/unittests/StdLibTests.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,43 @@ TEST(StdLibTests, EndToEnd) {
158158
UnorderedElementsAre(StdlibSymbol("list"), StdlibSymbol("vector")));
159159
}
160160

161+
TEST(StdLibTests, StdLibDocComments) {
162+
Config Cfg;
163+
Cfg.Index.StandardLibrary = true;
164+
WithContextValue Enabled(Config::Key, std::move(Cfg));
165+
166+
MockFS FS;
167+
FS.Files["stdlib/vector"] = R"cpp(
168+
namespace std {
169+
template <typename T>
170+
class vector {
171+
public:
172+
/**doc comment*/
173+
unsigned int size() const;
174+
};
175+
}
176+
)cpp";
177+
MockCompilationDatabase CDB;
178+
CDB.ExtraClangFlags.push_back("-isystem" + testPath("stdlib"));
179+
ClangdServer::Options Opts = ClangdServer::optsForTest();
180+
Opts.BuildDynamicSymbolIndex = true; // also used for stdlib index
181+
ClangdServer Server(CDB, FS, Opts);
182+
183+
Annotations A(R"cpp(
184+
#include <vector>
185+
void foo() {
186+
std::vector<int> v;
187+
v.si^ze();
188+
}
189+
)cpp");
190+
191+
Server.addDocument(testPath("foo.cc"), A.code());
192+
ASSERT_TRUE(Server.blockUntilIdleForTest());
193+
auto HI = cantFail(runHover(Server, testPath("foo.cc"), A.point()));
194+
EXPECT_TRUE(HI.has_value());
195+
EXPECT_EQ(HI->Documentation, "doc comment");
196+
}
197+
161198
} // namespace
162199
} // namespace clangd
163200
} // namespace clang

clang-tools-extra/clangd/unittests/SyncAPI.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ template <typename T> CaptureProxy<T> capture(std::optional<T> &Target) {
6868
}
6969
} // namespace
7070

71+
llvm::Expected<std::optional<HoverInfo>> runHover(ClangdServer &Server,
72+
PathRef File, Position Pos) {
73+
std::optional<llvm::Expected<std::optional<HoverInfo>>> HI;
74+
Server.findHover(File, Pos, capture(HI));
75+
return std::move(*HI);
76+
}
77+
7178
llvm::Expected<CodeCompleteResult>
7279
runCodeComplete(ClangdServer &Server, PathRef File, Position Pos,
7380
clangd::CodeCompleteOptions Opts) {

clang-tools-extra/clangd/unittests/SyncAPI.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ void runAddDocument(ClangdServer &Server, PathRef File, StringRef Contents,
2929
WantDiagnostics WantDiags = WantDiagnostics::Auto,
3030
bool ForceRebuild = false);
3131

32+
llvm::Expected<std::optional<HoverInfo>> runHover(ClangdServer &Server,
33+
PathRef File, Position Pos);
34+
3235
llvm::Expected<CodeCompleteResult>
3336
runCodeComplete(ClangdServer &Server, PathRef File, Position Pos,
3437
clangd::CodeCompleteOptions Opts);

0 commit comments

Comments
 (0)