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;
21 ProBoundsConstantArrayIndexCheck::ProBoundsConstantArrayIndexCheck(
23 :
ClangTidyCheck(Name, Context), GslHeader(Options.get(
"GslHeader",
"")),
25 Options.get(
"IncludeStyle",
"llvm"))) {}
34 CompilerInstance &Compiler) {
39 Compiler.getLangOpts(), IncludeStyle));
40 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
47 Finder->addMatcher(arraySubscriptExpr(hasBase(ignoringImpCasts(hasType(
48 constantArrayType().bind(
"type")))),
49 hasIndex(expr().bind(
"index")))
55 hasOverloadedOperatorName(
"[]"),
57 0, hasType(cxxRecordDecl(hasName(
"::std::array")).bind(
"type"))),
58 hasArgument(1, expr().bind(
"index")))
64 const MatchFinder::MatchResult &
Result) {
65 const auto *Matched = Result.Nodes.getNodeAs<Expr>(
"expr");
66 const auto *IndexExpr = Result.Nodes.getNodeAs<Expr>(
"index");
68 if (!IndexExpr->isIntegerConstantExpr(Index, *Result.Context,
nullptr,
70 SourceRange BaseRange;
71 if (
const auto *ArraySubscriptE = dyn_cast<ArraySubscriptExpr>(Matched))
72 BaseRange = ArraySubscriptE->getBase()->getSourceRange();
75 dyn_cast<CXXOperatorCallExpr>(Matched)->getArg(0)->getSourceRange();
76 SourceRange IndexRange = IndexExpr->getSourceRange();
78 auto Diag =
diag(Matched->getExprLoc(),
79 "do not use array subscript when the index is "
80 "not an integer constant expression; use gsl::at() "
82 if (!GslHeader.empty()) {
83 Diag << FixItHint::CreateInsertion(BaseRange.getBegin(),
"gsl::at(")
84 << FixItHint::CreateReplacement(
85 SourceRange(BaseRange.getEnd().getLocWithOffset(1),
86 IndexRange.getBegin().getLocWithOffset(-1)),
88 << FixItHint::CreateReplacement(Matched->getLocEnd(),
")");
90 Optional<FixItHint> Insertion = Inserter->CreateIncludeInsertion(
91 Result.SourceManager->getMainFileID(), GslHeader,
94 Diag << Insertion.getValue();
99 const auto *StdArrayDecl =
100 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"type");
106 if (Index.isSigned() && Index.isNegative()) {
107 diag(Matched->getExprLoc(),
108 "std::array<> index %0 is negative")
109 << Index.toString(10);
113 const TemplateArgumentList &TemplateArgs = StdArrayDecl->getTemplateArgs();
114 if (TemplateArgs.size() < 2)
117 const auto &SizeArg = TemplateArgs[1];
118 if (SizeArg.getKind() != TemplateArgument::Integral)
120 llvm::APInt ArraySize = SizeArg.getAsIntegral();
124 if (Index.getZExtValue() >= ArraySize.getZExtValue()) {
125 diag(Matched->getExprLoc(),
"std::array<> index %0 is past the end of the array "
126 "(which contains %1 elements)")
127 << Index.toString(10) << ArraySize.toString(10,
false);
LangOptions getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers 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...
std::unique_ptr< ast_matchers::MatchFinder > Finder
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.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.