[clangd] Fix a selection tree crash for unmatched-bracket code.

Fixes https://github.com/clangd/clangd/issues/999

Differential Revision: https://reviews.llvm.org/D118322
This commit is contained in:
Haojian Wu 2022-01-27 08:43:31 +01:00
parent 39057240f5
commit 4cb1686bfe
2 changed files with 12 additions and 0 deletions

View File

@ -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.

View File

@ -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) {