clang API Documentation

UndefinedArraySubscriptChecker.cpp
Go to the documentation of this file.
00001 //===--- UndefinedArraySubscriptChecker.h ----------------------*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
00011 // that performs checks for undefined array subscripts.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "ClangSACheckers.h"
00016 #include "clang/StaticAnalyzer/Core/Checker.h"
00017 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00018 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00019 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00020 
00021 using namespace clang;
00022 using namespace ento;
00023 
00024 namespace {
00025 class UndefinedArraySubscriptChecker
00026   : public Checker< check::PreStmt<ArraySubscriptExpr> > {
00027   mutable OwningPtr<BugType> BT;
00028 
00029 public:
00030   void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
00031 };
00032 } // end anonymous namespace
00033 
00034 void 
00035 UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
00036                                              CheckerContext &C) const {
00037   if (C.getState()->getSVal(A->getIdx(), C.getLocationContext()).isUndef()) {
00038     if (ExplodedNode *N = C.generateSink()) {
00039       if (!BT)
00040         BT.reset(new BuiltinBug("Array subscript is undefined"));
00041 
00042       // Generate a report for this bug.
00043       BugReport *R = new BugReport(*BT, BT->getName(), N);
00044       R->addRange(A->getIdx()->getSourceRange());
00045       R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N,
00046                                                                  A->getIdx(),
00047                                                                  R));
00048       C.EmitReport(R);
00049     }
00050   }
00051 }
00052 
00053 void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
00054   mgr.registerChecker<UndefinedArraySubscriptChecker>();
00055 }