Author: Haojian Wu Date: 2022-01-18T10:22:26+01:00 New Revision: fd598e185972f76a50c45e1402ab3b0fd70664b9
URL: https://github.com/llvm/llvm-project/commit/fd598e185972f76a50c45e1402ab3b0fd70664b9 DIFF: https://github.com/llvm/llvm-project/commit/fd598e185972f76a50c45e1402ab3b0fd70664b9.diff LOG: [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 Added: Modified: clang-tools-extra/clangd/Selection.cpp clang-tools-extra/clangd/unittests/SelectionTests.cpp Removed: ################################################################################ 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 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> { } // 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 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> { 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). 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) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits