[clang-tools-extra] [clangd] Handle lambda scopes inside Node::getDeclContext() (PR #76329)
Younan Zhang via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 24 07:51:07 PST 2023
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/76329
>From c0b04507075b39e494d3e6fa39168bc47b92f338 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Sun, 24 Dec 2023 18:08:30 +0800
Subject: [PATCH 1/2] [clangd] Handle lambda scopes inside
Node::getDeclContext()
We used to consider the DeclContext for selection nodes inside
a lambda as the scope of the lambda expression locates in.
For example,
```cpp
void foo();
auto lambda = [] {
return ^foo();
};
```
where N is the selection node for the expression `foo()`,
`N.getDeclContext()` returns the TranslationUnitDecl previously,
which IMO is wrong, since the RecordDecl of the lambda is closer.
---
clang-tools-extra/clangd/Selection.cpp | 3 +++
.../clangd/unittests/SelectionTests.cpp | 13 +++++++++++++
2 files changed, 16 insertions(+)
diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp
index 8c6d5750ecefdb..a7413485b04ac3 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -1113,6 +1113,9 @@ const DeclContext &SelectionTree::Node::getDeclContext() const {
return *DC;
return *Current->getLexicalDeclContext();
}
+ if (auto *LE = CurrentNode->ASTNode.get<LambdaExpr>())
+ if (CurrentNode != this)
+ return *LE->getLambdaClass();
}
llvm_unreachable("A tree must always be rooted at TranslationUnitDecl.");
}
diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index 4c019a1524f3c3..cdb50dfc1c3095 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -880,6 +880,19 @@ TEST(SelectionTest, DeclContextIsLexical) {
}
}
+TEST(SelectionTest, DeclContextLambda) {
+ llvm::Annotations Test(R"cpp(
+ void foo();
+ auto lambda = [] {
+ return $1^foo();
+ };
+ )cpp");
+ auto AST = TestTU::withCode(Test.code()).build();
+ auto ST = SelectionTree::createRight(AST.getASTContext(), AST.getTokens(),
+ Test.point("1"), Test.point("1"));
+ EXPECT_FALSE(ST.commonAncestor()->getDeclContext().isTranslationUnit());
+}
+
} // namespace
} // namespace clangd
} // namespace clang
>From 18274ed552f955172a788391393c58f8e32a65a6 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Sun, 24 Dec 2023 23:50:10 +0800
Subject: [PATCH 2/2] DC should be operator()
---
clang-tools-extra/clangd/Selection.cpp | 4 ++--
clang-tools-extra/clangd/unittests/SelectionTests.cpp | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp
index a7413485b04ac3..277cb8769a1b12 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -1113,9 +1113,9 @@ const DeclContext &SelectionTree::Node::getDeclContext() const {
return *DC;
return *Current->getLexicalDeclContext();
}
- if (auto *LE = CurrentNode->ASTNode.get<LambdaExpr>())
+ if (const auto *LE = CurrentNode->ASTNode.get<LambdaExpr>())
if (CurrentNode != this)
- return *LE->getLambdaClass();
+ return *LE->getCallOperator();
}
llvm_unreachable("A tree must always be rooted at TranslationUnitDecl.");
}
diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index cdb50dfc1c3095..754e8c287c5148 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -890,7 +890,7 @@ TEST(SelectionTest, DeclContextLambda) {
auto AST = TestTU::withCode(Test.code()).build();
auto ST = SelectionTree::createRight(AST.getASTContext(), AST.getTokens(),
Test.point("1"), Test.point("1"));
- EXPECT_FALSE(ST.commonAncestor()->getDeclContext().isTranslationUnit());
+ EXPECT_TRUE(ST.commonAncestor()->getDeclContext().isFunctionOrMethod());
}
} // namespace
More information about the cfe-commits
mailing list