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/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"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
28 class UndefResultChecker
29 : public Checker< check::PostStmt<BinaryOperator> > {
31 mutable OwningPtr<BugType> BT;
34 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
36 } // end anonymous namespace
38 void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
39 CheckerContext &C) const {
40 ProgramStateRef state = C.getState();
41 const LocationContext *LCtx = C.getLocationContext();
42 if (state->getSVal(B, LCtx).isUndef()) {
44 // Do not report assignments of uninitialized values inside swap functions.
45 // This should allow to swap partially uninitialized structs
47 if (const FunctionDecl *EnclosingFunctionDecl =
48 dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
49 if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
52 // Generate an error node.
53 ExplodedNode *N = C.generateSink();
58 BT.reset(new BuiltinBug("Result of operation is garbage or undefined"));
60 SmallString<256> sbuf;
61 llvm::raw_svector_ostream OS(sbuf);
62 const Expr *Ex = NULL;
65 if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
66 Ex = B->getLHS()->IgnoreParenCasts();
69 else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
70 Ex = B->getRHS()->IgnoreParenCasts();
75 OS << "The " << (isLeft ? "left" : "right")
77 << BinaryOperator::getOpcodeStr(B->getOpcode())
78 << "' is a garbage value";
81 // Neither operand was undefined, but the result is undefined.
82 OS << "The result of the '"
83 << BinaryOperator::getOpcodeStr(B->getOpcode())
84 << "' expression is undefined";
86 BugReport *report = new BugReport(*BT, OS.str(), N);
88 report->addRange(Ex->getSourceRange());
89 bugreporter::trackNullOrUndefValue(N, Ex, *report);
92 bugreporter::trackNullOrUndefValue(N, B, *report);
98 void ento::registerUndefResultChecker(CheckerManager &mgr) {
99 mgr.registerChecker<UndefResultChecker>();