blob: 8946de3998124327c4aa4f01f2037976428f4723 [file] [log] [blame]
Zhongxing Xub1667122009-11-11 13:42:54 +00001//===--- 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
Zhongxing Xub1667122009-11-11 13:42:54 +000015#include "GRExprEngineInternalChecks.h"
Argyrios Kyrtzidis8d602a82010-12-22 18:51:49 +000016#include "clang/GR/BugReporter/BugType.h"
17#include "clang/GR/PathSensitive/CheckerVisitor.h"
Zhongxing Xub1667122009-11-11 13:42:54 +000018
19using namespace clang;
Argyrios Kyrtzidisca08fba2010-12-22 18:53:20 +000020using namespace GR;
Zhongxing Xub1667122009-11-11 13:42:54 +000021
22namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000023class UndefinedArraySubscriptChecker
Zhongxing Xub1667122009-11-11 13:42:54 +000024 : public CheckerVisitor<UndefinedArraySubscriptChecker> {
25 BugType *BT;
26public:
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
Argyrios Kyrtzidisca08fba2010-12-22 18:53:20 +000037void GR::RegisterUndefinedArraySubscriptChecker(GRExprEngine &Eng) {
Zhongxing Xub1667122009-11-11 13:42:54 +000038 Eng.registerCheck(new UndefinedArraySubscriptChecker());
39}
40
41void
42UndefinedArraySubscriptChecker::PreVisitArraySubscriptExpr(CheckerContext &C,
43 const ArraySubscriptExpr *A) {
Ted Kremenek57f09892010-02-08 16:18:51 +000044 if (C.getState()->getSVal(A->getIdx()).isUndef()) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +000045 if (ExplodedNode *N = C.generateSink()) {
Zhongxing Xub1667122009-11-11 13:42:54 +000046 if (!BT)
47 BT = new BuiltinBug("Array subscript is undefined");
48
49 // Generate a report for this bug.
Benjamin Kramerf4c511b2009-11-14 12:08:24 +000050 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
Zhongxing Xub1667122009-11-11 13:42:54 +000051 R->addRange(A->getIdx()->getSourceRange());
52 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
53 A->getIdx());
54 C.EmitReport(R);
55 }
56 }
57}