]> granicus.if.org Git - clang/commitdiff
Don't warn for the common pattern of disallowing copying:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sun, 15 Aug 2010 10:17:33 +0000 (10:17 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sun, 15 Aug 2010 10:17:33 +0000 (10:17 +0000)
class S {
  S(const S&); // DO NOT IMPLEMENT
  void operator=(const S&); // DO NOT IMPLEMENT
};

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111100 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/SemaCXX/warn-unused-filescoped.cpp

index ea65c8cf3026b3c4844d937dbcf119407455b059..3271997e0e70a4c037bac759c6572a184eda3339 100644 (file)
@@ -521,6 +521,24 @@ static void RemoveUsingDecls(LookupResult &R) {
   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 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
     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() &&
index b51aedc54aca464f49d951aec83521a305081f0d..50d41422d350b8dee5df28d8ed78f422d66aa708 100644 (file)
@@ -11,6 +11,8 @@ namespace {
     void m1() { }  // expected-warning{{unused}}
     void m2();  // expected-warning{{unused}}
     void m3();
+    S(const S&);
+    void operator=(const S&);
   };
 
   template <typename T>