clang-tools 10.0.0

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
9 #include "SimplifySubscriptExprCheck.h"
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
29 void SimplifySubscriptExprCheck::registerMatchers(MatchFinder *Finder) {
30 if (!getLangOpts().CPlusPlus)
31 return;
32
33 const auto TypesMatcher = hasUnqualifiedDesugaredType(
34 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
35 llvm::SmallVector<StringRef, 8>(Types.begin(), Types.end()))))));
36
37 Finder->addMatcher(
38 arraySubscriptExpr(hasBase(ignoringParenImpCasts(
39 cxxMemberCallExpr(
40 has(memberExpr().bind("member")),
41 on(hasType(qualType(
42 unless(anyOf(substTemplateTypeParmType(),
43 hasDescendant(substTemplateTypeParmType()))),
44 anyOf(TypesMatcher, pointerType(pointee(TypesMatcher)))))),
45 callee(namedDecl(hasName("data"))))
46 .bind("call")))),
47 this);
48 }
49
50 void SimplifySubscriptExprCheck::check(const MatchFinder::MatchResult &Result) {
51 const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
52 if (Result.Context->getSourceManager().isMacroBodyExpansion(
53 Call->getExprLoc()))
54 return;
55
56 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
57 auto DiagBuilder =
58 diag(Member->getMemberLoc(),
59 "accessing an element of the container does not require a call to "
60 "'data()'; did you mean to use 'operator[]'?");
61 if (Member->isArrow())
62 DiagBuilder << FixItHint::CreateInsertion(Member->getBeginLoc(), "(*")
63 << FixItHint::CreateInsertion(Member->getOperatorLoc(), ")");
64 DiagBuilder << FixItHint::CreateRemoval(
65 {Member->getOperatorLoc(), Call->getEndLoc()});
66 }
67
68 void SimplifySubscriptExprCheck::storeOptions(
69 ClangTidyOptions::OptionMap &Opts) {
70 Options.store(Opts, "Types", utils::options::serializeStringList(Types));
71 }
72
73 } // namespace readability
74 } // namespace tidy
75 } // namespace clang
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::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:68
clang::tidy::readability::kDefaultTypes
static const char kDefaultTypes[]
Definition: SimplifySubscriptExprCheck.cpp:20
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
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:184
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::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:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
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:50
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21