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 @@ -794,13 +794,16 @@ } // 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 @@ 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()) { + // 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(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). 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 @@ -516,6 +516,13 @@ 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) {