From b52d8d2e5f8b0ee514eced41079ac11ecaf221f8 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Mon, 21 May 2012 17:02:43 +0000 Subject: [PATCH] objective-c: When default synthesizing readonly IBOutlet properties provide a 'fixit' to change 'readonly' to 'readwrite'. // rdar://11448209 llvm-svn: 157193 --- clang/lib/Sema/SemaObjCProperty.cpp | 96 +++++++++++++---------------- clang/test/SemaObjC/iboutlet.m | 10 ++- 2 files changed, 51 insertions(+), 55 deletions(-) diff --git a/clang/lib/Sema/SemaObjCProperty.cpp b/clang/lib/Sema/SemaObjCProperty.cpp index 44e50852a55c..d944380ef85a 100644 --- a/clang/lib/Sema/SemaObjCProperty.cpp +++ b/clang/lib/Sema/SemaObjCProperty.cpp @@ -18,6 +18,8 @@ #include "clang/AST/ExprObjC.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/ASTMutationListener.h" +#include "clang/Lex/Lexer.h" +#include "clang/Basic/SourceManager.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/SmallString.h" @@ -200,51 +202,36 @@ makePropertyAttributesAsWritten(unsigned Attributes) { return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten; } -static std::string getPropertyAttributeString(const ObjCPropertyDecl *property, - unsigned Attributes) { - std::string attr; - if (!Attributes) - return attr; - attr = "("; - bool first = true; - if (Attributes & ObjCPropertyDecl::OBJC_PR_readonly) - {attr += !first ? ", readonly" : "readonly"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_readwrite) - {attr += !first ? ", readwrite" : "readwrite"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_getter) - { - if (!first) - attr += ", "; - attr += "getter="; - attr += property->getGetterName().getAsString(); - first = false; - } - if (Attributes & ObjCPropertyDecl::OBJC_PR_setter) - { - if (!first) - attr += ", "; - attr += "setter="; - attr += property->getSetterName().getAsString(); - first = false; - } - if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) - {attr += !first ? ", assign" : "assign"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_retain) - {attr += !first ? ", retain" : "retain"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_strong) - {attr += !first ? ", strong" : "strong"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) - {attr += !first ? ", weak" : "weak"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) - {attr += !first ? ", copy" : "copy"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) - {attr += !first ? ", unsafe_unretained" : "unsafe_unretained"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) - {attr += !first ? ", nonatomic" : "nonatomic"; first = false; } - if (Attributes & ObjCPropertyDecl::OBJC_PR_atomic) - {attr += !first ? ", atomic" : "atomic"; first = false; } - attr += ")"; - return attr; +static bool LocPropertyAttribute(const Sema &sema, + ASTContext &Context, const char *attrName, + SourceLocation LParenLoc, SourceLocation &Loc) { + if (LParenLoc.isMacroID()) + return false; + + SourceManager &SM = Context.getSourceManager(); + std::pair locInfo = SM.getDecomposedLoc(LParenLoc); + // Try to load the file buffer. + bool invalidTemp = false; + StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); + if (invalidTemp) + return false; + const char *tokenBegin = file.data() + locInfo.second; + + // Lex from the start of the given location. + Lexer lexer(SM.getLocForStartOfFile(locInfo.first), + Context.getLangOpts(), + file.begin(), tokenBegin, file.end()); + Token Tok; + do { + lexer.LexFromRawLexer(Tok); + if (Tok.is(tok::raw_identifier) && + StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) { + Loc = Tok.getLocation(); + return true; + } + } while (Tok.isNot(tok::r_paren)); + return false; + } Decl * @@ -684,15 +671,16 @@ Decl *Sema::ActOnPropertyImplDecl(Scope *S, rwPIKind &= (~ObjCPropertyDecl::OBJC_PR_readonly); Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property); Diag(property->getLocation(), diag::note_property_declare); - // FIXME. End location must be that of closing ')' which is currently - // unavailable. Need to add it. - SourceLocation endLoc = - property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); - SourceRange PropSourceRange(property->getLParenLoc(), endLoc); - Diag(property->getLocation(), - diag::note_auto_readonly_iboutlet_fixup_suggest) << - FixItHint::CreateReplacement(PropSourceRange, getPropertyAttributeString(property, - rwPIKind)); + SourceLocation readonlyLoc; + if (LocPropertyAttribute(*this, Context, "readonly", + property->getLParenLoc(), readonlyLoc)) { + SourceLocation endLoc = + readonlyLoc.getLocWithOffset(strlen("readonly")-1); + SourceRange ReadonlySourceRange(readonlyLoc, endLoc); + Diag(property->getLocation(), + diag::note_auto_readonly_iboutlet_fixup_suggest) << + FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite"); + } } } else if ((CatImplClass = dyn_cast(ClassImpDecl))) { diff --git a/clang/test/SemaObjC/iboutlet.m b/clang/test/SemaObjC/iboutlet.m index 13e5d53bb020..c9f5d8cf2a6a 100644 --- a/clang/test/SemaObjC/iboutlet.m +++ b/clang/test/SemaObjC/iboutlet.m @@ -2,6 +2,8 @@ // RUN: %clang_cc1 -x objective-c++ -fsyntax-only -fobjc-default-synthesize-properties -verify %s // rdar://11448209 +#define READONLY readonly + @class NSView; #define IBOutlet __attribute__((iboutlet)) @@ -9,7 +11,13 @@ @interface I @property (getter = MyGetter, readonly, assign) IBOutlet NSView *myView; // expected-note {{property declared here}} \ // expected-note {{readonly IBOutlet property should be changed to be readwrite}} + +@property (readonly) IBOutlet NSView *myView1; // expected-note {{readonly IBOutlet property should be changed to be readwrite}} \ + // expected-note {{property declared here}} + +@property (getter = MyGetter, READONLY) IBOutlet NSView *myView2; // expected-note {{property declared here}} + @end -@implementation I // expected-warning {{readonly IBOutlet property when auto-synthesized may not work correctly with 'nib' loader}} +@implementation I // expected-warning 3 {{readonly IBOutlet property when auto-synthesized may not work correctly with 'nib' loader}} @end