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" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.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" |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 24 | using namespace ento; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 27 | class UndefResultChecker |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 28 | : public Checker< check::PostStmt<BinaryOperator> > { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 29 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 30 | mutable OwningPtr<BugType> BT; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 31 | |
| 32 | public: |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 33 | void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 34 | }; |
| 35 | } // end anonymous namespace |
| 36 | |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 37 | void UndefResultChecker::checkPostStmt(const BinaryOperator *B, |
| 38 | CheckerContext &C) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 39 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 40 | const LocationContext *LCtx = C.getLocationContext(); |
| 41 | if (state->getSVal(B, LCtx).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 42 | // Generate an error node. |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 43 | ExplodedNode *N = C.generateSink(); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 44 | if (!N) |
| 45 | return; |
| 46 | |
| 47 | if (!BT) |
Argyrios Kyrtzidis | 180e03f | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 48 | BT.reset(new BuiltinBug("Result of operation is garbage or undefined")); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 49 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 50 | SmallString<256> sbuf; |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 51 | llvm::raw_svector_ostream OS(sbuf); |
| 52 | const Expr *Ex = NULL; |
| 53 | bool isLeft = true; |
| 54 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 55 | if (state->getSVal(B->getLHS(), LCtx).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 56 | Ex = B->getLHS()->IgnoreParenCasts(); |
| 57 | isLeft = true; |
| 58 | } |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 59 | else if (state->getSVal(B->getRHS(), LCtx).isUndef()) { |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 60 | Ex = B->getRHS()->IgnoreParenCasts(); |
| 61 | isLeft = false; |
| 62 | } |
| 63 | |
| 64 | if (Ex) { |
| 65 | OS << "The " << (isLeft ? "left" : "right") |
| 66 | << " operand of '" |
| 67 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
| 68 | << "' is a garbage value"; |
| 69 | } |
| 70 | else { |
| 71 | // Neither operand was undefined, but the result is undefined. |
| 72 | OS << "The result of the '" |
| 73 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
| 74 | << "' expression is undefined"; |
| 75 | } |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 76 | BugReport *report = new BugReport(*BT, OS.str(), N); |
Ted Kremenek | d4daa7c | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 77 | if (Ex) { |
| 78 | report->addRange(Ex->getSourceRange()); |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 79 | bugreporter::addTrackNullOrUndefValueVisitor(N, Ex, report); |
Ted Kremenek | d4daa7c | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 80 | } |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 81 | else |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 82 | bugreporter::addTrackNullOrUndefValueVisitor(N, B, report); |
Ted Kremenek | ce56fd3 | 2012-06-06 06:25:37 +0000 | [diff] [blame] | 83 | |
| 84 | report->disablePathPruning(); |
Zhongxing Xu | 668399b | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 85 | C.EmitReport(report); |
| 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 | } |