steven_wu updated this revision to Diff 49549.
steven_wu added a comment.

I did some more digging of the issue. It seems ObjCSubscriptRefExpr always
rebuilds and hinds both base and key behind OpaqueValueExpr. See 
ObjCSubscriptOpBuilder::rebuildAndCaptureObject so it looks the AST generated
is intentional. If that is the case, Richard's suggestion is the best fix I
can think about. Update the patch to reflect the suggestion.


http://reviews.llvm.org/D17627

Files:
  lib/Sema/SemaStmt.cpp
  test/SemaObjC/warn-loop-analysis.m

Index: test/SemaObjC/warn-loop-analysis.m
===================================================================
--- /dev/null
+++ test/SemaObjC/warn-loop-analysis.m
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -fsyntax-only -Wloop-analysis -verify %s
+// expected-no-diagnostics
+
+@interface MyArray
+- (id)objectAtIndexedSubscript:(unsigned int)idx;
+@end
+
+// Do not warn on objc classes has objectAtIndexedSubscript method.
+MyArray *test;
+void foo()
+{
+  unsigned int i;
+  for (i = 42; i > 0;) // No warnings here
+    (void)test[--i];
+}
Index: lib/Sema/SemaStmt.cpp
===================================================================
--- lib/Sema/SemaStmt.cpp
+++ lib/Sema/SemaStmt.cpp
@@ -1440,6 +1440,18 @@
           FoundDecl = true;
     }
 
+    void VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) {
+      // ObjCSubscriptRefExpr always converts its subexpressions behind OVE.
+      // Look pass OVE in case it has the only reference to Decl.
+      if (auto *OVE = dyn_cast<OpaqueValueExpr>(E->getKeyExpr())) {
+        if (Expr *Key = OVE->getSourceExpr())
+          Visit(Key);
+      } else
+        Visit(E->getKeyExpr());
+
+      Visit(E->getBaseExpr());
+    }
+
     bool FoundDeclInUse() { return FoundDecl; }
 
   };  // end class DeclMatcher


Index: test/SemaObjC/warn-loop-analysis.m
===================================================================
--- /dev/null
+++ test/SemaObjC/warn-loop-analysis.m
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -fsyntax-only -Wloop-analysis -verify %s
+// expected-no-diagnostics
+
+@interface MyArray
+- (id)objectAtIndexedSubscript:(unsigned int)idx;
+@end
+
+// Do not warn on objc classes has objectAtIndexedSubscript method.
+MyArray *test;
+void foo()
+{
+  unsigned int i;
+  for (i = 42; i > 0;) // No warnings here
+    (void)test[--i];
+}
Index: lib/Sema/SemaStmt.cpp
===================================================================
--- lib/Sema/SemaStmt.cpp
+++ lib/Sema/SemaStmt.cpp
@@ -1440,6 +1440,18 @@
           FoundDecl = true;
     }
 
+    void VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) {
+      // ObjCSubscriptRefExpr always converts its subexpressions behind OVE.
+      // Look pass OVE in case it has the only reference to Decl.
+      if (auto *OVE = dyn_cast<OpaqueValueExpr>(E->getKeyExpr())) {
+        if (Expr *Key = OVE->getSourceExpr())
+          Visit(Key);
+      } else
+        Visit(E->getKeyExpr());
+
+      Visit(E->getBaseExpr());
+    }
+
     bool FoundDeclInUse() { return FoundDecl; }
 
   };  // end class DeclMatcher
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to