[clang-tools-extra] [clangd] Fix crash with null check for Token at Loc (PR #94528)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 5 22:37:34 PDT 2024


================
@@ -1354,6 +1354,8 @@ maybeFindIncludeReferences(ParsedAST &AST, Position Pos,
 
         ReferencesResult::Reference Result;
         const auto *Token = AST.getTokens().spelledTokenAt(Loc);
+        if (!Token)
----------------
kadircet wrote:

it's a little bit hard to see what issue we're fixing/preventing here, can you add some context?

Looking at the test case, i guess we're getting a reference for the UDLs pointing at the middle of a token?

If that's the case, i think we might as well fix the feature instead of working around. Moreover I don't think that broken behavior is specific to xrefs on includes, it'll show up on other places that use `spelledTokenAt` too.

In this code path particularly, we actually don't want a `Token`, we can just use `Loc` and `Lexer::getLocForEndOfToken(Loc)` for the `range` calculation below.

I think a more complete fix would be to actually change `spelledTokenAt` to return the `Token` as long as `Loc` is contained (or have a new method that returns the token containing the location). That one is more involved though, so if you choose not to do that, can you at least file a bug around clangd features not working properly with UDLs pointing into this PR?

https://github.com/llvm/llvm-project/pull/94528


More information about the cfe-commits mailing list