Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 1 | //===--- UndefinedArraySubscriptChecker.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 | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 10 | // This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 11 | // that performs checks for undefined array subscripts. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 15 | #include "ExprEngineInternalChecks.h" |
Ted Kremenek | 2114258 | 2010-12-23 19:38:26 +0000 | [diff] [blame^] | 16 | #include "clang/StaticAnalyzer/BugReporter/BugType.h" |
| 17 | #include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h" |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 20 | using namespace ento; |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 21 | |
| 22 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 23 | class UndefinedArraySubscriptChecker |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 24 | : public CheckerVisitor<UndefinedArraySubscriptChecker> { |
| 25 | BugType *BT; |
| 26 | public: |
| 27 | UndefinedArraySubscriptChecker() : BT(0) {} |
| 28 | static void *getTag() { |
| 29 | static int x = 0; |
| 30 | return &x; |
| 31 | } |
| 32 | void PreVisitArraySubscriptExpr(CheckerContext &C, |
| 33 | const ArraySubscriptExpr *A); |
| 34 | }; |
| 35 | } // end anonymous namespace |
| 36 | |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 37 | void ento::RegisterUndefinedArraySubscriptChecker(ExprEngine &Eng) { |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 38 | Eng.registerCheck(new UndefinedArraySubscriptChecker()); |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | UndefinedArraySubscriptChecker::PreVisitArraySubscriptExpr(CheckerContext &C, |
| 43 | const ArraySubscriptExpr *A) { |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 44 | if (C.getState()->getSVal(A->getIdx()).isUndef()) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 45 | if (ExplodedNode *N = C.generateSink()) { |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 46 | if (!BT) |
| 47 | BT = new BuiltinBug("Array subscript is undefined"); |
| 48 | |
| 49 | // Generate a report for this bug. |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 50 | EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N); |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 51 | R->addRange(A->getIdx()->getSourceRange()); |
| 52 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 53 | A->getIdx()); |
| 54 | C.EmitReport(R); |
| 55 | } |
| 56 | } |
| 57 | } |