r186037 - ObjC migrator: Improve on hueristics.

Fariborz Jahanian fjahanian at apple.com
Wed Jul 10 14:30:22 PDT 2013


Author: fjahanian
Date: Wed Jul 10 16:30:22 2013
New Revision: 186037

URL: http://llvm.org/viewvc/llvm-project?rev=186037&view=rev
Log:
ObjC migrator: Improve on hueristics.
migrate to 'copy attribute if Object
class implements NSCopying otherwise 
assume implied 'strong'. Remove 
lifetime qualifier on property as it has
moved to property's attribute. Added TODO
comment for future work by poking into
setter implementation.

Modified:
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/lib/AST/DeclObjC.cpp
    cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp
    cfe/trunk/test/ARCMT/objcmt-property.m
    cfe/trunk/test/ARCMT/objcmt-property.m.result

Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=186037&r1=186036&r2=186037&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Wed Jul 10 16:30:22 2013
@@ -1136,6 +1136,8 @@ public:
     return lookupInstanceVariable(IVarName, ClassDeclared);
   }
 
+  ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
+                          
   // Lookup a method. First, we search locally. If a method isn't
   // found, we search referenced protocols and class categories.
   ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=186037&r1=186036&r2=186037&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Wed Jul 10 16:30:22 2013
@@ -441,6 +441,17 @@ ObjCInterfaceDecl *ObjCInterfaceDecl::lo
   return NULL;
 }
 
+ObjCProtocolDecl *
+ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
+  for (ObjCInterfaceDecl::all_protocol_iterator P =
+       all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
+       P != PE; ++P)
+    if ((*P)->lookupProtocolNamed(Name))
+      return (*P);
+  ObjCInterfaceDecl *SuperClass = getSuperClass();
+  return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
+}
+
 /// lookupMethod - This method returns an instance/class method by looking in
 /// the class, its categories, and its super classes (using a linear search).
 /// When argument category "C" is specified, any implicit method found

Modified: cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp?rev=186037&r1=186036&r2=186037&view=diff
==============================================================================
--- cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp (original)
+++ cfe/trunk/lib/Edit/RewriteObjCFoundationAPI.cpp Wed Jul 10 16:30:22 2013
@@ -358,23 +358,38 @@ bool edit::rewriteToObjCLiteralSyntax(co
 bool edit::rewriteToObjCProperty(const ObjCMethodDecl *Getter,
                                  const ObjCMethodDecl *Setter,
                                  const NSAPI &NS, Commit &commit) {
+  ASTContext &Context = NS.getASTContext();
   std::string PropertyString = "@property";
   const ParmVarDecl *argDecl = *Setter->param_begin();
-  QualType ArgType = argDecl->getType();
+  QualType ArgType = Context.getCanonicalType(argDecl->getType());
   Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime();
   
   if (ArgType->isObjCRetainableType() &&
       propertyLifetime == Qualifiers::OCL_Strong) {
-    PropertyString += "(copy)";
+    if (const ObjCObjectPointerType *ObjPtrTy =
+          ArgType->getAs<ObjCObjectPointerType>()) {
+      ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
+      if (IDecl &&
+          IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying")))
+        PropertyString += "(copy)";
+    }
   }
   else if (propertyLifetime == Qualifiers::OCL_Weak)
+    // TODO. More precise determination of 'weak' attribute requires
+    // looking into setter's implementation for backing weak ivar.
     PropertyString += "(weak)";
   else
     PropertyString += "(unsafe_unretained)";
-  
-  QualType PropQT = Getter->getResultType();
+
+  // strip off any ARC lifetime qualifier.
+  QualType CanResultTy = Context.getCanonicalType(Getter->getResultType());
+  if (CanResultTy.getQualifiers().hasObjCLifetime()) {
+    Qualifiers Qs = CanResultTy.getQualifiers();
+    Qs.removeObjCLifetime();
+    CanResultTy = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs);
+  }
   PropertyString += " ";
-  PropertyString += PropQT.getAsString(NS.getASTContext().getPrintingPolicy());
+  PropertyString += CanResultTy.getAsString(Context.getPrintingPolicy());
   PropertyString += " ";
   PropertyString += Getter->getNameAsString();
   commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(),

Modified: cfe/trunk/test/ARCMT/objcmt-property.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-property.m?rev=186037&r1=186036&r2=186037&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-property.m (original)
+++ cfe/trunk/test/ARCMT/objcmt-property.m Wed Jul 10 16:30:22 2013
@@ -4,7 +4,13 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c -fobjc-runtime-has-weak -fobjc-arc -fobjc-default-synthesize-properties %s.result
 
 @class NSString;
- at interface NSObject @end
+ at protocol NSCopying @end
+
+ at interface NSObject <NSCopying>
+ at end
+
+ at interface NSDictionary : NSObject
+ at end
 
 @interface I : NSObject {
   int ivarVal;
@@ -24,6 +30,8 @@
 - (NSString *) UnavailProp2;
 - (void) setUnavailProp2  : (NSString *)Val  __attribute__((unavailable));
 
+- (NSDictionary*) undoAction;
+- (void) setUndoAction: (NSDictionary*)Arg;
 @end
 
 @implementation I

Modified: cfe/trunk/test/ARCMT/objcmt-property.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-property.m.result?rev=186037&r1=186036&r2=186037&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-property.m.result (original)
+++ cfe/trunk/test/ARCMT/objcmt-property.m.result Wed Jul 10 16:30:22 2013
@@ -4,15 +4,21 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c -fobjc-runtime-has-weak -fobjc-arc -fobjc-default-synthesize-properties %s.result
 
 @class NSString;
- at interface NSObject @end
+ at protocol NSCopying @end
+
+ at interface NSObject <NSCopying>
+ at end
+
+ at interface NSDictionary : NSObject
+ at end
 
 @interface I : NSObject {
   int ivarVal;
 }
 
- at property(weak) NSString *__weak WeakProp;
+ at property(weak) NSString * WeakProp;
 
- at property(copy) NSString * StrongProp;
+ at property NSString * StrongProp;
 
 
 - (NSString *) UnavailProp  __attribute__((unavailable));
@@ -24,6 +30,8 @@
 - (NSString *) UnavailProp2;
 - (void) setUnavailProp2  : (NSString *)Val  __attribute__((unavailable));
 
+ at property(copy) NSDictionary * undoAction;
+
 @end
 
 @implementation I
@@ -42,8 +50,8 @@
 
 
 
- at property(copy) NSArray * names2;
- at property(copy) NSArray * names3;
- at property(copy) NSArray *__strong names4;
- at property(copy) NSArray * names1;
+ at property NSArray * names2;
+ at property NSArray * names3;
+ at property NSArray * names4;
+ at property NSArray * names1;
 @end





More information about the cfe-commits mailing list