This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Make use of token buffers in semantic highlighting
ClosedPublic

Authored by kadircet on Mar 2 2020, 4:21 AM.

Diff Detail

Event Timeline

kadircet created this revision.Mar 2 2020, 4:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2020, 4:21 AM
kadircet updated this revision to Diff 247612.Mar 2 2020, 5:22 AM
  • Add forgetten macroid check, before looking for an macroargexpansion.
sammccall added inline comments.Mar 2 2020, 5:54 AM
clang-tools-extra/clangd/SemanticHighlighting.cpp
149–151

nit: this can be const

158–159

I think using TokenBuffer to translate expanded -> spelled isn't actually an improvement here as the API exposes several possibilities that can't actually happen in our case.

The specialized translation from expanded to spelled location is *policy*, and it's pretty easy to implement I think:

// For a macro usage `DUMP(foo)`, we want:
//  - DUMP --> "macro"
//  - foo --> "variable".
SourceLocation getHighlightableSpellingToken(SourceLocation L) {
  if (L.isFileID()) return SM.isWrittenInMainFile(L) ? L : {};
  // Tokens expanded from the macro body contribute no highlightings.
  if (!SM.isMacroArgExpansion(L)) return {};
  // Tokens expanded from macro args are potentially highlightable.
  return getHighlightableSpellingToken(SM.getImmediateSpellingLocation(L));
}

once you have the spelling location, getting the range from tokenbuffer is easy :-)

164

only if this is a macro!

kadircet updated this revision to Diff 247695.Mar 2 2020, 11:24 AM
kadircet marked 2 inline comments as done.
  • Address comments
kadircet marked an inline comment as done.Mar 2 2020, 11:33 AM
sammccall accepted this revision.Mar 2 2020, 12:40 PM
sammccall added inline comments.
clang-tools-extra/clangd/SemanticHighlighting.cpp
165

&& Tok->location() == Loc, right?

(I think this would make a nice method TokenBuffer::spelledTokenAt(SourceLocation) -> const Token*)

This revision is now accepted and ready to land.Mar 2 2020, 12:40 PM
kadircet updated this revision to Diff 247805.Mar 2 2020, 11:58 PM
  • Use spelledTokenAt
kadircet marked an inline comment as done.Mar 3 2020, 12:00 AM
sammccall accepted this revision.Mar 3 2020, 12:14 AM
This revision was automatically updated to reflect the committed changes.