Skip to content

Commit 46575f6

Browse files
v1nh1shungrytom-anders
authored andcommitted
[clangd] Fix action RemoveUsingNamespace for inline namespace
Existing version ignores symbols declared in an inline namespace `ns` when removing `using namespace ns` Reviewed By: tom-anders Differential Revision: https://reviews.llvm.org/D138028
1 parent 25671db commit 46575f6

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,15 @@ bool isTopLevelDecl(const SelectionTree::Node *Node) {
8989
return Node->Parent && Node->Parent->ASTNode.get<TranslationUnitDecl>();
9090
}
9191

92-
// Returns the first visible context that contains this DeclContext.
93-
// For example: Returns ns1 for S1 and a.
94-
// namespace ns1 {
95-
// inline namespace ns2 { struct S1 {}; }
96-
// enum E { a, b, c, d };
97-
// }
98-
const DeclContext *visibleContext(const DeclContext *D) {
99-
while (D->isInlineNamespace() || D->isTransparentContext())
92+
// Return true if `LHS` is declared in `RHS`
93+
bool isDeclaredIn(const NamedDecl *LHS, const DeclContext *RHS) {
94+
const auto *D = LHS->getDeclContext();
95+
while (D->isInlineNamespace() || D->isTransparentContext()) {
96+
if (D->Equals(RHS))
97+
return true;
10098
D = D->getParent();
101-
return D;
99+
}
100+
return D->Equals(RHS);
102101
}
103102

104103
bool RemoveUsingNamespace::prepare(const Selection &Inputs) {
@@ -152,8 +151,7 @@ Expected<Tweak::Effect> RemoveUsingNamespace::apply(const Selection &Inputs) {
152151
return; // This reference is already qualified.
153152

154153
for (auto *T : Ref.Targets) {
155-
if (!visibleContext(T->getDeclContext())
156-
->Equals(TargetDirective->getNominatedNamespace()))
154+
if (!isDeclaredIn(T, TargetDirective->getNominatedNamespace()))
157155
return;
158156
auto Kind = T->getDeclName().getNameKind();
159157
// Avoid adding qualifiers before operators, e.g.

clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ TEST_F(RemoveUsingNamespaceTest, All) {
264264
}
265265
266266
int main() { 1.5_w; }
267+
)cpp"},
268+
{
269+
R"cpp(
270+
namespace a { inline namespace b { void foobar(); } }
271+
using namespace a::[[b]];
272+
int main() { foobar(); }
273+
)cpp",
274+
R"cpp(
275+
namespace a { inline namespace b { void foobar(); } }
276+
277+
int main() { a::b::foobar(); }
267278
)cpp"}};
268279
for (auto C : Cases)
269280
EXPECT_EQ(C.second, apply(C.first)) << C.first;

0 commit comments

Comments
 (0)