This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Better handling symbols defined in macros.
ClosedPublic

Authored by hokein on Jan 26 2018, 5:48 AM.

Details

Summary

For symbols defined inside macros:

  • use expansion location, if the symbol is formed via macro concatenation.
  • use spelling location, otherwise.

This will fix some symbols that have ill-format location (especial invalid filepath).

Diff Detail

Repository
rL LLVM

Event Timeline

hokein created this revision.Jan 26 2018, 5:48 AM
ioeric added inline comments.Jan 29 2018, 3:12 AM
clangd/index/SymbolCollector.cpp
108 ↗(On Diff #131578)

PrintLoc? PrintableLoc sounds like a checker for whether a location is printable.

110 ↗(On Diff #131578)

This works for the use case below (i.e. to get around "<scratch" prefix check) but might cause problem when it's used else where. Please clearly document the behavior, or I'd probably make this a lambda inside GetSymbolLocation.

118 ↗(On Diff #131578)

Pull the comments in the patch summary here?

127 ↗(On Diff #131578)

To reduce some code, consider

Loc, Start, End = spelling locs;
if (D->getLocation().isMacroID() && llvm::StringRef(PrintableLoc(Loc, SM)).startswith("<scratch")) {
  Loc, Start, End = expension locs;
}
// Generate SymbolLocation with Loc, Start, End

Also, I think you could use getLocStart in place of getLocation?

unittests/clangd/Annotations.h
63 ↗(On Diff #131578)

This doesn't seem to be necessary? I think you could use the range method above and convert it to offsets in the test matcher.

unittests/clangd/SymbolCollectorTests.cpp
216–219 ↗(On Diff #131578)

Consider adding another expansion and spelling in the main file (with IndexMainFiles enabled), so that we could also check file paths are handled correctly?

hokein updated this revision to Diff 132122.Jan 31 2018, 2:23 AM
hokein marked 4 inline comments as done.
  • address review comments.
  • add tests for indexing main files
  • use SourceLocation.printToString
hokein updated this revision to Diff 132123.Jan 31 2018, 2:25 AM

Update test.

clangd/index/SymbolCollector.cpp
108 ↗(On Diff #131578)

SourceLocation has provided printToString API already, we don't need to reinvent the wheel now.

127 ↗(On Diff #131578)

There is no much duplicated code among the cases here (only one line of generating the SymbolLocation). I prefer to the current way -- it shows the logic a bit clearer, and also saves the cost of getSpellingLoc for the macro case.

unittests/clangd/Annotations.h
63 ↗(On Diff #131578)

I think this is a useful method (probably be used in the future). Putting it to test matcher, we have to pass Code parameter to the matcher (LocationOffset(range, Header.code())) everywhere. Annotation seems to be the best place.

ioeric accepted this revision.Jan 31 2018, 3:26 AM

lg

clangd/index/SymbolCollector.cpp
127 ↗(On Diff #131578)

I still think there is value in reducing the duplicates. For example, when the interface of makeAbsolutePath or SymbolLocation changes, we would only need to modify one place rather than two.

I wouldn't worry about the cost of getSpellingLoc for the macro case since it is a cheap operation for a rare case anyway...

unittests/clangd/Annotations.h
63 ↗(On Diff #131578)

I see. Didn't realize code is needed.

unittests/clangd/SymbolCollectorTests.cpp
240 ↗(On Diff #132123)

Maybe put this in the header file to make the test more meaningful?

This revision is now accepted and ready to land.Jan 31 2018, 3:26 AM
This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.