Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 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. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "GRExprEngineInternalChecks.h" |
| 16 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
| 17 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 18 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 23 | class UndefResultChecker |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 24 | : public CheckerVisitor<UndefResultChecker> { |
| 25 | |
| 26 | BugType *BT; |
| 27 | |
| 28 | public: |
| 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 | |
| 35 | void clang::RegisterUndefResultChecker(GRExprEngine &Eng) { |
| 36 | Eng.registerCheck(new UndefResultChecker()); |
| 37 | } |
| 38 | |
| 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; |
| 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 | |
| 56 | if (state->getSVal(B->getLHS()).isUndef()) { |
| 57 | Ex = B->getLHS()->IgnoreParenCasts(); |
| 58 | isLeft = true; |
| 59 | } |
| 60 | else if (state->getSVal(B->getRHS()).isUndef()) { |
| 61 | 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 Kramer | 4988a9a | 2009-11-29 18:03:28 +0000 | [diff] [blame] | 77 | EnhancedBugReport *report = new EnhancedBugReport(*BT, OS.str(), N); |
Ted Kremenek | d4daa7c | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 78 | if (Ex) { |
| 79 | report->addRange(Ex->getSourceRange()); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 80 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex); |
Ted Kremenek | d4daa7c | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 81 | } |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 82 | else |
| 83 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, B); |
| 84 | C.EmitReport(report); |
| 85 | } |
| 86 | } |