This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Add main file macros into the main-file index.
ClosedPublic

Authored by ArcsinX on Jan 12 2021, 1:53 AM.

Details

Summary

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.

Diff Detail

Event Timeline

ArcsinX created this revision.Jan 12 2021, 1:53 AM
ArcsinX requested review of this revision.Jan 12 2021, 1:53 AM

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.

sammccall accepted this revision.Jan 12 2021, 4:52 AM

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

This revision is now accepted and ready to land.Jan 12 2021, 4:52 AM
ArcsinX updated this revision to Diff 316331.Jan 12 2021, 11:49 PM
  • std::pair<Range, bool> => struct MacroOccurrence
  • remove addressed fixme comment
  • assert() => cantFail()
  • use toSourceCode() to get the macro name
ArcsinX marked 5 inline comments as done.Jan 12 2021, 11:51 PM
This revision was automatically updated to reflect the committed changes.