[cfe-commits] r111100 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/SemaCXX/warn-unused-filescoped.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Sun Aug 15 03:17:33 PDT 2010


Author: akirtzidis
Date: Sun Aug 15 05:17:33 2010
New Revision: 111100

URL: http://llvm.org/viewvc/llvm-project?rev=111100&view=rev
Log:
Don't warn for the common pattern of disallowing copying:

class S {
  S(const S&); // DO NOT IMPLEMENT
  void operator=(const S&); // DO NOT IMPLEMENT
};

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=111100&r1=111099&r2=111100&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sun Aug 15 05:17:33 2010
@@ -521,6 +521,24 @@
   F.done();
 }
 
+/// \brief Check for this common pattern:
+/// @code
+/// class S {
+///   S(const S&); // DO NOT IMPLEMENT
+///   void operator=(const S&); // DO NOT IMPLEMENT
+/// };
+/// @endcode
+static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) {
+  // FIXME: Should check for private access too but access is set after we get
+  // the decl here.
+  if (D->isThisDeclarationADefinition())
+    return false;
+
+  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D))
+    return CD->isCopyConstructor();
+  return D->isCopyAssignment();
+}
+
 bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
   assert(D);
 
@@ -536,23 +554,23 @@
     return false;
 
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
-      if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
-        return false;
+    if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
+      return false;
 
-      if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
-        if (MD->isVirtual())
-          return false;
-      } else {
-        // 'static inline' functions are used in headers; don't warn.
-        if (FD->getStorageClass() == FunctionDecl::Static &&
-            FD->isInlineSpecified())
-          return false;
-      }
+    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
+      if (MD->isVirtual() || IsDisallowedCopyOrAssign(MD))
+        return false;
+    } else {
+      // 'static inline' functions are used in headers; don't warn.
+      if (FD->getStorageClass() == FunctionDecl::Static &&
+          FD->isInlineSpecified())
+        return false;
+    }
 
     if (FD->isThisDeclarationADefinition())
       return !Context.DeclMustBeEmitted(FD);
     return true;
-   }
+  }
 
   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
     if (VD->isStaticDataMember() &&

Modified: cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp?rev=111100&r1=111099&r2=111100&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp Sun Aug 15 05:17:33 2010
@@ -11,6 +11,8 @@
     void m1() { }  // expected-warning{{unused}}
     void m2();  // expected-warning{{unused}}
     void m3();
+    S(const S&);
+    void operator=(const S&);
   };
 
   template <typename T>





More information about the cfe-commits mailing list