1 //=== UndefResultChecker.cpp ------------------------------------*- 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 UndefResultChecker, a builtin check in ExprEngine that
11 // performs checks for undefined results of non-assignment binary operators.
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"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
22 using namespace clang;
26 class UndefResultChecker
27 : public Checker< check::PostStmt<BinaryOperator> > {
29 mutable llvm::OwningPtr<BugType> BT;
32 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
34 } // end anonymous namespace
36 void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
37 CheckerContext &C) const {
38 const ProgramState *state = C.getState();
39 if (state->getSVal(B).isUndef()) {
40 // Generate an error node.
41 ExplodedNode *N = C.generateSink();
46 BT.reset(new BuiltinBug("Result of operation is garbage or undefined"));
48 llvm::SmallString<256> sbuf;
49 llvm::raw_svector_ostream OS(sbuf);
50 const Expr *Ex = NULL;
53 if (state->getSVal(B->getLHS()).isUndef()) {
54 Ex = B->getLHS()->IgnoreParenCasts();
57 else if (state->getSVal(B->getRHS()).isUndef()) {
58 Ex = B->getRHS()->IgnoreParenCasts();
63 OS << "The " << (isLeft ? "left" : "right")
65 << BinaryOperator::getOpcodeStr(B->getOpcode())
66 << "' is a garbage value";
69 // Neither operand was undefined, but the result is undefined.
70 OS << "The result of the '"
71 << BinaryOperator::getOpcodeStr(B->getOpcode())
72 << "' expression is undefined";
74 BugReport *report = new BugReport(*BT, OS.str(), N);
76 report->addRange(Ex->getSourceRange());
77 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
80 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, B));
85 void ento::registerUndefResultChecker(CheckerManager &mgr) {
86 mgr.registerChecker<UndefResultChecker>();