clang  15.0.0git
UndefinedArraySubscriptChecker.cpp
Go to the documentation of this file.
1 //===--- UndefinedArraySubscriptChecker.h ----------------------*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
10 // that performs checks for undefined array subscripts.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "clang/AST/DeclCXX.h"
20 
21 using namespace clang;
22 using namespace ento;
23 
24 namespace {
25 class UndefinedArraySubscriptChecker
26  : public Checker< check::PreStmt<ArraySubscriptExpr> > {
27  mutable std::unique_ptr<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->isDefaulted())
46  return;
47 
48  ExplodedNode *N = C.generateErrorNode();
49  if (!N)
50  return;
51  if (!BT)
52  BT.reset(new BuiltinBug(this, "Array subscript is undefined"));
53 
54  // Generate a report for this bug.
55  auto R = std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);
56  R->addRange(A->getIdx()->getSourceRange());
58  C.emitReport(std::move(R));
59 }
60 
61 void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
62  mgr.registerChecker<UndefinedArraySubscriptChecker>();
63 }
64 
65 bool ento::shouldRegisterUndefinedArraySubscriptChecker(const CheckerManager &mgr) {
66  return true;
67 }
clang::CXXConstructorDecl
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2434
clang::Stmt::getSourceRange
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:324
AttributeLangSupport::C
@ C
Definition: SemaDeclAttr.cpp:55
clang::ArraySubscriptExpr::getIdx
Expr * getIdx()
Definition: Expr.h:2685
DeclCXX.h
BuiltinCheckerRegistration.h
CheckerManager.h
clang::ento::bugreporter::trackExpressionValue
bool trackExpressionValue(const ExplodedNode *N, const Expr *E, PathSensitiveBugReport &R, TrackingOptions Opts={})
Attempts to add visitors to track expression value back to its point of origin.
BugType.h
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::ArraySubscriptExpr
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition: Expr.h:2645
CheckerContext.h
Checker.h
clang
Definition: CalledOnceCheck.h:17
clang::Expr
This represents one expression.
Definition: Expr.h:109