1 // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This checker detects blocks that capture uninitialized values.
12 //===----------------------------------------------------------------------===//
14 #include "ClangSACheckers.h"
15 #include "clang/StaticAnalyzer/Core/Checker.h"
16 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
26 class UndefCapturedBlockVarChecker
27 : public Checker< check::PostStmt<BlockExpr> > {
28 mutable llvm::OwningPtr<BugType> BT;
31 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
33 } // end anonymous namespace
35 static const BlockDeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
37 if (const BlockDeclRefExpr *BR = dyn_cast<BlockDeclRefExpr>(S))
38 if (BR->getDecl() == VD)
41 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
43 if (const Stmt *child = *I) {
44 const BlockDeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
53 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
54 CheckerContext &C) const {
55 if (!BE->getBlockDecl()->hasCaptures())
58 const GRState *state = C.getState();
59 const BlockDataRegion *R =
60 cast<BlockDataRegion>(state->getSVal(BE).getAsRegion());
62 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
63 E = R->referenced_vars_end();
66 // This VarRegion is the region associated with the block; we need
67 // the one associated with the encompassing context.
68 const VarRegion *VR = *I;
69 const VarDecl *VD = VR->getDecl();
71 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
74 // Get the VarRegion associated with VD in the local stack frame.
75 const LocationContext *LC = C.getPredecessor()->getLocationContext();
76 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
78 if (state->getSVal(VR).isUndef())
79 if (ExplodedNode *N = C.generateSink()) {
81 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
83 // Generate a bug report.
84 llvm::SmallString<128> buf;
85 llvm::raw_svector_ostream os(buf);
87 os << "Variable '" << VD->getName()
88 << "' is uninitialized when captured by block";
90 EnhancedBugReport *R = new EnhancedBugReport(*BT, os.str(), N);
91 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
92 R->addRange(Ex->getSourceRange());
93 R->addVisitorCreator(bugreporter::registerFindLastStore, VR);
94 // need location of block
100 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101 mgr.registerChecker<UndefCapturedBlockVarChecker>();