[cfe-commits] r152451 - in /cfe/trunk: lib/Rewrite/RewriteModernObjC.cpp test/Rewriter/rewrite-modern-ivars-2.mm

Fariborz Jahanian fjahanian at apple.com
Fri Mar 9 15:46:23 PST 2012


Author: fjahanian
Date: Fri Mar  9 17:46:23 2012
New Revision: 152451

URL: http://llvm.org/viewvc/llvm-project?rev=152451&view=rev
Log:
objective-c modern rewriter. More fixes related to rewriting
ivars in the modern rewriter.


Added:
    cfe/trunk/test/Rewriter/rewrite-modern-ivars-2.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=152451&r1=152450&r2=152451&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp (original)
+++ cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp Fri Mar  9 17:46:23 2012
@@ -336,6 +336,8 @@
     
     void RewriteObjCFieldDecl(FieldDecl *fieldDecl, std::string &Result);
     
+    bool RewriteObjCFieldDeclType(QualType &Type, std::string &Result);
+    
     void RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl,
                                   std::string &Result);
     
@@ -3168,14 +3170,15 @@
   return false;
 }
 
-/// RewriteObjCFieldDecl - This routine rewrites a field into the buffer.
+/// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer.
 /// It handles elaborated types, as well as enum types in the process.
-void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl, 
-                                             std::string &Result) {
-  QualType Type = fieldDecl->getType();
-  std::string Name = fieldDecl->getNameAsString();
-
-  if (Type->isRecordType()) {
+bool RewriteModernObjC::RewriteObjCFieldDeclType(QualType &Type, 
+                                                 std::string &Result) {
+  if (Type->isArrayType()) {
+    QualType ElemTy = Context->getBaseElementType(Type);
+    return RewriteObjCFieldDeclType(ElemTy, Result);
+  }
+  else if (Type->isRecordType()) {
     RecordDecl *RD = Type->getAs<RecordType>()->getDecl();
     if (RD->isCompleteDefinition()) {
       if (RD->isStruct())
@@ -3184,23 +3187,22 @@
         Result += "\n\tunion ";
       else
         assert(false && "class not allowed as an ivar type");
-  
+      
       Result += RD->getName();
       if (TagsDefinedInIvarDecls.count(RD)) {
         // This struct is already defined. Do not write its definition again.
-        Result += " "; Result += Name; Result += ";\n";
-        return;
+        Result += " ";
+        return true;
       }
       TagsDefinedInIvarDecls.insert(RD);
       Result += " {\n";
       for (RecordDecl::field_iterator i = RD->field_begin(), 
-          e = RD->field_end(); i != e; ++i) {
+           e = RD->field_end(); i != e; ++i) {
         FieldDecl *FD = *i;
         RewriteObjCFieldDecl(FD, Result);
       }
       Result += "\t} "; 
-      Result += Name; Result += ";\n";
-      return;
+      return true;
     }
   }
   else if (Type->isEnumeralType()) {
@@ -3210,8 +3212,8 @@
       Result += ED->getName();
       if (TagsDefinedInIvarDecls.count(ED)) {
         // This enum is already defined. Do not write its definition again.
-        Result += " "; Result += Name; Result += ";\n";
-        return;
+        Result += " ";
+        return true;
       }
       TagsDefinedInIvarDecls.insert(ED);
       
@@ -3224,19 +3226,43 @@
         Result += ",\n";
       }
       Result += "\t} "; 
-      Result += Name; Result += ";\n";
-      return;
+      return true;
     }
   }
   
   Result += "\t";
   convertObjCTypeToCStyleType(Type);
+  return false;
+}
+
+
+/// RewriteObjCFieldDecl - This routine rewrites a field into the buffer.
+/// It handles elaborated types, as well as enum types in the process.
+void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl, 
+                                             std::string &Result) {
+  QualType Type = fieldDecl->getType();
+  std::string Name = fieldDecl->getNameAsString();
   
-  Type.getAsStringInternal(Name, Context->getPrintingPolicy());
+  bool EleboratedType = RewriteObjCFieldDeclType(Type, Result); 
+  if (!EleboratedType)
+    Type.getAsStringInternal(Name, Context->getPrintingPolicy());
   Result += Name;
   if (fieldDecl->isBitField()) {
     Result += " : "; Result += utostr(fieldDecl->getBitWidthValue(*Context));
   }
+  else if (EleboratedType && Type->isArrayType()) {
+    CanQualType CType = Context->getCanonicalType(Type);
+    while (isa<ArrayType>(CType)) {
+      if (const ConstantArrayType *CAT = Context->getAsConstantArrayType(CType)) {
+        Result += "[";
+        llvm::APInt Dim = CAT->getSize();
+        Result += utostr(Dim.getZExtValue());
+        Result += "]";
+      }
+      CType = CType->getAs<ArrayType>()->getElementType();
+    }
+  }
+  
   Result += ";\n";
 }
 

Added: cfe/trunk/test/Rewriter/rewrite-modern-ivars-2.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Rewriter/rewrite-modern-ivars-2.mm?rev=152451&view=auto
==============================================================================
--- cfe/trunk/test/Rewriter/rewrite-modern-ivars-2.mm (added)
+++ cfe/trunk/test/Rewriter/rewrite-modern-ivars-2.mm Fri Mar  9 17:46:23 2012
@@ -0,0 +1,73 @@
+// RUN: %clang_cc1 -x objective-c++ -Wno-return-type -fblocks -fms-extensions -rewrite-objc %s -o %t-rw.cpp
+// RUN: %clang_cc1 -fsyntax-only -fblocks -Wno-address-of-temporary -D"Class=void*" -D"id=void*" -D"SEL=void*" -D"__declspec(X)=" %t-rw.cpp
+
+ at interface B @end
+
+ at interface A {
+  struct s0 {
+    int f0;
+    int f1;
+  } f0;
+  id f1;
+__weak B *f2;
+  int f3 : 5;
+  struct s1 {
+    int *f0;
+    int *f1;
+  } f4[2][1];
+}
+ at end
+
+ at interface C : A
+ at property int p3;
+ at end
+
+ at implementation C
+ at synthesize p3 = _p3;
+ at end
+
+ at interface A()
+ at property int p0;
+ at property (assign) __strong id p1;
+ at property (assign) __weak id p2;
+ at end
+
+// FIXME: Check layout for this class, once it is clear what the right
+// answer is.
+ at implementation A
+ at synthesize p0 = _p0;
+ at synthesize p1 = _p1;
+ at synthesize p2 = _p2;
+ at end
+
+ at interface D : A
+ at property int p3;
+ at end
+
+// FIXME: Check layout for this class, once it is clear what the right
+// answer is.
+ at implementation D
+ at synthesize p3 = _p3;
+ at end
+
+typedef unsigned short UInt16;
+
+
+typedef signed char BOOL;
+typedef unsigned int FSCatalogInfoBitmap;
+
+ at interface NSFileLocationComponent {
+    @private
+
+    id _specifierOrStandardizedPath;
+    BOOL _carbonCatalogInfoAndNameAreValid;
+    FSCatalogInfoBitmap _carbonCatalogInfoMask;
+    id _name;
+    id _containerComponent;
+    id _presentableName;
+    id _iconAsAttributedString;
+}
+ at end
+
+ at implementation NSFileLocationComponent @end
+





More information about the cfe-commits mailing list