clang-tools  14.0.0git
SimplifySubscriptExprCheck.cpp
Go to the documentation of this file.
1 //===--- SimplifySubscriptExprCheck.cpp - clang-tidy-----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "../utils/OptionsUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace readability {
19 
20 static const char KDefaultTypes[] =
21  "::std::basic_string;::std::basic_string_view;::std::vector;::std::array";
22 
23 SimplifySubscriptExprCheck::SimplifySubscriptExprCheck(
24  StringRef Name, ClangTidyContext *Context)
25  : ClangTidyCheck(Name, Context), Types(utils::options::parseStringList(
26  Options.get("Types", KDefaultTypes))) {
27 }
28 
30  const auto TypesMatcher = hasUnqualifiedDesugaredType(
31  recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
32  llvm::SmallVector<StringRef, 8>(Types.begin(), Types.end()))))));
33 
34  Finder->addMatcher(
35  arraySubscriptExpr(hasBase(
36  cxxMemberCallExpr(
37  has(memberExpr().bind("member")),
38  on(hasType(qualType(
39  unless(anyOf(substTemplateTypeParmType(),
40  hasDescendant(substTemplateTypeParmType()))),
41  anyOf(TypesMatcher, pointerType(pointee(TypesMatcher)))))),
42  callee(namedDecl(hasName("data"))))
43  .bind("call"))),
44  this);
45 }
46 
47 void SimplifySubscriptExprCheck::check(const MatchFinder::MatchResult &Result) {
48  const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
49  if (Result.Context->getSourceManager().isMacroBodyExpansion(
50  Call->getExprLoc()))
51  return;
52 
53  const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
54  auto DiagBuilder =
55  diag(Member->getMemberLoc(),
56  "accessing an element of the container does not require a call to "
57  "'data()'; did you mean to use 'operator[]'?");
58  if (Member->isArrow())
59  DiagBuilder << FixItHint::CreateInsertion(Member->getBeginLoc(), "(*")
60  << FixItHint::CreateInsertion(Member->getOperatorLoc(), ")");
61  DiagBuilder << FixItHint::CreateRemoval(
62  {Member->getOperatorLoc(), Call->getEndLoc()});
63 }
64 
67  Options.store(Opts, "Types", utils::options::serializeStringList(Types));
68 }
69 
70 } // namespace readability
71 } // namespace tidy
72 } // namespace clang
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::readability::SimplifySubscriptExprCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SimplifySubscriptExprCheck.cpp:65
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::utils::options::serializeStringList
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Definition: OptionsUtils.cpp:30
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:18
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:71
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang::tidy::readability::SimplifySubscriptExprCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SimplifySubscriptExprCheck.cpp:29
SimplifySubscriptExprCheck.h
clang::tidy::readability::KDefaultTypes
static const char KDefaultTypes[]
Definition: SimplifySubscriptExprCheck.cpp:20
clang::tidy::readability::SimplifySubscriptExprCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SimplifySubscriptExprCheck.cpp:47
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:120