1 //=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
;
23 class UndefResultChecker
24 : public CheckerVisitor
<UndefResultChecker
> {
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();
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
;
56 if (state
->getSVal(B
->getLHS()).isUndef()) {
57 Ex
= B
->getLHS()->IgnoreParenCasts();
60 else if (state
->getSVal(B
->getRHS()).isUndef()) {
61 Ex
= B
->getRHS()->IgnoreParenCasts();
66 OS
<< "The " << (isLeft
? "left" : "right")
68 << BinaryOperator::getOpcodeStr(B
->getOpcode())
69 << "' is a garbage value";
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
);
79 report
->addRange(Ex
->getSourceRange());
80 report
->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue
, Ex
);
83 report
->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue
, B
);