20 using namespace clang;
24 class UndefinedAssignmentChecker
25 :
public Checker<check::Bind> {
26 mutable std::unique_ptr<BugType> BT;
29 void checkBind(SVal location, SVal val,
const Stmt *S,
30 CheckerContext &C)
const;
34 void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
36 CheckerContext &C)
const {
44 dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
45 if (C.getCalleeName(EnclosingFunctionDecl) ==
"swap")
48 ExplodedNode *N = C.generateErrorNode();
53 static const char *
const DefaultMsg =
54 "Assigned value is garbage or undefined";
56 BT.reset(
new BuiltinBug(
this, DefaultMsg));
60 llvm::raw_svector_ostream
OS(Str);
62 const Expr *ex =
nullptr;
65 if (
const UnaryOperator *U = dyn_cast<UnaryOperator>(StoreE)) {
66 OS <<
"The expression is an uninitialized value. " 67 "The computed value will also be garbage";
74 if (B->isCompoundAssignmentOp()) {
75 if (C.getSVal(B->getLHS()).isUndef()) {
76 OS <<
"The left expression of the compound assignment is an " 77 "uninitialized value. The computed value will also be garbage";
87 if (
const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
93 dyn_cast<CXXConstructorDecl>(C.getStackFrame()->getDecl())) {
94 if (CD->isImplicit()) {
95 for (
auto I : CD->inits()) {
96 if (I->getInit()->IgnoreImpCasts() == StoreE) {
97 OS <<
"Value assigned to field '" << I->getMember()->getName()
98 <<
"' in implicit constructor is garbage or undefined";
108 if (
OS.str().empty())
111 auto R = llvm::make_unique<BugReport>(*BT,
OS.str(), N);
113 R->addRange(ex->getSourceRange());
114 bugreporter::trackExpressionValue(N, ex, *R);
116 C.emitReport(std::move(R));
119 void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
120 mgr.registerChecker<UndefinedAssignmentChecker>();
123 bool ento::shouldRegisterUndefinedAssignmentChecker(
const LangOptions &LO) {
Represents a function declaration or definition.
Stmt - This represents one statement.
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...
A builtin binary operation expression such as "x + y" or "x <= y".
This represents one expression.
UnaryOperator - This represents the unary-expression's (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
Dataflow Directional Tag Classes.
const Expr * getInit() const
Indicates that the tracking object is a descendant of a referenced-counted OSObject, used in the Darwin kernel.