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 | |
| 15 | #include "clang/Analysis/PathSensitive/Checkers/UndefinedAssignmentChecker.h" |
| 16 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | void *UndefinedAssignmentChecker::getTag() { |
| 21 | static int x = 0; |
| 22 | return &x; |
| 23 | } |
| 24 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 25 | void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C, |
| 26 | const Stmt *AssignE, |
| 27 | const Stmt *StoreE, |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 28 | SVal location, |
| 29 | SVal val) { |
| 30 | if (!val.isUndef()) |
| 31 | return; |
| 32 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 33 | ExplodedNode *N = C.GenerateNode(StoreE, true); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 34 | |
| 35 | if (!N) |
| 36 | return; |
| 37 | |
| 38 | if (!BT) |
Ted Kremenek | 2c791bd | 2009-11-06 00:44:32 +0000 | [diff] [blame] | 39 | BT = new BuiltinBug("Assigned value is garbage or undefined"); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 40 | |
| 41 | // Generate a report for this bug. |
| 42 | EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName().c_str(), N); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 44 | if (AssignE) { |
| 45 | const Expr *ex = 0; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 47 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(AssignE)) |
| 48 | ex = B->getRHS(); |
| 49 | else if (const DeclStmt *DS = dyn_cast<DeclStmt>(AssignE)) { |
| 50 | const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 51 | ex = VD->getInit(); |
| 52 | } |
| 53 | if (ex) { |
| 54 | R->addRange(ex->getSourceRange()); |
| 55 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex); |
| 56 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | C.EmitReport(R); |
| 60 | } |
| 61 | |