[clang] [Clang] disallow attributes on void parameters (PR #124920)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 29 05:21:18 PST 2025


https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/124920

Fixes #108819 

--- 

This PR introduces diagnostics to disallow the use of attributes on void parameters

```cpp
void f([[deprecated]] void) {}
```



>From bd731e4be65fc9c1746aa6a8f63d206eb54bb2be Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 29 Jan 2025 15:17:06 +0200
Subject: [PATCH] [Clang] disallow attributes on void parameters

---
 clang/docs/ReleaseNotes.rst            | 2 ++
 clang/lib/Parse/ParseDecl.cpp          | 7 +++++++
 clang/test/Parser/cxx0x-attributes.cpp | 9 +++++++++
 3 files changed, 18 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7fafe2807bd388..0c87e52007d546 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -100,6 +100,8 @@ Removed Compiler Flags
 Attribute Changes in Clang
 --------------------------
 
+- Clang now disallows the use of attributes on void parameters. (#GH108819)
+
 Improvements to Clang's diagnostics
 -----------------------------------
 
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index f136d5007e8a5f..0b88dd4449b1e2 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -7986,6 +7986,13 @@ void Parser::ParseParameterDeclarationClause(
     if (getLangOpts().HLSL)
       MaybeParseHLSLAnnotations(DS.getAttributes());
 
+    if (ParmDeclarator.getIdentifier() == nullptr &&
+        ParmDeclarator.getDeclarationAttributes().size() &&
+        ParmDeclarator.getDeclSpec().getTypeSpecType() == DeclSpec::TST_void) {
+      SourceRange AttrRange = ParmDeclarator.getDeclarationAttributes().Range;
+      Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed) << AttrRange;
+    }
+
     if (Tok.is(tok::kw_requires)) {
       // User tried to define a requires clause in a parameter declaration,
       // which is surely not a function declaration.
diff --git a/clang/test/Parser/cxx0x-attributes.cpp b/clang/test/Parser/cxx0x-attributes.cpp
index fad3010c98b9c2..13fcdd142fa841 100644
--- a/clang/test/Parser/cxx0x-attributes.cpp
+++ b/clang/test/Parser/cxx0x-attributes.cpp
@@ -453,3 +453,12 @@ namespace P2361 {
 }
 
 alignas(int) struct AlignAsAttribute {}; // expected-error {{misplaced attributes; expected attributes here}}
+
+namespace GH108819 {
+void a([[maybe_unused]] void) {}                 // expected-error {{an attribute list cannot appear here}} \
+                                                 // expected-warning {{use of the 'maybe_unused' attribute is a C++17 extension}}
+void b([[deprecated]] void) {}                   // expected-error {{an attribute list cannot appear here}} \
+                                                 // expected-warning {{use of the 'deprecated' attribute is a C++14 extension}}
+void c([[clang::lifetimebound]] void) {}         // expected-error {{an attribute list cannot appear here}}
+void d([[clang::annotate("a", "b", 1)]] void) {} // expected-error {{an attribute list cannot appear here}}
+}



More information about the cfe-commits mailing list