blob: 673356319833fa2f62873b14ac390131cfc330fa [file] [log] [blame]
Zhongxing Xuc6123a12009-11-24 08:24:26 +00001//=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000010// This defines UndefResultChecker, a builtin check in ExprEngine that
Zhongxing Xuc6123a12009-11-24 08:24:26 +000011// performs checks for undefined results of non-assignment binary operators.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000015#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000022#include "llvm/Support/raw_ostream.h"
Zhongxing Xuc6123a12009-11-24 08:24:26 +000023
24using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000025using namespace ento;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000026
27namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000028class UndefResultChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000029 : public Checker< check::PostStmt<BinaryOperator> > {
Zhongxing Xuc6123a12009-11-24 08:24:26 +000030
Dylan Noblesmithe2778992012-02-05 02:12:40 +000031 mutable OwningPtr<BugType> BT;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000032
33public:
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000034 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000035};
36} // end anonymous namespace
37
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000038void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
39 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000040 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000041 const LocationContext *LCtx = C.getLocationContext();
42 if (state->getSVal(B, LCtx).isUndef()) {
Zhongxing Xuc6123a12009-11-24 08:24:26 +000043 // Generate an error node.
Ted Kremenek750b7ac2010-12-20 21:19:09 +000044 ExplodedNode *N = C.generateSink();
Zhongxing Xuc6123a12009-11-24 08:24:26 +000045 if (!N)
46 return;
47
48 if (!BT)
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000049 BT.reset(new BuiltinBug("Result of operation is garbage or undefined"));
Zhongxing Xuc6123a12009-11-24 08:24:26 +000050
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000051 SmallString<256> sbuf;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000052 llvm::raw_svector_ostream OS(sbuf);
53 const Expr *Ex = NULL;
54 bool isLeft = true;
55
Ted Kremenek632e3b72012-01-06 22:09:28 +000056 if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
Zhongxing Xuc6123a12009-11-24 08:24:26 +000057 Ex = B->getLHS()->IgnoreParenCasts();
58 isLeft = true;
59 }
Ted Kremenek632e3b72012-01-06 22:09:28 +000060 else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
Zhongxing Xuc6123a12009-11-24 08:24:26 +000061 Ex = B->getRHS()->IgnoreParenCasts();
62 isLeft = false;
63 }
64
65 if (Ex) {
66 OS << "The " << (isLeft ? "left" : "right")
67 << " operand of '"
68 << BinaryOperator::getOpcodeStr(B->getOpcode())
69 << "' is a garbage value";
70 }
71 else {
72 // Neither operand was undefined, but the result is undefined.
73 OS << "The result of the '"
74 << BinaryOperator::getOpcodeStr(B->getOpcode())
75 << "' expression is undefined";
76 }
Anna Zaks3a6bdf82011-08-17 23:00:25 +000077 BugReport *report = new BugReport(*BT, OS.str(), N);
Ted Kremenek33e88a72009-11-29 06:37:44 +000078 if (Ex) {
79 report->addRange(Ex->getSourceRange());
Jordan Rosea0f7d352012-08-28 00:50:51 +000080 bugreporter::trackNullOrUndefValue(N, Ex, *report);
Ted Kremenek33e88a72009-11-29 06:37:44 +000081 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +000082 else
Jordan Rosea0f7d352012-08-28 00:50:51 +000083 bugreporter::trackNullOrUndefValue(N, B, *report);
Ted Kremenekf470a4c2012-06-06 06:25:37 +000084
Jordan Rosee10d5a72012-11-02 01:53:40 +000085 C.emitReport(report);
Zhongxing Xuc6123a12009-11-24 08:24:26 +000086 }
87}
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000088
89void ento::registerUndefResultChecker(CheckerManager &mgr) {
90 mgr.registerChecker<UndefResultChecker>();
91}