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

Conversation

Michael137
Copy link
Member

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 #96963.

…mespaces

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.
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2024

@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)

Changes

This patch allows expressions to reference entities in anonymous namespaces. Previously this would have resulted in:

(lldb) expr foo::FooAnonymousVar
error: &lt;user expression 0&gt;: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 #96963.


Full diff: https://github.com/llvm/llvm-project/pull/97275.diff

3 Files Affected:

  • (modified) lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp (+14-4)
  • (modified) lldb/test/API/lang/cpp/namespace/TestNamespace.py (+5)
  • (modified) lldb/test/API/lang/cpp/namespace/main.cpp (+18-1)
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index cd1c500d9aa29..093d27a92d718 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -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;
 }
diff --git a/lldb/test/API/lang/cpp/namespace/TestNamespace.py b/lldb/test/API/lang/cpp/namespace/TestNamespace.py
index d747e2be77c8e..83bfe173658cc 100644
--- a/lldb/test/API/lang/cpp/namespace/TestNamespace.py
+++ b/lldb/test/API/lang/cpp/namespace/TestNamespace.py
@@ -253,3 +253,8 @@ 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")
diff --git a/lldb/test/API/lang/cpp/namespace/main.cpp b/lldb/test/API/lang/cpp/namespace/main.cpp
index 6a8efa160766b..2edfab8437639 100644
--- a/lldb/test/API/lang/cpp/namespace/main.cpp
+++ b/lldb/test/API/lang/cpp/namespace/main.cpp
@@ -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[])
 {
@@ -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;
 }

Copy link

github-actions bot commented Jul 1, 2024

✅ With the latest revision this PR passed the Python code formatter.

@Michael137 Michael137 merged commit 65c807e into llvm:main Jul 1, 2024
6 checks passed
@Michael137 Michael137 deleted the lldb/lookup-in-anon-namespace branch July 1, 2024 15:22
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
…mespaces (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.
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
…mespaces (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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[lldb] Add support for evaluating expressions in anonymous namespaces.
3 participants