From a81f102bd396fd1da61b47337a2389f352baf539 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 11 Sep 2013 17:05:15 +0000 Subject: [PATCH] ObjectiveC migrator. methods which look like a getter and start with "get" are inferreed as a readonly properties. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190532 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/ARCMigrate/ObjCMT.cpp | 26 ++++++++++++++++---------- test/ARCMT/objcmt-property.m | 8 ++++++++ test/ARCMT/objcmt-property.m.result | 8 ++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index efe2e858c4..f550e7cad0 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -249,12 +249,12 @@ static void append_attr(std::string &PropertyString, const char *attr) { static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter, const ObjCMethodDecl *Setter, const NSAPI &NS, edit::Commit &commit, - bool GetterHasIsPrefix) { + unsigned LengthOfPrefix) { ASTContext &Context = NS.getASTContext(); std::string PropertyString = "@property(nonatomic"; std::string PropertyNameString = Getter->getNameAsString(); StringRef PropertyName(PropertyNameString); - if (GetterHasIsPrefix) { + if (LengthOfPrefix > 0) { PropertyString += ", getter="; PropertyString += PropertyNameString; } @@ -305,11 +305,11 @@ static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter, PropertyString += " "; PropertyString += RT.getAsString(Context.getPrintingPolicy()); PropertyString += " "; - if (GetterHasIsPrefix) { + if (LengthOfPrefix > 0) { // property name must strip off "is" and lower case the first character // after that; e.g. isContinuous will become continuous. StringRef PropertyNameStringRef(PropertyNameString); - PropertyNameStringRef = PropertyNameStringRef.drop_front(2); + PropertyNameStringRef = PropertyNameStringRef.drop_front(LengthOfPrefix); PropertyNameString = PropertyNameStringRef; std::string NewPropertyNameString = PropertyNameString; NewPropertyNameString[0] = toLowercase(NewPropertyNameString[0]); @@ -732,13 +732,19 @@ bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx, PP.getSelectorTable(), getterName); ObjCMethodDecl *SetterMethod = D->lookupMethod(SetterSelector, true); - bool GetterHasIsPrefix = false; + unsigned LengthOfPrefix = 0; if (!SetterMethod) { // try a different naming convention for getter: isXxxxx StringRef getterNameString = getterName->getName(); - if (getterNameString.startswith("is") && !GRT->isObjCRetainableType()) { - GetterHasIsPrefix = true; - const char *CGetterName = getterNameString.data() + 2; + bool IsPrefix = getterNameString.startswith("is"); + if ((IsPrefix && !GRT->isObjCRetainableType()) || + getterNameString.startswith("get")) { + LengthOfPrefix = (IsPrefix ? 2 : 3); + const char *CGetterName = getterNameString.data() + LengthOfPrefix; + // Make sure that first character after "is" or "get" prefix can + // start an identifier. + if (!isIdentifierHead(CGetterName[0])) + return false; if (CGetterName[0] && isUppercase(CGetterName[0])) { getterName = &Ctx.Idents.get(CGetterName); SetterSelector = @@ -761,7 +767,7 @@ bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx, return false; edit::Commit commit(*Editor); rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit, - GetterHasIsPrefix); + LengthOfPrefix); Editor->commit(commit); return true; } @@ -770,7 +776,7 @@ bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx, // as a 'readonly' property. edit::Commit commit(*Editor); rewriteToObjCProperty(Method, 0 /*SetterMethod*/, *NSAPIObj, commit, - GetterHasIsPrefix); + LengthOfPrefix); Editor->commit(commit); return true; } diff --git a/test/ARCMT/objcmt-property.m b/test/ARCMT/objcmt-property.m index 8f9c5f7fc5..924679841f 100644 --- a/test/ARCMT/objcmt-property.m +++ b/test/ARCMT/objcmt-property.m @@ -98,6 +98,14 @@ typedef char BOOL; + (double) D; - (void *)JSObject WEBKIT_OBJC_METHOD_ANNOTATION(AVAILABLE_WEBKIT_VERSION_3_0_AND_LATER); - (BOOL)isIgnoringInteractionEvents; + +- (NSString *)getStringValue; +- (BOOL)getCounterValue; +- (void)setStringValue:(NSString *)stringValue AVAILABLE_WEBKIT_VERSION_3_0_AND_LATER; +- (NSDictionary *)getns_dixtionary; + +- (BOOL)is3bar; // watch out +- (NSString *)get3foo; // watch out @end diff --git a/test/ARCMT/objcmt-property.m.result b/test/ARCMT/objcmt-property.m.result index a2cecd5dcf..f1caec3137 100644 --- a/test/ARCMT/objcmt-property.m.result +++ b/test/ARCMT/objcmt-property.m.result @@ -98,6 +98,14 @@ typedef char BOOL; + (double) D; @property(nonatomic, readonly) void * JSObject WEBKIT_OBJC_METHOD_ANNOTATION(AVAILABLE_WEBKIT_VERSION_3_0_AND_LATER); @property(nonatomic, getter=isIgnoringInteractionEvents, readonly) BOOL ignoringInteractionEvents; + +@property(nonatomic, getter=getStringValue, retain) NSString * stringValue; +@property(nonatomic, getter=getCounterValue, readonly) BOOL counterValue; + +@property(nonatomic, getter=getns_dixtionary, readonly) NSDictionary * ns_dixtionary; + +- (BOOL)is3bar; // watch out +- (NSString *)get3foo; // watch out @end -- 2.50.1