From 4eacc32672e60113b835c4356d1c398dc1e30279 Mon Sep 17 00:00:00 2001 From: =?utf8?q?D=C3=A1vid=20Bolvansk=C3=BD?= Date: Tue, 26 Nov 2019 12:13:13 +0100 Subject: [PATCH] Partially reland "[Diagnostics] Put "deprecated copy" warnings into -Wdeprecated-copy"" But do not enable it under -Wextra until libcxx issue is solved. --- clang/include/clang/Basic/DiagnosticGroups.td | 4 ++++ clang/include/clang/Basic/DiagnosticSemaKinds.td | 10 +++++++--- clang/lib/Sema/SemaDeclCXX.cpp | 10 +++++----- clang/test/SemaCXX/deprecated-copy.cpp | 22 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 clang/test/SemaCXX/deprecated-copy.cpp diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index 6b83bf5..9f5900f 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -128,6 +128,8 @@ def CXX11CompatDeprecatedWritableStr : def DeprecatedAttributes : DiagGroup<"deprecated-attributes">; def DeprecatedCommaSubscript : DiagGroup<"deprecated-comma-subscript">; +def DeprecatedCopy : DiagGroup<"deprecated-copy">; +def DeprecatedCopyDtor : DiagGroup<"deprecated-copy-dtor">; def DeprecatedDeclarations : DiagGroup<"deprecated-declarations">; def UnavailableDeclarations : DiagGroup<"unavailable-declarations">; def UnguardedAvailabilityNew : DiagGroup<"unguarded-availability-new">; @@ -147,6 +149,8 @@ def DeprecatedWritableStr : DiagGroup<"deprecated-writable-strings", // FIXME: Why is DeprecatedImplementations not in this group? def Deprecated : DiagGroup<"deprecated", [DeprecatedAttributes, DeprecatedCommaSubscript, + DeprecatedCopy, + DeprecatedCopyDtor, DeprecatedDeclarations, DeprecatedDynamicExceptionSpec, DeprecatedIncrementBool, diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 799b3ed..c19862a 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -551,9 +551,13 @@ def err_access_decl : Error< "use using declarations instead">; def warn_deprecated_copy_operation : Warning< "definition of implicit copy %select{constructor|assignment operator}1 " - "for %0 is deprecated because it has a user-declared " - "%select{copy %select{assignment operator|constructor}1|destructor}2">, - InGroup, DefaultIgnore; + "for %0 is deprecated because it has a user-declared copy " + "%select{assignment operator|constructor}1">, + InGroup, DefaultIgnore; +def warn_deprecated_copy_dtor_operation : Warning< + "definition of implicit copy %select{constructor|assignment operator}1 " + "for %0 is deprecated because it has a user-declared destructor">, + InGroup, DefaultIgnore; def warn_cxx17_compat_exception_spec_in_signature : Warning< "mangled name of %0 will change in C++17 due to non-throwing exception " "specification in function signature">, InGroup; diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index e3ea978..f469580 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -12406,8 +12406,7 @@ static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp) { // In Microsoft mode, assignment operations don't affect constructors and // vice versa. - if (RD->hasUserDeclaredDestructor() && - RD->getDestructor()->isUserProvided()) { + if (RD->hasUserDeclaredDestructor()) { UserDeclaredOperation = RD->getDestructor(); } else if (!isa(CopyOp) && RD->hasUserDeclaredCopyConstructor() && @@ -12435,9 +12434,10 @@ static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp) { if (UserDeclaredOperation && UserDeclaredOperation->isUserProvided()) { S.Diag(UserDeclaredOperation->getLocation(), - diag::warn_deprecated_copy_operation) - << RD << /*copy assignment*/!isa(CopyOp) - << /*destructor*/isa(UserDeclaredOperation); + isa(UserDeclaredOperation) + ? diag::warn_deprecated_copy_dtor_operation + : diag::warn_deprecated_copy_operation) + << RD << /*copy assignment*/ !isa(CopyOp); } } diff --git a/clang/test/SemaCXX/deprecated-copy.cpp b/clang/test/SemaCXX/deprecated-copy.cpp new file mode 100644 index 0000000..c2ab3c4 --- /dev/null +++ b/clang/test/SemaCXX/deprecated-copy.cpp @@ -0,0 +1,22 @@ +// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated-copy -verify +// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated-copy-dtor -DDEPRECATED_COPY_DTOR -verify + +#ifdef DEPRECATED_COPY_DTOR +struct A { + int *ptr; + ~A() { delete ptr; } // expected-warning {{definition of implicit copy constructor for 'A' is deprecated because it has a user-declared destructor}} +}; + +void foo() { + A a{}; + A b = a; // expected-note {{implicit copy constructor for 'A' first required here}} +} +#else +struct B { + B &operator=(const B &); // expected-warning {{definition of implicit copy constructor for 'B' is deprecated because it has a user-declared copy assignment operator}} +}; + +void bar() { + B b1, b2(b1); // expected-note {{implicit copy constructor for 'B' first required here}} +} +#endif -- 2.7.4