From: Fariborz Jahanian Date: Tue, 15 Feb 2011 00:59:30 +0000 (+0000) Subject: Warn if method for a deprecated method is implemented. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b1224f69089815fd66b04ae33215b7fba10780aa;p=clang Warn if method for a deprecated method is implemented. Warn if class for a deprecated class is implemented. Warn if category for a deprecated class is implemented. All under control of -Wdeprecated-implementations. // rdar://8973810. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125545 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index b2ab86b052..e257c482ba 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -39,6 +39,8 @@ def DeprecatedWritableStr : DiagGroup<"deprecated-writable-strings">; def Deprecated : DiagGroup<"deprecated", [ DeprecatedDeclarations] >, DiagCategory<"Deprecations">; +def DeprecatedImplementations :DiagGroup<"deprecated-implementations">; + def : DiagGroup<"disabled-optimization">; def : DiagGroup<"discard-qual">; def : DiagGroup<"div-by-zero">; diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 4e42fefdf1..272ca6892a 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1969,6 +1969,9 @@ def warn_deprecated_message : Warning<"%0 is deprecated: %1">, def warn_deprecated_fwdclass_message : Warning< "%0 maybe deprecated because receiver type is unknown">, InGroup; +def warn_depercated_def : Warning< + "Implementing deprecated %select{method|class|category}0">, + InGroup, DefaultIgnore; def err_unavailable : Error<"%0 is unavailable">; def err_unavailable_message : Error<"%0 is unavailable: %1">; def warn_unavailable_fwdclass_message : Warning< diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp index a3d93ab85e..3aeb50b032 100644 --- a/lib/Sema/SemaDeclObjC.cpp +++ b/lib/Sema/SemaDeclObjC.cpp @@ -64,6 +64,21 @@ void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { if ((*PI)->getIdentifier()) PushOnScopeChains(*PI, FnBodyScope); } + // Warn on implementating deprecated methods under + // -Wdeprecated-implementations flag. + // FIXME. Refactor using common routine. + unsigned DIAG = diag::warn_depercated_def; + if (Diags.getDiagnosticLevel(DIAG, MDecl->getLocation()) + != Diagnostic::Ignored) + if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { + if (ObjCMethodDecl *IMD = + IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod())) + if (NamedDecl *ND = dyn_cast(IMD)) + if (ND->getAttr()) { + Diag(MDecl->getLocation(), DIAG) << 0; + Diag(IMD->getLocation(), diag::note_method_declared_at); + } + } } Decl *Sema:: @@ -537,8 +552,21 @@ Decl *Sema::ActOnStartCategoryImplementation( << CatName; Diag(CatIDecl->getImplementation()->getLocation(), diag::note_previous_definition); - } else + } else { CatIDecl->setImplementation(CDecl); + // Warn on implementating category of deprecated class under + // -Wdeprecated-implementations flag. + // FIXME. Refactor using common routine. + unsigned DIAG = diag::warn_depercated_def; + if (Diags.getDiagnosticLevel(DIAG, CDecl->getLocation()) + != Diagnostic::Ignored) + if (NamedDecl *ND = dyn_cast(IDecl)) + if (ND->getAttr()) { + Diag(CDecl->getLocation(), DIAG) << 2; + Diag(IDecl->getLocation(), diag::note_previous_decl) << "class"; + } + + } } CheckObjCDeclScope(CDecl); @@ -647,6 +675,17 @@ Decl *Sema::ActOnStartClassImplementation( } else { // add it to the list. IDecl->setImplementation(IMPDecl); PushOnScopeChains(IMPDecl, TUScope); + // Warn on implementating deprecated class under + // -Wdeprecated-implementations flag. + // FIXME. Refactor using common routine. + unsigned DIAG = diag::warn_depercated_def; + if (Diags.getDiagnosticLevel(DIAG, IMPDecl->getLocation()) + != Diagnostic::Ignored) + if (NamedDecl *ND = dyn_cast(IDecl)) + if (ND->getAttr()) { + Diag(IMPDecl->getLocation(), DIAG) << 1; + Diag(IDecl->getLocation(), diag::note_previous_decl) << "class"; + } } return IMPDecl; } diff --git a/test/SemaObjC/warn-deprecated-implementations.m b/test/SemaObjC/warn-deprecated-implementations.m new file mode 100644 index 0000000000..7bcd10cc3e --- /dev/null +++ b/test/SemaObjC/warn-deprecated-implementations.m @@ -0,0 +1,45 @@ +// RUN: %clang_cc1 -fsyntax-only -Wdeprecated-implementations -verify %s +// rdar://8973810 + +@protocol P +- (void) D __attribute__((deprecated)); // expected-note {{method declared here}} +@end + +@interface A

++ (void)F __attribute__((deprecated)); // expected-note {{method declared here}} +@end + +@interface A() +- (void) E __attribute__((deprecated)); // expected-note {{method declared here}} +@end + +@implementation A ++ (void)F { } // expected-warning {{Implementing deprecated method}} +- (void) D {} // expected-warning {{Implementing deprecated method}} +- (void) E {} // expected-warning {{Implementing deprecated method}} +@end + +__attribute__((deprecated)) +@interface CL // expected-note 2 {{class declared here}} +@end + +@implementation CL // expected-warning {{Implementing deprecated class}} +@end + +@implementation CL ( SomeCategory ) // expected-warning {{Implementing deprecated category}} +@end + +@interface CL_SUB : CL // expected-warning {{'CL' is deprecated}} +@end + +@interface BASE +- (void) B __attribute__((deprecated)); // expected-note {{method declared here}} +@end + +@interface SUB : BASE +@end + +@implementation SUB +- (void) B {} // expected-warning {{Implementing deprecated method}} +@end +