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