This patch is a try to fix WorkspaceSymbols.Macros test after D93796.
If a macro definition is in the preamble section, then it appears to be in the preamble (static) index and not in the main-file (dynamic) index.
Thus, a such macro could not be found at a symbol search according to the logic that we skip symbols from the static index if the location of these symbols is inside the dynamic index files.
To fix this behavior this patch adds main file macros into the main-file (dynamic) index.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
I am not happy with my solution, hope to hear an advice.
Initial discussion about this problem: https://reviews.llvm.org/D93683#2468842
Ah this is really unfortunate :( In theory we already know which slabs belong to main file while updating the index for the preamble, as we shard them per file. But it is really inconvenient layering wise to transfer that slab from updatePreamble to updateMain in parsing callbacks :/
Another layer to fix the issue would be to somehow make main file parsing, re-parse those macro definitions through replay mechanism or preamble patching, but these might have unwanted consequences as we would see a re-definition of the macro now. Also they are considerably hard :/
So it feels like the solution proposed here isn't so bad after all (unless there are other alternatives i am missing ATM). I would suggest having a separate container for macro definitions though, rather than modifying the existing "references container". We could directly have SymbolSlab/vector<Symbol> for the definitions and merge them with SymbolCollector's existing slab during the post-processing. That way we can also re-use the logic in SymbolCollector::handleMacroOccurence to generate a Symbol from MacroInfo.
On a side note, we can also easily skip the shard belonging to main file inside FileIndex::updatePreamble, which is going to be suppressed by new merged-index logic anyway. Though I think it deserves a separate patch. Also I don't fully grasp the workings of the suppression logic yet, so it might not be wise to do so if there are any cases in which we still merge information from static index.
Ah, this is a bit ugly... I'm not really happy with our representation of info extracted from the preprocessor, but that's something to tackle another time.
Thanks for fixing this!
clang-tools-extra/clangd/CollectMacros.h | ||
---|---|---|
27–33 | this pair should be a struct instead - MacroOccurrence? | |
37 | this should probably also be MacroOccurrence? | |
clang-tools-extra/clangd/index/SymbolCollector.cpp | ||
397–398 | this fixme is now addressed | |
404 | nit: prefer SourceLocation StartLoc = cantFail(sourceLocationMainFile(...)); | |
407 | nit: we have toSourceCode(SourceRange(StartLoc, EndLoc)) for this |
- std::pair<Range, bool> => struct MacroOccurrence
- remove addressed fixme comment
- assert() => cantFail()
- use toSourceCode() to get the macro name
this pair should be a struct instead - MacroOccurrence?