Skip to content

Commit abf3ca6

Browse files
[Diagnostics] Restore -Wdeprecated warning when user-declared copy assignment operator is defined as deleted (PR45634)
Solves https://bugs.llvm.org/show_bug.cgi?id=45634 Be more agressive than GCC with -Wdeprecated-copy. Also provide -W(no-)deprecated-copy-user-provided-copy/dtor options to on/off this behaviour. Reviewed By: Quuxplusone Differential Revision: https://reviews.llvm.org/D79714
1 parent d4ee603 commit abf3ca6

8 files changed

+94
-34
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,12 @@ def CXX11CompatDeprecatedWritableStr :
162162
def DeprecatedArrayCompare : DiagGroup<"deprecated-array-compare">;
163163
def DeprecatedAttributes : DiagGroup<"deprecated-attributes">;
164164
def DeprecatedCommaSubscript : DiagGroup<"deprecated-comma-subscript">;
165-
def DeprecatedCopy : DiagGroup<"deprecated-copy">;
166-
def DeprecatedCopyDtor : DiagGroup<"deprecated-copy-dtor">;
165+
def DeprecatedCopyWithUserProvidedCopy : DiagGroup<"deprecated-copy-with-user-provided-copy">;
166+
def DeprecatedCopyWithUserProvidedDtor : DiagGroup<"deprecated-copy-with-user-provided-dtor">;
167+
def DeprecatedCopy : DiagGroup<"deprecated-copy", [DeprecatedCopyWithUserProvidedCopy]>;
168+
def DeprecatedCopyWithDtor : DiagGroup<"deprecated-copy-with-dtor", [DeprecatedCopyWithUserProvidedDtor]>;
169+
// For compatibility with GCC.
170+
def : DiagGroup<"deprecated-copy-dtor", [DeprecatedCopyWithDtor]>;
167171
def DeprecatedDeclarations : DiagGroup<"deprecated-declarations">;
168172
def UnavailableDeclarations : DiagGroup<"unavailable-declarations">;
169173
def UnguardedAvailabilityNew : DiagGroup<"unguarded-availability-new">;
@@ -186,7 +190,7 @@ def Deprecated : DiagGroup<"deprecated", [DeprecatedAnonEnumEnumConversion,
186190
DeprecatedAttributes,
187191
DeprecatedCommaSubscript,
188192
DeprecatedCopy,
189-
DeprecatedCopyDtor,
193+
DeprecatedCopyWithDtor,
190194
DeprecatedDeclarations,
191195
DeprecatedDynamicExceptionSpec,
192196
DeprecatedEnumCompare,

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,15 +567,24 @@ def warn_access_decl_deprecated : Warning<
567567
def err_access_decl : Error<
568568
"ISO C++11 does not allow access declarations; "
569569
"use using declarations instead">;
570-
def warn_deprecated_copy_operation : Warning<
570+
def warn_deprecated_copy : Warning<
571571
"definition of implicit copy %select{constructor|assignment operator}1 "
572572
"for %0 is deprecated because it has a user-declared copy "
573573
"%select{assignment operator|constructor}1">,
574574
InGroup<DeprecatedCopy>, DefaultIgnore;
575-
def warn_deprecated_copy_dtor_operation : Warning<
575+
def warn_deprecated_copy_with_dtor : Warning<
576576
"definition of implicit copy %select{constructor|assignment operator}1 "
577577
"for %0 is deprecated because it has a user-declared destructor">,
578-
InGroup<DeprecatedCopyDtor>, DefaultIgnore;
578+
InGroup<DeprecatedCopyWithDtor>, DefaultIgnore;
579+
def warn_deprecated_copy_with_user_provided_copy: Warning<
580+
"definition of implicit copy %select{constructor|assignment operator}1 "
581+
"for %0 is deprecated because it has a user-provided copy "
582+
"%select{assignment operator|constructor}1">,
583+
InGroup<DeprecatedCopyWithUserProvidedCopy>, DefaultIgnore;
584+
def warn_deprecated_copy_with_user_provided_dtor : Warning<
585+
"definition of implicit copy %select{constructor|assignment operator}1 "
586+
"for %0 is deprecated because it has a user-provided destructor">,
587+
InGroup<DeprecatedCopyWithUserProvidedDtor>, DefaultIgnore;
579588
def warn_cxx17_compat_exception_spec_in_signature : Warning<
580589
"mangled name of %0 will change in C++17 due to non-throwing exception "
581590
"specification in function signature">, InGroup<CXX17CompatMangling>;

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14027,12 +14027,20 @@ static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp) {
1402714027
assert(UserDeclaredOperation);
1402814028
}
1402914029

14030-
if (UserDeclaredOperation && UserDeclaredOperation->isUserProvided()) {
14031-
S.Diag(UserDeclaredOperation->getLocation(),
14032-
isa<CXXDestructorDecl>(UserDeclaredOperation)
14033-
? diag::warn_deprecated_copy_dtor_operation
14034-
: diag::warn_deprecated_copy_operation)
14035-
<< RD << /*copy assignment*/ !isa<CXXConstructorDecl>(CopyOp);
14030+
if (UserDeclaredOperation) {
14031+
bool UDOIsUserProvided = UserDeclaredOperation->isUserProvided();
14032+
bool UDOIsDestructor = isa<CXXDestructorDecl>(UserDeclaredOperation);
14033+
bool IsCopyAssignment = !isa<CXXConstructorDecl>(CopyOp);
14034+
unsigned DiagID =
14035+
(UDOIsUserProvided && UDOIsDestructor)
14036+
? diag::warn_deprecated_copy_with_user_provided_dtor
14037+
: (UDOIsUserProvided && !UDOIsDestructor)
14038+
? diag::warn_deprecated_copy_with_user_provided_copy
14039+
: (!UDOIsUserProvided && UDOIsDestructor)
14040+
? diag::warn_deprecated_copy_with_dtor
14041+
: diag::warn_deprecated_copy;
14042+
S.Diag(UserDeclaredOperation->getLocation(), DiagID)
14043+
<< RD << IsCopyAssignment;
1403614044
}
1403714045
}
1403814046

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated -verify
2+
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated-copy-dtor -verify
3+
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated-copy-with-dtor -verify
4+
5+
class A {
6+
public:
7+
~A() = default; // expected-warning {{definition of implicit copy constructor for 'A' is deprecated because it has a user-declared destructor}}
8+
};
9+
10+
void test() {
11+
A a1;
12+
A a2 = a1; // expected-note {{in implicit copy constructor for 'A' first required here}}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated -verify
2+
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated-copy-with-user-provided-copy -verify
3+
4+
struct A {
5+
A &operator=(const A &); // expected-warning {{definition of implicit copy constructor for 'A' is deprecated because it has a user-provided copy assignment operator}}
6+
};
7+
8+
void foo() {
9+
A a1;
10+
A a2(a1); // expected-note {{implicit copy constructor for 'A' first required here}}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated -verify
2+
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated-copy-with-user-provided-dtor -verify
3+
4+
struct A {
5+
~A(); // expected-warning {{definition of implicit copy constructor for 'A' is deprecated because it has a user-provided destructor}}
6+
};
7+
8+
void test() {
9+
A a1;
10+
A a2(a1); // expected-note {{implicit copy constructor for 'A' first required here}}
11+
}
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1+
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated -verify
12
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated-copy -verify
2-
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated-copy-dtor -DDEPRECATED_COPY_DTOR -verify
3-
// RUN: %clang_cc1 -std=c++11 %s -Wextra -verify
43

5-
#ifdef DEPRECATED_COPY_DTOR
64
struct A {
7-
int *ptr;
8-
~A() { delete ptr; } // expected-warning {{definition of implicit copy constructor for 'A' is deprecated because it has a user-declared destructor}}
5+
A& operator=(const A&) = default; // expected-warning {{definition of implicit copy constructor for 'A' is deprecated because it has a user-declared copy assignment operator}}
96
};
107

11-
void foo() {
12-
A a{};
13-
A b = a; // expected-note {{implicit copy constructor for 'A' first required here}}
14-
}
15-
#else
168
struct B {
17-
B &operator=(const B &); // expected-warning {{definition of implicit copy constructor for 'B' is deprecated because it has a user-declared copy assignment operator}}
9+
B& operator=(const B&) = delete; // expected-warning {{definition of implicit copy constructor for 'B' is deprecated because it has a user-declared copy assignment operator}}
1810
};
1911

20-
void bar() {
21-
B b1, b2(b1); // expected-note {{implicit copy constructor for 'B' first required here}}
12+
void test() {
13+
A a1;
14+
A a2(a1); // expected-note {{implicit copy constructor for 'A' first required here}}
15+
16+
B b1;
17+
B b2(b1); // expected-note {{implicit copy constructor for 'B' first required here}}
2218
}
23-
#endif
19+
20+
// PR45634
21+
struct S {
22+
int i;
23+
S& operator=(const S&) = delete; // expected-warning {{definition of implicit copy constructor for 'S' is deprecated because it has a user-declared copy assignment operator}}
24+
};
25+
26+
S test(const S &s) { return S(s); } // expected-note {{implicit copy constructor for 'S' first required here}}

clang/test/SemaCXX/deprecated.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,31 @@ struct T : private S {
8383
#if __cplusplus >= 201103L
8484
namespace DeprecatedCopy {
8585
struct Assign {
86-
Assign &operator=(const Assign&); // expected-warning {{definition of implicit copy constructor for 'Assign' is deprecated because it has a user-declared copy assignment operator}}
86+
Assign &operator=(const Assign&); // expected-warning {{definition of implicit copy constructor for 'Assign' is deprecated because it has a user-provided copy assignment operator}}
8787
};
8888
Assign a1, a2(a1); // expected-note {{implicit copy constructor for 'DeprecatedCopy::Assign' first required here}}
8989

9090
struct Ctor {
9191
Ctor();
92-
Ctor(const Ctor&); // expected-warning {{definition of implicit copy assignment operator for 'Ctor' is deprecated because it has a user-declared copy constructor}}
92+
Ctor(const Ctor&); // expected-warning {{definition of implicit copy assignment operator for 'Ctor' is deprecated because it has a user-provided copy constructor}}
9393
};
9494
Ctor b1, b2;
9595
void f() { b1 = b2; } // expected-note {{implicit copy assignment operator for 'DeprecatedCopy::Ctor' first required here}}
9696

9797
struct Dtor {
9898
~Dtor();
99-
// expected-warning@-1 {{definition of implicit copy constructor for 'Dtor' is deprecated because it has a user-declared destructor}}
100-
// expected-warning@-2 {{definition of implicit copy assignment operator for 'Dtor' is deprecated because it has a user-declared destructor}}
99+
// expected-warning@-1 {{definition of implicit copy constructor for 'Dtor' is deprecated because it has a user-provided destructor}}
100+
// expected-warning@-2 {{definition of implicit copy assignment operator for 'Dtor' is deprecated because it has a user-provided destructor}}
101101
};
102102
Dtor c1, c2(c1); // expected-note {{implicit copy constructor for 'DeprecatedCopy::Dtor' first required here}}
103103
void g() { c1 = c2; } // expected-note {{implicit copy assignment operator for 'DeprecatedCopy::Dtor' first required here}}
104104

105105
struct DefaultedDtor {
106-
~DefaultedDtor() = default;
107-
};
108-
DefaultedDtor d1, d2(d1);
109-
void h() { d1 = d2; }
106+
~DefaultedDtor() = default; // expected-warning {{definition of implicit copy constructor for 'DefaultedDtor' is deprecated because it has a user-declared destructor}}
107+
}; // expected-warning@-1 {{definition of implicit copy assignment operator for 'DefaultedDtor' is deprecated because it has a user-declared destructor}}
108+
DefaultedDtor d1;
109+
DefaultedDtor d2(d1); // expected-note {{in implicit copy constructor for 'DeprecatedCopy::DefaultedDtor' first required here}}
110+
void h() { d1 = d2; } // expected-note {{in implicit copy assignment operator for 'DeprecatedCopy::DefaultedDtor' first required here}}
110111
}
111112
#endif
112113

0 commit comments

Comments
 (0)