Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
3f57fca
Enforce SL.con.3: Add check to replace operator[] with at() on std co…
paulhdk Aug 4, 2025
b8bbd2c
EugeneZelenko's comments
sebwolf-de Apr 26, 2024
168fc45
Refactoring, but not finished yet
sebwolf-de May 24, 2024
9392bf6
Rename AvoidBoundsErrorsCheck to PreferAtOverSubscriptOperatorCheck
paulhdk Jun 10, 2024
68d7481
Add a user-customizable mechanism for excluding classes from the anal…
paulhdk Jun 10, 2024
6e77871
Update the documentation
paulhdk Jun 10, 2024
5b90a59
Add remaining tests requested by @PiotrZSL
paulhdk Jun 10, 2024
d1a1e6f
Remove unused and rename variables
paulhdk Jun 10, 2024
52dd31c
Update types in PreferAtOverSubscriptOperatorCheck::check()
paulhdk Jun 12, 2024
7256673
Fix length of underline in prefer-at-over-subscript-operator.rst
paulhdk Jun 12, 2024
1f7391d
Enforce 80 char column width in prefer-at-over-subscript-operator.rst
paulhdk Jun 12, 2024
e1d81a4
Synchronise prefer-at-over-subscript-operator.rst with ReleaseNotes.rst
paulhdk Jun 13, 2024
0dcff27
Move RUN lines to the top of prefer-at-over-subscript-operator.cpp
paulhdk Jun 17, 2024
71a3b57
Add an empty lien after each `code-block` in prefer-at-over-subscript…
paulhdk Jun 17, 2024
7fb8ed6
Use ofClass() instead of hasParent() in PreferAtOverSubscriptOperator…
paulhdk Jun 17, 2024
7625866
Use matchesAnyListedName() in matcher instead of explicit std::find()
paulhdk Jun 17, 2024
6f0a7c6
Match source range of expression in warnings emitted by PreferAtOverS…
paulhdk Jun 17, 2024
a52d03d
PreferAtOverSubscriptOpterator ->
paulhdk Jun 21, 2024
e38b2ae
Remove extra comment
paulhdk Jun 21, 2024
48b75dd
Mention bounds safety profile explicitly
paulhdk Jun 21, 2024
b8be8bf
Move paragraph about which rule/profile is being implemented above
paulhdk Jun 21, 2024
77eee28
Rename variables related to class exclusion mechanism
paulhdk Jun 21, 2024
10c6063
Remove redundant nullptr cast
paulhdk Jun 30, 2024
c1b0a0f
Refactor the check's matcher as per 5chmidti's suggestion
paulhdk Jun 30, 2024
def978b
Check that alternative is accessible in findAlternative() as per @5ch…
paulhdk Jun 30, 2024
eb755e3
Use upticks when diagnostic messages refer to code
paulhdk Jun 30, 2024
13bd78b
Make findAlternative() static
paulhdk Jun 30, 2024
a572f22
Move MatchedExpr definition closer to where it's first used in
paulhdk Jun 30, 2024
c1d504d
Synchronise comment in header file with ReleaseNotes and check
paulhdk Jun 30, 2024
e21e660
Reword reasoning re: class exclusion in pro-bounds-avoid-unchecked-co…
paulhdk Jul 8, 2024
ca06e1c
Replace array with container inpro-bounds-avoid-unchecked-container-a…
paulhdk Jul 8, 2024
287a659
Update unique_ptr example in pro-bounds-avoid-unchecked-container-acc…
paulhdk Jul 8, 2024
939f792
Remove outdated comment in ProBoundsAvoidUncheckedContainerAccess.cpp
paulhdk Jul 8, 2024
026c64b
Use TK_IgnoreUnlessSpelledInSource as traversal kind
paulhdk Jul 8, 2024
86e342c
Revert "Use TK_IgnoreUnlessSpelledInSource as traversal kind"
paulhdk Jul 25, 2024
a7c14e1
Add template test with std::map as an argument
paulhdk Aug 2, 2024
d4ac05d
Use getNameInfo().getSourceRange() instead of getSourceRange() when
paulhdk Aug 2, 2024
1e6c970
Replace use of Method->getNameInfo().getAsString() with Method->getNa…
paulhdk Aug 3, 2024
ee6c0aa
Add TODO comment re: findAlternative()'s capabilities in class
paulhdk Aug 8, 2024
feef504
Reduce size of SourceRange when printing warnings
paulhdk Aug 8, 2024
867dd4c
Add customizable fixit suggestions
paulhdk Aug 8, 2024
b4ef25d
Document customizable fixit suggestions
paulhdk Aug 8, 2024
12c3aa1
Test customizable fixit suggestions
paulhdk Aug 8, 2024
a48face
Update one-sentence description of `cppcoreguidelines-pro-bounds-avoi…
paulhdk Oct 26, 2024
e7a0e89
Format description of the `cppcoreguidelines-pro-bounds-avoid-uncheck…
paulhdk Oct 26, 2024
60a01be
Generalise description of excluded classes in `pro-bounds-avoid-unche…
paulhdk Oct 26, 2024
8d20083
s/bound-safe/bounds-safe
paulhdk Oct 26, 2024
4aabe8e
docs: remove whitespace
paulhdk Nov 4, 2024
df68ad3
Update clang-tools-extra/docs/ReleaseNotes.rst
paulhdk Nov 5, 2024
7fa4e81
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk Nov 5, 2024
02bddef
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk Nov 5, 2024
e897d10
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk Nov 5, 2024
7d4f6be
SubscriptExcludeClasses -> ExcludeClasses
paulhdk Feb 14, 2025
c22b899
SubscriptFixMode -> FixMode
paulhdk Feb 15, 2025
b322098
SubscriptFixFunction -> FixFunction
paulhdk Feb 15, 2025
dcd1935
Remove "Subscript" prefix for variable names
paulhdk Feb 15, 2025
0784a47
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk Feb 15, 2025
612eb35
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk Feb 15, 2025
a474092
Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsAvoidU…
paulhdk Feb 15, 2025
ea45352
Remove contradicting "but" in cppcoreguidelines/pro-bounds-avoid-unch…
paulhdk Feb 15, 2025
a523bf3
Only match subscript operators with a parameter list of size 0 or 1
paulhdk Feb 17, 2025
f02f22e
Simplify how user-specified excluded classes are stored
paulhdk Feb 17, 2025
2025953
Remove unused import
paulhdk May 8, 2025
226aa8d
Make FixMode "none" lowercase
paulhdk May 10, 2025
801ad8f
Separate C++23 tests
paulhdk May 11, 2025
4d1aa5a
Change semantics around excluded classes
paulhdk May 27, 2025
6d30c8d
Add tests for new "ExcludedClasses" semantics and split tests into
paulhdk May 27, 2025
cba734c
Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsAvoidU…
paulhdk May 27, 2025
a419064
Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsAvoidU…
paulhdk May 27, 2025
97aa6a4
Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsAvoidU…
paulhdk May 27, 2025
f509f6d
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk May 27, 2025
c5ec18a
s/flags/finds in docs
paulhdk May 27, 2025
023df9d
Mention default option at end of description in docs
paulhdk May 27, 2025
c233df3
Refactor `ExcludedClassesStr`
paulhdk May 27, 2025
ce215eb
Formatting
paulhdk May 27, 2025
0109ec4
Fix tests
paulhdk Jun 19, 2025
db38dc2
Initialise ProBoundsAvoidUncheckedContainerAccesses members in
paulhdk Jul 7, 2025
94aac08
docs: outline examples of safe alterantives to subscript operator
paulhdk Jul 7, 2025
c805a86
Reorder `Option.store`s and remove empty line
paulhdk Jul 8, 2025
eb9d841
Add FixFunctionEmptyArgs option
paulhdk Jul 8, 2025
b2d78fb
Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsAvoidU…
paulhdk Jul 8, 2025
1c06763
Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsAvoidU…
paulhdk Jul 8, 2025
de2b199
Format
paulhdk Jul 8, 2025
e18eeac
Use `%` formatting for diagnostics
paulhdk Jul 9, 2025
5ffcfdb
docs: point out default values of options at the end of their
paulhdk Jul 9, 2025
1d81006
Fix format strings
paulhdk Jul 9, 2025
e5d6c27
Excplitly check for `FixFunctionEmptyArgs` option being empty
paulhdk Jul 9, 2025
581b873
Use CHECK-MESSAGES-NOT in `FixFunctionEmptyArgs` tests
paulhdk Jul 9, 2025
a4306af
Print warning when FixFunctionEmptyArgs is explicitly set to "", but
paulhdk Jul 12, 2025
c512fdf
docs: reword paragraph about the STL containers which are excluded by…
paulhdk Jul 12, 2025
f23a48c
docs: use :option:... syntax in FixFunction documentation
paulhdk Jul 12, 2025
b70899d
docs: fix typo
paulhdk Jul 12, 2025
77256f7
s/accesses/access in check name
paulhdk Jul 12, 2025
f8fc495
Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsAvoidU…
paulhdk Jul 12, 2025
57e1cd0
Fix header comment alignment
paulhdk Jul 12, 2025
bd5fa97
docs: reword paragraph about excluded STL containers
paulhdk Jul 14, 2025
a453616
Update ProBoundsAvoidUncheckedContainerAccess.h
paulhdk Jul 15, 2025
743b42b
docs: add missing backticks
paulhdk Jul 15, 2025
d6ccbfc
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk Aug 4, 2025
9630952
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk Aug 4, 2025
83e138f
Update clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro…
paulhdk Aug 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_clang_library(clangTidyCppCoreGuidelinesModule STATIC
OwningMemoryCheck.cpp
PreferMemberInitializerCheck.cpp
ProBoundsArrayToPointerDecayCheck.cpp
ProBoundsAvoidUncheckedContainerAccess.cpp
ProBoundsConstantArrayIndexCheck.cpp
ProBoundsPointerArithmeticCheck.cpp
ProTypeConstCastCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "OwningMemoryCheck.h"
#include "PreferMemberInitializerCheck.h"
#include "ProBoundsArrayToPointerDecayCheck.h"
#include "ProBoundsAvoidUncheckedContainerAccess.h"
#include "ProBoundsConstantArrayIndexCheck.h"
#include "ProBoundsPointerArithmeticCheck.h"
#include "ProTypeConstCastCheck.h"
Expand Down Expand Up @@ -107,6 +108,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
"cppcoreguidelines-prefer-member-initializer");
CheckFactories.registerCheck<ProBoundsArrayToPointerDecayCheck>(
"cppcoreguidelines-pro-bounds-array-to-pointer-decay");
CheckFactories.registerCheck<ProBoundsAvoidUncheckedContainerAccess>(
"cppcoreguidelines-pro-bounds-avoid-unchecked-container-access");
CheckFactories.registerCheck<ProBoundsConstantArrayIndexCheck>(
"cppcoreguidelines-pro-bounds-constant-array-index");
CheckFactories.registerCheck<ProBoundsPointerArithmeticCheck>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
//===--- ProBoundsAvoidUncheckedContainerAccess.cpp - clang-tidy ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ProBoundsAvoidUncheckedContainerAccess.h"
#include "../utils/Matchers.h"
#include "../utils/OptionsUtils.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/ADT/StringRef.h"

using namespace clang::ast_matchers;

namespace clang::tidy::cppcoreguidelines {

static constexpr llvm::StringRef DefaultExclusionStr =
"::std::map;::std::unordered_map;::std::flat_map";

ProBoundsAvoidUncheckedContainerAccess::ProBoundsAvoidUncheckedContainerAccess(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
ExcludedClasses(utils::options::parseStringList(
Options.get("ExcludeClasses", DefaultExclusionStr))),
FixMode(Options.get("FixMode", None)),
FixFunction(Options.get("FixFunction", "gsl::at")),
FixFunctionEmptyArgs(Options.get("FixFunctionEmptyArgs", FixFunction)) {}

void ProBoundsAvoidUncheckedContainerAccess::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "ExcludeClasses",
utils::options::serializeStringList(ExcludedClasses));
Options.store(Opts, "FixMode", FixMode);
Options.store(Opts, "FixFunction", FixFunction);
Options.store(Opts, "FixFunctionEmptyArgs", FixFunctionEmptyArgs);
}

// TODO: if at() is defined in another class in the class hierarchy of the class
// that defines the operator[] we matched on, findAlternative() will not detect
// it.
static const CXXMethodDecl *
findAlternativeAt(const CXXMethodDecl *MatchedOperator) {
const CXXRecordDecl *Parent = MatchedOperator->getParent();
const QualType SubscriptThisObjType =
MatchedOperator->getFunctionObjectParameterReferenceType();

for (const CXXMethodDecl *Method : Parent->methods()) {
// Require 'Method' to be as accessible as 'MatchedOperator' or more
if (MatchedOperator->getAccess() < Method->getAccess())
continue;

if (MatchedOperator->isConst() != Method->isConst())
continue;

const QualType AtThisObjType =
Method->getFunctionObjectParameterReferenceType();
if (SubscriptThisObjType != AtThisObjType)
continue;

if (!Method->getNameInfo().getName().isIdentifier() ||
Method->getName() != "at")
continue;

const bool SameReturnType =
Method->getReturnType() == MatchedOperator->getReturnType();
if (!SameReturnType)
continue;

const bool SameNumberOfArguments =
Method->getNumParams() == MatchedOperator->getNumParams();
if (!SameNumberOfArguments)
continue;

for (unsigned ArgInd = 0; ArgInd < Method->getNumParams(); ArgInd++) {
const bool SameArgType =
Method->parameters()[ArgInd]->getOriginalType() ==
MatchedOperator->parameters()[ArgInd]->getOriginalType();
if (!SameArgType)
continue;
}

return Method;
}
return nullptr;
}

void ProBoundsAvoidUncheckedContainerAccess::registerMatchers(
MatchFinder *Finder) {
Finder->addMatcher(
mapAnyOf(cxxOperatorCallExpr, cxxMemberCallExpr)
.with(callee(
cxxMethodDecl(
hasOverloadedOperatorName("[]"),
anyOf(parameterCountIs(0), parameterCountIs(1)),
unless(matchers::matchesAnyListedName(ExcludedClasses)))
.bind("operator")))
.bind("caller"),
this);
}

void ProBoundsAvoidUncheckedContainerAccess::check(
const MatchFinder::MatchResult &Result) {

const auto *MatchedExpr = Result.Nodes.getNodeAs<CallExpr>("caller");

if (FixMode == None) {
diag(MatchedExpr->getCallee()->getBeginLoc(),
"possibly unsafe 'operator[]', consider bounds-safe alternatives")
<< MatchedExpr->getCallee()->getSourceRange();
return;
}

if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(MatchedExpr)) {
// Case: a[i]
const auto LeftBracket = SourceRange(OCE->getCallee()->getBeginLoc(),
OCE->getCallee()->getBeginLoc());
const auto RightBracket =
SourceRange(OCE->getOperatorLoc(), OCE->getOperatorLoc());

if (FixMode == At) {
// Case: a[i] => a.at(i)
const auto *MatchedOperator =
Result.Nodes.getNodeAs<CXXMethodDecl>("operator");
const CXXMethodDecl *Alternative = findAlternativeAt(MatchedOperator);

if (!Alternative) {
diag(MatchedExpr->getCallee()->getBeginLoc(),
"possibly unsafe 'operator[]', consider "
"bounds-safe alternatives")
<< MatchedExpr->getCallee()->getSourceRange();
return;
}

diag(MatchedExpr->getCallee()->getBeginLoc(),
"possibly unsafe 'operator[]', consider "
"bounds-safe alternative 'at()'")
<< MatchedExpr->getCallee()->getSourceRange()
<< FixItHint::CreateReplacement(LeftBracket, ".at(")
<< FixItHint::CreateReplacement(RightBracket, ")");

diag(Alternative->getBeginLoc(), "viable 'at()' is defined here",
DiagnosticIDs::Note)
<< Alternative->getNameInfo().getSourceRange();

} else if (FixMode == Function) {
// Case: a[i] => f(a, i)
//
// Since C++23, the subscript operator may also be called without an
// argument, which makes the following distinction necessary
const bool EmptySubscript =
MatchedExpr->getDirectCallee()->getNumParams() == 0;

if (EmptySubscript) {
auto D = diag(MatchedExpr->getCallee()->getBeginLoc(),
"possibly unsafe 'operator[]'%select{, use safe "
"function '%1() instead|}0")
<< FixFunctionEmptyArgs.empty() << FixFunctionEmptyArgs.str()
<< MatchedExpr->getCallee()->getSourceRange();
if (!FixFunctionEmptyArgs.empty()) {
D << FixItHint::CreateInsertion(OCE->getArg(0)->getBeginLoc(),
FixFunctionEmptyArgs.str() + "(")
<< FixItHint::CreateRemoval(LeftBracket)
<< FixItHint::CreateReplacement(RightBracket, ")");
}
} else {
diag(MatchedExpr->getCallee()->getBeginLoc(),
"possibly unsafe 'operator[]', use safe function '%0()' instead")
<< FixFunction.str() << MatchedExpr->getCallee()->getSourceRange()
<< FixItHint::CreateInsertion(OCE->getArg(0)->getBeginLoc(),
FixFunction.str() + "(")
<< FixItHint::CreateReplacement(LeftBracket, ", ")
<< FixItHint::CreateReplacement(RightBracket, ")");
}
}
} else if (const auto *MCE = dyn_cast<CXXMemberCallExpr>(MatchedExpr)) {
// Case: a.operator[](i) or a->operator[](i)
const auto *Callee = dyn_cast<MemberExpr>(MCE->getCallee());

if (FixMode == At) {
// Cases: a.operator[](i) => a.at(i) and a->operator[](i) => a->at(i)

const auto *MatchedOperator =
Result.Nodes.getNodeAs<CXXMethodDecl>("operator");

const CXXMethodDecl *Alternative = findAlternativeAt(MatchedOperator);
if (!Alternative) {
diag(Callee->getBeginLoc(), "possibly unsafe 'operator[]', consider "
"bounds-safe alternative 'at()'")
<< Callee->getSourceRange();
return;
}
diag(MatchedExpr->getCallee()->getBeginLoc(),
"possibly unsafe 'operator[]', consider "
"bounds-safe alternative 'at()'")
<< FixItHint::CreateReplacement(
SourceRange(Callee->getMemberLoc(), Callee->getEndLoc()),
"at");

diag(Alternative->getBeginLoc(), "viable 'at()' defined here",
DiagnosticIDs::Note)
<< Alternative->getNameInfo().getSourceRange();

} else if (FixMode == Function) {
// Cases: a.operator[](i) => f(a, i) and a->operator[](i) => f(*a, i)
const auto *Callee = dyn_cast<MemberExpr>(MCE->getCallee());

const bool EmptySubscript =
MCE->getMethodDecl()->getNumNonObjectParams() == 0;

std::string BeginInsertion =
(EmptySubscript ? FixFunctionEmptyArgs.str() : FixFunction.str()) +
"(";

if (Callee->isArrow())
BeginInsertion += "*";

// Since C++23, the subscript operator may also be called without an
// argument, which makes the following distinction necessary
if (EmptySubscript) {
auto D = diag(MatchedExpr->getCallee()->getBeginLoc(),
"possibly unsafe 'operator[]'%select{, use safe "
"function '%1()' instead|}0")
<< FixFunctionEmptyArgs.empty() << FixFunctionEmptyArgs.str()
<< Callee->getSourceRange();

if (!FixFunctionEmptyArgs.empty()) {
D << FixItHint::CreateInsertion(MatchedExpr->getBeginLoc(),
BeginInsertion)
<< FixItHint::CreateRemoval(
SourceRange(Callee->getOperatorLoc(),
MCE->getRParenLoc().getLocWithOffset(-1)));
}
} else {
diag(Callee->getBeginLoc(),
"possibly unsafe 'operator[]', use safe function '%0()' instead")
<< FixFunction.str() << Callee->getSourceRange()
<< FixItHint::CreateInsertion(MatchedExpr->getBeginLoc(),
BeginInsertion)
<< FixItHint::CreateReplacement(
SourceRange(
Callee->getOperatorLoc(),
MCE->getArg(0)->getBeginLoc().getLocWithOffset(-1)),
", ");
}
}
}
}

} // namespace clang::tidy::cppcoreguidelines

namespace clang::tidy {
using P = cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccess;

llvm::ArrayRef<std::pair<P::FixModes, StringRef>>
OptionEnumMapping<P::FixModes>::getEnumMapping() {
static constexpr std::pair<P::FixModes, StringRef> Mapping[] = {
{P::None, "none"}, {P::At, "at"}, {P::Function, "function"}};
return {Mapping};
}
} // namespace clang::tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===--- ProBoundsAvoidUncheckedContainerAccess.h - clang-tidy --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_BOUNDS_AVOID_UNCHECKED_CONTAINER_ACCESS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_BOUNDS_AVOID_UNCHECKED_CONTAINER_ACCESS_H

#include "../ClangTidyCheck.h"

namespace clang::tidy::cppcoreguidelines {

/// Flags calls to operator[] in STL containers and suggests replacing it with
/// safe alternatives.
///
/// See
/// https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon3-avoid-bounds-errors
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/pro-bounds-avoid-unchecked-container-access.html
class ProBoundsAvoidUncheckedContainerAccess : public ClangTidyCheck {
public:
ProBoundsAvoidUncheckedContainerAccess(StringRef Name,
ClangTidyContext *Context);
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus;
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;

enum FixModes { None, At, Function };

private:
// A list of class names that are excluded from the warning
std::vector<llvm::StringRef> ExcludedClasses;
// Setting which fix to suggest
FixModes FixMode;
llvm::StringRef FixFunction;
llvm::StringRef FixFunctionEmptyArgs;
};
} // namespace clang::tidy::cppcoreguidelines

namespace clang::tidy {
template <>
struct OptionEnumMapping<
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccess::FixModes> {
static ArrayRef<std::pair<
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccess::FixModes,
StringRef>>
getEnumMapping();
};
} // namespace clang::tidy
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_BOUNDS_AVOID_UNCHECKED_CONTAINER_ACCESS_H
7 changes: 7 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ New checks
Detects default initialization (to 0) of variables with ``enum`` type where
the enum has no enumerator with value of 0.

- New :doc:`cppcoreguidelines-pro-bounds-avoid-unchecked-container-access
<clang-tidy/checks/cppcoreguidelines/pro-bounds-avoid-unchecked-container-access>`
check.

Finds calls to ``operator[]`` in STL containers and suggests replacing them
with safe alternatives.

- New :doc:`llvm-mlir-op-builder
<clang-tidy/checks/llvm/use-new-mlir-op-builder>` check.

Expand Down
Loading