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