10b57cec5SDimitry Andric //===--- UndefinedArraySubscriptChecker.h ----------------------*- C++ -*--===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
100b57cec5SDimitry Andric // that performs checks for undefined array subscripts.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
150b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h"
160b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
190b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace clang;
220b57cec5SDimitry Andric using namespace ento;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace {
250b57cec5SDimitry Andric class UndefinedArraySubscriptChecker
260b57cec5SDimitry Andric   : public Checker< check::PreStmt<ArraySubscriptExpr> > {
27*647cbc5dSDimitry Andric   const BugType BT{this, "Array subscript is undefined"};
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric public:
300b57cec5SDimitry Andric   void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
310b57cec5SDimitry Andric };
320b57cec5SDimitry Andric } // end anonymous namespace
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric void
checkPreStmt(const ArraySubscriptExpr * A,CheckerContext & C) const350b57cec5SDimitry Andric UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
360b57cec5SDimitry Andric                                              CheckerContext &C) const {
370b57cec5SDimitry Andric   const Expr *Index = A->getIdx();
380b57cec5SDimitry Andric   if (!C.getSVal(Index).isUndef())
390b57cec5SDimitry Andric     return;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // Sema generates anonymous array variables for copying array struct fields.
420b57cec5SDimitry Andric   // Don't warn if we're in an implicitly-generated constructor.
430b57cec5SDimitry Andric   const Decl *D = C.getLocationContext()->getDecl();
440b57cec5SDimitry Andric   if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D))
450b57cec5SDimitry Andric     if (Ctor->isDefaulted())
460b57cec5SDimitry Andric       return;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   ExplodedNode *N = C.generateErrorNode();
490b57cec5SDimitry Andric   if (!N)
500b57cec5SDimitry Andric     return;
510b57cec5SDimitry Andric   // Generate a report for this bug.
52*647cbc5dSDimitry Andric   auto R = std::make_unique<PathSensitiveBugReport>(BT, BT.getDescription(), N);
530b57cec5SDimitry Andric   R->addRange(A->getIdx()->getSourceRange());
540b57cec5SDimitry Andric   bugreporter::trackExpressionValue(N, A->getIdx(), *R);
550b57cec5SDimitry Andric   C.emitReport(std::move(R));
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
registerUndefinedArraySubscriptChecker(CheckerManager & mgr)580b57cec5SDimitry Andric void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
590b57cec5SDimitry Andric   mgr.registerChecker<UndefinedArraySubscriptChecker>();
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric 
shouldRegisterUndefinedArraySubscriptChecker(const CheckerManager & mgr)625ffd83dbSDimitry Andric bool ento::shouldRegisterUndefinedArraySubscriptChecker(const CheckerManager &mgr) {
630b57cec5SDimitry Andric   return true;
640b57cec5SDimitry Andric }
65