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"
21 #include "llvm/ADT/SmallString.h"
23 using namespace clang;
27 class UndefResultChecker
28 : public Checker< check::PostStmt<BinaryOperator> > {
30 mutable OwningPtr<BugType> BT;
33 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
35 } // end anonymous namespace
37 void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
38 CheckerContext &C) const {
39 ProgramStateRef state = C.getState();
40 const LocationContext *LCtx = C.getLocationContext();
41 if (state->getSVal(B, LCtx).isUndef()) {
42 // Generate an error node.
43 ExplodedNode *N = C.generateSink();
48 BT.reset(new BuiltinBug("Result of operation is garbage or undefined"));
50 SmallString<256> sbuf;
51 llvm::raw_svector_ostream OS(sbuf);
52 const Expr *Ex = NULL;
55 if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
56 Ex = B->getLHS()->IgnoreParenCasts();
59 else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
60 Ex = B->getRHS()->IgnoreParenCasts();
65 OS << "The " << (isLeft ? "left" : "right")
67 << BinaryOperator::getOpcodeStr(B->getOpcode())
68 << "' is a garbage value";
71 // Neither operand was undefined, but the result is undefined.
72 OS << "The result of the '"
73 << BinaryOperator::getOpcodeStr(B->getOpcode())
74 << "' expression is undefined";
76 BugReport *report = new BugReport(*BT, OS.str(), N);
78 report->addRange(Ex->getSourceRange());
79 bugreporter::trackNullOrUndefValue(N, Ex, *report);
82 bugreporter::trackNullOrUndefValue(N, B, *report);
88 void ento::registerUndefResultChecker(CheckerManager &mgr) {
89 mgr.registerChecker<UndefResultChecker>();