[cfe-commits] r156125 - in /cfe/trunk: lib/Rewrite/RewriteModernObjC.cpp test/Rewriter/rewrite-modern-default-property-synthesis.mm

Fariborz Jahanian fjahanian at apple.com
Thu May 3 15:52:13 PDT 2012


Author: fjahanian
Date: Thu May  3 17:52:13 2012
New Revision: 156125

URL: http://llvm.org/viewvc/llvm-project?rev=156125&view=rev
Log:
modern objc translator: support for default property
synthesis translation. // rdar://11374235 - wip.

Added:
    cfe/trunk/test/Rewriter/rewrite-modern-default-property-synthesis.mm
Modified:
    cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp

Modified: cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp?rev=156125&r1=156124&r2=156125&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp (original)
+++ cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp Thu May  3 17:52:13 2012
@@ -781,19 +781,34 @@
   return S;
 }
 
+/// mustSynthesizeSetterGetterMethod - returns true if setter or getter has not
+/// been found in the class implementation. In this case, it must be synthesized.
+static bool mustSynthesizeSetterGetterMethod(ObjCImplementationDecl *IMP,
+                                             ObjCPropertyDecl *PD,
+                                             bool getter) {
+  return getter ? !IMP->getInstanceMethod(PD->getGetterName())
+                : !IMP->getInstanceMethod(PD->getSetterName());
+  
+}
+
 void RewriteModernObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
                                           ObjCImplementationDecl *IMD,
                                           ObjCCategoryImplDecl *CID) {
   static bool objcGetPropertyDefined = false;
   static bool objcSetPropertyDefined = false;
-  SourceLocation startLoc = PID->getLocStart();
-  InsertText(startLoc, "// ");
-  const char *startBuf = SM->getCharacterData(startLoc);
-  assert((*startBuf == '@') && "bogus @synthesize location");
-  const char *semiBuf = strchr(startBuf, ';');
-  assert((*semiBuf == ';') && "@synthesize: can't find ';'");
-  SourceLocation onePastSemiLoc =
-    startLoc.getLocWithOffset(semiBuf-startBuf+1);
+  SourceLocation startGetterSetterLoc;
+  
+  if (PID->getLocStart().isValid()) {
+    SourceLocation startLoc = PID->getLocStart();
+    InsertText(startLoc, "// ");
+    const char *startBuf = SM->getCharacterData(startLoc);
+    assert((*startBuf == '@') && "bogus @synthesize location");
+    const char *semiBuf = strchr(startBuf, ';');
+    assert((*semiBuf == ';') && "@synthesize: can't find ';'");
+    startGetterSetterLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1);
+  }
+  else
+    startGetterSetterLoc = IMD ? IMD->getLocEnd() : CID->getLocEnd();
 
   if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
     return; // FIXME: is this correct?
@@ -805,7 +820,7 @@
   if (!OID)
     return;
   unsigned Attributes = PD->getPropertyAttributes();
-  if (!PD->getGetterMethodDecl()->isDefined()) {
+  if (mustSynthesizeSetterGetterMethod(IMD, PD, true /*getter*/)) {
     bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
                           (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | 
                                          ObjCPropertyDecl::OBJC_PR_copy));
@@ -857,10 +872,11 @@
     else
       Getr += "return " + getIvarAccessString(OID);
     Getr += "; }";
-    InsertText(onePastSemiLoc, Getr);
+    InsertText(startGetterSetterLoc, Getr);
   }
   
-  if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined())
+  if (PD->isReadOnly() || 
+      !mustSynthesizeSetterGetterMethod(IMD, PD, false /*setter*/))
     return;
 
   // Generate the 'setter' function.
@@ -898,8 +914,8 @@
     Setr += getIvarAccessString(OID) + " = ";
     Setr += PD->getName();
   }
-  Setr += "; }";
-  InsertText(onePastSemiLoc, Setr);
+  Setr += "; }\n";
+  InsertText(startGetterSetterLoc, Setr);
 }
 
 static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl,
@@ -6837,12 +6853,12 @@
     if (!PD)
       continue;
     if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
-      if (!Getter->isDefined())
+      if (mustSynthesizeSetterGetterMethod(IDecl, PD, true /*getter*/))
         InstanceMethods.push_back(Getter);
     if (PD->isReadOnly())
       continue;
     if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
-      if (!Setter->isDefined())
+      if (mustSynthesizeSetterGetterMethod(IDecl, PD, false /*setter*/))
         InstanceMethods.push_back(Setter);
   }
   

Added: cfe/trunk/test/Rewriter/rewrite-modern-default-property-synthesis.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Rewriter/rewrite-modern-default-property-synthesis.mm?rev=156125&view=auto
==============================================================================
--- cfe/trunk/test/Rewriter/rewrite-modern-default-property-synthesis.mm (added)
+++ cfe/trunk/test/Rewriter/rewrite-modern-default-property-synthesis.mm Thu May  3 17:52:13 2012
@@ -0,0 +1,60 @@
+// RUN: %clang_cc1 -x objective-c++ -fms-extensions -fobjc-default-synthesize-properties -rewrite-objc %s -o %t-rw.cpp 
+// RUN: %clang_cc1 -fsyntax-only  -DSEL="void *" -Did="struct objc_object *" -Wno-attributes -Wno-address-of-temporary -D"__declspec(X)=" %t-rw.cpp
+// rdar://11374235
+
+extern "C" void *sel_registerName(const char *);
+
+ at interface NSObject 
+- (void) release;
+- (id) retain;
+ at end
+ at class NSString;
+
+ at interface SynthItAll : NSObject
+ at property int howMany;
+ at property (retain) NSString* what; 
+ at end
+
+ at implementation SynthItAll
+ at end
+
+
+ at interface SynthSetter : NSObject
+ at property (nonatomic) int howMany; 
+ at property (nonatomic, retain) NSString* what; 
+ at end
+
+ at implementation SynthSetter
+
+- (int) howMany {
+    return _howMany;
+}
+// - (void) setHowMany: (int) value
+
+- (NSString*) what {
+    return _what;
+}
+// - (void) setWhat: (NSString*) value    
+ at end
+
+
+ at interface SynthGetter : NSObject
+ at property (nonatomic) int howMany;
+ at property (nonatomic, retain) NSString* what;
+ at end
+
+ at implementation SynthGetter
+// - (int) howMany
+- (void) setHowMany: (int) value {
+    _howMany = value;
+}
+
+// - (NSString*) what
+- (void) setWhat: (NSString*) value {
+    if (_what != value) {
+        [_what release];
+        _what = [value retain];
+    }
+}
+ at end
+





More information about the cfe-commits mailing list