]> granicus.if.org Git - clang/commitdiff
objc-arc: Support objc_arc_weak_unavailable on those
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 6 Jul 2011 19:24:05 +0000 (19:24 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 6 Jul 2011 19:24:05 +0000 (19:24 +0000)
classes which are incompatible with weak references.
// rdar://9693477

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

include/clang/Basic/Attr.td
include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Sema/AttributeList.h
lib/Sema/AttributeList.cpp
lib/Sema/SemaDeclAttr.cpp
lib/Sema/SemaType.cpp
test/SemaObjC/arc-unavailable-for-weakref.m [new file with mode: 0644]

index dea67ab9f35730e9cba7ca9ce4f19b05ea0622e6..e64dc6a2ade097a6eb4cf541e18d6909a1b75398 100644 (file)
@@ -484,6 +484,10 @@ def Unavailable : InheritableAttr {
   let Args = [StringArgument<"Message">];
 }
 
+def ArcWeakrefUnavailable : InheritableAttr {
+  let Spellings = ["objc_arc_weak_reference_unavailable"];
+}
+
 def Unused : InheritableAttr {
   let Spellings = ["unused"];
 }
index c5039ff9b90149cf899b15d07edb12f57f034c2b..b2301e18d7637b7e1f4a2adf83a595b25b9b8b3a 100644 (file)
@@ -357,6 +357,8 @@ def err_class_extension_after_impl : Error<
   "cannot declare class extension for %0 after class implementation">;
 def note_implementation_declared : Note<
   "class implementation is declared here">;
+def note_class_declared : Note<
+  "class is declared here">;
 def warn_dup_category_def : Warning<
   "duplicate definition of category %1 on interface %0">;
 def err_conflicting_super_class : Error<"conflicting super class name %0">;
@@ -2568,6 +2570,8 @@ let CategoryName = "Automatic Reference Counting Issue" in {
 // ARC-mode diagnostics.
 def err_arc_weak_no_runtime : Error<
   "the current deployment target does not support automated __weak references">;
+def err_arc_unsupported_weak_class : Error<
+  "class is incompatible with __weak references">;
 def err_arc_illegal_explicit_message : Error<
   "ARC forbids explicit message send of %0">;
 def err_arc_unused_init_message : Error<
index d34d7f584cab8d9896c7cf8caf0ab93fffdc996f..4e6a0dfd4b6dbfebeffeb3e373163d959818776d 100644 (file)
@@ -243,6 +243,7 @@ public:
     AT_weak,
     AT_weak_import,
     AT_weakref,
+    AT_arc_weakref_unavailable,
     IgnoredAttribute,
     UnknownAttribute
   };
index afc23e9be2018f177c641c991bb543bb152286d1..5a8330bbfd77d6575cc97533fa5f71a2e0e8a4bf 100644 (file)
@@ -107,6 +107,7 @@ AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) {
   return llvm::StringSwitch<AttributeList::Kind>(AttrName)
     .Case("weak", AT_weak)
     .Case("weakref", AT_weakref)
+    .Case("objc_arc_weak_reference_unavailable", AT_arc_weakref_unavailable)
     .Case("pure", AT_pure)
     .Case("mode", AT_mode)
     .Case("used", AT_used)
index 103976bc12f89126022bf384e1d461a4b8fcac3d..adef528b0a47f904dae964722aae689f5d5b43f3 100644 (file)
@@ -1091,6 +1091,18 @@ static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
   D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
 }
 
+static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, 
+                                            const AttributeList &Attr) {
+  unsigned NumArgs = Attr.getNumArgs();
+  if (NumArgs > 0) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
+    return;
+  }
+  
+  D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
+                                          Attr.getLoc(), S.Context));
+}
+
 static void handleAvailabilityAttr(Sema &S, Decl *D,
                                    const AttributeList &Attr) {
   IdentifierInfo *Platform = Attr.getParameterName();
@@ -3011,6 +3023,9 @@ static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
   case AttributeList::AT_MsStruct:    handleMsStructAttr    (S, D, Attr); break;
   case AttributeList::AT_section:     handleSectionAttr     (S, D, Attr); break;
   case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
+  case AttributeList::AT_arc_weakref_unavailable: 
+    handleArcWeakrefUnavailableAttr (S, D, Attr); 
+    break;
   case AttributeList::AT_unused:      handleUnusedAttr      (S, D, Attr); break;
   case AttributeList::AT_used:        handleUsedAttr        (S, D, Attr); break;
   case AttributeList::AT_visibility:  handleVisibilityAttr  (S, D, Attr); break;
index 14184220064d9be8f42387e850e572e192a9ecf0..ae1ccf9886d35c14d1471a2c83cdb59dbdc1919b 100644 (file)
@@ -3207,7 +3207,30 @@ static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
     attr.setInvalid();
     return true;
   }
-
+    
+  // Forbid __weak for class objects marked as 
+  // objc_arc_weak_reference_unavailable
+  if (lifetime == Qualifiers::OCL_Weak) {
+    QualType T = type;
+    if (T->isReferenceType()) {
+      T = T->getAs<ReferenceType>()->getPointeeType();
+    }
+    while (const PointerType *ptr = T->getAs<PointerType>())
+      T = ptr->getPointeeType();
+    if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
+      ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
+      while (Class) {
+        if (Class->hasAttr<ArcWeakrefUnavailableAttr>()) {
+          S.Diag(attr.getLoc(), diag::err_arc_unsupported_weak_class);
+          S.Diag(ObjT->getInterfaceDecl()->getLocation(), 
+                 diag::note_class_declared);
+          break;
+        }
+        Class = Class->getSuperClass();
+      }
+    }
+  }
+  
   return true;
 }
 
diff --git a/test/SemaObjC/arc-unavailable-for-weakref.m b/test/SemaObjC/arc-unavailable-for-weakref.m
new file mode 100644 (file)
index 0000000..07a7e7b
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-nonfragile-abi -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify %s
+// rdar://9693477
+
+__attribute__((objc_arc_weak_reference_unavailable))
+@interface NSOptOut1072  // expected-note {{class is declared here}}
+@end
+
+@interface sub : NSOptOut1072 @end // expected-note 2 {{class is declared here}}
+
+int main() {
+  __weak sub *w2; // expected-error {{class is incompatible with __weak references}}
+
+  __weak NSOptOut1072 *ns1; // expected-error {{class is incompatible with __weak references}}
+
+  id obj;
+
+  ns1 = (__weak sub *)obj; // expected-error {{class is incompatible with __weak references}}
+}