[PATCH] D112712: [clangd] SelectionTree should prefer lexical declcontext
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 28 05:58:47 PDT 2021
kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added subscribers: usaxena95, arphaman.
kadircet requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.
This is important especially for code that tries to traverse scopes as
written in code, which is the contract SelectionTree tries to satisfy.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D112712
Files:
clang-tools-extra/clangd/Selection.cpp
clang-tools-extra/clangd/unittests/SelectionTests.cpp
Index: clang-tools-extra/clangd/unittests/SelectionTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -739,6 +739,21 @@
EXPECT_EQ(1u, Seen) << "one tree for nontrivial selection";
}
+TEST(SelectionTest, DeclContext) {
+ 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
Index: clang-tools-extra/clangd/Selection.cpp
===================================================================
--- clang-tools-extra/clangd/Selection.cpp
+++ clang-tools-extra/clangd/Selection.cpp
@@ -897,7 +897,7 @@
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.");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112712.383007.patch
Type: text/x-patch
Size: 1568 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211028/8d2d0617/attachment-0001.bin>
More information about the cfe-commits
mailing list