-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clangd] Store documentation when indexing standard library #133681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clangd @llvm/pr-subscribers-clang-tools-extra Author: Nathan Ridge (HighCommander4) ChangesFixes clangd/clangd#2344 Full diff: https://github.com/llvm/llvm-project/pull/133681.diff 6 Files Affected:
diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp
index 496d1455def4b..8013e9ea86112 100644
--- a/clang-tools-extra/clangd/index/Background.cpp
+++ b/clang-tools-extra/clangd/index/Background.cpp
@@ -306,6 +306,7 @@ llvm::Error BackgroundIndex::index(tooling::CompileCommand Cmd) {
return true;
};
IndexOpts.CollectMainFileRefs = true;
+ IndexOpts.StoreAllDocumentation = false;
IndexFileIn Index;
auto Action = createStaticIndexingAction(
diff --git a/clang-tools-extra/clangd/index/IndexAction.cpp b/clang-tools-extra/clangd/index/IndexAction.cpp
index ed56c2a9d2e81..cec5f4558455c 100644
--- a/clang-tools-extra/clangd/index/IndexAction.cpp
+++ b/clang-tools-extra/clangd/index/IndexAction.cpp
@@ -223,7 +223,6 @@ std::unique_ptr<FrontendAction> createStaticIndexingAction(
Opts.CollectIncludePath = true;
if (Opts.Origin == SymbolOrigin::Unknown)
Opts.Origin = SymbolOrigin::Static;
- Opts.StoreAllDocumentation = false;
if (RefsCallback != nullptr) {
Opts.RefFilter = RefKind::All;
Opts.RefsInHeaders = true;
diff --git a/clang-tools-extra/clangd/indexer/IndexerMain.cpp b/clang-tools-extra/clangd/indexer/IndexerMain.cpp
index bc5d1a7408991..806734f6ad40e 100644
--- a/clang-tools-extra/clangd/indexer/IndexerMain.cpp
+++ b/clang-tools-extra/clangd/indexer/IndexerMain.cpp
@@ -55,6 +55,7 @@ class IndexActionFactory : public tooling::FrontendActionFactory {
std::unique_ptr<FrontendAction> create() override {
SymbolCollector::Options Opts;
Opts.CountReferences = true;
+ Opts.StoreAllDocumentation = false;
Opts.FileFilter = [&](const SourceManager &SM, FileID FID) {
const auto F = SM.getFileEntryRefForID(FID);
if (!F)
diff --git a/clang-tools-extra/clangd/unittests/StdLibTests.cpp b/clang-tools-extra/clangd/unittests/StdLibTests.cpp
index a7a33f78303d3..00c6d629e1c25 100644
--- a/clang-tools-extra/clangd/unittests/StdLibTests.cpp
+++ b/clang-tools-extra/clangd/unittests/StdLibTests.cpp
@@ -158,6 +158,43 @@ TEST(StdLibTests, EndToEnd) {
UnorderedElementsAre(StdlibSymbol("list"), StdlibSymbol("vector")));
}
+TEST(StdLibTests, StdLibDocComments) {
+ Config Cfg;
+ Cfg.Index.StandardLibrary = true;
+ WithContextValue Enabled(Config::Key, std::move(Cfg));
+
+ MockFS FS;
+ FS.Files["stdlib/vector"] = R"cpp(
+ namespace std {
+ template <typename T>
+ class vector {
+ public:
+ /**doc comment*/
+ unsigned int size() const;
+ };
+ }
+ )cpp";
+ MockCompilationDatabase CDB;
+ CDB.ExtraClangFlags.push_back("-isystem" + testPath("stdlib"));
+ ClangdServer::Options Opts = ClangdServer::optsForTest();
+ Opts.BuildDynamicSymbolIndex = true; // also used for stdlib index
+ ClangdServer Server(CDB, FS, Opts);
+
+ Annotations A(R"cpp(
+ #include <vector>
+ void foo() {
+ std::vector<int> v;
+ v.si^ze();
+ }
+ )cpp");
+
+ Server.addDocument(testPath("foo.cc"), A.code());
+ ASSERT_TRUE(Server.blockUntilIdleForTest());
+ auto HI = cantFail(runHover(Server, testPath("foo.cc"), A.point()));
+ EXPECT_TRUE(HI.has_value());
+ EXPECT_EQ(HI->Documentation, "doc comment");
+}
+
} // namespace
} // namespace clangd
} // namespace clang
diff --git a/clang-tools-extra/clangd/unittests/SyncAPI.cpp b/clang-tools-extra/clangd/unittests/SyncAPI.cpp
index d48622eba5378..00bec7afd1a98 100644
--- a/clang-tools-extra/clangd/unittests/SyncAPI.cpp
+++ b/clang-tools-extra/clangd/unittests/SyncAPI.cpp
@@ -68,6 +68,13 @@ template <typename T> CaptureProxy<T> capture(std::optional<T> &Target) {
}
} // namespace
+llvm::Expected<std::optional<HoverInfo>> runHover(ClangdServer &Server,
+ PathRef File, Position Pos) {
+ std::optional<llvm::Expected<std::optional<HoverInfo>>> HI;
+ Server.findHover(File, Pos, capture(HI));
+ return std::move(*HI);
+}
+
llvm::Expected<CodeCompleteResult>
runCodeComplete(ClangdServer &Server, PathRef File, Position Pos,
clangd::CodeCompleteOptions Opts) {
diff --git a/clang-tools-extra/clangd/unittests/SyncAPI.h b/clang-tools-extra/clangd/unittests/SyncAPI.h
index cf3de4f742e84..e0c7c4d72e73e 100644
--- a/clang-tools-extra/clangd/unittests/SyncAPI.h
+++ b/clang-tools-extra/clangd/unittests/SyncAPI.h
@@ -29,6 +29,9 @@ void runAddDocument(ClangdServer &Server, PathRef File, StringRef Contents,
WantDiagnostics WantDiags = WantDiagnostics::Auto,
bool ForceRebuild = false);
+llvm::Expected<std::optional<HoverInfo>> runHover(ClangdServer &Server,
+ PathRef File, Position Pos);
+
llvm::Expected<CodeCompleteResult>
runCodeComplete(ClangdServer &Server, PathRef File, Position Pos,
clangd::CodeCompleteOptions Opts);
|
See diagnosis here:
(and additional details earlier in the thread). |
Review ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(sorry for the late reply, we were both OOO :()
thanks a lot for great analysis in clangd/clangd#2344!
IIUC, the root cause is we're putting stdlib-index slabs into dynamic-index, but we're building those slabs as if they're static. Hence I think the fix should actually be limited to stdlib-index itself; we should either:
- layer it as a static-index, so that it won't take precedence over dynamic-index'd symbols.
- index using
indexHeaderSymbols
so that it gets the same treatment as rest of the dynamic-index'd symbols.
I must admit that it wasn't the best idea to use an option struct with some defaults partially in createStaticIndexingAction
, but I think any more changes in there is going to muddy the waters further. the reason why we don't store docs for static indexing is subtle but valid (we rely on dynamic-index taking over and we always index docs for code-completion enabled symbols). I wouldn't rely on caller to decide on that.
So do you mind changing the stdlib-indexing instead? 2nd alternative I mentioned above might be easier, by just using a SyntaxOnlyAction
and calling indexHeaderSymbols
after running Execute
on the action. But I haven't tried it myself, so I'll leave that call to you.
646963c
to
bd514d4
Compare
The described options seem a bit more involved than necessary to fix this bug, given that it's just the value of the I revised the patch to add a new parameter to |
Maybe I miscommunicated something, but I was talking about a change like kadircet@ff0c31d.
I think this still leaves possibility for future divergences. Conceptually we should either make stdlib index act as dynamic-index (what I am suggesting) or change its priority to be similar to static-index. Otherwise we're likely to hit more discrepancies as the code evolves. Moreover changing |
Thanks for the more fleshed-out suggestion. As written, this would also have the following effects:
Would I be right to say that (1) and (2) are undesirable? ((3) may actually be an improvement, and (4) may not make any difference as I'm guessing standard library implementations are unlikely to contain symbols with nesting depth 10 or more.) To fix (1) and (2), I think we would need to add a new parameter to the |
thanks for catching that, I think that's OK to pass in an extra
I think this is WAI. Firstly we shouldn't have (m)any symbols in main-file when running stdlib indexing (as we just stitch together a file that This also ensures we don't get different behavior if
agreed that this sounds like an improvement.
I think this was just an oversight, we should probably have that in
Just to be clear, my concern is not about adding more knobs to
That would be great, thanks a lot for taking care of this! |
bd514d4
to
aab6aef
Compare
Addressed review comments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks a lot, implementation LG, just a small hurdle with the test (sorry for not checking that out earlier)
aab6aef
to
451a889
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, LGTM!
FuzzyFindRequest Req; | ||
Req.Query = "inner"; | ||
Req.Scopes = {"std::vector::"}; | ||
SymbolSlab Result = runFuzzyFind(*Server.getIndexForTest(), Req); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, it might be better to use runCustomAction
from syncapi (we should have index in the inputs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks for the suggestion!
451a889
to
ce15317
Compare
Fixes clangd/clangd#2344