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/AST/DeclCXX.h"
00017 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00018 #include "clang/StaticAnalyzer/Core/Checker.h"
00019 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00020 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00021 
00022 using namespace clang;
00023 using namespace ento;
00024 
00025 namespace {
00026 class UndefinedArraySubscriptChecker
00027   : public Checker< check::PreStmt<ArraySubscriptExpr> > {
00028   mutable std::unique_ptr<BugType> BT;
00029 
00030 public:
00031   void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
00032 };
00033 } // end anonymous namespace
00034 
00035 void 
00036 UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
00037                                              CheckerContext &C) const {
00038   const Expr *Index = A->getIdx();
00039   if (!C.getSVal(Index).isUndef())
00040     return;
00041 
00042   // Sema generates anonymous array variables for copying array struct fields.
00043   // Don't warn if we're in an implicitly-generated constructor.
00044   const Decl *D = C.getLocationContext()->getDecl();
00045   if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D))
00046     if (Ctor->isDefaulted())
00047       return;
00048 
00049   ExplodedNode *N = C.generateSink();
00050   if (!N)
00051     return;
00052   if (!BT)
00053     BT.reset(new BuiltinBug(this, "Array subscript is undefined"));
00054 
00055   // Generate a report for this bug.
00056   BugReport *R = new BugReport(*BT, BT->getName(), N);
00057   R->addRange(A->getIdx()->getSourceRange());
00058   bugreporter::trackNullOrUndefValue(N, A->getIdx(), *R);
00059   C.emitReport(R);
00060 }
00061 
00062 void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
00063   mgr.registerChecker<UndefinedArraySubscriptChecker>();
00064 }