[cfe-commits] r157193 - in /cfe/trunk: lib/Sema/SemaObjCProperty.cpp test/SemaObjC/iboutlet.m
Fariborz Jahanian
fjahanian at apple.com
Mon May 21 10:02:43 PDT 2012
Author: fjahanian
Date: Mon May 21 12:02:43 2012
New Revision: 157193
URL: http://llvm.org/viewvc/llvm-project?rev=157193&view=rev
Log:
objective-c: When default synthesizing readonly IBOutlet properties
provide a 'fixit' to change 'readonly' to 'readwrite'. // rdar://11448209
Modified:
cfe/trunk/lib/Sema/SemaObjCProperty.cpp
cfe/trunk/test/SemaObjC/iboutlet.m
Modified: cfe/trunk/lib/Sema/SemaObjCProperty.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaObjCProperty.cpp?rev=157193&r1=157192&r2=157193&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaObjCProperty.cpp (original)
+++ cfe/trunk/lib/Sema/SemaObjCProperty.cpp Mon May 21 12:02:43 2012
@@ -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 @@
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<FileID, unsigned> 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 @@
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<ObjCCategoryImplDecl>(ClassImpDecl))) {
Modified: cfe/trunk/test/SemaObjC/iboutlet.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/iboutlet.m?rev=157193&r1=157192&r2=157193&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/iboutlet.m (original)
+++ cfe/trunk/test/SemaObjC/iboutlet.m Mon May 21 12:02:43 2012
@@ -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}}
+
+ at property (readonly) IBOutlet NSView *myView1; // expected-note {{readonly IBOutlet property should be changed to be readwrite}} \
+ // expected-note {{property declared here}}
+
+ at property (getter = MyGetter, READONLY) IBOutlet NSView *myView2; // expected-note {{property declared here}}
+
@end
- at implementation I // expected-warning {{readonly IBOutlet property when auto-synthesized may not work correctly with 'nib' loader}}
+ at implementation I // expected-warning 3 {{readonly IBOutlet property when auto-synthesized may not work correctly with 'nib' loader}}
@end
More information about the cfe-commits
mailing list