[objcmt] When rewriting to subscripting syntax, make sure we put
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 14 May 2012 23:33:49 +0000 (23:33 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 14 May 2012 23:33:49 +0000 (23:33 +0000)
the receiver in parentheses when necessary.

Part of rdar://11438360

llvm-svn: 156789

clang/lib/Edit/RewriteObjCFoundationAPI.cpp
clang/test/ARCMT/objcmt-subscripting-literals.m
clang/test/ARCMT/objcmt-subscripting-literals.m.result

index 8c3c7a5..218ef22 100644 (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) ||
index 1475951..0371e2e 100644 (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
 
index 5c8a4ec..333d0b4 100644 (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