[PATCH] D139926: [clangd] Add semantic token for angle brackets

Christian Kandeler via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 31 06:38:46 PST 2023


ckandeler added inline comments.


================
Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:382
+
+    // For the inner element of a nested template instantiation with no space
+    // between the '>' characters, TemplateSpecializationLocInfo::RAngleLoc has
----------------
kadircet wrote:
> i am actually having a hard time following the logic here. it feels like what you really want is map `>` back to its file location, possibly to a `>>` token, and deal with second case specially.
> so maybe something like:
> ```
> // RLoc might be pointing at a virtual buffer when it's part of a `>>` token.
> RLoc = SM.getFileLoc(RLoc);
> // Make sure token is part of the main file.
> RLoc = getHighlightableSpellingToken(RLoc);
> if(!RLoc.isValid())
>   return;
> 
> const auto *RTok = TB.spelledTokenAt(RLoc);
> // Handle `>>`. RLoc is always pointing at the right location, just change
> // the end to be offset by 1.
> // We'll either point at the beginning of `>>`, hence get a proper spelled
> // or point in the middle of `>>` hence get no spelled tok.
> if (!RTok || RTok->kind() == tok::greatergreater) {
>   Position Begin = sourceLocToPosition(SourceMgr, RLoc);
>   Position End = sourceLocToPosition(SourceMgr, RLoc.getLocWithOffset(1));
>   addToken(*LRange, HighlightingKind::Bracket);
>   addToken({Begin, End}, HighlightingKind::Bracket);
>   return;
> }
> 
> // Easy case, we have the `>` token directly available.
> if (RTok->kind() == tok::greater) {
>   if (auto RRange = getRangeForSourceLocation(RLoc)) {
>     addToken(*LRange, HighlightingKind::Bracket);
>     addToken(*RRange, HighlightingKind::Bracket);
>   }
>   return;
> }
> ```
> 
> This should also make sure you're handling line continuations properly.
Indeed, thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139926/new/

https://reviews.llvm.org/D139926



More information about the cfe-commits mailing list