10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/Frontend/CompilerInstance.h" 13 #include "clang/Lex/Preprocessor.h" 19 namespace cppcoreguidelines {
21 ProBoundsConstantArrayIndexCheck::ProBoundsConstantArrayIndexCheck(
23 :
ClangTidyCheck(Name, Context), GslHeader(Options.get(
"GslHeader",
"")),
24 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
25 Options.getLocalOrGlobal(
"IncludeStyle",
"llvm"))) {}
34 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
38 Inserter = llvm::make_unique<utils::IncludeInserter>(SM,
getLangOpts(),
40 PP->addPPCallbacks(Inserter->CreatePPCallbacks());
51 hasBase(ignoringImpCasts(hasType(constantArrayType().bind(
"type")))),
52 hasIndex(expr().bind(
"index")), unless(hasAncestor(isImplicit())))
58 hasOverloadedOperatorName(
"[]"),
60 0, hasType(cxxRecordDecl(hasName(
"::std::array")).bind(
"type"))),
61 hasArgument(1, expr().bind(
"index")))
67 const MatchFinder::MatchResult &
Result) {
68 const auto *Matched = Result.Nodes.getNodeAs<Expr>(
"expr");
69 const auto *IndexExpr = Result.Nodes.getNodeAs<Expr>(
"index");
71 if (IndexExpr->isValueDependent())
75 if (!IndexExpr->isIntegerConstantExpr(Index, *Result.Context,
nullptr,
77 SourceRange BaseRange;
78 if (
const auto *ArraySubscriptE = dyn_cast<ArraySubscriptExpr>(Matched))
79 BaseRange = ArraySubscriptE->getBase()->getSourceRange();
82 dyn_cast<CXXOperatorCallExpr>(Matched)->getArg(0)->getSourceRange();
83 SourceRange IndexRange = IndexExpr->getSourceRange();
85 auto Diag =
diag(Matched->getExprLoc(),
86 "do not use array subscript when the index is " 87 "not an integer constant expression; use gsl::at() " 89 if (!GslHeader.empty()) {
90 Diag << FixItHint::CreateInsertion(BaseRange.getBegin(),
"gsl::at(")
91 << FixItHint::CreateReplacement(
92 SourceRange(BaseRange.getEnd().getLocWithOffset(1),
93 IndexRange.getBegin().getLocWithOffset(-1)),
95 << FixItHint::CreateReplacement(Matched->getEndLoc(),
")");
97 Optional<FixItHint> Insertion = Inserter->CreateIncludeInsertion(
98 Result.SourceManager->getMainFileID(), GslHeader,
101 Diag << Insertion.getValue();
106 const auto *StdArrayDecl =
107 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"type");
113 if (Index.isSigned() && Index.isNegative()) {
114 diag(Matched->getExprLoc(),
"std::array<> index %0 is negative")
115 << Index.toString(10);
119 const TemplateArgumentList &TemplateArgs = StdArrayDecl->getTemplateArgs();
120 if (TemplateArgs.size() < 2)
123 const auto &SizeArg = TemplateArgs[1];
124 if (SizeArg.getKind() != TemplateArgument::Integral)
126 llvm::APInt ArraySize = SizeArg.getAsIntegral();
130 if (Index.getZExtValue() >= ArraySize.getZExtValue()) {
131 diag(Matched->getExprLoc(),
132 "std::array<> index %0 is past the end of the array " 133 "(which contains %1 elements)")
134 << 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...
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.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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.
const SymbolIndex * Index