diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp --- a/clang-tools-extra/clangd/Selection.cpp +++ b/clang-tools-extra/clangd/Selection.cpp @@ -306,6 +306,12 @@ return SelectionTree::Unselected; } + // The eof token is used as a sentinel. + // In general, source range from an AST node should not claim the eof token, + // but it could occur for unmatched-bracket cases. + if (ExpandedTokens.back().kind() == tok::eof) + ExpandedTokens = ExpandedTokens.drop_back(); + SelectionTree::Selection Result = NoTokens; while (!ExpandedTokens.empty()) { // Take consecutive tokens from the same context together for efficiency. diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp --- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp +++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp @@ -523,6 +523,10 @@ auto l = [^[[foo = bar]]] { }; })cpp", "VarDecl"}, + {R"cpp( + /*error-ok*/ + void func() [[{^]])cpp", + "CompoundStmt"}, }; for (const Case &C : Cases) {