[PATCH] D116778: [clang-tidy][clang] Don't trigger unused-parameter warnings on naked functions

Tommaso Bonvicini via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 6 16:23:40 PST 2022


MuAlphaOmegaEpsilon created this revision.
MuAlphaOmegaEpsilon added a reviewer: aaron.ballman.
Herald added subscribers: carlosgalvezp, xazax.hun.
MuAlphaOmegaEpsilon requested review of this revision.
Herald added projects: clang, clang-tools-extra.
Herald added a subscriber: cfe-commits.

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. Usage of the
naked attribute implies the user taking full responsibility in the
handling of prologue and epilogue of the function, therefore no
unused-parameter warning should ever be triggered whenever a function is
marked naked.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116778

Files:
  clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
  clang/lib/Sema/SemaDecl.cpp


Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -14632,8 +14632,16 @@
         Diag(FD->getLocation(), diag::ext_pure_function_definition);
 
       if (!FD->isInvalidDecl()) {
+        bool FDHasNakedAttr{false};
+        if (FD->hasAttrs())
+          for (const clang::Attr *A : FD->getAttrs())
+            if (A->getParsedKind() == Attr::AT_Naked) {
+              FDHasNakedAttr = true;
+              break;
+            }
         // Don't diagnose unused parameters of defaulted or deleted functions.
-        if (!FD->isDeleted() && !FD->isDefaulted() && !FD->hasSkippedBody())
+        if (!FD->isDeleted() && !FD->isDefaulted() && !FD->hasSkippedBody() &&
+            !FDHasNakedAttr)
           DiagnoseUnusedParameters(FD->parameters());
         DiagnoseSizeOfParametersAndReturnValue(FD->parameters(),
                                                FD->getReturnType(), FD);
Index: clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
@@ -174,6 +174,10 @@
   const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
   if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
     return;
+  if (Function->hasAttrs())
+    for (const clang::Attr *A : Function->getAttrs())
+      if (A->getParsedKind() == Attr::AT_Naked)
+        return;
   if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
     if (Method->isLambdaStaticInvoker())
       return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116778.398013.patch
Type: text/x-patch
Size: 1738 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220107/5bddd8d3/attachment-0001.bin>


More information about the cfe-commits mailing list