]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/lib/StaticAnalyzer/Checkers/UndefinedArraySubscriptChecker.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / lib / StaticAnalyzer / Checkers / UndefinedArraySubscriptChecker.cpp
1 //===--- 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 ExprEngine
11 // that performs checks for undefined array subscripts.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20
21 using namespace clang;
22 using namespace ento;
23
24 namespace {
25 class UndefinedArraySubscriptChecker
26   : public Checker< check::PreStmt<ArraySubscriptExpr> > {
27   mutable OwningPtr<BugType> BT;
28
29 public:
30   void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
31 };
32 } // end anonymous namespace
33
34 void 
35 UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
36                                              CheckerContext &C) const {
37   const Expr *Index = A->getIdx();
38   if (!C.getSVal(Index).isUndef())
39     return;
40
41   // Sema generates anonymous array variables for copying array struct fields.
42   // Don't warn if we're in an implicitly-generated constructor.
43   const Decl *D = C.getLocationContext()->getDecl();
44   if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D))
45     if (Ctor->isImplicitlyDefined())
46       return;
47
48   ExplodedNode *N = C.generateSink();
49   if (!N)
50     return;
51   if (!BT)
52     BT.reset(new BuiltinBug("Array subscript is undefined"));
53
54   // Generate a report for this bug.
55   BugReport *R = new BugReport(*BT, BT->getName(), N);
56   R->addRange(A->getIdx()->getSourceRange());
57   bugreporter::trackNullOrUndefValue(N, A->getIdx(), *R);
58   C.emitReport(R);
59 }
60
61 void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
62   mgr.registerChecker<UndefinedArraySubscriptChecker>();
63 }