2009-11-11 21:42:54 +08:00
|
|
|
//===--- UndefinedArraySubscriptChecker.h ----------------------*- C++ -*--===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-11-11 21:42:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-23 02:53:44 +08:00
|
|
|
// This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
|
2009-11-11 21:42:54 +08:00
|
|
|
// that performs checks for undefined array subscripts.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2013-06-17 20:56:08 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:27:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2009-11-11 21:42:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-11 21:42:54 +08:00
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class UndefinedArraySubscriptChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::PreStmt<ArraySubscriptExpr> > {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
2011-02-28 09:27:41 +08:00
|
|
|
|
2009-11-11 21:42:54 +08:00
|
|
|
public:
|
2011-02-28 09:27:41 +08:00
|
|
|
void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
|
2009-11-11 21:42:54 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
void
|
2011-02-28 09:27:41 +08:00
|
|
|
UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
|
|
|
|
CheckerContext &C) const {
|
2013-04-03 09:39:08 +08:00
|
|
|
const Expr *Index = A->getIdx();
|
|
|
|
if (!C.getSVal(Index).isUndef())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Sema generates anonymous array variables for copying array struct fields.
|
|
|
|
// Don't warn if we're in an implicitly-generated constructor.
|
|
|
|
const Decl *D = C.getLocationContext()->getDecl();
|
|
|
|
if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D))
|
2013-08-08 00:16:48 +08:00
|
|
|
if (Ctor->isDefaulted())
|
2013-04-03 09:39:08 +08:00
|
|
|
return;
|
|
|
|
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = C.generateErrorNode();
|
2013-04-03 09:39:08 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BuiltinBug(this, "Array subscript is undefined"));
|
2013-04-03 09:39:08 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2015-06-23 21:15:32 +08:00
|
|
|
auto R = llvm::make_unique<BugReport>(*BT, BT->getName(), N);
|
2013-04-03 09:39:08 +08:00
|
|
|
R->addRange(A->getIdx()->getSourceRange());
|
2018-10-24 02:24:53 +08:00
|
|
|
bugreporter::trackExpressionValue(N, A->getIdx(), *R);
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(R));
|
2009-11-11 21:42:54 +08:00
|
|
|
}
|
2011-02-28 09:27:41 +08:00
|
|
|
|
|
|
|
void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefinedArraySubscriptChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterUndefinedArraySubscriptChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|