r219173 - Objective-C SDK modernizer. Patch to support modernization

Fariborz Jahanian fjahanian at apple.com
Mon Oct 6 16:50:37 PDT 2014


Author: fjahanian
Date: Mon Oct  6 18:50:37 2014
New Revision: 219173

URL: http://llvm.org/viewvc/llvm-project?rev=219173&view=rev
Log:
Objective-C SDK modernizer. Patch to support modernization
to NS_ENUM/NS_OPTION macros when typedef names are other
than NSInteger/NSUInteger (int8_t, etc.).
rdar://18532199

Modified:
    cfe/trunk/include/clang/AST/NSAPI.h
    cfe/trunk/lib/ARCMigrate/ObjCMT.cpp
    cfe/trunk/lib/AST/NSAPI.cpp
    cfe/trunk/test/ARCMT/objcmt-ns-macros.m
    cfe/trunk/test/ARCMT/objcmt-ns-macros.m.result

Modified: cfe/trunk/include/clang/AST/NSAPI.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/NSAPI.h?rev=219173&r1=219172&r2=219173&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/NSAPI.h (original)
+++ cfe/trunk/include/clang/AST/NSAPI.h Mon Oct  6 18:50:37 2014
@@ -104,7 +104,7 @@ public:
     NSDict_initWithObjectsForKeysCount
   };
   static const unsigned NumNSDictionaryMethods = 12;
-
+  
   /// \brief The Objective-C NSDictionary selectors.
   Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const;
 
@@ -187,6 +187,9 @@ public:
   bool isObjCNSIntegerType(QualType T) const;
   /// \brief Returns true if \param T is a typedef of "NSUInteger" in objective-c.
   bool isObjCNSUIntegerType(QualType T) const;
+  /// \brief Returns one of NSIntegral typedef names if \param T is a typedef
+  /// of that name in objective-c.
+  StringRef GetNSIntegralKind(QualType T) const;
 
 private:
   bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const;

Modified: cfe/trunk/lib/ARCMigrate/ObjCMT.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/ObjCMT.cpp?rev=219173&r1=219172&r2=219173&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/ObjCMT.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/ObjCMT.cpp Mon Oct  6 18:50:37 2014
@@ -584,18 +584,32 @@ static bool rewriteToObjCInterfaceDecl(c
   return true;
 }
 
+static StringRef GetUnsignedName(StringRef NSIntegerName) {
+  StringRef UnsignedName = llvm::StringSwitch<StringRef>(NSIntegerName)
+    .Case("int8_t", "uint8_t")
+    .Case("int16_t", "uint16_t")
+    .Case("int32_t", "uint32_t")
+    .Case("NSInteger", "NSUInteger")
+    .Case("int64_t", "uint64_t")
+    .Default(NSIntegerName);
+  return UnsignedName;
+}
+
 static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl,
                                 const TypedefDecl *TypedefDcl,
                                 const NSAPI &NS, edit::Commit &commit,
-                                bool IsNSIntegerType,
+                                StringRef NSIntegerName,
                                 bool NSOptions) {
   std::string ClassString;
-  if (NSOptions)
-    ClassString = "typedef NS_OPTIONS(NSUInteger, ";
-  else
-    ClassString =
-      IsNSIntegerType ? "typedef NS_ENUM(NSInteger, "
-                      : "typedef NS_ENUM(NSUInteger, ";
+  if (NSOptions) {
+    ClassString = "typedef NS_OPTIONS(";
+    ClassString += GetUnsignedName(NSIntegerName);
+  }
+  else {
+    ClassString = "typedef NS_ENUM(";
+    ClassString += NSIntegerName;
+  }
+  ClassString += ", ";
   
   ClassString += TypedefDcl->getIdentifier()->getName();
   ClassString += ')';
@@ -788,10 +802,9 @@ bool ObjCMigrateASTConsumer::migrateNSEn
     return false;
   
   QualType qt = TypedefDcl->getTypeSourceInfo()->getType();
-  bool IsNSIntegerType = NSAPIObj->isObjCNSIntegerType(qt);
-  bool IsNSUIntegerType = !IsNSIntegerType && NSAPIObj->isObjCNSUIntegerType(qt);
+  StringRef NSIntegerName = NSAPIObj->GetNSIntegralKind(qt);
   
-  if (!IsNSIntegerType && !IsNSUIntegerType) {
+  if (NSIntegerName.empty()) {
     // Also check for typedef enum {...} TD;
     if (const EnumType *EnumTy = qt->getAs<EnumType>()) {
       if (EnumTy->getDecl() == EnumDcl) {
@@ -813,15 +826,12 @@ bool ObjCMigrateASTConsumer::migrateNSEn
   
   // We may still use NS_OPTIONS based on what we find in the enumertor list.
   bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl);
-  // NS_ENUM must be available.
-  if (IsNSIntegerType && !Ctx.Idents.get("NS_ENUM").hasMacroDefinition())
-    return false;
-  // NS_OPTIONS must be available.
-  if (IsNSUIntegerType && !Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition())
+  // For sanity check, see if macro NS_ENUM can be seen.
+  if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition())
     return false;
   edit::Commit commit(*Editor);
   bool Res = rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj,
-                                 commit, IsNSIntegerType, NSOptions);
+                                 commit, NSIntegerName, NSOptions);
   Editor->commit(commit);
   return Res;
 }

Modified: cfe/trunk/lib/AST/NSAPI.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/NSAPI.cpp?rev=219173&r1=219172&r2=219173&view=diff
==============================================================================
--- cfe/trunk/lib/AST/NSAPI.cpp (original)
+++ cfe/trunk/lib/AST/NSAPI.cpp Mon Oct  6 18:50:37 2014
@@ -10,6 +10,7 @@
 #include "clang/AST/NSAPI.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Expr.h"
+#include "llvm/ADT/StringSwitch.h"
 
 using namespace clang;
 
@@ -400,6 +401,32 @@ bool NSAPI::isObjCNSUIntegerType(QualTyp
   return isObjCTypedef(T, "NSUInteger", NSUIntegerId);
 }
 
+StringRef NSAPI::GetNSIntegralKind(QualType T) const {
+  if (!Ctx.getLangOpts().ObjC1 || T.isNull())
+    return StringRef();
+  
+  while (const TypedefType *TDT = T->getAs<TypedefType>()) {
+    StringRef NSIntegralResust =
+      llvm::StringSwitch<StringRef>(
+        TDT->getDecl()->getDeclName().getAsIdentifierInfo()->getName())
+    .Case("int8_t", "int8_t")
+    .Case("int16_t", "int16_t")
+    .Case("int32_t", "int32_t")
+    .Case("NSInteger", "NSInteger")
+    .Case("int64_t", "int64_t")
+    .Case("uint8_t", "uint8_t")
+    .Case("uint16_t", "uint16_t")
+    .Case("uint32_t", "uint32_t")
+    .Case("NSUInteger", "NSUInteger")
+    .Case("uint64_t", "uint64_t")
+    .Default(StringRef());
+    if (!NSIntegralResust.empty())
+      return NSIntegralResust;
+    T = TDT->desugar();
+  }
+  return StringRef();
+}
+
 bool NSAPI::isObjCTypedef(QualType T,
                           StringRef name, IdentifierInfo *&II) const {
   if (!Ctx.getLangOpts().ObjC1)

Modified: cfe/trunk/test/ARCMT/objcmt-ns-macros.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-ns-macros.m?rev=219173&r1=219172&r2=219173&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-ns-macros.m (original)
+++ cfe/trunk/test/ARCMT/objcmt-ns-macros.m Mon Oct  6 18:50:37 2014
@@ -3,8 +3,17 @@
 // 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 -fobjc-runtime-has-weak -fobjc-arc %s.result
 
+typedef signed char int8_t;
+typedef short int16_t;
+typedef int int32_t;
 typedef long NSInteger;
+typedef long long int64_t;
+
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
 typedef unsigned long NSUInteger;
+typedef unsigned long long uint64_t;
 
 #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
 #define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
@@ -322,3 +331,35 @@ typedef enum {
 
   NumericEnum4;
 
+// rdar://18532199
+enum
+{
+  UI8one = 1
+};
+typedef int8_t MyEnumeratedType;
+
+
+enum {
+  UI16One = 0,
+  UI16Two = 0x1,
+  UI16three = 0x8,
+  UI16Four = 0x100
+};
+typedef int16_t UI16;
+
+enum {
+    UI32ViewAutoresizingNone                 = 0,
+    UI32ViewAutoresizingFlexibleLeftMargin,
+    UI32ViewAutoresizingFlexibleWidth,
+    UI32ViewAutoresizingFlexibleRightMargin,
+    UI32ViewAutoresizingFlexibleTopMargin,
+    UI32ViewAutoresizingFlexibleHeight,
+    UI32ViewAutoresizingFlexibleBottomMargin
+};
+typedef uint32_t UI32TableViewCellStyle;
+
+enum
+{
+        UIU8one = 1
+};
+typedef uint8_t UI8Type;

Modified: cfe/trunk/test/ARCMT/objcmt-ns-macros.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-ns-macros.m.result?rev=219173&r1=219172&r2=219173&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-ns-macros.m.result (original)
+++ cfe/trunk/test/ARCMT/objcmt-ns-macros.m.result Mon Oct  6 18:50:37 2014
@@ -3,8 +3,17 @@
 // 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 -fobjc-runtime-has-weak -fobjc-arc %s.result
 
+typedef signed char int8_t;
+typedef short int16_t;
+typedef int int32_t;
 typedef long NSInteger;
+typedef long long int64_t;
+
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
 typedef unsigned long NSUInteger;
+typedef unsigned long long uint64_t;
 
 #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
 #define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
@@ -302,3 +311,31 @@ typedef NS_OPTIONS(NSUInteger, NumericEn
     Four = 4
 };
 
+// rdar://18532199
+typedef NS_ENUM(int8_t, MyEnumeratedType)
+{
+  UI8one = 1
+};
+
+
+typedef NS_OPTIONS(uint16_t, UI16) {
+  UI16One = 0,
+  UI16Two = 0x1,
+  UI16three = 0x8,
+  UI16Four = 0x100
+};
+
+typedef NS_ENUM(uint32_t, UI32TableViewCellStyle) {
+    UI32ViewAutoresizingNone                 = 0,
+    UI32ViewAutoresizingFlexibleLeftMargin,
+    UI32ViewAutoresizingFlexibleWidth,
+    UI32ViewAutoresizingFlexibleRightMargin,
+    UI32ViewAutoresizingFlexibleTopMargin,
+    UI32ViewAutoresizingFlexibleHeight,
+    UI32ViewAutoresizingFlexibleBottomMargin
+};
+
+typedef NS_ENUM(uint8_t, UI8Type)
+{
+        UIU8one = 1
+};





More information about the cfe-commits mailing list