20#include "llvm/ADT/SmallString.h"
21#include "llvm/Support/raw_ostream.h"
28class UndefCapturedBlockVarChecker
29 :
public Checker< check::PostStmt<BlockExpr> > {
30 mutable std::unique_ptr<BugType> BT;
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());
61 E = R->referenced_vars_end();
73 if (std::optional<UndefinedVal>
V =
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 = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
89 R->addRange(Ex->getSourceRange());
91 {bugreporter::TrackingKind::Thorough,
93 R->disablePathPruning();
95 C.emitReport(std::move(R));
101void ento::registerUndefCapturedBlockVarChecker(
CheckerManager &mgr) {
105bool 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.
LLVM_ATTRIBUTE_RETURNS_NONNULL const VarRegion * getCapturedRegion() const
LLVM_ATTRIBUTE_RETURNS_NONNULL const VarRegion * getOriginalRegion() const
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
const VarDecl * getDecl() const override=0
void trackStoredValue(KnownSVal 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.
@ C
Languages that the frontend can parse and compile.