2009-11-24 16:24:26 +08:00
|
|
|
//=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines UndefResultChecker, a builtin check in GRExprEngine that
|
|
|
|
// performs checks for undefined results of non-assignment binary operators.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRExprEngineInternalChecks.h"
|
2010-03-28 05:19:47 +08:00
|
|
|
#include "clang/Checker/BugReporter/BugType.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/CheckerVisitor.h"
|
|
|
|
#include "clang/Checker/PathSensitive/GRExprEngine.h"
|
2009-11-24 16:24:26 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class UndefResultChecker
|
2009-11-24 16:24:26 +08:00
|
|
|
: public CheckerVisitor<UndefResultChecker> {
|
|
|
|
|
|
|
|
BugType *BT;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UndefResultChecker() : BT(0) {}
|
|
|
|
static void *getTag() { static int tag = 0; return &tag; }
|
|
|
|
void PostVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void clang::RegisterUndefResultChecker(GRExprEngine &Eng) {
|
|
|
|
Eng.registerCheck(new UndefResultChecker());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndefResultChecker::PostVisitBinaryOperator(CheckerContext &C,
|
|
|
|
const BinaryOperator *B) {
|
|
|
|
const GRState *state = C.getState();
|
2010-02-09 00:18:51 +08:00
|
|
|
if (state->getSVal(B).isUndef()) {
|
2009-11-24 16:24:26 +08:00
|
|
|
// Generate an error node.
|
|
|
|
ExplodedNode *N = C.GenerateSink();
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT)
|
|
|
|
BT = new BuiltinBug("Result of operation is garbage or undefined");
|
|
|
|
|
|
|
|
llvm::SmallString<256> sbuf;
|
|
|
|
llvm::raw_svector_ostream OS(sbuf);
|
|
|
|
const Expr *Ex = NULL;
|
|
|
|
bool isLeft = true;
|
|
|
|
|
2010-02-09 00:18:51 +08:00
|
|
|
if (state->getSVal(B->getLHS()).isUndef()) {
|
2009-11-24 16:24:26 +08:00
|
|
|
Ex = B->getLHS()->IgnoreParenCasts();
|
|
|
|
isLeft = true;
|
|
|
|
}
|
2010-02-09 00:18:51 +08:00
|
|
|
else if (state->getSVal(B->getRHS()).isUndef()) {
|
2009-11-24 16:24:26 +08:00
|
|
|
Ex = B->getRHS()->IgnoreParenCasts();
|
|
|
|
isLeft = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ex) {
|
|
|
|
OS << "The " << (isLeft ? "left" : "right")
|
|
|
|
<< " operand of '"
|
|
|
|
<< BinaryOperator::getOpcodeStr(B->getOpcode())
|
|
|
|
<< "' is a garbage value";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Neither operand was undefined, but the result is undefined.
|
|
|
|
OS << "The result of the '"
|
|
|
|
<< BinaryOperator::getOpcodeStr(B->getOpcode())
|
|
|
|
<< "' expression is undefined";
|
|
|
|
}
|
2009-11-30 02:03:28 +08:00
|
|
|
EnhancedBugReport *report = new EnhancedBugReport(*BT, OS.str(), N);
|
2009-11-29 14:37:44 +08:00
|
|
|
if (Ex) {
|
|
|
|
report->addRange(Ex->getSourceRange());
|
2009-11-24 16:24:26 +08:00
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
|
2009-11-29 14:37:44 +08:00
|
|
|
}
|
2009-11-24 16:24:26 +08:00
|
|
|
else
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, B);
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
|
|
|
}
|