]> granicus.if.org Git - clang/commitdiff
-Wuninitialized should not warn about variables captured by blocks as byref.
authorTed Kremenek <kremenek@apple.com>
Thu, 31 Mar 2011 22:32:41 +0000 (22:32 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 31 Mar 2011 22:32:41 +0000 (22:32 +0000)
Note this can potentially be enhanced to detect if the __block variable
is actually written by the block, or only when the block "escapes" or
is actually used, but that requires more analysis than it is probably worth
for this simple check.

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

lib/Analysis/UninitializedValues.cpp
test/Sema/uninit-variables.c

index f3cf905af2768eddd0700f77b39a2cd187e4873f..58191ec0b042ead5d8f406258f776f2d2e0c3f90 100644 (file)
@@ -440,13 +440,18 @@ void TransferFunctions::BlockStmt_VisitObjCForCollectionStmt(
 void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
   if (!flagBlockUses || !handler)
     return;
-  AnalysisContext::referenced_decls_iterator i, e;
-  llvm::tie(i, e) = ac.getReferencedBlockVars(be->getBlockDecl());
-  for ( ; i != e; ++i) {
-    const VarDecl *vd = *i;
-    if (vd->getAttr<BlocksAttr>() || !vd->hasLocalStorage() || 
-        !isTrackedVar(vd))
+  const BlockDecl *bd = be->getBlockDecl();
+  for (BlockDecl::capture_const_iterator i = bd->capture_begin(),
+        e = bd->capture_end() ; i != e; ++i) {
+    const VarDecl *vd = i->getVariable();
+    if (!vd->hasLocalStorage())
       continue;
+    if (!isTrackedVar(vd))
+      continue;
+    if (i->isByRef()) {
+      vals[vd] = Initialized;
+      continue;
+    }
     Value v = vals[vd];
     if (isUninitialized(v))
       handler->handleUseOfUninitVariable(be, vd, isAlwaysUninit(v));
index 85e6394edac37fcdf08ee224a52769442bbd96d7..17bd07f3e55640e90c1ec6c3fab7f5f701edb291 100644 (file)
@@ -328,3 +328,12 @@ void test50()
   char c[1 ? : 2]; // no-warning
 }
 
+int test51(void)
+{
+    __block int a;
+    ^(void) {
+      a = 42;
+    }();
+    return a; // no-warning
+}
+