]> granicus.if.org Git - clang/commitdiff
objective-C: when diagnosing deprecated/unavailable usage of
authorFariborz Jahanian <fjahanian@apple.com>
Fri, 21 Sep 2012 20:46:37 +0000 (20:46 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Fri, 21 Sep 2012 20:46:37 +0000 (20:46 +0000)
setter or getter backing a deprecated/unavailable property,
also not location of the property. // rdar://12324295

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@164412 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Sema/DelayedDiagnostic.h
include/clang/Sema/Sema.h
lib/Sema/DelayedDiagnostic.cpp
lib/Sema/SemaDeclAttr.cpp
lib/Sema/SemaExpr.cpp
lib/Sema/SemaObjCProperty.cpp
test/SemaObjC/arc-system-header.m
test/SemaObjC/attr-deprecated.m
test/SemaObjC/property-deprecated-warning.m [new file with mode: 0644]

index 1f5804f01ff1984d2caab41fcb4e230c3bc33d7e..86d00c2c7e24daaa912b71d9e042f28c2d44c2bd 100644 (file)
@@ -588,6 +588,8 @@ def warn_accessor_property_type_mismatch : Warning<
   "type of property %0 does not match type of accessor %1">;
 def not_conv_function_declared_at : Note<"type conversion function declared here">;
 def note_method_declared_at : Note<"method %0 declared here">;
+def note_property_attribute : Note<"property %0 is declared "
+  "%select{deprecated|unavailable}1 here">;
 def err_setter_type_void : Error<"type of setter must be void">;
 def err_duplicate_method_decl : Error<"duplicate declaration of method %0">;
 def warn_duplicate_method_decl : 
index 1ee8a0dc769b632084e5b601548a751ca5c12e54..a20480c7e44b854fef446d6dca779edb3ed15619 100644 (file)
@@ -124,6 +124,7 @@ public:
   static DelayedDiagnostic makeDeprecation(SourceLocation Loc,
            const NamedDecl *D,
            const ObjCInterfaceDecl *UnknownObjCClass,
+           const ObjCPropertyDecl  *ObjCProperty,
            StringRef Msg);
 
   static DelayedDiagnostic makeAccess(SourceLocation Loc,
@@ -193,12 +194,17 @@ public:
     return DeprecationData.UnknownObjCClass;
   }
 
+  const ObjCPropertyDecl *getObjCProperty() const {
+    return DeprecationData.ObjCProperty;
+  }
+  
 private:
   union {
     /// Deprecation.
     struct {
       const NamedDecl *Decl;
       const ObjCInterfaceDecl *UnknownObjCClass;
+      const ObjCPropertyDecl  *ObjCProperty;
       const char *Message;
       size_t MessageLen;
     } DeprecationData;
index 25e8b45b3e91f8805fcc58daa96520c2c714d014..74114a8d32325e23627671c912187a989650cbdd 100644 (file)
@@ -2295,6 +2295,10 @@ public:
   /// its protocols.
   ObjCPropertyDecl *LookupPropertyDecl(const ObjCContainerDecl *CDecl,
                                        IdentifierInfo *II);
+  
+  /// PropertyIfSetterOrGetter - Looks up the property if named declaration
+  /// is a setter or getter method backing a property.
+  ObjCPropertyDecl *PropertyIfSetterOrGetter(NamedDecl *D);
 
   /// Called by ActOnProperty to handle \@property declarations in
   /// class extensions.
@@ -2686,7 +2690,8 @@ public:
 
   void EmitDeprecationWarning(NamedDecl *D, StringRef Message,
                               SourceLocation Loc,
-                              const ObjCInterfaceDecl *UnknownObjCClass=0);
+                              const ObjCInterfaceDecl *UnknownObjCClass,
+                              const ObjCPropertyDecl  *ObjCProperty);
 
   void HandleDelayedDeprecationCheck(sema::DelayedDiagnostic &DD, Decl *Ctx);
 
index 876f9d7a95458efa299ec947e1cefe7126385a74..31004328855407e33c4353c6d574756f34527a25 100644 (file)
@@ -22,6 +22,7 @@ using namespace sema;
 DelayedDiagnostic DelayedDiagnostic::makeDeprecation(SourceLocation Loc,
                                     const NamedDecl *D,
                                     const ObjCInterfaceDecl *UnknownObjCClass,
+                                    const ObjCPropertyDecl  *ObjCProperty,
                                     StringRef Msg) {
   DelayedDiagnostic DD;
   DD.Kind = Deprecation;
@@ -29,6 +30,7 @@ DelayedDiagnostic DelayedDiagnostic::makeDeprecation(SourceLocation Loc,
   DD.Loc = Loc;
   DD.DeprecationData.Decl = D;
   DD.DeprecationData.UnknownObjCClass = UnknownObjCClass;
+  DD.DeprecationData.ObjCProperty = ObjCProperty;
   char *MessageData = 0;
   if (Msg.size()) {
     MessageData = new char [Msg.size()];
index ae394d78b9616917fdf4b5d1b6ab8e12d5a2530a..b09ec08651f53abe71ee1d98d6da697530429415 100644 (file)
@@ -4803,18 +4803,25 @@ static bool isDeclDeprecated(Decl *D) {
 static void
 DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
                          SourceLocation Loc,
-                         const ObjCInterfaceDecl *UnknownObjCClass) {
+                         const ObjCInterfaceDecl *UnknownObjCClass,
+                         const ObjCPropertyDecl *ObjCPropery) {
   DeclarationName Name = D->getDeclName();
   if (!Message.empty()) {
     S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
     S.Diag(D->getLocation(),
            isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
                                   : diag::note_previous_decl) << Name;
+    if (ObjCPropery)
+      S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
+        << ObjCPropery->getDeclName() << 0;
   } else if (!UnknownObjCClass) {
     S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
     S.Diag(D->getLocation(),
            isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
                                   : diag::note_previous_decl) << Name;
+    if (ObjCPropery)
+      S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
+        << ObjCPropery->getDeclName() << 0;
   } else {
     S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
     S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
@@ -4829,16 +4836,19 @@ void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
   DD.Triggered = true;
   DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
                            DD.getDeprecationMessage(), DD.Loc,
-                           DD.getUnknownObjCClass());
+                           DD.getUnknownObjCClass(),
+                           DD.getObjCProperty());
 }
 
 void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
                                   SourceLocation Loc,
-                                  const ObjCInterfaceDecl *UnknownObjCClass) {
+                                  const ObjCInterfaceDecl *UnknownObjCClass,
+                                  const ObjCPropertyDecl  *ObjCProperty) {
   // Delay if we're currently parsing a declaration.
   if (DelayedDiagnostics.shouldDelayDiagnostics()) {
     DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, 
                                                               UnknownObjCClass,
+                                                              ObjCProperty,
                                                               Message));
     return;
   }
@@ -4846,5 +4856,5 @@ void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
   // Otherwise, don't warn if our current context is deprecated.
   if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
     return;
-  DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass);
+  DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
 }
index 97591be98eb2971a0a97c1733a05edc919f5d918..ff7e9101f1c67cbdf467af2050d1c26e427f8782 100644 (file)
@@ -87,6 +87,13 @@ static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S,
       if (const EnumDecl *TheEnumDecl = dyn_cast<EnumDecl>(DC))
         Result = TheEnumDecl->getAvailability(&Message);
     }
+  const ObjCPropertyDecl *ObjCPDecl = 0;
+  if (Result == AR_Deprecated || Result == AR_Unavailable)
+    if (ObjCPropertyDecl *ND = S.PropertyIfSetterOrGetter(D)) {
+      AvailabilityResult PDeclResult = ND->getAvailability(0);
+      if (PDeclResult == Result)
+        ObjCPDecl = ND;
+    }
   
   switch (Result) {
     case AR_Available:
@@ -94,23 +101,30 @@ static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S,
       break;
             
     case AR_Deprecated:
-      S.EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass);
+      S.EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass, ObjCPDecl);
       break;
             
     case AR_Unavailable:
       if (S.getCurContextAvailability() != AR_Unavailable) {
         if (Message.empty()) {
-          if (!UnknownObjCClass)
+          if (!UnknownObjCClass) {
             S.Diag(Loc, diag::err_unavailable) << D->getDeclName();
+            if (ObjCPDecl)
+              S.Diag(ObjCPDecl->getLocation(), diag::note_property_attribute)
+                << ObjCPDecl->getDeclName() << 1;
+          }
           else
             S.Diag(Loc, diag::warn_unavailable_fwdclass_message) 
               << D->getDeclName();
         }
-        else 
+        else
           S.Diag(Loc, diag::err_unavailable_message) 
             << D->getDeclName() << Message;
-          S.Diag(D->getLocation(), diag::note_unavailable_here) 
-          << isa<FunctionDecl>(D) << false;
+        S.Diag(D->getLocation(), diag::note_unavailable_here)
+                  << isa<FunctionDecl>(D) << false;
+        if (ObjCPDecl)
+          S.Diag(ObjCPDecl->getLocation(), diag::note_property_attribute)
+          << ObjCPDecl->getDeclName() << 1;
       }
       break;
     }
index 0f6ae4c7c7e44048ade785918c8504d88e7726e9..82b8094e768585866bfa73f711da2154780964e1 100644 (file)
@@ -22,6 +22,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/SmallString.h"
+#include "clang/Lex/Preprocessor.h"
 
 using namespace clang;
 
@@ -1560,6 +1561,58 @@ ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
         return Prop;
     }
   }
+  else if (const ObjCCategoryDecl *CatDecl =
+            dyn_cast<ObjCCategoryDecl>(CDecl)) {
+    for (ObjCContainerDecl::prop_iterator P = CatDecl->prop_begin(),
+         E = CatDecl->prop_end(); P != E; ++P) {
+      ObjCPropertyDecl *Prop = *P;
+      if (Prop->getIdentifier() == II)
+        return Prop;
+    }
+  }
+  return 0;
+}
+
+/// PropertyIfSetterOrGetter - Looks up the property if named declaration
+/// is a setter or getter method backing a property.
+ObjCPropertyDecl *Sema::PropertyIfSetterOrGetter(NamedDecl *D) {
+  if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
+    Selector Sel = Method->getSelector();
+    IdentifierInfo *Id = 0;
+    if (Sel.getNumArgs() == 0)
+      Id = Sel.getIdentifierInfoForSlot(0);
+    else if (Sel.getNumArgs() == 1) {
+      StringRef str = Sel.getNameForSlot(0);
+      if (str.startswith("set")) {
+        str = str.substr(3);
+        char front = str.front();
+        front = islower(front) ? toupper(front) : tolower(front);
+        SmallString<100> PropertyName = str;
+        PropertyName[0] = front;
+        Id = &PP.getIdentifierTable().get(PropertyName);
+      }
+    }
+    if (Id) {
+      if (isa<ObjCInterfaceDecl>(Method->getDeclContext())) {
+        const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
+        while (IDecl) {
+          ObjCPropertyDecl  *PDecl =
+            LookupPropertyDecl(cast<ObjCContainerDecl>(IDecl), Id);
+          if (PDecl)
+            return PDecl;
+          for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
+             Category; Category = Category->getNextClassCategory())
+            if ((PDecl =
+                 LookupPropertyDecl(cast<ObjCContainerDecl>(Category), Id)))
+              return PDecl;
+          IDecl = IDecl->getSuperClass();
+        }
+      } else if (ObjCPropertyDecl  *PDecl =
+                  LookupPropertyDecl(
+                    cast<ObjCContainerDecl>(Method->getDeclContext()), Id))
+               return PDecl;
+    }
+  }
   return 0;
 }
 
index 1a7c39d4e1cde7b18296ea202dd019bac3c1fa01..3443bda99bb31e55d0fb1aa8e012aa087142e3df 100644 (file)
@@ -38,7 +38,7 @@ id test6() {
   x = (id) (test6_helper(), kMagicConstant);
 }
 
-// workaround expected-note 4 {{marked unavailable here}}
+// workaround expected-note 4 {{marked unavailable here}} expected-note 2 {{property 'prop' is declared unavailable here}}
 void test7(Test7 *p) {
   *p.prop = 0; // expected-error {{'prop' is unavailable: this system declaration uses an unsupported type}}
   p.prop = 0; // expected-error {{'prop' is unavailable: this system declaration uses an unsupported type}}
index 260462abc0b8719625d1d823d20e8c375ebd261e..c0aa9fc07071d7dd899368617549206da2ae1ca7 100644 (file)
@@ -107,7 +107,8 @@ __attribute ((deprecated))
 
 
 @interface Test2
-@property int test2 __attribute__((deprecated)); // expected-note 4 {{declared here}}
+@property int test2 __attribute__((deprecated)); // expected-note 4 {{declared here}} \
+                                                // expected-note 2 {{property 'test2' is declared deprecated here}}
 @end
 
 void test(Test2 *foo) {
diff --git a/test/SemaObjC/property-deprecated-warning.m b/test/SemaObjC/property-deprecated-warning.m
new file mode 100644 (file)
index 0000000..b3c6620
--- /dev/null
@@ -0,0 +1,38 @@
+// RUN: %clang_cc1  -fsyntax-only -triple thumbv6-apple-ios3.0 -verify -Wno-objc-root-class %s
+// RUN: %clang_cc1 -x objective-c++ -fsyntax-only -triple thumbv6-apple-ios3.0 -verify -Wno-objc-root-class %s
+// rdar://12324295
+
+@protocol P
+@property(nonatomic,assign) id ptarget __attribute__((availability(ios,introduced=2.0,deprecated=3.0))); // expected-note {{property 'ptarget' is declared deprecated here}}
+@end
+
+@protocol P1<P>
+- (void)setPtarget:(id)arg; // expected-note {{method 'setPtarget:' declared here}}
+@end
+
+
+@interface UITableViewCell<P1>
+@property(nonatomic,assign) id target __attribute__((availability(ios,introduced=2.0,deprecated=3.0))); // expected-note {{property 'target' is declared deprecated here}}
+@end
+
+@interface PSTableCell : UITableViewCell
+ - (void)setTarget:(id)target; // expected-note {{method 'setTarget:' declared here}}
+@end
+
+@interface UITableViewCell(UIDeprecated)
+@property(nonatomic,assign) id dep_target  __attribute__((availability(ios,introduced=2.0,deprecated=3.0))); // expected-note {{method 'dep_target' declared here}} \
+                                                                                    // expected-note 2 {{property 'dep_target' is declared deprecated here}} \
+                                                                                    // expected-note {{method 'setDep_target:' declared here}}
+@end
+
+@implementation PSTableCell
+- (void)setTarget:(id)target {};
+- (void)setPtarget:(id)val {};
+- (void) Meth {
+  [self setTarget: (id)0]; // expected-warning {{'setTarget:' is deprecated: first deprecated in iOS 3.0}}
+  [self setDep_target: [self dep_target]]; // expected-warning {{'dep_target' is deprecated: first deprecated in iOS 3.0}} \
+                                           // expected-warning {{'setDep_target:' is deprecated: first deprecated in iOS 3.0}}
+                                          
+  [self setPtarget: (id)0]; // expected-warning {{setPtarget:' is deprecated: first deprecated in iOS 3.0}}
+}
+@end