From fd598e185972f76a50c45e1402ab3b0fd70664b9 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Mon, 17 Jan 2022 14:27:29 +0100 Subject: [PATCH] [clangd] Bring back early-claim approach to fix a selection-tree regression. The early-claim hack was removed in 96f5cc1ee417f863f85756d1e56b1bed1bd76a7e, 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 --- clang-tools-extra/clangd/Selection.cpp | 23 +++++++++++++++++++ .../clangd/unittests/SelectionTests.cpp | 7 ++++++ 2 files changed, 30 insertions(+) diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp index da80e01a926c..69e99a9a8e28 100644 --- a/clang-tools-extra/clangd/Selection.cpp +++ b/clang-tools-extra/clangd/Selection.cpp @@ -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()) { + // 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 index c4050b2fc2ba..30e0ff0e41b4 100644 --- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp +++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp @@ -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) {