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)
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());
89 R->addVisitor(std::make_unique<FindLastStoreBRVisitor>(
91 bugreporter::TrackingKind::Thorough));
92 R->disablePathPruning();
94 C.emitReport(std::move(R));
100 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101 mgr.registerChecker<UndefCapturedBlockVarChecker>();
104 bool ento::shouldRegisterUndefCapturedBlockVarChecker(
const LangOptions &LO) {
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.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
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.