[PATCH] D17627: Fix false positives for for-loop-analysis warning

Steven Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 1 14:12:31 PST 2016


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
+
+ at interface MyArray
+- (id)objectAtIndexedSubscript:(unsigned int)idx;
+ at 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17627.49549.patch
Type: text/x-patch
Size: 1268 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160301/db2450a1/attachment-0001.bin>


More information about the cfe-commits mailing list