Skip to content

Commit ccce1a0

Browse files
MuAlphaOmegaEpsilonAaronBallman
authored andcommitted
Don't trigger unused-parameter warnings on naked functions
This commit checks if a function is marked with the naked attribute and, if it is, will silence the emission of any unused-parameter warning. Inside a naked function only the usage of basic ASM instructions is expected. In this context the parameters can actually be used by fetching them according to the underlying ABI. Since parameters might be used through ASM instructions, the linter and the compiler will have a hard time understanding if one of those is unused or not, therefore no unused-parameter warning should ever be triggered whenever a function is marked naked.
1 parent 8f972cb commit ccce1a0

File tree

7 files changed

+28
-8
lines changed

7 files changed

+28
-8
lines changed

clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ bool isOverrideMethod(const FunctionDecl *Function) {
3131
} // namespace
3232

3333
void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
34-
Finder->addMatcher(
35-
functionDecl(isDefinition(), hasBody(stmt()), hasAnyParameter(decl()))
36-
.bind("function"),
37-
this);
34+
Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
35+
hasAnyParameter(decl()),
36+
unless(hasAttr(attr::Kind::Naked)))
37+
.bind("function"),
38+
this);
3839
}
3940

4041
template <typename T>

clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters-strict.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ class F {
2222
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'j' is unused
2323
// CHECK-FIXES: {{^}} F(int /*j*/) : i() {}{{$}}
2424
};
25+
26+
// Do not warn on naked functions.
27+
[[gnu::naked]] int nakedFunction(int a, float b, const char *c) { ; }
28+
__attribute__((naked)) void nakedFunction(int a, int b) { ; }
2529
}

clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ static void b(int i) {;}
1515
// ===============
1616
void h(i, c, d) int i; char *c, *d; {} // Don't mess with K&R style
1717

18+
// Do not warn on naked functions.
19+
__attribute__((naked)) void nakedFunction(int a, int b) { ; }

clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,7 @@ void test() {
286286
f([](int I) { return; });
287287
}
288288
} // namespace lambda
289+
290+
// Do not warn on naked functions.
291+
[[gnu::naked]] int nakedFunction(int a, float b, const char *c) { ; }
292+
__attribute__((naked)) void nakedFunction(int a, int b) { ; }

clang/lib/Sema/SemaDecl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14694,8 +14694,10 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
1469414694
Diag(FD->getLocation(), diag::ext_pure_function_definition);
1469514695

1469614696
if (!FD->isInvalidDecl()) {
14697-
// Don't diagnose unused parameters of defaulted or deleted functions.
14698-
if (!FD->isDeleted() && !FD->isDefaulted() && !FD->hasSkippedBody())
14697+
// Don't diagnose unused parameters of defaulted, deleted or naked
14698+
// functions.
14699+
if (!FD->isDeleted() && !FD->isDefaulted() && !FD->hasSkippedBody() &&
14700+
!FD->hasAttr<NakedAttr>())
1469914701
DiagnoseUnusedParameters(FD->parameters());
1470014702
DiagnoseSizeOfParametersAndReturnValue(FD->parameters(),
1470114703
FD->getReturnType(), FD);

clang/test/Sema/warn-unused-parameters.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ void f1(void) {
1717
// warnings for the above cases.
1818
static void achor() {};
1919

20+
// Do not warn on naked functions.
21+
__attribute__((naked)) static void nakedFunction(int a, int b) { }
22+
2023
// CHECK: 5:12: warning: unused parameter 'y'
2124
// CHECK: 12:15: warning: unused parameter 'y'
22-
// CHECK-unused: 1 warning generated
25+
// CHECK-unused: 2 warnings generated
2326

2427
// RUN: %clang_cc1 -fblocks -fsyntax-only -Weverything %s 2>&1 | FileCheck -check-prefix=CHECK-everything %s
2528
// RUN: not %clang_cc1 -fblocks -fsyntax-only -Weverything -Werror %s 2>&1 | FileCheck -check-prefix=CHECK-everything-error %s
2629
// RUN: %clang_cc1 -fblocks -fsyntax-only -Weverything -Wno-unused %s 2>&1 | FileCheck -check-prefix=CHECK-everything-no-unused %s
27-
// CHECK-everything: 6 warnings generated
30+
// CHECK-everything: 7 warnings generated
2831
// CHECK-everything-error: 5 errors generated
2932
// CHECK-everything-no-unused: 5 warnings generated
3033

clang/test/SemaCXX/warn-unused-parameters.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ static int test_pack(T... t, T... s)
3232
auto l = [&t...]() { return sizeof...(s); };
3333
return l();
3434
}
35+
36+
// Do not warn on naked functions.
37+
[[gnu::naked]] int nakedFunction(int a, float b, const char* c) { ; }
38+
__attribute__((naked)) void nakedFunction(int a, int b) { ; }

0 commit comments

Comments
 (0)