[clangd] Fix a crash in semantic highlighting.

We encounter a few internal reports that we're dereference a nullptr.
Unfortunately, no small reproduce testcase for this crash yet, but it makes the
clangd more robost on broken code.

Differential Revision: https://reviews.llvm.org/D143486
This commit is contained in:
Haojian Wu 2023-02-07 12:40:00 +01:00
parent 4da283872a
commit 41a6085252

View File

@ -519,10 +519,11 @@ private:
Loc = getHighlightableSpellingToken(Loc, SourceMgr);
if (Loc.isInvalid())
return std::nullopt;
// We might have offsets in the main file that don't correspond to any
// spelled tokens.
const auto *Tok = TB.spelledTokenAt(Loc);
assert(Tok);
if (!Tok)
return std::nullopt;
return halfOpenToRange(SourceMgr,
Tok->range(SourceMgr).toCharRange(SourceMgr));
}