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/AST/Attr.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
28 class UndefCapturedBlockVarChecker
29 : public Checker< check::PostStmt<BlockExpr> > {
30 mutable OwningPtr<BugType> BT;
33 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
35 } // end anonymous namespace
37 static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
39 if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
40 if (BR->getDecl() == VD)
43 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
45 if (const Stmt *child = *I) {
46 const DeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
55 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
56 CheckerContext &C) const {
57 if (!BE->getBlockDecl()->hasCaptures())
60 ProgramStateRef state = C.getState();
61 const BlockDataRegion *R =
62 cast<BlockDataRegion>(state->getSVal(BE,
63 C.getLocationContext()).getAsRegion());
65 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
66 E = R->referenced_vars_end();
69 // This VarRegion is the region associated with the block; we need
70 // the one associated with the encompassing context.
71 const VarRegion *VR = I.getCapturedRegion();
72 const VarDecl *VD = VR->getDecl();
74 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
77 // Get the VarRegion associated with VD in the local stack frame.
78 if (Optional<UndefinedVal> V =
79 state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
80 if (ExplodedNode *N = C.generateSink()) {
82 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
84 // Generate a bug report.
86 llvm::raw_svector_ostream os(buf);
88 os << "Variable '" << VD->getName()
89 << "' is uninitialized when captured by block";
91 BugReport *R = new BugReport(*BT, os.str(), N);
92 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
93 R->addRange(Ex->getSourceRange());
94 R->addVisitor(new FindLastStoreBRVisitor(*V, VR,
95 /*EnableNullFPSuppression*/false));
96 R->disablePathPruning();
97 // need location of block
104 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
105 mgr.registerChecker<UndefCapturedBlockVarChecker>();