Skip to content

Commit b2bf21e

Browse files
Michael137kbluck
authored andcommitted
[lldb][TypeSystemClang] Allow transparent lookup through anonymous namespaces (llvm#97275)
This patch allows expressions to reference entities in anonymous namespaces. Previously this would have resulted in: ``` (lldb) expr foo::FooAnonymousVar error: <user expression 0>:1:6: no member named 'FooAnonymousVar' in namespace 'foo' 1 | foo::FooAnonymousVar | ~~~~~^ ``` We already allow such lookups through inline namespaces, and for the purposes of lookup, anonymous namespaces shouldn't behave any different. Fixes llvm#96963.
1 parent 5aadcc7 commit b2bf21e

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9484,14 +9484,24 @@ bool TypeSystemClang::DeclContextIsContainedInLookup(
94849484
auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
94859485
auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
94869486

9487+
// If we have an inline or anonymous namespace, then the lookup of the
9488+
// parent context also includes those namespace contents.
9489+
auto is_transparent_lookup_allowed = [](clang::DeclContext *DC) {
9490+
if (DC->isInlineNamespace())
9491+
return true;
9492+
9493+
if (auto const *NS = dyn_cast<NamespaceDecl>(DC))
9494+
return NS->isAnonymousNamespace();
9495+
9496+
return false;
9497+
};
9498+
94879499
do {
94889500
// A decl context always includes its own contents in its lookup.
94899501
if (decl_ctx == other)
94909502
return true;
9491-
9492-
// If we have an inline namespace, then the lookup of the parent context
9493-
// also includes the inline namespace contents.
9494-
} while (other->isInlineNamespace() && (other = other->getParent()));
9503+
} while (is_transparent_lookup_allowed(other) &&
9504+
(other = other->getParent()));
94959505

94969506
return false;
94979507
}

lldb/test/API/lang/cpp/namespace/TestNamespace.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,16 @@ def test_with_run_command(self):
253253
self.expect_expr(
254254
"((::B::Bar*)&::B::bar)->x()", result_type="int", result_value="42"
255255
)
256+
257+
self.expect_expr("InAnon1::var_in_anon", result_type="int", result_value="10")
258+
self.expect_expr(
259+
"InAnon1::InAnon2::var_in_anon", result_type="int", result_value="5"
260+
)
261+
self.expect_expr(
262+
"InAnon1::inline_ns::var_in_anon", result_type="int", result_value="15"
263+
)
264+
self.expect_expr(
265+
"InAnon1::inline_ns::InAnon2::var_in_anon",
266+
result_type="int",
267+
result_value="5",
268+
)

lldb/test/API/lang/cpp/namespace/main.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ struct Foo {
127127
};
128128
} // namespace NS2
129129

130+
namespace {
131+
namespace InAnon1 {
132+
int var_in_anon = 10;
133+
namespace {
134+
inline namespace inline_ns {
135+
int var_in_anon = 15;
136+
namespace InAnon2 {
137+
namespace {
138+
int var_in_anon = 5;
139+
} // namespace
140+
} // namespace InAnon2
141+
} // namespace inline_ns
142+
} // namespace
143+
} // namespace InAnon1
144+
} // namespace
145+
130146
int
131147
main (int argc, char const *argv[])
132148
{
@@ -140,5 +156,6 @@ main (int argc, char const *argv[])
140156
::B::Bar bb;
141157
A::B::Bar ab;
142158
return Foo::myfunc(12) + bb.x() + ab.y() + NS1::NS2::Foo{}.bar() +
143-
NS2::Foo{}.bar();
159+
NS2::Foo{}.bar() + InAnon1::var_in_anon +
160+
InAnon1::InAnon2::var_in_anon + InAnon1::inline_ns::var_in_anon;
144161
}

0 commit comments

Comments
 (0)