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" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Ted Kremenek | 6b67630 | 2010-01-25 17:10:22 +0000 | [diff] [blame] | 17 | #include "clang/Checker/BugReporter/BugReporter.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(); |
| 28 | virtual void PreVisitBind(CheckerContext &C, const Stmt *AssignE, |
| 29 | const Stmt *StoreE, SVal location, |
| 30 | SVal val); |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | void clang::RegisterUndefinedAssignmentChecker(GRExprEngine &Eng){ |
| 35 | Eng.registerCheck(new UndefinedAssignmentChecker()); |
| 36 | } |
| 37 | |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 38 | void *UndefinedAssignmentChecker::getTag() { |
| 39 | static int x = 0; |
| 40 | return &x; |
| 41 | } |
| 42 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 43 | void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C, |
| 44 | const Stmt *AssignE, |
| 45 | const Stmt *StoreE, |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 46 | SVal location, |
| 47 | SVal val) { |
| 48 | if (!val.isUndef()) |
| 49 | return; |
| 50 | |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 51 | ExplodedNode *N = C.GenerateSink(); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 52 | |
| 53 | if (!N) |
| 54 | return; |
| 55 | |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame^] | 56 | const char *str = "Assigned value is garbage or undefined"; |
| 57 | |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 58 | if (!BT) |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame^] | 59 | BT = new BuiltinBug(str); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 60 | |
| 61 | // Generate a report for this bug. |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame^] | 62 | const Expr *ex = 0; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame^] | 64 | while (AssignE) { |
| 65 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(AssignE)) { |
| 66 | if (B->isCompoundAssignmentOp()) { |
| 67 | const GRState *state = C.getState(); |
| 68 | if (state->getSVal(B->getLHS()).isUndef()) { |
| 69 | str = "The left expression of the compound assignment is an " |
| 70 | "uninitialized value. The computed value will also be garbage"; |
| 71 | ex = B->getLHS(); |
| 72 | break; |
| 73 | } |
| 74 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 76 | ex = B->getRHS(); |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame^] | 77 | break; |
| 78 | } |
| 79 | |
| 80 | if (const DeclStmt *DS = dyn_cast<DeclStmt>(AssignE)) { |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 81 | const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 82 | ex = VD->getInit(); |
| 83 | } |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame^] | 84 | |
| 85 | break; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame^] | 88 | EnhancedBugReport *R = new EnhancedBugReport(*BT, str, N); |
| 89 | if (ex) { |
| 90 | R->addRange(ex->getSourceRange()); |
| 91 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex); |
| 92 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 93 | C.EmitReport(R); |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame^] | 94 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 95 | |