/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/UndefinedArraySubscriptChecker.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
14 | | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
15 | | #include "clang/AST/DeclCXX.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 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 | 16.9k | CheckerContext &C) const { |
37 | 16.9k | const Expr *Index = A->getIdx(); |
38 | 16.9k | if (!C.getSVal(Index).isUndef()) |
39 | 16.9k | 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 | 2 | const Decl *D = C.getLocationContext()->getDecl(); |
44 | 2 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) |
45 | 0 | if (Ctor->isDefaulted()) |
46 | 0 | return; |
47 | | |
48 | 2 | ExplodedNode *N = C.generateErrorNode(); |
49 | 2 | if (!N) |
50 | 0 | return; |
51 | 2 | if (!BT) |
52 | 2 | BT.reset(new BugType(this, "Array subscript is undefined")); |
53 | | |
54 | | // Generate a report for this bug. |
55 | 2 | auto R = std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N); |
56 | 2 | R->addRange(A->getIdx()->getSourceRange()); |
57 | 2 | bugreporter::trackExpressionValue(N, A->getIdx(), *R); |
58 | 2 | C.emitReport(std::move(R)); |
59 | 2 | } |
60 | | |
61 | 1.27k | void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) { |
62 | 1.27k | mgr.registerChecker<UndefinedArraySubscriptChecker>(); |
63 | 1.27k | } |
64 | | |
65 | 2.55k | bool ento::shouldRegisterUndefinedArraySubscriptChecker(const CheckerManager &mgr) { |
66 | 2.55k | return true; |
67 | 2.55k | } |