1e5dd7070Spatrick //===--- UndefinedArraySubscriptChecker.h ----------------------*- C++ -*--===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
10e5dd7070Spatrick // that performs checks for undefined array subscripts.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick
14e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15e5dd7070Spatrick #include "clang/AST/DeclCXX.h"
16e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/Checker.h"
18e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20e5dd7070Spatrick
21e5dd7070Spatrick using namespace clang;
22e5dd7070Spatrick using namespace ento;
23e5dd7070Spatrick
24e5dd7070Spatrick namespace {
25e5dd7070Spatrick class UndefinedArraySubscriptChecker
26e5dd7070Spatrick : public Checker< check::PreStmt<ArraySubscriptExpr> > {
27e5dd7070Spatrick mutable std::unique_ptr<BugType> BT;
28e5dd7070Spatrick
29e5dd7070Spatrick public:
30e5dd7070Spatrick void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
31e5dd7070Spatrick };
32e5dd7070Spatrick } // end anonymous namespace
33e5dd7070Spatrick
34e5dd7070Spatrick void
checkPreStmt(const ArraySubscriptExpr * A,CheckerContext & C) const35e5dd7070Spatrick UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
36e5dd7070Spatrick CheckerContext &C) const {
37e5dd7070Spatrick const Expr *Index = A->getIdx();
38e5dd7070Spatrick if (!C.getSVal(Index).isUndef())
39e5dd7070Spatrick return;
40e5dd7070Spatrick
41e5dd7070Spatrick // Sema generates anonymous array variables for copying array struct fields.
42e5dd7070Spatrick // Don't warn if we're in an implicitly-generated constructor.
43e5dd7070Spatrick const Decl *D = C.getLocationContext()->getDecl();
44e5dd7070Spatrick if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D))
45e5dd7070Spatrick if (Ctor->isDefaulted())
46e5dd7070Spatrick return;
47e5dd7070Spatrick
48e5dd7070Spatrick ExplodedNode *N = C.generateErrorNode();
49e5dd7070Spatrick if (!N)
50e5dd7070Spatrick return;
51e5dd7070Spatrick if (!BT)
52e5dd7070Spatrick BT.reset(new BuiltinBug(this, "Array subscript is undefined"));
53e5dd7070Spatrick
54e5dd7070Spatrick // Generate a report for this bug.
55e5dd7070Spatrick auto R = std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);
56e5dd7070Spatrick R->addRange(A->getIdx()->getSourceRange());
57e5dd7070Spatrick bugreporter::trackExpressionValue(N, A->getIdx(), *R);
58e5dd7070Spatrick C.emitReport(std::move(R));
59e5dd7070Spatrick }
60e5dd7070Spatrick
registerUndefinedArraySubscriptChecker(CheckerManager & mgr)61e5dd7070Spatrick void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
62e5dd7070Spatrick mgr.registerChecker<UndefinedArraySubscriptChecker>();
63e5dd7070Spatrick }
64e5dd7070Spatrick
shouldRegisterUndefinedArraySubscriptChecker(const CheckerManager & mgr)65*ec727ea7Spatrick bool ento::shouldRegisterUndefinedArraySubscriptChecker(const CheckerManager &mgr) {
66e5dd7070Spatrick return true;
67e5dd7070Spatrick }
68