[analyzer] Refactoring: include/clang/Checker -> include/clang/GR
[clang.git] / lib / Checker / UndefResultChecker.cpp
blobdf3ad3e9f889f5e24f79468d51a68ea139203f8e
1 //=== 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.
13 //===----------------------------------------------------------------------===//
15 #include "GRExprEngineInternalChecks.h"
16 #include "clang/GR/BugReporter/BugType.h"
17 #include "clang/GR/PathSensitive/CheckerVisitor.h"
18 #include "clang/GR/PathSensitive/GRExprEngine.h"
20 using namespace clang;
22 namespace {
23 class UndefResultChecker
24 : public CheckerVisitor<UndefResultChecker> {
26 BugType *BT;
28 public:
29 UndefResultChecker() : BT(0) {}
30 static void *getTag() { static int tag = 0; return &tag; }
31 void PostVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
33 } // end anonymous namespace
35 void clang::RegisterUndefResultChecker(GRExprEngine &Eng) {
36 Eng.registerCheck(new UndefResultChecker());
39 void UndefResultChecker::PostVisitBinaryOperator(CheckerContext &C,
40 const BinaryOperator *B) {
41 const GRState *state = C.getState();
42 if (state->getSVal(B).isUndef()) {
43 // Generate an error node.
44 ExplodedNode *N = C.generateSink();
45 if (!N)
46 return;
48 if (!BT)
49 BT = new BuiltinBug("Result of operation is garbage or undefined");
51 llvm::SmallString<256> sbuf;
52 llvm::raw_svector_ostream OS(sbuf);
53 const Expr *Ex = NULL;
54 bool isLeft = true;
56 if (state->getSVal(B->getLHS()).isUndef()) {
57 Ex = B->getLHS()->IgnoreParenCasts();
58 isLeft = true;
60 else if (state->getSVal(B->getRHS()).isUndef()) {
61 Ex = B->getRHS()->IgnoreParenCasts();
62 isLeft = false;
65 if (Ex) {
66 OS << "The " << (isLeft ? "left" : "right")
67 << " operand of '"
68 << BinaryOperator::getOpcodeStr(B->getOpcode())
69 << "' is a garbage value";
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";
77 EnhancedBugReport *report = new EnhancedBugReport(*BT, OS.str(), N);
78 if (Ex) {
79 report->addRange(Ex->getSourceRange());
80 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
82 else
83 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, B);
84 C.EmitReport(report);