From 1921b58a2aba4f5073d6634d476356b6a4ff8815 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian <fjahanian@apple.com> Date: Mon, 8 Jul 2013 21:42:08 +0000 Subject: [PATCH] [Objective-C migrator] replace candidate user setter/getter with their equivalent property declaration. wip. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185873 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Edit/Commit.h | 9 +++++++++ lib/ARCMigrate/ObjCMT.cpp | 2 +- lib/Edit/Commit.cpp | 2 +- lib/Edit/RewriteObjCFoundationAPI.cpp | 28 ++++++++++++++++++++++++++- test/ARCMT/objcmt-property.m | 20 +++++++++++++++++++ test/ARCMT/objcmt-property.m.result | 20 +++++++++++++++++++ 6 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 test/ARCMT/objcmt-property.m create mode 100644 test/ARCMT/objcmt-property.m.result diff --git a/include/clang/Edit/Commit.h b/include/clang/Edit/Commit.h index 2a296421d31..0ff7034ba00 100644 --- a/include/clang/Edit/Commit.h +++ b/include/clang/Edit/Commit.h @@ -13,6 +13,7 @@ #include "clang/Edit/FileOffset.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/Allocator.h" namespace clang { class LangOptions; @@ -51,6 +52,8 @@ private: bool IsCommitable; SmallVector<Edit, 8> CachedEdits; + + llvm::BumpPtrAllocator StrAlloc; public: explicit Commit(EditedSource &Editor); @@ -131,6 +134,12 @@ private: SourceLocation *MacroBegin = 0) const; bool isAtEndOfMacroExpansion(SourceLocation loc, SourceLocation *MacroEnd = 0) const; + + StringRef copyString(StringRef str) { + char *buf = StrAlloc.Allocate<char>(str.size()); + std::memcpy(buf, str.data(), str.size()); + return StringRef(buf, str.size()); + } }; } diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index ec19f6e8f06..d01dca45a2a 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -212,7 +212,7 @@ void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx, continue; const ParmVarDecl *argDecl = *SetterMethod->param_begin(); QualType ArgType = argDecl->getType(); - if (!Ctx.hasSameType(ArgType, GRT)) + if (!Ctx.hasSameUnqualifiedType(ArgType, GRT)) continue; edit::Commit commit(*Editor); edit::rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit); diff --git a/lib/Edit/Commit.cpp b/lib/Edit/Commit.cpp index 0b4ea3e0cda..9c08cc28ac9 100644 --- a/lib/Edit/Commit.cpp +++ b/lib/Edit/Commit.cpp @@ -183,7 +183,7 @@ void Commit::addInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text, data.Kind = Act_Insert; data.OrigLoc = OrigLoc; data.Offset = Offs; - data.Text = text; + data.Text = copyString(text); data.BeforePrev = beforePreviousInsertions; CachedEdits.push_back(data); } diff --git a/lib/Edit/RewriteObjCFoundationAPI.cpp b/lib/Edit/RewriteObjCFoundationAPI.cpp index 9af53037eac..05abbed14c3 100644 --- a/lib/Edit/RewriteObjCFoundationAPI.cpp +++ b/lib/Edit/RewriteObjCFoundationAPI.cpp @@ -358,7 +358,33 @@ bool edit::rewriteToObjCLiteralSyntax(const ObjCMessageExpr *Msg, bool edit::rewriteToObjCProperty(const ObjCMethodDecl *Getter, const ObjCMethodDecl *Setter, const NSAPI &NS, Commit &commit) { - return false; + std::string PropertyString = "@property"; + const ParmVarDecl *argDecl = *Setter->param_begin(); + QualType ArgType = argDecl->getType(); + Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime(); + if (propertyLifetime != Qualifiers::OCL_None) { + PropertyString += "("; + if (propertyLifetime == Qualifiers::OCL_Strong) + PropertyString += "strong"; + else if (propertyLifetime == Qualifiers::OCL_Weak) + PropertyString += "weak"; + else + PropertyString += "unsafe_unretained"; + PropertyString += ")"; + } + QualType PropQT = Getter->getResultType(); + PropertyString += " "; + PropertyString += PropQT.getAsString(NS.getASTContext().getPrintingPolicy()); + PropertyString += " "; + PropertyString += Getter->getNameAsString(); + commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(), + Getter->getDeclaratorEndLoc()), + PropertyString); + SourceLocation EndLoc = Setter->getDeclaratorEndLoc(); + // Get location past ';' + EndLoc = EndLoc.getLocWithOffset(1); + commit.remove(CharSourceRange::getCharRange(Setter->getLocStart(), EndLoc)); + return true; } /// \brief Returns true if the immediate message arguments of \c Msg should not diff --git a/test/ARCMT/objcmt-property.m b/test/ARCMT/objcmt-property.m new file mode 100644 index 00000000000..f2d5e1a421f --- /dev/null +++ b/test/ARCMT/objcmt-property.m @@ -0,0 +1,20 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -objcmt-migrate-literals -objcmt-migrate-subscripting -mt-migrate-directory %t %s -x objective-c -fobjc-runtime-has-weak -fobjc-arc -fobjc-default-synthesize-properties -triple x86_64-apple-darwin11 +// 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 -fobjc-default-synthesize-properties %s.result + +@class NSString; +@interface NSObject @end + +@interface I : NSObject { + int ivarVal; +} +- (void) setWeakProp : (NSString *__weak)Val; +- (NSString *__weak) WeakProp; + +- (NSString *) StrongProp; +- (void) setStrongProp : (NSString *)Val; +@end + +@implementation I +@end diff --git a/test/ARCMT/objcmt-property.m.result b/test/ARCMT/objcmt-property.m.result new file mode 100644 index 00000000000..87b71e1f4f1 --- /dev/null +++ b/test/ARCMT/objcmt-property.m.result @@ -0,0 +1,20 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -objcmt-migrate-literals -objcmt-migrate-subscripting -mt-migrate-directory %t %s -x objective-c -fobjc-runtime-has-weak -fobjc-arc -fobjc-default-synthesize-properties -triple x86_64-apple-darwin11 +// 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 -fobjc-default-synthesize-properties %s.result + +@class NSString; +@interface NSObject @end + +@interface I : NSObject { + int ivarVal; +} + +@property(weak) NSString *__weak WeakProp; + +@property(strong) NSString * StrongProp; + +@end + +@implementation I +@end -- GitLab