[clang] [clang-tools-extra] [clangd] Handle an expanded token range that ends in the `eof` token in TokenBuffer::spelledForExpanded() (PR #78092)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 09:09:27 PST 2024


================
@@ -816,6 +816,18 @@ TEST_F(TokenBufferTest, SpelledByExpanded) {
   EXPECT_EQ(Buffer.spelledForExpanded(findExpanded("prev good")), std::nullopt);
 }
 
+TEST_F(TokenBufferTest, NoCrashForEofToken) {
+  recordTokens(R"cpp(
+    int main() {
+  )cpp");
+  // Calling spelledForExpanded() on the entire range of expanded tokens (which
+  // includes the `eof` token at the end) produces the range of all the spelled
+  // tokens (the `eof` is ignored).
+  EXPECT_THAT(
----------------
ilya-biryukov wrote:

NIT: it's redundant, but for documentation purposes I suggest to add a sanity check:
```cpp
ASSERT(!Buffers.expandedToken().empty());
ASSERT_EQ(Buffers.expandedToken().back().kind(), tok::eof);
```

This should allow to shorten the comment above a bit too.

But also feel free to leave as is.

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


More information about the cfe-commits mailing list