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 | // |
| 10 | // This defines UndefinedAssginmentChecker, a builtin check in GRExprEngine that |
| 11 | // checks for assigning undefined values. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Zhongxing Xu | c3372e0 | 2009-11-22 12:29:52 +0000 | [diff] [blame] | 15 | #include "GRExprEngineInternalChecks.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" |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Zhongxing Xu | c3372e0 | 2009-11-22 12:29:52 +0000 | [diff] [blame] | 21 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 22 | class UndefinedAssignmentChecker |
Zhongxing Xu | c3372e0 | 2009-11-22 12:29:52 +0000 | [diff] [blame] | 23 | : public CheckerVisitor<UndefinedAssignmentChecker> { |
| 24 | BugType *BT; |
| 25 | public: |
| 26 | UndefinedAssignmentChecker() : BT(0) {} |
| 27 | static void *getTag(); |
Ted Kremenek | 79d7304 | 2010-09-02 00:56:20 +0000 | [diff] [blame] | 28 | virtual void PreVisitBind(CheckerContext &C, const Stmt *StoreE, |
| 29 | SVal location, SVal val); |
Zhongxing Xu | c3372e0 | 2009-11-22 12:29:52 +0000 | [diff] [blame] | 30 | }; |
| 31 | } |
| 32 | |
| 33 | void clang::RegisterUndefinedAssignmentChecker(GRExprEngine &Eng){ |
| 34 | Eng.registerCheck(new UndefinedAssignmentChecker()); |
| 35 | } |
| 36 | |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 37 | void *UndefinedAssignmentChecker::getTag() { |
| 38 | static int x = 0; |
| 39 | return &x; |
| 40 | } |
| 41 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 42 | void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C, |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 43 | const Stmt *StoreE, |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 44 | SVal location, |
| 45 | SVal val) { |
| 46 | if (!val.isUndef()) |
| 47 | return; |
| 48 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 49 | ExplodedNode *N = C.generateSink(); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 50 | |
| 51 | if (!N) |
| 52 | return; |
| 53 | |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 54 | const char *str = "Assigned value is garbage or undefined"; |
| 55 | |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 56 | if (!BT) |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 57 | BT = new BuiltinBug(str); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 58 | |
| 59 | // Generate a report for this bug. |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 60 | const Expr *ex = 0; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | 79d7304 | 2010-09-02 00:56:20 +0000 | [diff] [blame] | 62 | while (StoreE) { |
| 63 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) { |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 64 | if (B->isCompoundAssignmentOp()) { |
| 65 | const GRState *state = C.getState(); |
| 66 | if (state->getSVal(B->getLHS()).isUndef()) { |
| 67 | str = "The left expression of the compound assignment is an " |
| 68 | "uninitialized value. The computed value will also be garbage"; |
| 69 | ex = B->getLHS(); |
| 70 | break; |
| 71 | } |
| 72 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 74 | ex = B->getRHS(); |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 75 | break; |
| 76 | } |
| 77 | |
Ted Kremenek | 79d7304 | 2010-09-02 00:56:20 +0000 | [diff] [blame] | 78 | if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) { |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 79 | const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 80 | ex = VD->getInit(); |
| 81 | } |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 82 | |
| 83 | break; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 86 | EnhancedBugReport *R = new EnhancedBugReport(*BT, str, N); |
| 87 | if (ex) { |
| 88 | R->addRange(ex->getSourceRange()); |
| 89 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex); |
| 90 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 91 | C.EmitReport(R); |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 93 | |