This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Fix Origin and MainFileOnly-ness for macros
ClosedPublic

Authored by kadircet on Jul 22 2020, 1:43 AM.

Details

Summary

This was resulting in macros coming from preambles vanishing when user
have opened the source header. For example:

// test.h:

and

// test.cc
#include "test.h"
^

If user only opens test.cc, we'll get X as a completion candidate,
since it is indexed as part of the preamble. But if the user opens
test.h afterwards we would index it as part of the main file and lose
the symbol (as new index shard for test.h will override the existing one
in dynamic index).

Also we were not setting origins for macros correctly, this patch also
fixes it.

Fixes https://github.com/clangd/clangd/issues/461

Diff Detail

Event Timeline

kadircet created this revision.Jul 22 2020, 1:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 22 2020, 1:43 AM
kadircet edited the summary of this revision. (Show Details)Jul 22 2020, 1:44 AM
hokein accepted this revision.Jul 22 2020, 2:10 AM

thanks!

the test.h in the patch description is missing a #define X.

clang-tools-extra/clangd/index/SymbolCollector.cpp
383

nit: move this var to Line 398, in some cases (builtin macros), it is not used, so would save some cost.

this is duplicated with the one in handleDeclOccurrence, creating a new function seems not worthy...

This revision is now accepted and ready to land.Jul 22 2020, 2:10 AM
kadircet updated this revision to Diff 279731.Jul 22 2020, 2:24 AM
kadircet marked 2 inline comments as done.
  • Move declarations closer to use.

the test.h in the patch description is missing a #define X.

ah right, git descriptions omitting lines starting with # fixed it for include, but missed the define :face_palm:

clang-tools-extra/clangd/index/SymbolCollector.cpp
383

yeah and these are slightly different, the one in declOccurence makes sure the decl is written inside the main file, whereas this one also accepts remappings through #line directives.

not sure if it is necessary, but it was the old behavior, so I am just keeping it, no tests seem to break and we already do not index builtin macros or the ones spelled in a builtin-file. I don't think line directives are widely used within the rest, but again such a change would deserve a separate patch.

This revision was automatically updated to reflect the committed changes.