From: Argyrios Kyrtzidis Date: Mon, 14 May 2012 23:33:49 +0000 (+0000) Subject: [objcmt] When rewriting to subscripting syntax, make sure we put X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=20119a87fbb7719c161d81fc5f721f1ee6ed7e66;p=clang [objcmt] When rewriting to subscripting syntax, make sure we put the receiver in parentheses when necessary. Part of rdar://11438360 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156789 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Edit/RewriteObjCFoundationAPI.cpp b/lib/Edit/RewriteObjCFoundationAPI.cpp index 8c3c7a5bf3..218ef2274f 100644 --- a/lib/Edit/RewriteObjCFoundationAPI.cpp +++ b/lib/Edit/RewriteObjCFoundationAPI.cpp @@ -77,9 +77,10 @@ bool edit::rewriteObjCRedundantCallWithLiteral(const ObjCMessageExpr *Msg, // rewriteToObjCSubscriptSyntax. //===----------------------------------------------------------------------===// +static bool subscriptOperatorNeedsParens(const Expr *FullExpr); + static void maybePutParensOnReceiver(const Expr *Receiver, Commit &commit) { - Receiver = Receiver->IgnoreImpCasts(); - if (isa(Receiver) || isa(Receiver)) { + if (subscriptOperatorNeedsParens(Receiver)) { SourceRange RecRange = Receiver->getSourceRange(); commit.insertWrap("(", RecRange, ")"); } @@ -600,6 +601,29 @@ static bool rewriteToNumberLiteral(const ObjCMessageExpr *Msg, return true; } +// FIXME: Make determination of operator precedence more general and +// make it broadly available. +static bool subscriptOperatorNeedsParens(const Expr *FullExpr) { + const Expr* Expr = FullExpr->IgnoreImpCasts(); + if (isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(Expr) || + isa(FullExpr) || + isa(Expr) || + isa(Expr)) + return false; + + return true; +} static bool castOperatorNeedsParens(const Expr *FullExpr) { const Expr* Expr = FullExpr->IgnoreImpCasts(); if (isa(Expr) || diff --git a/test/ARCMT/objcmt-subscripting-literals.m b/test/ARCMT/objcmt-subscripting-literals.m index 147595182d..0371e2e2ca 100644 --- a/test/ARCMT/objcmt-subscripting-literals.m +++ b/test/ARCMT/objcmt-subscripting-literals.m @@ -136,6 +136,8 @@ typedef const struct __CFString * CFStringRef; [mdict setObject:[dict objectForKey:@"key1"] forKey:[dict objectForKey:[NSArray arrayWithObject:@"arrkey"]]]; __strong NSArray **parr = 0; o = [*parr objectAtIndex:2]; + void *hd; + o = [(NSArray*)hd objectAtIndex:2]; } @end diff --git a/test/ARCMT/objcmt-subscripting-literals.m.result b/test/ARCMT/objcmt-subscripting-literals.m.result index 5c8a4ecc40..333d0b4c38 100644 --- a/test/ARCMT/objcmt-subscripting-literals.m.result +++ b/test/ARCMT/objcmt-subscripting-literals.m.result @@ -136,6 +136,8 @@ typedef const struct __CFString * CFStringRef; mdict[dict[@[@"arrkey"]]] = dict[@"key1"]; __strong NSArray **parr = 0; o = (*parr)[2]; + void *hd; + o = ((NSArray*)hd)[2]; } @end