Skip to content

[lldb][TypeSystemClang] Allow transparent lookup through anonymous namespaces #97275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9484,14 +9484,24 @@ bool TypeSystemClang::DeclContextIsContainedInLookup(
auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
auto *other = (clang::DeclContext *)other_opaque_decl_ctx;

// If we have an inline or anonymous namespace, then the lookup of the
// parent context also includes those namespace contents.
auto is_transparent_lookup_allowed = [](clang::DeclContext *DC) {
if (DC->isInlineNamespace())
return true;

if (auto const *NS = dyn_cast<NamespaceDecl>(DC))
return NS->isAnonymousNamespace();

return false;
};

do {
// A decl context always includes its own contents in its lookup.
if (decl_ctx == other)
return true;

// If we have an inline namespace, then the lookup of the parent context
// also includes the inline namespace contents.
} while (other->isInlineNamespace() && (other = other->getParent()));
} while (is_transparent_lookup_allowed(other) &&
(other = other->getParent()));

return false;
}
Expand Down
13 changes: 13 additions & 0 deletions lldb/test/API/lang/cpp/namespace/TestNamespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,16 @@ def test_with_run_command(self):
self.expect_expr(
"((::B::Bar*)&::B::bar)->x()", result_type="int", result_value="42"
)

self.expect_expr("InAnon1::var_in_anon", result_type="int", result_value="10")
self.expect_expr(
"InAnon1::InAnon2::var_in_anon", result_type="int", result_value="5"
)
self.expect_expr(
"InAnon1::inline_ns::var_in_anon", result_type="int", result_value="15"
)
self.expect_expr(
"InAnon1::inline_ns::InAnon2::var_in_anon",
result_type="int",
result_value="5",
)
19 changes: 18 additions & 1 deletion lldb/test/API/lang/cpp/namespace/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ struct Foo {
};
} // namespace NS2

namespace {
namespace InAnon1 {
int var_in_anon = 10;
namespace {
inline namespace inline_ns {
int var_in_anon = 15;
namespace InAnon2 {
namespace {
int var_in_anon = 5;
} // namespace
} // namespace InAnon2
} // namespace inline_ns
} // namespace
} // namespace InAnon1
} // namespace

int
main (int argc, char const *argv[])
{
Expand All @@ -140,5 +156,6 @@ main (int argc, char const *argv[])
::B::Bar bb;
A::B::Bar ab;
return Foo::myfunc(12) + bb.x() + ab.y() + NS1::NS2::Foo{}.bar() +
NS2::Foo{}.bar();
NS2::Foo{}.bar() + InAnon1::var_in_anon +
InAnon1::InAnon2::var_in_anon + InAnon1::inline_ns::var_in_anon;
}
Loading