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" |
| 17 | #include "clang/Checker/PathSensitive/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 | |
| 56 | if (!BT) |
Ted Kremenek | 2c791bd | 2009-11-06 00:44:32 +0000 | [diff] [blame] | 57 | BT = new BuiltinBug("Assigned value is garbage or undefined"); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 58 | |
| 59 | // Generate a report for this bug. |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 60 | EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 62 | if (AssignE) { |
| 63 | const Expr *ex = 0; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | 50ecd15 | 2009-11-05 00:42:23 +0000 | [diff] [blame] | 65 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(AssignE)) |
| 66 | ex = B->getRHS(); |
| 67 | else if (const DeclStmt *DS = dyn_cast<DeclStmt>(AssignE)) { |
| 68 | const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 69 | ex = VD->getInit(); |
| 70 | } |
| 71 | if (ex) { |
| 72 | R->addRange(ex->getSourceRange()); |
| 73 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex); |
| 74 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | C.EmitReport(R); |
| 78 | } |
| 79 | |