[cfe-commits] r124347 - in /cfe/trunk: lib/Analysis/UninitializedValuesV2.cpp test/SemaObjC/uninit-variables.m

Ted Kremenek kremenek at apple.com
Wed Jan 26 18:01:31 PST 2011


Author: kremenek
Date: Wed Jan 26 20:01:31 2011
New Revision: 124347

URL: http://llvm.org/viewvc/llvm-project?rev=124347&view=rev
Log:
Teach -Wuninitialized about ObjC fast enumeration loops.

Added:
    cfe/trunk/test/SemaObjC/uninit-variables.m
Modified:
    cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp

Modified: cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp?rev=124347&r1=124346&r2=124347&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp (original)
+++ cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp Wed Jan 26 20:01:31 2011
@@ -311,6 +311,7 @@
   void VisitBinaryOperator(BinaryOperator *bo);
   void VisitCastExpr(CastExpr *ce);
   void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se);
+  void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs);
 };
 }
 
@@ -319,6 +320,43 @@
   if (handler) handler->handleUseOfUninitVariable(ex, vd);
 }
 
+static FindVarResult findBlockVarDecl(Expr* ex) {
+  if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
+    if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
+      if (isTrackedVar(vd))
+        return FindVarResult(vd, dr);
+  
+  return FindVarResult(0, 0);
+}
+
+void TransferFunctions::BlockStmt_VisitObjCForCollectionStmt(
+    ObjCForCollectionStmt *fs) {
+  
+  Visit(fs->getCollection());
+  
+  // This represents an initialization of the 'element' value.
+  Stmt *element = fs->getElement();
+  const VarDecl* vd = 0;
+  
+  if (DeclStmt* ds = dyn_cast<DeclStmt>(element)) {
+    vd = cast<VarDecl>(ds->getSingleDecl());
+    if (!isTrackedVar(vd))
+      vd = 0;
+  }
+  else {
+    // Initialize the value of the reference variable.
+    const FindVarResult &res = findBlockVarDecl(cast<Expr>(element));
+    vd = res.getDecl();
+    if (!vd) {
+      Visit(element);
+      return;
+    }
+  }
+  
+  if (vd)
+    vals[vd] = Initialized;
+}
+
 void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
   if (!flagBlockUses || !handler)
     return;
@@ -366,15 +404,6 @@
         vals[vd] = Initialized;
 }
 
-static FindVarResult findBlockVarDecl(Expr* ex) {
-  if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
-    if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
-      if (isTrackedVar(vd))
-        return FindVarResult(vd, dr);
-
-  return FindVarResult(0, 0);
-}
-
 void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) {
   if (bo->isAssignmentOp()) {
     const FindVarResult &res = findBlockVarDecl(bo->getLHS());

Added: cfe/trunk/test/SemaObjC/uninit-variables.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/uninit-variables.m?rev=124347&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/uninit-variables.m (added)
+++ cfe/trunk/test/SemaObjC/uninit-variables.m Wed Jan 26 20:01:31 2011
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -fsyntax-only -Wuninitialized -fsyntax-only -fblocks %s -verify
+
+// Duplicated from uninit-variables.c.
+// Test just to ensure the analysis is working.
+int test1() {
+  int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
+  return x; // expected-note{{variable 'x' is possibly uninitialized when used here}}
+}
+
+// Test ObjC fast enumeration.
+void test2() {
+  id collection = 0;
+  for (id obj in collection) {
+    if (0 == obj) // no-warning
+      break;
+  }
+}
+
+void test3() {
+  id collection = 0;
+  id obj;
+  for (obj in collection) { // no-warning
+    if (0 == obj) // no-warning
+      break;
+  }
+}
+





More information about the cfe-commits mailing list