Skip to content

[Clang] disallow attributes on void parameters #124920

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 23 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bd731e4
[Clang] disallow attributes on void parameters
a-tarasyuk Jan 29, 2025
063f767
remove unnecessary name check
a-tarasyuk Jan 29, 2025
3f34315
use empty instead of size
a-tarasyuk Jan 29, 2025
9919006
move handling of void parameter attributes to sema decl
a-tarasyuk Jan 30, 2025
c76ad55
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Jan 30, 2025
35e67d1
Merge branch 'main' into fix/108819
a-tarasyuk Jan 30, 2025
8abec1e
remove useless hasAttrs condition
a-tarasyuk Jan 31, 2025
cb11b95
update release notes
a-tarasyuk Jan 31, 2025
58d5021
fix formatting
a-tarasyuk Jan 31, 2025
afe40bf
Merge branch 'fix/108819' of https://github.com/a-tarasyuk/llvm-proje…
a-tarasyuk Jan 31, 2025
8491f87
fix formatting
a-tarasyuk Jan 31, 2025
8dc35b4
exclude void parameters in pragma attrs push diagnostics
a-tarasyuk Feb 3, 2025
114284b
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Feb 3, 2025
54a2172
remove identifier check
a-tarasyuk Feb 4, 2025
3a2b9d1
use auto instead of explicit type
a-tarasyuk Feb 4, 2025
da7d009
move invalid attribute handling on void parameters to SemaType
a-tarasyuk Feb 4, 2025
c77d82d
add additional test cases
a-tarasyuk Feb 4, 2025
6b0a473
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Feb 4, 2025
3c358e5
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Feb 4, 2025
85b17fa
Merge branch 'main' into fix/108819
a-tarasyuk Feb 5, 2025
4b511ba
Merge branch 'main' into fix/108819
a-tarasyuk Feb 6, 2025
3f05665
Merge branch 'main' into fix/108819
a-tarasyuk Feb 7, 2025
0324a4c
Merge branch 'main' into fix/108819
a-tarasyuk Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ Removed Compiler Flags
Attribute Changes in Clang
--------------------------

- Clang now disallows the use of attributes on void parameters. (#GH108819)

Improvements to Clang's diagnostics
-----------------------------------

Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -3830,6 +3830,8 @@ def warn_type_attribute_wrong_type : Warning<
"'%0' only applies to %select{function|pointer|"
"Objective-C object or block pointer}1 types; type here is %2">,
InGroup<IgnoredAttributes>;
def warn_attribute_on_void_param: Warning<
"attribute %0 cannot be applied to a 'void' parameter">, InGroup<IgnoredAttributes>;
def err_type_attribute_wrong_type : Error<
warn_type_attribute_wrong_type.Summary>;
def warn_incomplete_encoded_type : Warning<
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10326,6 +10326,15 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
}
}

if (FTIHasSingleVoidParameter(FTI)) {
ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[0].Param);
if (Param->hasAttrs()) {
for (const auto *A : Param->attrs())
Diag(A->getLoc(), diag::warn_attribute_on_void_param)
<< A << A->getRange();
}
}

if (!getLangOpts().CPlusPlus) {
// In C, find all the tag declarations from the prototype and move them
// into the function DeclContext. Remove them from the surrounding tag
Expand Down
16 changes: 8 additions & 8 deletions clang/test/Misc/pragma-attribute-strict-subjects.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ struct testRecoverStrictnessStruct { };

#pragma clang attribute pop

#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function, record(unless(is_union)), variable, enum))
// expected-error@-1 {{attribute 'abi_tag' cannot be applied to 'enum'}}
#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function, record(unless(is_union)), variable, enum)) // expected-warning {{attribute 'abi_tag' cannot be applied to a 'void' parameter}}
// expected-error@-1 {{attribute 'abi_tag' cannot be applied to 'enum'}}

int testRecoverExtraVar = 0;
// CHECK-LABEL: VarDecl{{.*}} testRecoverExtraVar
Expand Down Expand Up @@ -90,7 +90,7 @@ struct testSubset1Struct { };

#pragma clang attribute pop

#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = variable)
#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = variable) // expected-warning {{attribute 'abi_tag' cannot be applied to a 'void' parameter}}

int testSubset2Var;
// CHECK-LABEL: VarDecl{{.*}} testSubset2Var
Expand Down Expand Up @@ -122,7 +122,7 @@ struct testSubset3Struct { };

#pragma clang attribute pop

#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function, variable))
#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function, variable)) // expected-warning {{attribute 'abi_tag' cannot be applied to a 'void' parameter}}

int testSubset4Var;
// CHECK-LABEL: VarDecl{{.*}} testSubset4Var
Expand All @@ -138,7 +138,7 @@ struct testSubset4Struct { };

#pragma clang attribute pop

#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(variable, record(unless(is_union))))
#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(variable, record(unless(is_union)))) // expected-warning {{attribute 'abi_tag' cannot be applied to a 'void' parameter}}

int testSubset5Var;
// CHECK-LABEL: VarDecl{{.*}} testSubset5Var
Expand Down Expand Up @@ -170,7 +170,7 @@ struct testSubset6Struct { };

#pragma clang attribute pop

#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union)), function, variable))
#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union)), function, variable)) // expected-warning {{attribute 'abi_tag' cannot be applied to a 'void' parameter}}

int testSubset7Var;
// CHECK-LABEL: VarDecl{{.*}} testSubset7Var
Expand All @@ -187,8 +187,8 @@ struct testSubset7Struct { };
#pragma clang attribute pop


#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union)), function, variable, enum, enum_constant))
// expected-error@-1 {{attribute 'abi_tag' cannot be applied to 'enum_constant', and 'enum'}}
#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union)), function, variable, enum, enum_constant)) // expected-warning {{attribute 'abi_tag' cannot be applied to a 'void' parameter}}
// expected-error@-1 {{attribute 'abi_tag' cannot be applied to 'enum_constant', and 'enum'}}

int testSubsetRecoverVar;
// CHECK-LABEL: VarDecl{{.*}} testSubsetRecoverVar
Expand Down
11 changes: 11 additions & 0 deletions clang/test/SemaCXX/attr-cxx0x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ alignas(4) auto PR19252 = 0;

// Check the diagnostic message
class alignas(void) AlignasVoid {}; // expected-error {{invalid application of 'alignas' to an incomplete type 'void'}}

namespace GH108819 {
void a([[maybe_unused]] void) {} // expected-warning {{attribute 'maybe_unused' cannot be applied to a 'void' parameter}}\
// expected-warning {{use of the 'maybe_unused' attribute is a C++17 extension}}
void b([[deprecated, maybe_unused]] void) {} // expected-warning {{attribute 'deprecated' cannot be applied to a 'void' parameter}} \
// expected-warning {{attribute 'maybe_unused' cannot be applied to a 'void' parameter}} \
// expected-warning {{use of the 'deprecated' attribute is a C++14 extension}} \
// expected-warning {{use of the 'maybe_unused' attribute is a C++17 extension}}
void c([[clang::lifetimebound]] void) {} // expected-warning {{attribute 'lifetimebound' cannot be applied to a 'void' parameter}}
void d([[clang::annotate("a", "b", 1)]] void) {} // expected-warning {{attribute 'annotate' cannot be applied to a 'void' parameter}}
}