20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace clang;
27 class UndefCapturedBlockVarChecker
28 :
public Checker< check::PostStmt<BlockExpr> > {
29 mutable std::unique_ptr<BugType> BT;
32 void checkPostStmt(
const BlockExpr *BE, CheckerContext &C)
const;
38 if (
const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
39 if (BR->getDecl() == VD)
42 for (
const Stmt *Child : S->children())
51 UndefCapturedBlockVarChecker::checkPostStmt(
const BlockExpr *BE,
52 CheckerContext &C)
const {
57 auto *R = cast<BlockDataRegion>(
C.getSVal(BE).getAsRegion());
59 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
60 E = R->referenced_vars_end();
65 const VarRegion *VR = I.getCapturedRegion();
66 const VarDecl *VD = VR->getDecl();
73 state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
74 if (ExplodedNode *N =
C.generateErrorNode()) {
77 new BuiltinBug(
this,
"uninitialized variable captured by block"));
81 llvm::raw_svector_ostream os(buf);
83 os <<
"Variable '" << VD->
getName()
84 <<
"' is uninitialized when captured by block";
86 auto R = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
88 R->addRange(Ex->getSourceRange());
92 R->disablePathPruning();
94 C.emitReport(std::move(R));
100 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101 mgr.registerChecker<UndefCapturedBlockVarChecker>();
104 bool ento::shouldRegisterUndefCapturedBlockVarChecker(
const CheckerManager &mgr) {