Author: rtrieu Date: Tue Feb 28 21:07:55 2017 New Revision: 296572 URL: http://llvm.org/viewvc/llvm-project?rev=296572&view=rev Log: Add warning for inconsistent overrides on destructor.
The exisiting warning for inconsistent overrides does not include the destructor as it was noted in review that it was too noisy. Instead, add to a separate warning group that is off by default for users who want consistent warnings between methods and destructors. Added: cfe/trunk/test/SemaCXX/warn-inconsistent-missing-destructor-override Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td cfe/trunk/lib/Sema/SemaDeclCXX.cpp Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=296572&r1=296571&r2=296572&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Feb 28 21:07:55 2017 @@ -175,6 +175,8 @@ def CXX98CompatPedantic : DiagGroup<"c++ def CXX11Narrowing : DiagGroup<"c++11-narrowing">; +def CXX11WarnOverrideDestructor : + DiagGroup<"inconsistent-missing-destructor-override">; def CXX11WarnOverrideMethod : DiagGroup<"inconsistent-missing-override">; // Original name of this warning in Clang Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=296572&r1=296571&r2=296572&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Feb 28 21:07:55 2017 @@ -2039,6 +2039,9 @@ def override_keyword_hides_virtual_membe "%select{function|functions}1">; def err_function_marked_override_not_overriding : Error< "%0 marked 'override' but does not override any member functions">; +def warn_destructor_marked_not_override_overriding : Warning < + "%0 overrides a destructor but is not marked 'override'">, + InGroup<CXX11WarnOverrideDestructor>, DefaultIgnore; def warn_function_marked_not_override_overriding : Warning < "%0 overrides a member function but is not marked 'override'">, InGroup<CXX11WarnOverrideMethod>; Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=296572&r1=296571&r2=296572&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original) +++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Feb 28 21:07:55 2017 @@ -2716,8 +2716,7 @@ void Sema::DiagnoseAbsenceOfOverrideCont if (D->isInvalidDecl() || D->hasAttr<OverrideAttr>()) return; CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); - if (!MD || MD->isImplicit() || MD->hasAttr<FinalAttr>() || - isa<CXXDestructorDecl>(MD)) + if (!MD || MD->isImplicit() || MD->hasAttr<FinalAttr>()) return; SourceLocation Loc = MD->getLocation(); @@ -2727,10 +2726,12 @@ void Sema::DiagnoseAbsenceOfOverrideCont SpellingLoc = getSourceManager().getSpellingLoc(SpellingLoc); if (SpellingLoc.isValid() && getSourceManager().isInSystemHeader(SpellingLoc)) return; - + if (MD->size_overridden_methods() > 0) { - Diag(MD->getLocation(), diag::warn_function_marked_not_override_overriding) - << MD->getDeclName(); + unsigned DiagID = isa<CXXDestructorDecl>(MD) + ? diag::warn_destructor_marked_not_override_overriding + : diag::warn_function_marked_not_override_overriding; + Diag(MD->getLocation(), DiagID) << MD->getDeclName(); const CXXMethodDecl *OMD = *MD->begin_overridden_methods(); Diag(OMD->getLocation(), diag::note_overridden_virtual_function); } Added: cfe/trunk/test/SemaCXX/warn-inconsistent-missing-destructor-override URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-inconsistent-missing-destructor-override?rev=296572&view=auto ============================================================================== --- cfe/trunk/test/SemaCXX/warn-inconsistent-missing-destructor-override (added) +++ cfe/trunk/test/SemaCXX/warn-inconsistent-missing-destructor-override Tue Feb 28 21:07:55 2017 @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify -Winconsistent-missing-destructor-override + +class A { + public: + ~A() {} + void virtual run() {} +}; + +class B : public A { + public: + void run() override {} + ~B() {} +}; + +class C { + public: + virtual void run() {} + virtual ~C() {} // expected-note 2{{overridden virtual function is here}} +}; + +class D : public C { + public: + void run() override {} + ~D() {} + // expected-warning@-1 {{'~D' overrides a destructor but is not marked 'override'}} +}; + +class E : public C { + public: + void run() override {} + virtual ~E() {} + // expected-warning@-1 {{'~E' overrides a destructor but is not marked 'override'}} +}; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits