From: Fariborz Jahanian Date: Thu, 11 Feb 2010 01:11:34 +0000 (+0000) Subject: Diagnose when user provided getter is being used as lvalue X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e9ff443040cb571ae2c5c2626c4dc9a9a812d84a;p=clang Diagnose when user provided getter is being used as lvalue using property dot-syntax. Fixes radar 7628953. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@95838 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index f192b6f156..4a8b0dbc62 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -149,7 +149,8 @@ public: LV_DuplicateVectorComponents, LV_InvalidExpression, LV_MemberFunction, - LV_SubObjCPropertySetting + LV_SubObjCPropertySetting, + LV_SubObjCPropertyGetterSetting }; isLvalueResult isLvalue(ASTContext &Ctx) const; @@ -179,7 +180,8 @@ public: MLV_ReadonlyProperty, MLV_NoSetterProperty, MLV_MemberFunction, - MLV_SubObjCPropertySetting + MLV_SubObjCPropertySetting, + MLV_SubObjCPropertyGetterSetting }; isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc = 0) const; diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 705ec85529..aacaf4e429 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1869,7 +1869,11 @@ def ext_integer_complement_complex : Extension< def error_nosetter_property_assignment : Error< "setter method is needed to assign to object using property" " assignment syntax">; def error_no_subobject_property_setting : Error< - "cannot assign to a sub-structure of an ivar using property" " assignment syntax">; + "cannot assign to a sub-structure of an ivar using property" + " assignment syntax">; +def error_no_subobject_property_getter_setting : Error< + "cannot assign to a sub-structure returned via a getter using property" + " assignment syntax">; def ext_freestanding_complex : Extension< "complex numbers are an extension in a freestanding C99 implementation">; diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 4e6cdcab00..1af4bf8773 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -1068,8 +1068,11 @@ Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const { if (m->isArrow()) return LV_Valid; Expr *BaseExp = m->getBase(); - return (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) ? - LV_SubObjCPropertySetting : BaseExp->isLvalue(Ctx); + if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) + return LV_SubObjCPropertySetting; + return + (BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass) ? + LV_SubObjCPropertyGetterSetting : BaseExp->isLvalue(Ctx); } // -- If it refers to a static member function [...], then @@ -1092,8 +1095,11 @@ Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const { if (m->isArrow()) return LV_Valid; Expr *BaseExp = m->getBase(); - return (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) ? - LV_SubObjCPropertySetting : BaseExp->isLvalue(Ctx); + if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) + return LV_SubObjCPropertySetting; + return + (BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass) ? + LV_SubObjCPropertyGetterSetting : BaseExp->isLvalue(Ctx); } case UnaryOperatorClass: if (cast(this)->getOpcode() == UnaryOperator::Deref) @@ -1283,7 +1289,9 @@ Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { } return MLV_InvalidExpression; case LV_MemberFunction: return MLV_MemberFunction; - case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; + case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; + case LV_SubObjCPropertyGetterSetting: + return MLV_SubObjCPropertyGetterSetting; } // The following is illegal: diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index c12c7776ee..6f5269a251 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -5734,6 +5734,9 @@ static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { case Expr::MLV_SubObjCPropertySetting: Diag = diag::error_no_subobject_property_setting; break; + case Expr::MLV_SubObjCPropertyGetterSetting: + Diag = diag::error_no_subobject_property_getter_setting; + break; } SourceRange Assign; diff --git a/test/SemaObjC/property-not-lvalue.m b/test/SemaObjC/property-not-lvalue.m index f1bda094c2..473ef8649f 100644 --- a/test/SemaObjC/property-not-lvalue.m +++ b/test/SemaObjC/property-not-lvalue.m @@ -18,3 +18,17 @@ void foo() { f.size.width = 2.2; // expected-error {{cannot assign to a sub-structure of an ivar using property assignment syntax}} f.size.inner.dim = 200; // expected-error {{cannot assign to a sub-structure of an ivar using property assignment syntax}} } + +// radar 7628953 + +@interface Gorf { +} +- (NSSize)size; +@end + +@implementation Gorf +- (void)MyView_sharedInit { + self.size.width = 2.2; // expected-error {{cannot assign to a sub-structure returned via a getter using property assignment syntax}} +} +- (NSSize)size {} +@end