ahatanak created this revision.
ahatanak added reviewers: erik.pilkington, rjmccall.
ahatanak added a project: clang.
Herald added subscribers: dexonsmith, jkorous.

The new warning `-Wunused-parameter-non-objc-method` works exactly the same as 
`-Wunused-parameter ` except for unused parameters of ObjC methods. This can be 
handy when users want to use `-Wunused-parameter ` but don't want to annotate 
each unused ObjC method parameters with `__attribute__((unused))` or insert 
`(void)unused_param` into the body of the method to silence the warning since, 
unlike C/C++ functions, it's not possible to comment out the unused parameters 
of ObjC methods.

  // Can't do this for ObjC methods.
  int foo(int /*param*) {}

rdar://problem/41561853


Repository:
  rC Clang

https://reviews.llvm.org/D61147

Files:
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Sema/Sema.h
  lib/Sema/SemaDecl.cpp
  test/Sema/warn-unused-parameters.c
  test/SemaObjC/method-unused-attribute.m

Index: test/SemaObjC/method-unused-attribute.m
===================================================================
--- test/SemaObjC/method-unused-attribute.m
+++ test/SemaObjC/method-unused-attribute.m
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1  -fsyntax-only -Wunused-parameter -verify -Wno-objc-root-class %s
+// RUN: %clang_cc1  -fsyntax-only -Wunused-parameter-non-objc-method -verify -Wno-objc-root-class -DOBJMETHOD %s
 
 @interface INTF
 - (void) correct_use_of_unused: (void *) notice : (id)another_arg;
@@ -9,7 +10,13 @@
 @implementation INTF
 - (void) correct_use_of_unused: (void *)  __attribute__((unused)) notice : (id) __attribute__((unused)) newarg{
 }
-- (void) will_warn_unused_arg: (void *) __attribute__((unused))  notice : (id)warn_unused {}  // expected-warning {{unused parameter 'warn_unused'}}
-- (void) unused_attr_on_decl_ignored: (void *)  will_warn{}  // expected-warning {{unused parameter 'will_warn'}}
+- (void) will_warn_unused_arg: (void *) __attribute__((unused))  notice : (id)warn_unused {}
+- (void) unused_attr_on_decl_ignored: (void *)  will_warn{}
 @end
 
+void foo(int unused1) {} // expected-warning {{unused parameter 'unused1'}}
+
+#ifndef OBJMETHOD
+// expected-warning@-7 {{unused parameter 'warn_unused'}}
+// expected-warning@-7 {{unused parameter 'will_warn'}}
+#endif
Index: test/Sema/warn-unused-parameters.c
===================================================================
--- test/Sema/warn-unused-parameters.c
+++ test/Sema/warn-unused-parameters.c
@@ -24,7 +24,7 @@
 // RUN: %clang_cc1 -fblocks -fsyntax-only -Weverything %s 2>&1 | FileCheck -check-prefix=CHECK-everything %s
 // RUN: not %clang_cc1 -fblocks -fsyntax-only -Weverything -Werror %s 2>&1 | FileCheck -check-prefix=CHECK-everything-error %s
 // RUN: %clang_cc1 -fblocks -fsyntax-only -Weverything -Wno-unused %s 2>&1 | FileCheck -check-prefix=CHECK-everything-no-unused %s
-// CHECK-everything: 6 warnings generated
-// CHECK-everything-error: 5 errors generated
-// CHECK-everything-no-unused: 5 warnings generated
+// CHECK-everything: 8 warnings generated
+// CHECK-everything-error: 7 errors generated
+// CHECK-everything-no-unused: 7 warnings generated
 
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -12545,7 +12545,8 @@
   return Param;
 }
 
-void Sema::DiagnoseUnusedParameters(ArrayRef<ParmVarDecl *> Parameters) {
+void Sema::DiagnoseUnusedParameters(ArrayRef<ParmVarDecl *> Parameters,
+                                    bool IsObjCMethodParam) {
   // Don't diagnose unused-parameter errors in template instantiations; we
   // will already have done so in the template itself.
   if (inTemplateInstantiation())
@@ -12556,6 +12557,10 @@
         !Parameter->hasAttr<UnusedAttr>()) {
       Diag(Parameter->getLocation(), diag::warn_unused_parameter)
         << Parameter->getDeclName();
+      if (!IsObjCMethodParam)
+        Diag(Parameter->getLocation(),
+             diag::warn_unused_parameter_non_objc_method)
+            << Parameter->getDeclName();
     }
   }
 }
@@ -13355,7 +13360,7 @@
     MD->setBody(Body);
     if (!MD->isInvalidDecl()) {
       if (!MD->hasSkippedBody())
-        DiagnoseUnusedParameters(MD->parameters());
+        DiagnoseUnusedParameters(MD->parameters(), true);
       DiagnoseSizeOfParametersAndReturnValue(MD->parameters(),
                                              MD->getReturnType(), MD);
 
Index: include/clang/Sema/Sema.h
===================================================================
--- include/clang/Sema/Sema.h
+++ include/clang/Sema/Sema.h
@@ -2157,7 +2157,8 @@
 
   /// Diagnose any unused parameters in the given sequence of
   /// ParmVarDecl pointers.
-  void DiagnoseUnusedParameters(ArrayRef<ParmVarDecl *> Parameters);
+  void DiagnoseUnusedParameters(ArrayRef<ParmVarDecl *> Parameters,
+                                bool IsObjCMethodParam = false);
 
   /// Diagnose whether the size of parameters or return value of a
   /// function or obj-c method definition is pass-by-value and larger than a
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -271,6 +271,9 @@
   "repeated RISC-V 'interrupt' attribute is here">;
 def warn_unused_parameter : Warning<"unused parameter %0">,
   InGroup<UnusedParameter>, DefaultIgnore;
+def warn_unused_parameter_non_objc_method : Warning<
+  "unused parameter %0">,
+  InGroup<UnusedParameterNonObjCMethod>, DefaultIgnore;
 def warn_unused_variable : Warning<"unused variable %0">,
   InGroup<UnusedVariable>, DefaultIgnore;
 def warn_unused_local_typedef : Warning<
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -598,6 +598,8 @@
 def UnusedLabel : DiagGroup<"unused-label">;
 def UnusedLambdaCapture : DiagGroup<"unused-lambda-capture">;
 def UnusedParameter : DiagGroup<"unused-parameter">;
+def UnusedParameterNonObjCMethod : DiagGroup<"unused-parameter-non-objc-method">;
+
 def UnusedResult : DiagGroup<"unused-result">;
 def PotentiallyEvaluatedExpression : DiagGroup<"potentially-evaluated-expression">;
 def UnevaluatedExpression : DiagGroup<"unevaluated-expression",
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to