20#include "llvm/ADT/SmallString.h"
21#include "llvm/Support/raw_ostream.h"
28class UndefCapturedBlockVarChecker
29 :
public Checker< check::PostStmt<BlockExpr> > {
30 const BugType BT{
this,
"uninitialized variable captured by block"};
39 if (
const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
40 if (BR->getDecl() == VD)
43 for (
const Stmt *Child : S->children())
52UndefCapturedBlockVarChecker::checkPostStmt(
const BlockExpr *BE,
58 auto *R = cast<BlockDataRegion>(
C.getSVal(BE).getAsRegion());
60 for (
auto Var : R->referenced_vars()) {
63 const VarRegion *VR = Var.getCapturedRegion();
70 if (std::optional<UndefinedVal>
V =
71 state->getSVal(Var.getOriginalRegion()).getAs<
UndefinedVal>()) {
75 llvm::raw_svector_ostream os(buf);
77 os <<
"Variable '" << VD->
getName()
78 <<
"' is uninitialized when captured by block";
80 auto R = std::make_unique<PathSensitiveBugReport>(BT, os.str(), N);
82 R->addRange(Ex->getSourceRange());
84 {bugreporter::TrackingKind::Thorough,
86 R->disablePathPruning();
88 C.emitReport(std::move(R));
94void ento::registerUndefCapturedBlockVarChecker(
CheckerManager &mgr) {
98bool ento::shouldRegisterUndefCapturedBlockVarChecker(
const CheckerManager &mgr) {
static const DeclRefExpr * FindBlockDeclRefExpr(const Stmt *S, const VarDecl *VD)
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
const Stmt * getBody() const
const BlockDecl * getBlockDecl() const
A reference to a declared variable, function, enum, etc.
This represents one expression.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Stmt - This represents one statement.
Represents a variable declaration or definition.
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
const VarDecl * getDecl() const override=0
void trackStoredValue(SVal V, const MemRegion *R, PathSensitiveBugReport &Report, TrackingOptions Opts={}, const StackFrameContext *Origin=nullptr)
Track how the value got stored into the given region and where it came from.
The JSON file list parser is used to communicate input to InstallAPI.