[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 07:14:12 PDT 2024
https://github.com/katzdm created https://github.com/llvm/llvm-project/pull/86122
Clang's current behavior is to ignore the trailing qualifiers, but the [grammar](https://eel.is/c++draft/namespace.alias#nt:namespace-alias-definition) for `namespace-alias-definition` requires an `identifier` without qualification.
https://godbolt.org/z/1zvW5q4f8
>From 319b7d99b4010514a1680ffd99fb0586b5e7221d 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] 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 816c3ff5f8b2aa..d45a1f0b6ad1c0 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() {
More information about the cfe-commits
mailing list