Skip to content

Commit aeeeeab

Browse files
authored
[Clang] disallow attributes on void parameters (#124920)
Fixes #108819 --- This PR introduces diagnostics to disallow the use of attributes on void parameters ```cpp void f([[deprecated]] void) {} ```
1 parent 0c6c4a9 commit aeeeeab

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Attribute Changes in Clang
117117
--------------------------
118118

119119
- The ``no_sanitize`` attribute now accepts both ``gnu`` and ``clang`` names.
120+
- Clang now diagnoses use of declaration attributes on void parameters. (#GH108819)
120121

121122
Improvements to Clang's diagnostics
122123
-----------------------------------

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,9 @@ def warn_type_attribute_wrong_type : Warning<
38323832
"'%0' only applies to %select{function|pointer|"
38333833
"Objective-C object or block pointer}1 types; type here is %2">,
38343834
InGroup<IgnoredAttributes>;
3835+
def warn_attribute_on_void_param: Warning<
3836+
"attribute %0 cannot be applied to a 'void' parameter">,
3837+
InGroup<IgnoredAttributes>;
38353838
def err_type_attribute_wrong_type : Error<
38363839
warn_type_attribute_wrong_type.Summary>;
38373840
def warn_incomplete_encoded_type : Warning<

clang/lib/Sema/SemaAttr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,11 @@ void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc,
11891189
void Sema::AddPragmaAttributes(Scope *S, Decl *D) {
11901190
if (PragmaAttributeStack.empty())
11911191
return;
1192+
1193+
if (const auto *P = dyn_cast<ParmVarDecl>(D))
1194+
if (P->getType()->isVoidType())
1195+
return;
1196+
11921197
for (auto &Group : PragmaAttributeStack) {
11931198
for (auto &Entry : Group.Entries) {
11941199
ParsedAttr *Attribute = Entry.Attribute;

clang/lib/Sema/SemaType.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5183,6 +5183,11 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
51835183
if (ParamTy.hasQualifiers())
51845184
S.Diag(DeclType.Loc, diag::err_void_param_qualified);
51855185

5186+
for (const auto *A : Param->attrs()) {
5187+
S.Diag(A->getLoc(), diag::warn_attribute_on_void_param)
5188+
<< A << A->getRange();
5189+
}
5190+
51865191
// Reject, but continue to parse 'float(this void)' as
51875192
// 'float(void)'.
51885193
if (Param->isExplicitObjectParameter()) {

clang/test/SemaCXX/attr-cxx0x.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,26 @@ alignas(4) auto PR19252 = 0;
5353

5454
// Check the diagnostic message
5555
class alignas(void) AlignasVoid {}; // expected-error {{invalid application of 'alignas' to an incomplete type 'void'}}
56+
57+
namespace GH108819 {
58+
void a([[maybe_unused]] void) {} // expected-warning {{attribute 'maybe_unused' cannot be applied to a 'void' parameter}}\
59+
// expected-warning {{use of the 'maybe_unused' attribute is a C++17 extension}}
60+
void b([[deprecated, maybe_unused]] void) {} // expected-warning {{attribute 'deprecated' cannot be applied to a 'void' parameter}} \
61+
// expected-warning {{attribute 'maybe_unused' cannot be applied to a 'void' parameter}} \
62+
// expected-warning {{use of the 'deprecated' attribute is a C++14 extension}} \
63+
// expected-warning {{use of the 'maybe_unused' attribute is a C++17 extension}}
64+
void c([[clang::lifetimebound]] void) {} // expected-warning {{attribute 'lifetimebound' cannot be applied to a 'void' parameter}}
65+
void d([[clang::annotate("a", "b", 1)]] void) {} // expected-warning {{attribute 'annotate' cannot be applied to a 'void' parameter}}
66+
67+
struct S {
68+
void e([[maybe_unused]] void) {} // expected-warning {{attribute 'maybe_unused' cannot be applied to a 'void' parameter}} \
69+
// expected-warning {{use of the 'maybe_unused' attribute is a C++17 extension}}
70+
};
71+
72+
template <typename T>
73+
void f([[maybe_unused]] void) {} // expected-warning {{attribute 'maybe_unused' cannot be applied to a 'void' parameter}} \
74+
// expected-warning {{use of the 'maybe_unused' attribute is a C++17 extension}}
75+
76+
auto g = []([[maybe_unused]] void) { }; // expected-warning {{attribute 'maybe_unused' cannot be applied to a 'void' parameter}} \
77+
// expected-warning {{use of the 'maybe_unused' attribute is a C++17 extension}}
78+
}

0 commit comments

Comments
 (0)