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
65 lines
2.2 KiB
C++
65 lines
2.2 KiB
C++
//===--- UndefinedArraySubscriptChecker.h ----------------------*- C++ -*--===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
|
|
// that performs checks for undefined array subscripts.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
|
#include "clang/AST/DeclCXX.h"
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
using namespace clang;
|
|
using namespace ento;
|
|
|
|
namespace {
|
|
class UndefinedArraySubscriptChecker
|
|
: public Checker< check::PreStmt<ArraySubscriptExpr> > {
|
|
mutable std::unique_ptr<BugType> BT;
|
|
|
|
public:
|
|
void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
|
|
};
|
|
} // end anonymous namespace
|
|
|
|
void
|
|
UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
|
|
CheckerContext &C) const {
|
|
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))
|
|
if (Ctor->isDefaulted())
|
|
return;
|
|
|
|
ExplodedNode *N = C.generateErrorNode();
|
|
if (!N)
|
|
return;
|
|
if (!BT)
|
|
BT.reset(new BuiltinBug(this, "Array subscript is undefined"));
|
|
|
|
// Generate a report for this bug.
|
|
auto R = llvm::make_unique<BugReport>(*BT, BT->getName(), N);
|
|
R->addRange(A->getIdx()->getSourceRange());
|
|
bugreporter::trackExpressionValue(N, A->getIdx(), *R);
|
|
C.emitReport(std::move(R));
|
|
}
|
|
|
|
void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
|
|
mgr.registerChecker<UndefinedArraySubscriptChecker>();
|
|
}
|