From bc8b44c4ee7f9c4c3ad296369e72feda61bdb580 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 31 Mar 2011 22:32:41 +0000 Subject: [PATCH] -Wuninitialized should not warn about variables captured by blocks as byref. 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 | 17 +++++++++++------ test/Sema/uninit-variables.c | 9 +++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/Analysis/UninitializedValues.cpp b/lib/Analysis/UninitializedValues.cpp index f3cf905af2..58191ec0b0 100644 --- a/lib/Analysis/UninitializedValues.cpp +++ b/lib/Analysis/UninitializedValues.cpp @@ -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() || !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)); diff --git a/test/Sema/uninit-variables.c b/test/Sema/uninit-variables.c index 85e6394eda..17bd07f3e5 100644 --- a/test/Sema/uninit-variables.c +++ b/test/Sema/uninit-variables.c @@ -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 +} + -- 2.40.0