11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Lex/Preprocessor.h"
16 using namespace clang::ast_matchers;
20 namespace cppcoreguidelines {
22 ProBoundsConstantArrayIndexCheck::ProBoundsConstantArrayIndexCheck(
24 :
ClangTidyCheck(Name, Context), GslHeader(Options.get(
"GslHeader",
"")),
25 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
26 Options.get(
"IncludeStyle",
"llvm"))) {}
35 CompilerInstance &Compiler) {
40 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
41 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
52 hasBase(ignoringImpCasts(hasType(constantArrayType().bind(
"type")))),
53 hasIndex(expr().bind(
"index")), unless(hasAncestor(isImplicit())))
59 hasOverloadedOperatorName(
"[]"),
61 0, hasType(cxxRecordDecl(hasName(
"::std::array")).bind(
"type"))),
62 hasArgument(1, expr().bind(
"index")))
68 const MatchFinder::MatchResult &
Result) {
69 const auto *Matched = Result.Nodes.getNodeAs<Expr>(
"expr");
70 const auto *IndexExpr = Result.Nodes.getNodeAs<Expr>(
"index");
72 if (IndexExpr->isValueDependent())
76 if (!IndexExpr->isIntegerConstantExpr(Index, *Result.Context,
nullptr,
78 SourceRange BaseRange;
79 if (
const auto *ArraySubscriptE = dyn_cast<ArraySubscriptExpr>(Matched))
80 BaseRange = ArraySubscriptE->getBase()->getSourceRange();
83 dyn_cast<CXXOperatorCallExpr>(Matched)->getArg(0)->getSourceRange();
84 SourceRange IndexRange = IndexExpr->getSourceRange();
86 auto Diag =
diag(Matched->getExprLoc(),
87 "do not use array subscript when the index is "
88 "not an integer constant expression; use gsl::at() "
90 if (!GslHeader.empty()) {
91 Diag << FixItHint::CreateInsertion(BaseRange.getBegin(),
"gsl::at(")
92 << FixItHint::CreateReplacement(
93 SourceRange(BaseRange.getEnd().getLocWithOffset(1),
94 IndexRange.getBegin().getLocWithOffset(-1)),
96 << FixItHint::CreateReplacement(Matched->getLocEnd(),
")");
98 Optional<FixItHint> Insertion = Inserter->CreateIncludeInsertion(
99 Result.SourceManager->getMainFileID(), GslHeader,
102 Diag << Insertion.getValue();
107 const auto *StdArrayDecl =
108 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"type");
114 if (Index.isSigned() && Index.isNegative()) {
115 diag(Matched->getExprLoc(),
"std::array<> index %0 is negative")
116 << Index.toString(10);
120 const TemplateArgumentList &TemplateArgs = StdArrayDecl->getTemplateArgs();
121 if (TemplateArgs.size() < 2)
124 const auto &SizeArg = TemplateArgs[1];
125 if (SizeArg.getKind() != TemplateArgument::Integral)
127 llvm::APInt ArraySize = SizeArg.getAsIntegral();
131 if (Index.getZExtValue() >= ArraySize.getZExtValue()) {
132 diag(Matched->getExprLoc(),
133 "std::array<> index %0 is past the end of the array "
134 "(which contains %1 elements)")
135 << Index.toString(10) << ArraySize.toString(10,
false);
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
LangOptions getLangOpts() const
Returns the language options from the context.
std::unique_ptr< ast_matchers::MatchFinder > Finder
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Base class for all clang-tidy checks.
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.
std::map< std::string, std::string > OptionMap
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
Produces fixes to insert specified includes to source files, if not yet present.
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.