]> granicus.if.org Git - clang/commitdiff
Fix false positives for for-loop-analysis warning
authorSteven Wu <stevenwu@apple.com>
Thu, 10 Mar 2016 02:02:48 +0000 (02:02 +0000)
committerSteven Wu <stevenwu@apple.com>
Thu, 10 Mar 2016 02:02:48 +0000 (02:02 +0000)
Summary:
For PseudoObjectExpr, the DeclMatcher need to search only all the semantics
but also need to search pass OpaqueValueExpr for all potential uses for the
Decl.

Reviewers: thakis, rtrieu, rjmccall, doug.gregor

Subscribers: xazax.hun, rjmccall, doug.gregor, cfe-commits

Differential Revision: http://reviews.llvm.org/D17627

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@263087 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaStmt.cpp
test/SemaObjC/warn-loop-analysis.m [new file with mode: 0644]

index b73b4f088414ad452fc24eb3b9ab28a7e88055a1..1975fcb11027e7505c56fd388511fd8aec4d1be6 100644 (file)
@@ -1440,6 +1440,18 @@ namespace {
           FoundDecl = true;
     }
 
+    void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
+      // Only need to visit the semantics for POE.
+      // SyntaticForm doesn't really use the Decal.
+      for (auto *S : POE->semantics()) {
+        if (auto *OVE = dyn_cast<OpaqueValueExpr>(S))
+          // Look past the OVE into the expression it binds.
+          Visit(OVE->getSourceExpr());
+        else
+          Visit(S);
+      }
+    }
+
     bool FoundDeclInUse() { return FoundDecl; }
 
   };  // end class DeclMatcher
diff --git a/test/SemaObjC/warn-loop-analysis.m b/test/SemaObjC/warn-loop-analysis.m
new file mode 100644 (file)
index 0000000..8ae7375
--- /dev/null
@@ -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];
+}