[cfe-commits] r146904 - in /cfe/trunk: include/clang/Basic/DiagnosticParseKinds.td lib/Basic/IdentifierTable.cpp lib/Basic/Targets.cpp lib/Parse/ParseExpr.cpp test/SemaObjC/arc-bridged-cast.m test/SemaObjC/illegal-nonarc-bridged-cast.m

Fariborz Jahanian fjahanian at apple.com
Mon Dec 19 13:06:15 PST 2011


Author: fjahanian
Date: Mon Dec 19 15:06:15 2011
New Revision: 146904

URL: http://llvm.org/viewvc/llvm-project?rev=146904&view=rev
Log:
objc-arc: bridge casts in non-objc-arc mode are ignord.
But, warn too. // rdar://10597832

Added:
    cfe/trunk/test/SemaObjC/illegal-nonarc-bridged-cast.m
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
    cfe/trunk/lib/Basic/IdentifierTable.cpp
    cfe/trunk/lib/Basic/Targets.cpp
    cfe/trunk/lib/Parse/ParseExpr.cpp
    cfe/trunk/test/SemaObjC/arc-bridged-cast.m

Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=146904&r1=146903&r2=146904&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Mon Dec 19 15:06:15 2011
@@ -326,6 +326,9 @@
 let CategoryName = "ARC Parse Issue" in {
 def err_arc_bridge_retain : Error<
   "unknown cast annotation __bridge_retain; did you mean __bridge_retained?">;
+def warn_arc_bridge_retain : Warning<
+  "bridge casts will have no effect in non-arc mode and will be ignored">,
+  InGroup<DiagGroup<"bridge-casts-non-arc-mode">>;
 }
   
 def err_objc_illegal_visibility_spec : Error<

Modified: cfe/trunk/lib/Basic/IdentifierTable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/IdentifierTable.cpp?rev=146904&r1=146903&r2=146904&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/IdentifierTable.cpp (original)
+++ cfe/trunk/lib/Basic/IdentifierTable.cpp Mon Dec 19 15:06:15 2011
@@ -125,7 +125,9 @@
   else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
   else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
   else if (LangOpts.C1X && (Flags & KEYC1X)) AddResult = 2;
-  else if (LangOpts.ObjCAutoRefCount && (Flags & KEYARC)) AddResult = 2;
+  // We treat bridge casts as objective-C keywords so we can warn on them
+  // in non-arc mode.
+  else if (LangOpts.ObjC2 && (Flags & KEYARC)) AddResult = 2;
   else if (LangOpts.CPlusPlus && (Flags & KEYCXX0X)) AddResult = 3;
 
   // Don't add this keyword if disabled in this language.

Modified: cfe/trunk/lib/Basic/Targets.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Targets.cpp?rev=146904&r1=146903&r2=146904&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Targets.cpp (original)
+++ cfe/trunk/lib/Basic/Targets.cpp Mon Dec 19 15:06:15 2011
@@ -99,13 +99,6 @@
     // allow this in C, since one might have block pointers in structs that
     // are used in pure C code and in Objective-C ARC.
     Builder.defineMacro("__unsafe_unretained", "");
-
-    // The Objective-C bridged cast keywords are defined to nothing in non-ARC
-    // mode; then they become normal, C-style casts.
-    Builder.defineMacro("__bridge", "");
-    Builder.defineMacro("__bridge_transfer", "");
-    Builder.defineMacro("__bridge_retained", "");
-    Builder.defineMacro("__bridge_retain", "");
   }
 
   if (Opts.Static)

Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=146904&r1=146903&r2=146904&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Mon Dec 19 15:06:15 2011
@@ -1817,9 +1817,23 @@
     return ExprError();
   }
 
+  // Diagnose use of bridge casts in non-arc mode.
+  bool BridgeCast = (getLang().ObjC2 &&
+                     (Tok.is(tok::kw___bridge) || 
+                      Tok.is(tok::kw___bridge_transfer) ||
+                      Tok.is(tok::kw___bridge_retained) ||
+                      Tok.is(tok::kw___bridge_retain)));
+  if (BridgeCast && !getLang().ObjCAutoRefCount) {
+    SourceLocation BridgeKeywordLoc = ConsumeToken();
+    if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
+      Diag(BridgeKeywordLoc, diag::warn_arc_bridge_retain)
+      << FixItHint::CreateReplacement(BridgeKeywordLoc,
+                                      "");
+    BridgeCast = false;
+  }
+  
   // None of these cases should fall through with an invalid Result
   // unless they've already reported an error.
-  
   if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
     Diag(Tok, diag::ext_gnu_statement_expr);
     ParsedAttributes attrs(AttrFactory);
@@ -1829,11 +1843,7 @@
     // If the substmt parsed correctly, build the AST node.
     if (!Stmt.isInvalid())
       Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
-  } else if (ExprType >= CompoundLiteral && 
-             (Tok.is(tok::kw___bridge) || 
-              Tok.is(tok::kw___bridge_transfer) ||
-              Tok.is(tok::kw___bridge_retained) ||
-              Tok.is(tok::kw___bridge_retain))) {
+  } else if (ExprType >= CompoundLiteral && BridgeCast) {
     tok::TokenKind tokenKind = Tok.getKind();
     SourceLocation BridgeKeywordLoc = ConsumeToken();
 

Modified: cfe/trunk/test/SemaObjC/arc-bridged-cast.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc-bridged-cast.m?rev=146904&r1=146903&r2=146904&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/arc-bridged-cast.m (original)
+++ cfe/trunk/test/SemaObjC/arc-bridged-cast.m Mon Dec 19 15:06:15 2011
@@ -1,5 +1,4 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -fblocks -verify %s
-// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fblocks %s
 
 typedef const void *CFTypeRef;
 typedef const struct __CFString *CFStringRef;

Added: cfe/trunk/test/SemaObjC/illegal-nonarc-bridged-cast.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/illegal-nonarc-bridged-cast.m?rev=146904&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/illegal-nonarc-bridged-cast.m (added)
+++ cfe/trunk/test/SemaObjC/illegal-nonarc-bridged-cast.m Mon Dec 19 15:06:15 2011
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fblocks -verify %s
+// rdar://10597832
+
+typedef const void *CFTypeRef;
+typedef const struct __CFString *CFStringRef;
+
+ at interface NSString
+ at end
+
+CFTypeRef CFCreateSomething();
+CFStringRef CFCreateString();
+CFTypeRef CFGetSomething();
+CFStringRef CFGetString();
+
+id CreateSomething();
+NSString *CreateNSString();
+
+void from_cf() {
+  id obj1 = (__bridge_transfer id)CFCreateSomething(); // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}
+  id obj2 = (__bridge_transfer NSString*)CFCreateString(); // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}
+  (__bridge int*)CFCreateSomething();  // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}  \
+                                       // expected-warning {{expression result unused}}
+  id obj3 = (__bridge id)CFGetSomething(); // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}
+  id obj4 = (__bridge NSString*)CFGetString(); // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}
+}
+
+void to_cf(id obj) {
+  CFTypeRef cf1 = (__bridge_retained CFTypeRef)CreateSomething(); // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}
+  CFStringRef cf2 = (__bridge_retained CFStringRef)CreateNSString(); // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}
+  CFTypeRef cf3 = (__bridge CFTypeRef)CreateSomething(); // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}
+  CFStringRef cf4 = (__bridge CFStringRef)CreateNSString(); // expected-warning {{bridge casts will have no effect in non-arc mode and will be ignored}}
+}
+
+void fixits() {
+  id obj1 = (id)CFCreateSomething();
+  CFTypeRef cf1 = (CFTypeRef)CreateSomething();
+}





More information about the cfe-commits mailing list