[PATCH] [PATCH] Disabling warnings on unused parameters if function is deleted/ defaulted (cxx11) [PR19303]

Dinesh Dwivedi dinesh.d at samsung.com
Tue Apr 15 06:08:20 PDT 2014


Hi rsmith, nrieck,

if a function is defined out-of-line and marked as deleted/ defaulted [cxx11],
it is handled by Parser::ParseFunctionDefinition( Body = 0). This function calls 
Actions.ActOnFinishFunctionBody() which in-turn calls DiagnoseUnusedParameters()
and DiagnoseSizeOfParametersAndReturnValue(). After this, the function attributes
are set to deleted/ defaulted.

For deleted/ defaulted function, as they do not have body to diagnosed against,
DiagnoseXXX calls does not make much sense. I have added a patch with this check
before calling DiagnoseXXX functions.

I have tried other options like, first mark functions as deleted/ defaulted and
then call ActOnFinishFunctionBody() and check if function is marked before calling
DiagnoseXXX functions but it has its own side effects.

Please let me know if this patch is ok.

http://reviews.llvm.org/D3376

Files:
  lib/Sema/SemaDecl.cpp
  test/SemaCXX/cxx11-unused.cpp

Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -9932,9 +9932,13 @@
       Diag(FD->getLocation(), diag::warn_pure_function_definition);
 
     if (!FD->isInvalidDecl()) {
-      DiagnoseUnusedParameters(FD->param_begin(), FD->param_end());
-      DiagnoseSizeOfParametersAndReturnValue(FD->param_begin(), FD->param_end(),
-                                             FD->getReturnType(), FD);
+      // In case of cxx11, Body is null if function is marked deleted/ defaulted
+      // out-of-line. Calling these DiagnoseXXX in such cases is not useful.
+      if (Body) {
+        DiagnoseUnusedParameters(FD->param_begin(), FD->param_end());
+        DiagnoseSizeOfParametersAndReturnValue(
+            FD->param_begin(), FD->param_end(), FD->getReturnType(), FD);
+      }
 
       // If this is a constructor, we need a vtable.
       if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD))
Index: test/SemaCXX/cxx11-unused.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/cxx11-unused.cpp
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -std=c++11 -verify %s -Wunused-parameter
+
+// PR19303 : Make sure we don't get a unused expression warning for deleted and
+// defaulted functions
+
+// expected-no-diagnostics
+
+class A {
+public:
+  int x;
+  A() = default;
+  ~A() = default;
+  A(const A &other) = delete;
+
+  template <typename T>
+  void SetX(T x) {
+    this->x = x;
+  };
+
+  void SetX1(int x);
+};
+
+template <>
+void A::SetX(A x) = delete;
+
+class B {
+public:
+  B() = default;
+  ~B() = default;
+  B(const B &other);
+};
+
+B::B(const B &other) = default;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3376.1.patch
Type: text/x-patch
Size: 1748 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140415/8a3c2526/attachment.bin>


More information about the cfe-commits mailing list