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/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
21 using namespace clang;
25 class UndefinedAssignmentChecker
26 : public Checker<check::Bind> {
27 mutable llvm::OwningPtr<BugType> BT;
30 void checkBind(SVal location, SVal val, CheckerContext &C) const;
34 void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
35 CheckerContext &C) const {
39 ExplodedNode *N = C.generateSink();
44 const char *str = "Assigned value is garbage or undefined";
47 BT.reset(new BuiltinBug(str));
49 // Generate a report for this bug.
52 const Stmt *StoreE = C.getStmt();
54 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
55 if (B->isCompoundAssignmentOp()) {
56 const GRState *state = C.getState();
57 if (state->getSVal(B->getLHS()).isUndef()) {
58 str = "The left expression of the compound assignment is an "
59 "uninitialized value. The computed value will also be garbage";
69 if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
70 const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl());
77 EnhancedBugReport *R = new EnhancedBugReport(*BT, str, N);
79 R->addRange(ex->getSourceRange());
80 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex);
85 void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
86 mgr.registerChecker<UndefinedAssignmentChecker>();