[clang-tools-extra] 3d73548 - [clangd] SelectionTree should prefer lexical declcontext
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 28 08:40:01 PDT 2021
Author: Kadir Cetinkaya
Date: 2021-10-28T17:39:35+02:00
New Revision: 3d735480bd2a720af2126c80da58371690e6a02e
URL: https://github.com/llvm/llvm-project/commit/3d735480bd2a720af2126c80da58371690e6a02e
DIFF: https://github.com/llvm/llvm-project/commit/3d735480bd2a720af2126c80da58371690e6a02e.diff
LOG: [clangd] SelectionTree should prefer lexical declcontext
This is important especially for code that tries to traverse scopes as
written in code, which is the contract SelectionTree tries to satisfy.
Differential Revision: https://reviews.llvm.org/D112712
Added:
Modified:
clang-tools-extra/clangd/Selection.cpp
clang-tools-extra/clangd/Selection.h
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 a53673e074804..88ab1c5d058ee 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -897,7 +897,7 @@ const DeclContext &SelectionTree::Node::getDeclContext() const {
if (CurrentNode != this)
if (auto *DC = dyn_cast<DeclContext>(Current))
return *DC;
- return *Current->getDeclContext();
+ return *Current->getLexicalDeclContext();
}
}
llvm_unreachable("A tree must always be rooted at TranslationUnitDecl.");
diff --git a/clang-tools-extra/clangd/Selection.h b/clang-tools-extra/clangd/Selection.h
index 775006f40f6cc..f80ee83c4b94c 100644
--- a/clang-tools-extra/clangd/Selection.h
+++ b/clang-tools-extra/clangd/Selection.h
@@ -128,8 +128,8 @@ class SelectionTree {
DynTypedNode ASTNode;
// The extent to which this node is covered by the selection.
Selection Selected;
- // Walk up the AST to get the DeclContext of this Node,
- // which is not the node itself.
+ // Walk up the AST to get the lexical DeclContext of this Node, which is not
+ // the node itself.
const DeclContext &getDeclContext() const;
// Printable node kind, like "CXXRecordDecl" or "AutoTypeLoc".
std::string kind() const;
diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index 1c3bc20958486..6c6782a097db5 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -739,6 +739,21 @@ TEST(SelectionTest, CreateAll) {
EXPECT_EQ(1u, Seen) << "one tree for nontrivial selection";
}
+TEST(SelectionTest, DeclContextIsLexical) {
+ llvm::Annotations Test("namespace a { void $1^foo(); } void a::$2^foo();");
+ 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());
+ }
+ {
+ auto ST = SelectionTree::createRight(AST.getASTContext(), AST.getTokens(),
+ Test.point("2"), Test.point("2"));
+ EXPECT_TRUE(ST.commonAncestor()->getDeclContext().isTranslationUnit());
+ }
+}
+
} // namespace
} // namespace clangd
} // namespace clang
More information about the cfe-commits
mailing list