diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2013-03-28 19:50:55 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2013-03-28 19:50:55 +0000 |
commit | ec8deba768e7ba93ad9974763dc3902896924a3c (patch) | |
tree | 7ec97819af944a583050ff48b1e208ae9ebb0e21 /lib/Sema | |
parent | 5fd1fac0104e22533bc0284fdd0f42f843c975f5 (diff) |
Objective-C: Provide fixit suggestions when class object
is accessed via accessing 'isa' ivar to use
object_getClass/object_setClass apis.
// rdar://13503456
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@178282 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema')
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 30 | ||||
-rw-r--r-- | lib/Sema/SemaExprMember.cpp | 1 | ||||
-rw-r--r-- | lib/Sema/TreeTransform.h | 6 |
3 files changed, 31 insertions, 6 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 54422e81eb..238d833c38 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -491,8 +491,18 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) { } CheckForNullPointerDereference(*this, E); - if (isa<ObjCIsaExpr>(E->IgnoreParens())) - Diag(E->getExprLoc(), diag::warn_objc_isa_use); + if (const ObjCIsaExpr *OISA = dyn_cast<ObjCIsaExpr>(E->IgnoreParenCasts())) { + NamedDecl *ObjectGetClass = LookupSingleName(TUScope, + &Context.Idents.get("object_getClass"), + SourceLocation(), LookupOrdinaryName); + if (ObjectGetClass) + Diag(E->getExprLoc(), diag::warn_objc_isa_use) << + FixItHint::CreateInsertion(OISA->getLocStart(), "object_getClass(") << + FixItHint::CreateReplacement( + SourceRange(OISA->getOpLoc(), OISA->getIsaMemberLoc()), ")"); + else + Diag(E->getExprLoc(), diag::warn_objc_isa_use); + } // C++ [conv.lval]p1: // [...] If T is a non-class type, the type of the prvalue is the @@ -8537,8 +8547,20 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, CheckArrayAccess(LHS.get()); CheckArrayAccess(RHS.get()); - if (isa<ObjCIsaExpr>(LHS.get()->IgnoreParens())) - Diag(LHS.get()->getExprLoc(), diag::warn_objc_isa_assign); + if (const ObjCIsaExpr *OISA = dyn_cast<ObjCIsaExpr>(LHS.get()->IgnoreParenCasts())) { + NamedDecl *ObjectSetClass = LookupSingleName(TUScope, + &Context.Idents.get("object_setClass"), + SourceLocation(), LookupOrdinaryName); + if (ObjectSetClass && isa<ObjCIsaExpr>(LHS.get())) { + SourceLocation RHSLocEnd = PP.getLocForEndOfToken(RHS.get()->getLocEnd()); + Diag(LHS.get()->getExprLoc(), diag::warn_objc_isa_assign) << + FixItHint::CreateInsertion(LHS.get()->getLocStart(), "object_setClass(") << + FixItHint::CreateReplacement(SourceRange(OISA->getOpLoc(), OpLoc), ",") << + FixItHint::CreateInsertion(RHSLocEnd, ")"); + } + else + Diag(LHS.get()->getExprLoc(), diag::warn_objc_isa_assign); + } if (CompResultTy.isNull()) return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, diff --git a/lib/Sema/SemaExprMember.cpp b/lib/Sema/SemaExprMember.cpp index e41a2e9145..481c22135e 100644 --- a/lib/Sema/SemaExprMember.cpp +++ b/lib/Sema/SemaExprMember.cpp @@ -1132,6 +1132,7 @@ Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, // apparently. if (OTy->isObjCId() && Member->isStr("isa")) return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, + OpLoc, Context.getObjCClassType())); if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 1e424b6076..e228d7a0f2 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -2389,13 +2389,14 @@ public: /// By default, performs semantic analysis to build the new expression. /// Subclasses may override this routine to provide different behavior. ExprResult RebuildObjCIsaExpr(Expr *BaseArg, SourceLocation IsaLoc, + SourceLocation OpLoc, bool IsArrow) { CXXScopeSpec SS; ExprResult Base = getSema().Owned(BaseArg); LookupResult R(getSema(), &getSema().Context.Idents.get("isa"), IsaLoc, Sema::LookupMemberName); ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow, - /*FIME:*/IsaLoc, + OpLoc, SS, 0, false); if (Result.isInvalid() || Base.isInvalid()) return ExprError(); @@ -2404,7 +2405,7 @@ public: return Result; return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(), - /*FIXME:*/IsaLoc, IsArrow, + OpLoc, IsArrow, SS, SourceLocation(), /*FirstQualifierInScope=*/0, R, @@ -8788,6 +8789,7 @@ TreeTransform<Derived>::TransformObjCIsaExpr(ObjCIsaExpr *E) { return SemaRef.Owned(E); return getDerived().RebuildObjCIsaExpr(Base.get(), E->getIsaMemberLoc(), + E->getOpLoc(), E->isArrow()); } |