10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Lex/Preprocessor.h"
23 Inserter(Options.getLocalOrGlobal(
"IncludeStyle",
24 utils::IncludeSorter::IS_LLVM),
25 areDiagsSelfContained()) {}
34 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
41 Finder->addMatcher(arraySubscriptExpr(hasBase(ignoringImpCasts(hasType(
42 constantArrayType().bind(
"type")))),
43 hasIndex(expr().bind(
"index")),
44 unless(hasAncestor(decl(isImplicit()))))
50 hasOverloadedOperatorName(
"[]"),
52 ofClass(cxxRecordDecl(hasName(
"::std::array")).bind(
"type")))),
53 hasArgument(1, expr().bind(
"index")))
59 const MatchFinder::MatchResult &Result) {
60 const auto *Matched = Result.Nodes.getNodeAs<Expr>(
"expr");
61 const auto *IndexExpr = Result.Nodes.getNodeAs<Expr>(
"index");
66 if (isa<ArrayInitIndexExpr>(IndexExpr))
69 if (IndexExpr->isValueDependent())
72 std::optional<llvm::APSInt> Index =
73 IndexExpr->getIntegerConstantExpr(*Result.Context);
75 SourceRange BaseRange;
76 if (
const auto *ArraySubscriptE = dyn_cast<ArraySubscriptExpr>(Matched))
77 BaseRange = ArraySubscriptE->getBase()->getSourceRange();
80 cast<CXXOperatorCallExpr>(Matched)->getArg(0)->getSourceRange();
81 SourceRange IndexRange = IndexExpr->getSourceRange();
83 auto Diag =
diag(Matched->getExprLoc(),
84 "do not use array subscript when the index is "
85 "not an integer constant expression");
86 if (!GslHeader.empty()) {
87 Diag << FixItHint::CreateInsertion(BaseRange.getBegin(),
"gsl::at(")
88 << FixItHint::CreateReplacement(
89 SourceRange(BaseRange.getEnd().getLocWithOffset(1),
90 IndexRange.getBegin().getLocWithOffset(-1)),
92 << FixItHint::CreateReplacement(Matched->getEndLoc(),
")")
98 const auto *StdArrayDecl =
99 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"type");
105 if (Index->isSigned() && Index->isNegative()) {
106 diag(Matched->getExprLoc(),
"std::array<> index %0 is negative")
111 const TemplateArgumentList &TemplateArgs = StdArrayDecl->getTemplateArgs();
112 if (TemplateArgs.size() < 2)
115 const auto &SizeArg = TemplateArgs[1];
116 if (SizeArg.getKind() != TemplateArgument::Integral)
118 llvm::APInt ArraySize = SizeArg.getAsIntegral();
122 if (Index->getZExtValue() >= ArraySize.getZExtValue()) {
123 diag(Matched->getExprLoc(),
124 "std::array<> index %0 is past the end of the array "
125 "(which contains %1 elements)")
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
ProBoundsConstantArrayIndexCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
IncludeSorter::IncludeStyle getStyle() const
std::optional< FixItHint > createMainFileIncludeInsertion(llvm::StringRef Header)
Creates a Header inclusion directive fixit in the main file.
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
llvm::StringMap< ClangTidyValue > OptionMap