Skip to content

[clang][Sema] Emit warnings on incorrect AVR interrupt/signal handlers #125328

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ def warn_arm_interrupt_vfp_clobber : Warning<
def err_arm_interrupt_called : Error<
"interrupt service routine cannot be called directly">;
def warn_interrupt_attribute_invalid : Warning<
"%select{MIPS|MSP430|RISC-V}0 'interrupt' attribute only applies to "
"functions that have %select{no parameters|a 'void' return type}1">,
"%select{MIPS|MSP430|RISC-V|AVR}0 '%1' attribute only applies to "
"functions that have %select{no parameters|a 'void' return type}2">,
InGroup<IgnoredAttributes>;
def warn_riscv_repeated_interrupt_attribute : Warning<
"repeated RISC-V 'interrupt' attribute">, InGroup<IgnoredAttributes>;
Expand Down
24 changes: 24 additions & 0 deletions clang/lib/Sema/SemaAVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ void SemaAVR::handleInterruptAttr(Decl *D, const ParsedAttr &AL) {
if (!AL.checkExactlyNumArgs(SemaRef, 0))
return;

// AVR interrupt handlers must have no parameter and be void type.
if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*AVR*/ 3 << "interrupt" << 0;
return;
}
if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*AVR*/ 3 << "interrupt" << 1;
return;
}

handleSimpleAttribute<AVRInterruptAttr>(*this, D, AL);
}

Expand All @@ -43,6 +55,18 @@ void SemaAVR::handleSignalAttr(Decl *D, const ParsedAttr &AL) {
if (!AL.checkExactlyNumArgs(SemaRef, 0))
return;

// AVR signal handlers must have no parameter and be void type.
if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*AVR*/ 3 << "signal" << 0;
return;
}
if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*AVR*/ 3 << "signal" << 1;
return;
}

handleSimpleAttribute<AVRSignalAttr>(*this, D, AL);
}

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaMIPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ void SemaMIPS::handleInterruptAttr(Decl *D, const ParsedAttr &AL) {

if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*MIPS*/ 0 << 0;
<< /*MIPS*/ 0 << "interrupt" << 0;
return;
}

if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*MIPS*/ 0 << 1;
<< /*MIPS*/ 0 << "interrupt" << 1;
return;
}

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaMSP430.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ void SemaMSP430::handleInterruptAttr(Decl *D, const ParsedAttr &AL) {

if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*MSP430*/ 1 << 0;
<< /*MSP430*/ 1 << "interrupt" << 0;
return;
}

if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*MSP430*/ 1 << 1;
<< /*MSP430*/ 1 << "interrupt" << 1;
return;
}

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaRISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1458,13 +1458,13 @@ void SemaRISCV::handleInterruptAttr(Decl *D, const ParsedAttr &AL) {

if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*RISC-V*/ 2 << 0;
<< /*RISC-V*/ 2 << "interrupt" << 0;
return;
}

if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
<< /*RISC-V*/ 2 << 1;
<< /*RISC-V*/ 2 << "interrupt" << 1;
return;
}

Expand Down
12 changes: 11 additions & 1 deletion clang/test/Sema/avr-interrupt-attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ struct a test __attribute__((interrupt)); // expected-warning {{'interrupt' attr

__attribute__((interrupt(12))) void foo(void) { } // expected-error {{'interrupt' attribute takes no arguments}}

__attribute__((interrupt)) void food(void) {}
__attribute__((interrupt)) int fooa(void) { return 0; } // expected-warning {{'interrupt' attribute only applies to functions that have a 'void' return type}}

__attribute__((interrupt)) void foob(int a) {} // expected-warning {{'interrupt' attribute only applies to functions that have no parameters}}

__attribute__((signal)) int fooc(void) { return 0; } // expected-warning {{'signal' attribute only applies to functions that have a 'void' return type}}

__attribute__((signal)) void food(int a) {} // expected-warning {{'signal' attribute only applies to functions that have no parameters}}

__attribute__((interrupt)) void fooe(void) {}

__attribute__((signal)) void foof(void) {}