|
| 1 | +//===--- ProBoundsConstantArrayIndexCheck.cpp - clang-tidy-----------------===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "ProBoundsConstantArrayIndexCheck.h" |
| 11 | +#include "clang/AST/ASTContext.h" |
| 12 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | +#include "clang/Frontend/CompilerInstance.h" |
| 14 | +#include "clang/Lex/Preprocessor.h" |
| 15 | + |
| 16 | +using namespace clang::ast_matchers; |
| 17 | + |
| 18 | +namespace clang { |
| 19 | +namespace tidy { |
| 20 | + |
| 21 | +ProBoundsConstantArrayIndexCheck::ProBoundsConstantArrayIndexCheck( |
| 22 | + StringRef Name, ClangTidyContext *Context) |
| 23 | + : ClangTidyCheck(Name, Context), GslHeader(Options.get("GslHeader", "")), |
| 24 | + IncludeStyle(IncludeSorter::parseIncludeStyle( |
| 25 | + Options.get("IncludeStyle", "llvm"))) {} |
| 26 | + |
| 27 | +void ProBoundsConstantArrayIndexCheck::storeOptions( |
| 28 | + ClangTidyOptions::OptionMap &Opts) { |
| 29 | + Options.store(Opts, "GslHeader", GslHeader); |
| 30 | +} |
| 31 | + |
| 32 | +void ProBoundsConstantArrayIndexCheck::registerPPCallbacks( |
| 33 | + CompilerInstance &Compiler) { |
| 34 | + if (!getLangOpts().CPlusPlus) |
| 35 | + return; |
| 36 | + |
| 37 | + Inserter.reset(new IncludeInserter(Compiler.getSourceManager(), |
| 38 | + Compiler.getLangOpts(), IncludeStyle)); |
| 39 | + Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); |
| 40 | +} |
| 41 | + |
| 42 | +void ProBoundsConstantArrayIndexCheck::registerMatchers(MatchFinder *Finder) { |
| 43 | + if (!getLangOpts().CPlusPlus) |
| 44 | + return; |
| 45 | + |
| 46 | + Finder->addMatcher(arraySubscriptExpr(hasBase(ignoringImpCasts(hasType( |
| 47 | + constantArrayType().bind("type")))), |
| 48 | + hasIndex(expr().bind("index"))) |
| 49 | + .bind("expr"), |
| 50 | + this); |
| 51 | + |
| 52 | + Finder->addMatcher( |
| 53 | + cxxOperatorCallExpr( |
| 54 | + hasOverloadedOperatorName("[]"), |
| 55 | + hasArgument( |
| 56 | + 0, hasType(cxxRecordDecl(hasName("::std::array")).bind("type"))), |
| 57 | + hasArgument(1, expr().bind("index"))) |
| 58 | + .bind("expr"), |
| 59 | + this); |
| 60 | +} |
| 61 | + |
| 62 | +void ProBoundsConstantArrayIndexCheck::check( |
| 63 | + const MatchFinder::MatchResult &Result) { |
| 64 | + const auto *Matched = Result.Nodes.getNodeAs<Expr>("expr"); |
| 65 | + const auto *IndexExpr = Result.Nodes.getNodeAs<Expr>("index"); |
| 66 | + llvm::APSInt Index; |
| 67 | + if (!IndexExpr->isIntegerConstantExpr(Index, *Result.Context, nullptr, |
| 68 | + /*isEvaluated=*/true)) { |
| 69 | + SourceRange BaseRange; |
| 70 | + if (const auto *ArraySubscriptE = dyn_cast<ArraySubscriptExpr>(Matched)) |
| 71 | + BaseRange = ArraySubscriptE->getBase()->getSourceRange(); |
| 72 | + else |
| 73 | + BaseRange = |
| 74 | + dyn_cast<CXXOperatorCallExpr>(Matched)->getArg(0)->getSourceRange(); |
| 75 | + SourceRange IndexRange = IndexExpr->getSourceRange(); |
| 76 | + |
| 77 | + auto Diag = diag(Matched->getExprLoc(), |
| 78 | + "do not use array subscript when the index is " |
| 79 | + "not a compile-time constant; use gsl::at() " |
| 80 | + "instead"); |
| 81 | + if (!GslHeader.empty()) { |
| 82 | + Diag << FixItHint::CreateInsertion(BaseRange.getBegin(), "gsl::at(") |
| 83 | + << FixItHint::CreateReplacement( |
| 84 | + SourceRange(BaseRange.getEnd().getLocWithOffset(1), |
| 85 | + IndexRange.getBegin().getLocWithOffset(-1)), |
| 86 | + ", ") |
| 87 | + << FixItHint::CreateReplacement(Matched->getLocEnd(), ")"); |
| 88 | + |
| 89 | + auto Insertion = Inserter->CreateIncludeInsertion( |
| 90 | + Result.SourceManager->getMainFileID(), GslHeader, |
| 91 | + /*IsAngled=*/false); |
| 92 | + if (Insertion.hasValue()) |
| 93 | + Diag << Insertion.getValue(); |
| 94 | + } |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const auto *StdArrayDecl = |
| 99 | + Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("type"); |
| 100 | + |
| 101 | + // For static arrays, this is handled in clang-diagnostic-array-bounds. |
| 102 | + if (!StdArrayDecl) |
| 103 | + return; |
| 104 | + |
| 105 | + if (Index.isSigned() && Index.isNegative()) { |
| 106 | + diag(Matched->getExprLoc(), |
| 107 | + "std::array<> index %0 is before the beginning of the array") |
| 108 | + << Index.toString(10); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + const auto &TemplateArgs = StdArrayDecl->getTemplateArgs(); |
| 113 | + if (TemplateArgs.size() < 2) |
| 114 | + return; |
| 115 | + // First template arg of std::array is the type, second arg is the size. |
| 116 | + const auto &SizeArg = TemplateArgs[1]; |
| 117 | + if (SizeArg.getKind() != TemplateArgument::Integral) |
| 118 | + return; |
| 119 | + llvm::APInt ArraySize = SizeArg.getAsIntegral(); |
| 120 | + |
| 121 | + // Get uint64_t values, because different bitwidths would lead to an assertion |
| 122 | + // in APInt::uge. |
| 123 | + if (Index.getZExtValue() >= ArraySize.getZExtValue()) { |
| 124 | + diag(Matched->getExprLoc(), "std::array<> index %0 is past the end of the array " |
| 125 | + "(which contains %1 elements)") |
| 126 | + << Index.toString(10) << ArraySize.toString(10, false); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace tidy |
| 131 | +} // namespace clang |
0 commit comments