[clangd] Bring back early-claim approach to fix a selection-tree regression.

The early-claim hack was removed in 96f5cc1ee4,
we see a regression about captured var-decl in lambda.

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

Differential Revision: https://reviews.llvm.org/D117472
This commit is contained in:
Haojian Wu 2022-01-17 14:27:29 +01:00
parent 202d590a01
commit fd598e1859
2 changed files with 30 additions and 0 deletions

View File

@ -794,13 +794,16 @@ private:
}
// Pushes a node onto the ancestor stack. Pairs with pop().
// Performs early hit detection for some nodes (on the earlySourceRange).
void push(DynTypedNode Node) {
SourceRange Early = earlySourceRange(Node);
dlog("{1}push: {0}", printNodeToString(Node, PrintPolicy), indent());
Nodes.emplace_back();
Nodes.back().ASTNode = std::move(Node);
Nodes.back().Parent = Stack.top();
Nodes.back().Selected = NoTokens;
Stack.push(&Nodes.back());
claimRange(Early, Nodes.back().Selected);
}
// Pops a node off the ancestor stack, and finalizes it. Pairs with push().
@ -822,6 +825,26 @@ private:
Stack.pop();
}
// Returns the range of tokens that this node will claim directly, and
// is not available to the node's children.
// Usually empty, but sometimes children cover tokens but shouldn't own them.
SourceRange earlySourceRange(const DynTypedNode &N) {
if (const Decl *D = N.get<Decl>()) {
// We want the name in the var-decl to be claimed by the decl itself and
// not by any children. Ususally, we don't need this, because source
// ranges of children are not overlapped with their parent's.
// An exception is lambda captured var decl, where AutoTypeLoc is
// overlapped with the name loc.
// auto fun = [bar = foo]() { ... }
// ~~~~~~~~~ VarDecl
// ~~~ |- AutoTypeLoc
if (const auto *DD = llvm::dyn_cast<VarDecl>(D))
return DD->getLocation();
}
return SourceRange();
}
// Claim tokens for N, after processing its children.
// By default this claims all unclaimed tokens in getSourceRange().
// We override this if we want to claim fewer tokens (e.g. there are gaps).

View File

@ -516,6 +516,13 @@ TEST(SelectionTest, CommonAncestor) {
enum Bar : [[Fo^o]];
)cpp",
"TypedefTypeLoc"},
// lambda captured var-decl
{R"cpp(
void test(int bar) {
auto l = [^[[foo = bar]]] { };
})cpp",
"VarDecl"},
};
for (const Case &C : Cases) {