[PATCH] D68185: [Diagnostics] Warn when class implements a copy constructor/copy assignment operator, but missing the copy assignment operator/copy constructor

Dávid Bolvanský via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 28 13:19:50 PDT 2019


xbolva00 created this revision.
xbolva00 added a reviewer: rsmith.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
xbolva00 updated this revision to Diff 222301.

Clang version of https://www.viva64.com/en/w/v690/
Also requested by Chromium developers:
https://bugs.chromium.org/p/chromium/issues/detail?id=979077&q=component%3ATools%3ELLVM&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified


https://reviews.llvm.org/D68185

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaDeclCXX.cpp
  test/SemaCXX/warn-custom-copy.cpp


Index: test/SemaCXX/warn-custom-copy.cpp
===================================================================
--- test/SemaCXX/warn-custom-copy.cpp
+++ test/SemaCXX/warn-custom-copy.cpp
@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wcustom-copy %s
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+class Clz // expected-warning {{class implements custom copy assignment operator but missing custom copy constructor}}
+
+{
+  char *buf;
+
+public:
+  Clz() : buf(nullptr) {}
+  Clz &operator=(const Clz &r) {
+    buf = r.buf;
+    return *this;
+  }
+};
+
+class Clz2 // expected-warning {{class implements custom copy assignment operator but missing custom copy constructor}}
+
+{
+  char *buf;
+
+public:
+  Clz2() : buf(nullptr) {}
+  Clz2(const Clz2 &r) : buf(nullptr) {
+    (void)buf;
+    (void)r;
+  }
+};
+
+class Clz3 {
+  char *buf;
+
+public:
+  Clz3() : buf(nullptr) {}
+  Clz3(const Clz3 &r) : buf(nullptr) {
+    (void)buf;
+    (void)r;
+  }
+  Clz3 &operator=(const Clz3 &r) {
+    buf = r.buf;
+    return *this;
+  }
+};
Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp
+++ lib/Sema/SemaDeclCXX.cpp
@@ -6279,6 +6279,14 @@
     }
   }
 
+  // Warn if the class implements custom copy constructor but missing
+  // custom copy assignment operator and vice versa.
+  if (Record->hasUserDeclaredCopyAssignment() !=
+      Record->hasUserDeclaredCopyConstructor())
+    Diag(Record->getLocation(), diag::warn_missing_custom_copy)
+        << Record->hasUserDeclaredCopyConstructor()
+        << !Record->hasUserDeclaredCopyConstructor();
+
   // Warn if the class has virtual methods but non-virtual public destructor.
   if (Record->isPolymorphic() && !Record->isDependentType()) {
     CXXDestructorDecl *dtor = Record->getDestructor();
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -2245,6 +2245,11 @@
 def note_final_dtor_non_final_class_silence : Note<
   "mark %0 as '%select{final|sealed}1' to silence this warning">;
 
+def warn_missing_custom_copy : Warning<
+  "class implements custom %select{copy assignment operator|copy constructor}0 "
+  "but missing custom %select{copy assignment operator|copy constructor}1">,
+  InGroup<DiagGroup<"custom-copy">>;
+
 // C++11 attributes
 def err_repeat_attribute : Error<"%0 attribute cannot be repeated">;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68185.222301.patch
Type: text/x-patch
Size: 2535 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190928/f1bb3a3a/attachment-0001.bin>


More information about the cfe-commits mailing list