[clang] [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
Thu Jun 6 06:27:42 PDT 2024


================
@@ -387,8 +387,8 @@ const syntax::Token *TokenBuffer::spelledTokenAt(SourceLocation Loc) const {
   assert(Loc.isFileID());
   const auto *Tok = llvm::partition_point(
       spelledTokens(SourceMgr->getFileID(Loc)),
-      [&](const syntax::Token &Tok) { return Tok.location() < Loc; });
-  if (!Tok || Tok->location() != Loc)
+      [&](const syntax::Token &Tok) { return Tok.endLocation() <= Loc; });
+  if (!Tok || Tok->location() > Loc || Loc >= Tok->endLocation())
----------------
kadircet wrote:

i don't think `Loc >= Tok->endLocation()` can ever be true. `partition_point` will return either null or a `Tok` s.t. `Loc < Tok.endLocation()`.

nit: can you re-write the previous condition as `Loc < Tok->location()`, i think it's easier to visualize the relationship when it's left-to-right :D

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


More information about the cfe-commits mailing list