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.");
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.");
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits