[objcmt] When rewriting to subscripting syntax, make sure we put

the receiver in parentheses when necessary.

Part of rdar://11438360

llvm-svn: 156789
This commit is contained in:
Argyrios Kyrtzidis 2012-05-14 23:33:49 +00:00
parent 16d71bb834
commit 0bbe94f737
3 changed files with 30 additions and 2 deletions

View File

@ -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<BinaryOperator>(Receiver) || isa<UnaryOperator>(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<ArraySubscriptExpr>(Expr) ||
isa<CallExpr>(Expr) ||
isa<DeclRefExpr>(Expr) ||
isa<CXXNamedCastExpr>(Expr) ||
isa<CXXConstructExpr>(Expr) ||
isa<CXXThisExpr>(Expr) ||
isa<CXXTypeidExpr>(Expr) ||
isa<CXXUnresolvedConstructExpr>(Expr) ||
isa<ObjCMessageExpr>(Expr) ||
isa<ObjCPropertyRefExpr>(Expr) ||
isa<ObjCProtocolExpr>(Expr) ||
isa<MemberExpr>(Expr) ||
isa<ParenExpr>(FullExpr) ||
isa<ParenListExpr>(Expr) ||
isa<SizeOfPackExpr>(Expr))
return false;
return true;
}
static bool castOperatorNeedsParens(const Expr *FullExpr) {
const Expr* Expr = FullExpr->IgnoreImpCasts();
if (isa<ArraySubscriptExpr>(Expr) ||

View File

@ -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

View File

@ -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