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 | // |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 10 | // This defines UndefResultChecker, a builtin check in ExprEngine that |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 11 | // performs checks for undefined results of non-assignment binary operators. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 15 | #include "ExprEngineInternalChecks.h" |
Argyrios Kyrtzidis | 98cabba | 2010-12-22 18:51:49 +0000 | [diff] [blame] | 16 | #include "clang/GR/BugReporter/BugType.h" |
| 17 | #include "clang/GR/PathSensitive/CheckerVisitor.h" |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 18 | #include "clang/GR/PathSensitive/ExprEngine.h" |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 21 | using namespace GR; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 24 | class UndefResultChecker |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 25 | : public CheckerVisitor<UndefResultChecker> { |
| 26 | |
| 27 | BugType *BT; |
| 28 | |
| 29 | public: |
| 30 | UndefResultChecker() : BT(0) {} |
| 31 | static void *getTag() { static int tag = 0; return &tag; } |
| 32 | void PostVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
| 33 | }; |
| 34 | } // end anonymous namespace |
| 35 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 36 | void GR::RegisterUndefResultChecker(ExprEngine &Eng) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 37 | Eng.registerCheck(new UndefResultChecker()); |
| 38 | } |
| 39 | |
| 40 | void UndefResultChecker::PostVisitBinaryOperator(CheckerContext &C, |
| 41 | const BinaryOperator *B) { |
| 42 | const GRState *state = C.getState(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 43 | if (state->getSVal(B).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 44 | // Generate an error node. |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 45 | ExplodedNode *N = C.generateSink(); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 46 | if (!N) |
| 47 | return; |
| 48 | |
| 49 | if (!BT) |
| 50 | BT = new BuiltinBug("Result of operation is garbage or undefined"); |
| 51 | |
| 52 | llvm::SmallString<256> sbuf; |
| 53 | llvm::raw_svector_ostream OS(sbuf); |
| 54 | const Expr *Ex = NULL; |
| 55 | bool isLeft = true; |
| 56 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 57 | if (state->getSVal(B->getLHS()).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 58 | Ex = B->getLHS()->IgnoreParenCasts(); |
| 59 | isLeft = true; |
| 60 | } |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 61 | else if (state->getSVal(B->getRHS()).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 62 | Ex = B->getRHS()->IgnoreParenCasts(); |
| 63 | isLeft = false; |
| 64 | } |
| 65 | |
| 66 | if (Ex) { |
| 67 | OS << "The " << (isLeft ? "left" : "right") |
| 68 | << " operand of '" |
| 69 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
| 70 | << "' is a garbage value"; |
| 71 | } |
| 72 | else { |
| 73 | // Neither operand was undefined, but the result is undefined. |
| 74 | OS << "The result of the '" |
| 75 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
| 76 | << "' expression is undefined"; |
| 77 | } |
Benjamin Kramer | 4988a9a | 2009-11-29 18:03:28 +0000 | [diff] [blame] | 78 | EnhancedBugReport *report = new EnhancedBugReport(*BT, OS.str(), N); |
Ted Kremenek | d4daa7c | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 79 | if (Ex) { |
| 80 | report->addRange(Ex->getSourceRange()); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 81 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex); |
Ted Kremenek | d4daa7c | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 82 | } |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 83 | else |
| 84 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, B); |
| 85 | C.EmitReport(report); |
| 86 | } |
| 87 | } |