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 std::unique_ptr<BugType> BT;
33 void checkPostStmt(
const BlockExpr *BE, CheckerContext &C)
const;
39 if (
const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
40 if (BR->getDecl() == VD)
52 UndefCapturedBlockVarChecker::checkPostStmt(
const BlockExpr *BE,
53 CheckerContext &C)
const {
58 auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
60 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
61 E = R->referenced_vars_end();
66 const VarRegion *VR = I.getCapturedRegion();
67 const VarDecl *VD = VR->getDecl();
74 state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
75 if (ExplodedNode *N = C.generateErrorNode()) {
78 new BuiltinBug(
this,
"uninitialized variable captured by block"));
82 llvm::raw_svector_ostream os(buf);
84 os <<
"Variable '" << VD->
getName()
85 <<
"' is uninitialized when captured by block";
87 auto R = llvm::make_unique<BugReport>(*BT, os.str(), N);
89 R->addRange(Ex->getSourceRange());
90 R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
92 R->disablePathPruning();
94 C.emitReport(std::move(R));
100 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101 mgr.registerChecker<UndefCapturedBlockVarChecker>();
const BlockDecl * getBlockDecl() const
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
static const DeclRefExpr * FindBlockDeclRefExpr(const Stmt *S, const VarDecl *VD)
Stmt - This represents one statement.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
Represents a variable declaration or definition.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
const Stmt * getBody() const
This represents one expression.
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Dataflow Directional Tag Classes.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
A reference to a declared variable, function, enum, etc.