blob: 8b07aed10e30b1a1c022a94ff86851bac5a93e8c [file] [log] [blame]
Zhongxing Xu668399b2009-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//
10// This defines UndefResultChecker, a builtin check in GRExprEngine that
11// performs checks for undefined results of non-assignment binary operators.
12//
13//===----------------------------------------------------------------------===//
14
15#include "GRExprEngineInternalChecks.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000016#include "clang/Checker/BugReporter/BugType.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/CheckerVisitor.h"
18#include "clang/Checker/PathSensitive/GRExprEngine.h"
Zhongxing Xu668399b2009-11-24 08:24:26 +000019
20using namespace clang;
21
22namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000023class UndefResultChecker
Zhongxing Xu668399b2009-11-24 08:24:26 +000024 : public CheckerVisitor<UndefResultChecker> {
25
26 BugType *BT;
27
28public:
29 UndefResultChecker() : BT(0) {}
30 static void *getTag() { static int tag = 0; return &tag; }
31 void PostVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
32};
33} // end anonymous namespace
34
35void clang::RegisterUndefResultChecker(GRExprEngine &Eng) {
36 Eng.registerCheck(new UndefResultChecker());
37}
38
39void UndefResultChecker::PostVisitBinaryOperator(CheckerContext &C,
40 const BinaryOperator *B) {
41 const GRState *state = C.getState();
Ted Kremenek13976632010-02-08 16:18:51 +000042 if (state->getSVal(B).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000043 // Generate an error node.
44 ExplodedNode *N = C.GenerateSink();
45 if (!N)
46 return;
47
48 if (!BT)
49 BT = new BuiltinBug("Result of operation is garbage or undefined");
50
51 llvm::SmallString<256> sbuf;
52 llvm::raw_svector_ostream OS(sbuf);
53 const Expr *Ex = NULL;
54 bool isLeft = true;
55
Ted Kremenek13976632010-02-08 16:18:51 +000056 if (state->getSVal(B->getLHS()).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000057 Ex = B->getLHS()->IgnoreParenCasts();
58 isLeft = true;
59 }
Ted Kremenek13976632010-02-08 16:18:51 +000060 else if (state->getSVal(B->getRHS()).isUndef()) {
Zhongxing Xu668399b2009-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 }
Benjamin Kramer4988a9a2009-11-29 18:03:28 +000077 EnhancedBugReport *report = new EnhancedBugReport(*BT, OS.str(), N);
Ted Kremenekd4daa7c2009-11-29 06:37:44 +000078 if (Ex) {
79 report->addRange(Ex->getSourceRange());
Zhongxing Xu668399b2009-11-24 08:24:26 +000080 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
Ted Kremenekd4daa7c2009-11-29 06:37:44 +000081 }
Zhongxing Xu668399b2009-11-24 08:24:26 +000082 else
83 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, B);
84 C.EmitReport(report);
85 }
86}