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 all 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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Attribute Changes in Clang
--------------------------

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

Improvements to Clang's diagnostics
-----------------------------------
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -3832,6 +3832,9 @@ 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
5 changes: 5 additions & 0 deletions clang/lib/Sema/SemaAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,11 @@ void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc,
void Sema::AddPragmaAttributes(Scope *S, Decl *D) {
if (PragmaAttributeStack.empty())
return;

if (const auto *P = dyn_cast<ParmVarDecl>(D))
if (P->getType()->isVoidType())
return;

for (auto &Group : PragmaAttributeStack) {
for (auto &Entry : Group.Entries) {
ParsedAttr *Attribute = Entry.Attribute;
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5183,6 +5183,11 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
if (ParamTy.hasQualifiers())
S.Diag(DeclType.Loc, diag::err_void_param_qualified);

for (const auto *A : Param->attrs()) {
S.Diag(A->getLoc(), diag::warn_attribute_on_void_param)
<< A << A->getRange();
}

// Reject, but continue to parse 'float(this void)' as
// 'float(void)'.
if (Param->isExplicitObjectParameter()) {
Expand Down
23 changes: 23 additions & 0 deletions clang/test/SemaCXX/attr-cxx0x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,26 @@ 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}}

struct S {
void e([[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}}
};

template <typename T>
void f([[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}}

auto g = []([[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}}
}