2009-11-04 12:24:16 +08:00
|
|
|
//===--- UndefinedAssignmentChecker.h ---------------------------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines UndefinedAssginmentChecker, a builtin check in GRExprEngine that
|
|
|
|
// checks for assigning undefined values.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-22 20:29:52 +08:00
|
|
|
#include "GRExprEngineInternalChecks.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/CheckerVisitor.h"
|
2010-01-26 01:10:22 +08:00
|
|
|
#include "clang/Checker/BugReporter/BugReporter.h"
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-11-22 20:29:52 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class UndefinedAssignmentChecker
|
2009-11-22 20:29:52 +08:00
|
|
|
: public CheckerVisitor<UndefinedAssignmentChecker> {
|
|
|
|
BugType *BT;
|
|
|
|
public:
|
|
|
|
UndefinedAssignmentChecker() : BT(0) {}
|
|
|
|
static void *getTag();
|
|
|
|
virtual void PreVisitBind(CheckerContext &C, const Stmt *AssignE,
|
|
|
|
const Stmt *StoreE, SVal location,
|
|
|
|
SVal val);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::RegisterUndefinedAssignmentChecker(GRExprEngine &Eng){
|
|
|
|
Eng.registerCheck(new UndefinedAssignmentChecker());
|
|
|
|
}
|
|
|
|
|
2009-11-04 12:24:16 +08:00
|
|
|
void *UndefinedAssignmentChecker::getTag() {
|
|
|
|
static int x = 0;
|
|
|
|
return &x;
|
|
|
|
}
|
|
|
|
|
2009-11-05 08:42:23 +08:00
|
|
|
void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C,
|
|
|
|
const Stmt *AssignE,
|
|
|
|
const Stmt *StoreE,
|
2009-11-04 12:24:16 +08:00
|
|
|
SVal location,
|
|
|
|
SVal val) {
|
|
|
|
if (!val.isUndef())
|
|
|
|
return;
|
|
|
|
|
2009-11-24 06:22:01 +08:00
|
|
|
ExplodedNode *N = C.GenerateSink();
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT)
|
2009-11-06 08:44:32 +08:00
|
|
|
BT = new BuiltinBug("Assigned value is garbage or undefined");
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2009-11-14 20:08:24 +08:00
|
|
|
EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
|
2009-11-04 12:24:16 +08:00
|
|
|
|
2009-11-05 08:42:23 +08:00
|
|
|
if (AssignE) {
|
|
|
|
const Expr *ex = 0;
|
|
|
|
|
|
|
|
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(AssignE))
|
|
|
|
ex = B->getRHS();
|
|
|
|
else if (const DeclStmt *DS = dyn_cast<DeclStmt>(AssignE)) {
|
|
|
|
const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl());
|
|
|
|
ex = VD->getInit();
|
|
|
|
}
|
|
|
|
if (ex) {
|
|
|
|
R->addRange(ex->getSourceRange());
|
|
|
|
R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex);
|
|
|
|
}
|
2009-11-04 12:24:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
|