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 | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 25 | using namespace ento; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 26 | |
| 27 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 28 | class UndefResultChecker |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 29 | : public Checker< check::PostStmt<BinaryOperator> > { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 30 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 31 | mutable OwningPtr<BugType> BT; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 32 | |
| 33 | public: |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 34 | void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 35 | }; |
| 36 | } // end anonymous namespace |
| 37 | |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 38 | void UndefResultChecker::checkPostStmt(const BinaryOperator *B, |
| 39 | CheckerContext &C) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 40 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 41 | const LocationContext *LCtx = C.getLocationContext(); |
| 42 | if (state->getSVal(B, LCtx).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 43 | // Generate an error node. |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 44 | ExplodedNode *N = C.generateSink(); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 45 | if (!N) |
| 46 | return; |
| 47 | |
| 48 | if (!BT) |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 49 | BT.reset(new BuiltinBug("Result of operation is garbage or undefined")); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 50 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 51 | SmallString<256> sbuf; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 52 | llvm::raw_svector_ostream OS(sbuf); |
| 53 | const Expr *Ex = NULL; |
| 54 | bool isLeft = true; |
| 55 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 56 | if (state->getSVal(B->getLHS(), LCtx).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 57 | Ex = B->getLHS()->IgnoreParenCasts(); |
| 58 | isLeft = true; |
| 59 | } |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 60 | else if (state->getSVal(B->getRHS(), LCtx).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 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 | } |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 77 | BugReport *report = new BugReport(*BT, OS.str(), N); |
Ted Kremenek | d4daa7c | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 78 | if (Ex) { |
| 79 | report->addRange(Ex->getSourceRange()); |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 80 | bugreporter::trackNullOrUndefValue(N, Ex, *report); |
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 |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 83 | bugreporter::trackNullOrUndefValue(N, B, *report); |
Ted Kremenek | ce56fd3 | 2012-06-06 06:25:37 +0000 | [diff] [blame] | 84 | |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 85 | C.emitReport(report); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 88 | |
| 89 | void ento::registerUndefResultChecker(CheckerManager &mgr) { |
| 90 | mgr.registerChecker<UndefResultChecker>(); |
| 91 | } |