[clang] Raise an error on namespace aliases with qualified names. (PR #86122)

Daniel M. Katz via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 21 09:40:39 PDT 2024


https://github.com/katzdm updated https://github.com/llvm/llvm-project/pull/86122

>From 1998809477ce9ef03516278e1f0e9084426ea7f2 Mon Sep 17 00:00:00 2001
From: Dan Katz <katzdm at gmail.com>
Date: Thu, 21 Mar 2024 09:47:04 -0400
Subject: [PATCH 1/4] Raise an error on namespace aliases with qualified names.

Current behavior is to ignore the trailing qualifiers, but the grammar
for `namespace-alias-definition` requires an `identifier` without
qualification.

https://godbolt.org/z/1zvW5q4f8

https://eel.is/c++draft/namespace.alias#nt:namespace-alias-definition
---
 clang/include/clang/Basic/DiagnosticParseKinds.td | 2 ++
 clang/lib/Parse/ParseDeclCXX.cpp                  | 5 +++++
 clang/test/SemaCXX/namespace-alias.cpp            | 2 ++
 3 files changed, 9 insertions(+)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 48de5e2ef5f4af..5626307c80c2df 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -268,6 +268,8 @@ def err_expected_semi_after_namespace_name : Error<
   "expected ';' after namespace name">;
 def err_unexpected_namespace_attributes_alias : Error<
   "attributes cannot be specified on namespace alias">;
+def err_unexpected_qualified_namespace_alias : Error<
+  "unexpected nested name specifier in namespace alias definition">;
 def err_unexpected_nested_namespace_attribute : Error<
   "attributes cannot be specified on a nested namespace definition">;
 def err_inline_namespace_alias : Error<"namespace alias cannot be inline">;
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 77d2382ea6d907..0ef2f2fad2a9d1 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -140,6 +140,11 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
       SkipUntil(tok::semi);
       return nullptr;
     }
+    if (!ExtraNSs.empty()) {
+      Diag(IdentLoc, diag::err_unexpected_qualified_namespace_alias);
+      SkipUntil(tok::semi);
+      return nullptr;
+    }
     if (attrLoc.isValid())
       Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
     if (InlineLoc.isValid())
diff --git a/clang/test/SemaCXX/namespace-alias.cpp b/clang/test/SemaCXX/namespace-alias.cpp
index 281ee9962e8b52..10d2e8beeccaa0 100644
--- a/clang/test/SemaCXX/namespace-alias.cpp
+++ b/clang/test/SemaCXX/namespace-alias.cpp
@@ -47,6 +47,8 @@ namespace I {
   namespace A1 { int i; }
   
   namespace A2 = A1;
+
+  namespace A3::extra::specifiers = A2;  // expected-error {{unexpected nested name specifier}}
 }
 
 int f() {

>From e40b7af1981866adb21892305a0f9dfaa5bdc8eb Mon Sep 17 00:00:00 2001
From: Dan Katz <katzdm at gmail.com>
Date: Thu, 21 Mar 2024 11:44:03 -0400
Subject: [PATCH 2/4] Addressing feedback.

---
 clang/include/clang/Basic/DiagnosticParseKinds.td | 2 +-
 clang/lib/Parse/ParseDeclCXX.cpp                  | 5 ++++-
 clang/test/SemaCXX/namespace-alias.cpp            | 2 +-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 5626307c80c2df..46a44418a3153b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -269,7 +269,7 @@ def err_expected_semi_after_namespace_name : Error<
 def err_unexpected_namespace_attributes_alias : Error<
   "attributes cannot be specified on namespace alias">;
 def err_unexpected_qualified_namespace_alias : Error<
-  "unexpected nested name specifier in namespace alias definition">;
+  "namespace alias must be a single identifier">;
 def err_unexpected_nested_namespace_attribute : Error<
   "attributes cannot be specified on a nested namespace definition">;
 def err_inline_namespace_alias : Error<"namespace alias cannot be inline">;
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 0ef2f2fad2a9d1..63fe678cbb29e2 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -141,7 +141,10 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
       return nullptr;
     }
     if (!ExtraNSs.empty()) {
-      Diag(IdentLoc, diag::err_unexpected_qualified_namespace_alias);
+      Diag(ExtraNSs.front().NamespaceLoc,
+           diag::err_unexpected_qualified_namespace_alias)
+          << SourceRange(ExtraNSs.front().NamespaceLoc,
+                         ExtraNSs.back().IdentLoc);
       SkipUntil(tok::semi);
       return nullptr;
     }
diff --git a/clang/test/SemaCXX/namespace-alias.cpp b/clang/test/SemaCXX/namespace-alias.cpp
index 10d2e8beeccaa0..591957a657c03a 100644
--- a/clang/test/SemaCXX/namespace-alias.cpp
+++ b/clang/test/SemaCXX/namespace-alias.cpp
@@ -48,7 +48,7 @@ namespace I {
   
   namespace A2 = A1;
 
-  namespace A3::extra::specifiers = A2;  // expected-error {{unexpected nested name specifier}}
+  namespace A3::extra::specifiers = A2;  // expected-error {{alias must be a single identifier}}
 }
 
 int f() {

>From 4f65d9118a9030cc3d4e3a47041a706adaa7779b Mon Sep 17 00:00:00 2001
From: Dan Katz <katzdm at gmail.com>
Date: Thu, 21 Mar 2024 11:57:18 -0400
Subject: [PATCH 3/4] Add release notes.

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 50990140a53ad1..81f97a9f12591b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -419,6 +419,8 @@ Bug Fixes to C++ Support
 - Clang's __builtin_bit_cast will now produce a constant value for records with empty bases. See:
   (#GH82383)
 - Fix a crash when instantiating a lambda that captures ``this`` outside of its context. Fixes (#GH85343).
+- Fix an issue where a namespace alias could be defined using a qualified name (all name components
+  following the first `::` were ignored). (#GH86122)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^

>From 3c6632ea332f710df6b97624e79b8d85af9e3044 Mon Sep 17 00:00:00 2001
From: Dan Katz <katzdm at gmail.com>
Date: Thu, 21 Mar 2024 12:35:43 -0400
Subject: [PATCH 4/4] Fix release note.

---
 clang/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 81f97a9f12591b..356105a6249c0e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -420,7 +420,7 @@ Bug Fixes to C++ Support
   (#GH82383)
 - Fix a crash when instantiating a lambda that captures ``this`` outside of its context. Fixes (#GH85343).
 - Fix an issue where a namespace alias could be defined using a qualified name (all name components
-  following the first `::` were ignored). (#GH86122)
+  following the first `::` were ignored).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list