1 //===--- UndefinedAssignmentChecker.h ---------------------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines UndefinedAssignmentChecker, a builtin check in ExprEngine that
11 // checks for assigning undefined values.
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21 using namespace clang;
25 class UndefinedAssignmentChecker
26 : public Checker<check::Bind> {
27 mutable OwningPtr<BugType> BT;
30 void checkBind(SVal location, SVal val, const Stmt *S,
31 CheckerContext &C) const;
35 void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
37 CheckerContext &C) const {
41 ExplodedNode *N = C.generateSink();
46 const char *str = "Assigned value is garbage or undefined";
49 BT.reset(new BuiltinBug(str));
51 // Generate a report for this bug.
55 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
56 if (B->isCompoundAssignmentOp()) {
57 ProgramStateRef state = C.getState();
58 if (state->getSVal(B->getLHS(), C.getLocationContext()).isUndef()) {
59 str = "The left expression of the compound assignment is an "
60 "uninitialized value. The computed value will also be garbage";
70 if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
71 const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
78 BugReport *R = new BugReport(*BT, str, N);
80 R->addRange(ex->getSourceRange());
81 bugreporter::trackNullOrUndefValue(N, ex, *R);
86 void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
87 mgr.registerChecker<UndefinedAssignmentChecker>();