blob: a2792ad17ba1ddc9d0b57c95627f52e5408343de [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//===--- 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
15#include "clang/Checker/PathSensitive/CheckerVisitor.h"
16#include "clang/Checker/BugReporter/BugReporter.h"
17#include "GRExprEngineInternalChecks.h"
18
19using namespace clang;
20
21namespace {
22class UndefinedArraySubscriptChecker
23 : public CheckerVisitor<UndefinedArraySubscriptChecker> {
24 BugType *BT;
25public:
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
36void clang::RegisterUndefinedArraySubscriptChecker(GRExprEngine &Eng) {
37 Eng.registerCheck(new UndefinedArraySubscriptChecker());
38}
39
40void
41UndefinedArraySubscriptChecker::PreVisitArraySubscriptExpr(CheckerContext &C,
42 const ArraySubscriptExpr *A) {
43 if (C.getState()->getSVal(A->getIdx()).isUndef()) {
44 if (ExplodedNode *N = C.GenerateSink()) {
45 if (!BT)
46 BT = new BuiltinBug("Array subscript is undefined");
47
48 // Generate a report for this bug.
49 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
50 R->addRange(A->getIdx()->getSourceRange());
51 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
52 A->getIdx());
53 C.EmitReport(R);
54 }
55 }
56}