blob: 2cd293b2f42eaaef934a9ba032d50b7583775b30 [file] [log] [blame]
Zhongxing Xud6944852009-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//
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000010// This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
Zhongxing Xud6944852009-11-11 13:42:54 +000011// that performs checks for undefined array subscripts.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000015#include "ExprEngineInternalChecks.h"
Ted Kremenek21142582010-12-23 19:38:26 +000016#include "clang/StaticAnalyzer/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
Zhongxing Xud6944852009-11-11 13:42:54 +000018
19using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000020using namespace ento;
Zhongxing Xud6944852009-11-11 13:42:54 +000021
22namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000023class UndefinedArraySubscriptChecker
Zhongxing Xud6944852009-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
Ted Kremenek9ef65372010-12-23 07:20:52 +000037void ento::RegisterUndefinedArraySubscriptChecker(ExprEngine &Eng) {
Zhongxing Xud6944852009-11-11 13:42:54 +000038 Eng.registerCheck(new UndefinedArraySubscriptChecker());
39}
40
41void
42UndefinedArraySubscriptChecker::PreVisitArraySubscriptExpr(CheckerContext &C,
43 const ArraySubscriptExpr *A) {
Ted Kremenek13976632010-02-08 16:18:51 +000044 if (C.getState()->getSVal(A->getIdx()).isUndef()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +000045 if (ExplodedNode *N = C.generateSink()) {
Zhongxing Xud6944852009-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 Kramerd02e2322009-11-14 12:08:24 +000050 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
Zhongxing Xud6944852009-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}