[cfe-commits] r156782 - in /cfe/trunk: lib/Edit/EditedSource.cpp lib/Edit/RewriteObjCFoundationAPI.cpp test/ARCMT/objcmt-numeric-literals.m test/ARCMT/objcmt-numeric-literals.m.result test/ARCMT/objcmt-subscripting-literals.m test/ARCMT/objcmt-subscripting-literals.m.result

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon May 14 15:01:53 PDT 2012


Author: akirtzidis
Date: Mon May 14 17:01:53 2012
New Revision: 156782

URL: http://llvm.org/viewvc/llvm-project?rev=156782&view=rev
Log:
[objcmt] When rewriting to array/dictionary literals, add an explicit
cast to 'id' for any argument that requires it.

Part of rdar://11438360.

Modified:
    cfe/trunk/lib/Edit/EditedSource.cpp
    cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp
    cfe/trunk/test/ARCMT/objcmt-numeric-literals.m
    cfe/trunk/test/ARCMT/objcmt-numeric-literals.m.result
    cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m
    cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m.result

Modified: cfe/trunk/lib/Edit/EditedSource.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Edit/EditedSource.cpp?rev=156782&r1=156781&r2=156782&view=diff
==============================================================================
--- cfe/trunk/lib/Edit/EditedSource.cpp (original)
+++ cfe/trunk/lib/Edit/EditedSource.cpp Mon May 14 17:01:53 2012
@@ -100,8 +100,11 @@
     FileOffset B = I->first;
     FileOffset E = B.getWithOffset(FA.RemoveLen);
 
+    if (BeginOffs == B)
+      break;
+
     if (BeginOffs < E) {
-      if (BeginOffs >= B) {
+      if (BeginOffs > B) {
         BeginOffs = E;
         ++I;
       }

Modified: cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp?rev=156782&r1=156781&r2=156782&view=diff
==============================================================================
--- cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp (original)
+++ cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp Mon May 14 17:01:53 2012
@@ -229,6 +229,9 @@
 // rewriteToArrayLiteral.
 //===----------------------------------------------------------------------===//
 
+/// \brief Adds an explicit cast to 'id' if the type is not objc object.
+static void objectifyExpr(const Expr *E, Commit &commit);
+
 static bool rewriteToArrayLiteral(const ObjCMessageExpr *Msg,
                                   const NSAPI &NS, Commit &commit) {
   Selector Sel = Msg->getSelector();
@@ -244,6 +247,7 @@
   if (Sel == NS.getNSArraySelector(NSAPI::NSArr_arrayWithObject)) {
     if (Msg->getNumArgs() != 1)
       return false;
+    objectifyExpr(Msg->getArg(0), commit);
     SourceRange ArgRange = Msg->getArg(0)->getSourceRange();
     commit.replaceWithInner(MsgRange, ArgRange);
     commit.insertWrap("@[", ArgRange, "]");
@@ -257,6 +261,9 @@
     if (!NS.getASTContext().isSentinelNullExpr(SentinelExpr))
       return false;
 
+    for (unsigned i = 0, e = Msg->getNumArgs() - 1; i != e; ++i)
+      objectifyExpr(Msg->getArg(i), commit);
+
     if (Msg->getNumArgs() == 1) {
       commit.replace(MsgRange, "@[]");
       return true;
@@ -291,6 +298,10 @@
                                     NSAPI::NSDict_dictionaryWithObjectForKey)) {
     if (Msg->getNumArgs() != 2)
       return false;
+
+    objectifyExpr(Msg->getArg(0), commit);
+    objectifyExpr(Msg->getArg(1), commit);
+
     SourceRange ValRange = Msg->getArg(0)->getSourceRange();
     SourceRange KeyRange = Msg->getArg(1)->getSourceRange();
     // Insert key before the value.
@@ -319,6 +330,9 @@
     }
 
     for (unsigned i = 0; i < SentinelIdx; i += 2) {
+      objectifyExpr(Msg->getArg(i), commit);
+      objectifyExpr(Msg->getArg(i+1), commit);
+
       SourceRange ValRange = Msg->getArg(i)->getSourceRange();
       SourceRange KeyRange = Msg->getArg(i+1)->getSourceRange();
       // Insert value after key.
@@ -585,3 +599,52 @@
   }
   return true;
 }
+
+static bool castOperatorNeedsParens(const Expr *FullExpr) {
+  const Expr* Expr = FullExpr->IgnoreImpCasts();
+  if (isa<ArraySubscriptExpr>(Expr) ||
+      isa<CallExpr>(Expr) ||
+      isa<DeclRefExpr>(Expr) ||
+      isa<CastExpr>(Expr) ||
+      isa<CXXNewExpr>(Expr) ||
+      isa<CXXConstructExpr>(Expr) ||
+      isa<CXXDeleteExpr>(Expr) ||
+      isa<CXXNoexceptExpr>(Expr) ||
+      isa<CXXPseudoDestructorExpr>(Expr) ||
+      isa<CXXScalarValueInitExpr>(Expr) ||
+      isa<CXXThisExpr>(Expr) ||
+      isa<CXXTypeidExpr>(Expr) ||
+      isa<CXXUnresolvedConstructExpr>(Expr) ||
+      isa<ObjCMessageExpr>(Expr) ||
+      isa<ObjCPropertyRefExpr>(Expr) ||
+      isa<ObjCProtocolExpr>(Expr) ||
+      isa<MemberExpr>(Expr) ||
+      isa<ParenExpr>(FullExpr) ||
+      isa<ParenListExpr>(Expr) ||
+      isa<SizeOfPackExpr>(Expr) ||
+      isa<UnaryOperator>(Expr))
+    return false;
+
+  return true;
+}
+
+static void objectifyExpr(const Expr *E, Commit &commit) {
+  if (!E) return;
+
+  QualType T = E->getType();
+  if (T->isObjCObjectPointerType()) {
+    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
+      if (ICE->getCastKind() != CK_CPointerToObjCPointerCast)
+        return;
+    } else {
+      return;
+    }
+  } else if (!T->isPointerType()) {
+    return;
+  }
+
+  SourceRange Range = E->getSourceRange();
+  if (castOperatorNeedsParens(E))
+    commit.insertWrap("(", Range, ")");
+  commit.insertBefore(Range.getBegin(), "(id)");
+}

Modified: cfe/trunk/test/ARCMT/objcmt-numeric-literals.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-numeric-literals.m?rev=156782&r1=156781&r2=156782&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-numeric-literals.m (original)
+++ cfe/trunk/test/ARCMT/objcmt-numeric-literals.m Mon May 14 17:01:53 2012
@@ -1,6 +1,7 @@
 // RUN: rm -rf %t
 // RUN: %clang_cc1 -objcmt-migrate-literals -objcmt-migrate-subscripting -mt-migrate-directory %t %s -x objective-c++ 
 // RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c++ %s.result
 
 #define YES __objc_yes
 #define NO __objc_no

Modified: cfe/trunk/test/ARCMT/objcmt-numeric-literals.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-numeric-literals.m.result?rev=156782&r1=156781&r2=156782&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-numeric-literals.m.result (original)
+++ cfe/trunk/test/ARCMT/objcmt-numeric-literals.m.result Mon May 14 17:01:53 2012
@@ -1,6 +1,7 @@
 // RUN: rm -rf %t
 // RUN: %clang_cc1 -objcmt-migrate-literals -objcmt-migrate-subscripting -mt-migrate-directory %t %s -x objective-c++ 
 // RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c++ %s.result
 
 #define YES __objc_yes
 #define NO __objc_no

Modified: cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m?rev=156782&r1=156781&r2=156782&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m (original)
+++ cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m Mon May 14 17:01:53 2012
@@ -1,10 +1,13 @@
 // RUN: rm -rf %t
 // RUN: %clang_cc1 -objcmt-migrate-literals -objcmt-migrate-subscripting -mt-migrate-directory %t %s -x objective-c -triple x86_64-apple-darwin11 
 // RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c %s.result
 
 typedef signed char BOOL;
 #define nil ((void*) 0)
 
+typedef const struct __CFString * CFStringRef;
+
 @interface NSObject
 + (id)alloc;
 @end
@@ -135,3 +138,17 @@
   o = [*parr objectAtIndex:2];
 }
 @end
+
+extern const CFStringRef globStr;
+
+void test1(NSString *str) {
+  NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: str, globStr, nil];
+  dict = [NSDictionary dictionaryWithObjectsAndKeys: globStr, str, nil];
+  dict = [NSDictionary dictionaryWithObject:str forKey:globStr];
+  dict = [NSDictionary dictionaryWithObject:globStr forKey:str];
+
+  NSArray *arr = [NSArray arrayWithObjects: globStr, globStr, nil];
+  arr = [NSArray arrayWithObjects: str, globStr, nil];
+  arr = [NSArray arrayWithObjects: globStr, str, nil];
+  arr = [NSArray arrayWithObject:globStr];
+}

Modified: cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m.result?rev=156782&r1=156781&r2=156782&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m.result (original)
+++ cfe/trunk/test/ARCMT/objcmt-subscripting-literals.m.result Mon May 14 17:01:53 2012
@@ -1,10 +1,13 @@
 // RUN: rm -rf %t
 // RUN: %clang_cc1 -objcmt-migrate-literals -objcmt-migrate-subscripting -mt-migrate-directory %t %s -x objective-c -triple x86_64-apple-darwin11 
 // RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c %s.result
 
 typedef signed char BOOL;
 #define nil ((void*) 0)
 
+typedef const struct __CFString * CFStringRef;
+
 @interface NSObject
 + (id)alloc;
 @end
@@ -135,3 +138,17 @@
   o = (*parr)[2];
 }
 @end
+
+extern const CFStringRef globStr;
+
+void test1(NSString *str) {
+  NSDictionary *dict = @{(id)globStr: str};
+  dict = @{str: (id)globStr};
+  dict = @{(id)globStr: str};
+  dict = @{str: (id)globStr};
+
+  NSArray *arr = @[(id)globStr, (id)globStr];
+  arr = @[str, (id)globStr];
+  arr = @[(id)globStr, str];
+  arr = @[(id)globStr];
+}





More information about the cfe-commits mailing list