diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp index 6dfaa8be9591..dbfe05c8e8b5 100644 --- a/clang-tools-extra/clangd/Selection.cpp +++ b/clang-tools-extra/clangd/Selection.cpp @@ -306,6 +306,14 @@ public: 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. + // FIXME: fix it in TokenBuffer, expandedTokens(SourceRange) should not + // return the eof token. + 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 index 30e0ff0e41b4..73d6d0d23827 100644 --- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp +++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp @@ -523,6 +523,10 @@ TEST(SelectionTest, CommonAncestor) { auto l = [^[[foo = bar]]] { }; })cpp", "VarDecl"}, + {R"cpp( + /*error-ok*/ + void func() [[{^]])cpp", + "CompoundStmt"}, }; for (const Case &C : Cases) {