Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 1 | //===--- UndefinedAssignmentChecker.h ---------------------------*- 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 | 267aa5c | 2011-02-28 01:27:37 +0000 | [diff] [blame] | 10 | // This defines UndefinedAssignmentChecker, a builtin check in ExprEngine that |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 11 | // checks for assigning undefined values. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | 267aa5c | 2011-02-28 01:27:37 +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 | 267aa5c | 2011-02-28 01:27:37 +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 | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 22 | using namespace ento; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 23 | |
Zhongxing Xu | c3372e0 | 2009-11-22 12:29:52 +0000 | [diff] [blame] | 24 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 25 | class UndefinedAssignmentChecker |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 26 | : public Checker<check::Bind> { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 27 | mutable OwningPtr<BugType> BT; |
Argyrios Kyrtzidis | 267aa5c | 2011-02-28 01:27:37 +0000 | [diff] [blame] | 28 | |
Zhongxing Xu | c3372e0 | 2009-11-22 12:29:52 +0000 | [diff] [blame] | 29 | public: |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 30 | void checkBind(SVal location, SVal val, const Stmt *S, |
| 31 | CheckerContext &C) const; |
Zhongxing Xu | c3372e0 | 2009-11-22 12:29:52 +0000 | [diff] [blame] | 32 | }; |
| 33 | } |
| 34 | |
Argyrios Kyrtzidis | 267aa5c | 2011-02-28 01:27:37 +0000 | [diff] [blame] | 35 | void UndefinedAssignmentChecker::checkBind(SVal location, SVal val, |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 36 | const Stmt *StoreE, |
Argyrios Kyrtzidis | 267aa5c | 2011-02-28 01:27:37 +0000 | [diff] [blame] | 37 | CheckerContext &C) const { |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 38 | if (!val.isUndef()) |
| 39 | return; |
| 40 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 41 | ExplodedNode *N = C.generateSink(); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 42 | |
| 43 | if (!N) |
| 44 | return; |
| 45 | |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 46 | const char *str = "Assigned value is garbage or undefined"; |
| 47 | |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 48 | if (!BT) |
Argyrios Kyrtzidis | 267aa5c | 2011-02-28 01:27:37 +0000 | [diff] [blame] | 49 | BT.reset(new BuiltinBug(str)); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 50 | |
| 51 | // Generate a report for this bug. |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 52 | const Expr *ex = 0; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | 79d7304 | 2010-09-02 00:56:20 +0000 | [diff] [blame] | 54 | while (StoreE) { |
| 55 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) { |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 56 | if (B->isCompoundAssignmentOp()) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 57 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 58 | if (state->getSVal(B->getLHS(), C.getLocationContext()).isUndef()) { |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 59 | str = "The left expression of the compound assignment is an " |
| 60 | "uninitialized value. The computed value will also be garbage"; |
| 61 | ex = B->getLHS(); |
| 62 | break; |
| 63 | } |
| 64 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 66 | ex = B->getRHS(); |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 67 | break; |
| 68 | } |
| 69 | |
Ted Kremenek | 79d7304 | 2010-09-02 00:56:20 +0000 | [diff] [blame] | 70 | if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 71 | const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 72 | ex = VD->getInit(); |
| 73 | } |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 74 | |
| 75 | break; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 78 | BugReport *R = new BugReport(*BT, str, N); |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 79 | if (ex) { |
| 80 | R->addRange(ex->getSourceRange()); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 81 | R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, ex)); |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 83 | C.EmitReport(R); |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 84 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 85 | |
Argyrios Kyrtzidis | 267aa5c | 2011-02-28 01:27:37 +0000 | [diff] [blame] | 86 | void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) { |
| 87 | mgr.registerChecker<UndefinedAssignmentChecker>(); |
| 88 | } |