blob: d74b163986943abe9e2f7b9999c09da6bddb580f [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//
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000010// This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
Zhongxing Xub1667122009-11-11 13:42:54 +000011// that performs checks for undefined array subscripts.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisda6c7562011-02-28 01:27:41 +000015#include "ClangSACheckers.h"
Reid Kleckner2ab0ac52013-06-17 12:56:08 +000016#include "clang/AST/DeclCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisda6c7562011-02-28 01:27:41 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xub1667122009-11-11 13:42:54 +000021
22using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xub1667122009-11-11 13:42:54 +000024
25namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000026class UndefinedArraySubscriptChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000027 : public Checker< check::PreStmt<ArraySubscriptExpr> > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000028 mutable OwningPtr<BugType> BT;
Argyrios Kyrtzidisda6c7562011-02-28 01:27:41 +000029
Zhongxing Xub1667122009-11-11 13:42:54 +000030public:
Argyrios Kyrtzidisda6c7562011-02-28 01:27:41 +000031 void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
Zhongxing Xub1667122009-11-11 13:42:54 +000032};
33} // end anonymous namespace
34
Zhongxing Xub1667122009-11-11 13:42:54 +000035void
Argyrios Kyrtzidisda6c7562011-02-28 01:27:41 +000036UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
37 CheckerContext &C) const {
Jordan Rosebc74eb12013-04-03 01:39:08 +000038 const Expr *Index = A->getIdx();
39 if (!C.getSVal(Index).isUndef())
40 return;
Zhongxing Xub1667122009-11-11 13:42:54 +000041
Jordan Rosebc74eb12013-04-03 01:39:08 +000042 // Sema generates anonymous array variables for copying array struct fields.
43 // Don't warn if we're in an implicitly-generated constructor.
44 const Decl *D = C.getLocationContext()->getDecl();
45 if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D))
46 if (Ctor->isImplicitlyDefined())
47 return;
48
49 ExplodedNode *N = C.generateSink();
50 if (!N)
51 return;
52 if (!BT)
53 BT.reset(new BuiltinBug("Array subscript is undefined"));
54
55 // Generate a report for this bug.
56 BugReport *R = new BugReport(*BT, BT->getName(), N);
57 R->addRange(A->getIdx()->getSourceRange());
58 bugreporter::trackNullOrUndefValue(N, A->getIdx(), *R);
59 C.emitReport(R);
Zhongxing Xub1667122009-11-11 13:42:54 +000060}
Argyrios Kyrtzidisda6c7562011-02-28 01:27:41 +000061
62void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
63 mgr.registerChecker<UndefinedArraySubscriptChecker>();
64}