clang-tools 8.0.0

SimplifySubscriptExprCheck.cpp
Go to the documentation of this file.
1 //===--- SimplifySubscriptExprCheck.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 "SimplifySubscriptExprCheck.h"
11 #include "../utils/OptionsUtils.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace readability {
20
21 static const char kDefaultTypes[] =
22 "::std::basic_string;::std::basic_string_view;::std::vector;::std::array";
23
24 SimplifySubscriptExprCheck::SimplifySubscriptExprCheck(
25 StringRef Name, ClangTidyContext *Context)
26 : ClangTidyCheck(Name, Context), Types(utils::options::parseStringList(
27 Options.get("Types", kDefaultTypes))) {
28 }
29
30 void SimplifySubscriptExprCheck::registerMatchers(MatchFinder *Finder) {
31 if (!getLangOpts().CPlusPlus)
32 return;
33
34 const auto TypesMatcher = hasUnqualifiedDesugaredType(
35 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
36 llvm::SmallVector<StringRef, 8>(Types.begin(), Types.end()))))));
37
38 Finder->addMatcher(
39 arraySubscriptExpr(hasBase(ignoringParenImpCasts(
40 cxxMemberCallExpr(
41 has(memberExpr().bind("member")),
42 on(hasType(qualType(
43 unless(anyOf(substTemplateTypeParmType(),
44 hasDescendant(substTemplateTypeParmType()))),
45 anyOf(TypesMatcher, pointerType(pointee(TypesMatcher)))))),
46 callee(namedDecl(hasName("data"))))
47 .bind("call")))),
48 this);
49 }
50
51 void SimplifySubscriptExprCheck::check(const MatchFinder::MatchResult &Result) {
52 const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
53 if (Result.Context->getSourceManager().isMacroBodyExpansion(
54 Call->getExprLoc()))
55 return;
56
57 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
58 auto DiagBuilder =
59 diag(Member->getMemberLoc(),
60 "accessing an element of the container does not require a call to "
61 "'data()'; did you mean to use 'operator[]'?");
62 if (Member->isArrow())
63 DiagBuilder << FixItHint::CreateInsertion(Member->getBeginLoc(), "(*")
64 << FixItHint::CreateInsertion(Member->getOperatorLoc(), ")");
65 DiagBuilder << FixItHint::CreateRemoval(
66 {Member->getOperatorLoc(), Call->getEndLoc()});
67 }
68
69 void SimplifySubscriptExprCheck::storeOptions(
70 ClangTidyOptions::OptionMap &Opts) {
71 Options.store(Opts, "Types", utils::options::serializeStringList(Types));
72 }
73
74 } // namespace readability
75 } // namespace tidy
76 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::tidy::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: ClangTidy.cpp:473
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:31
clang::tidy::ClangTidyCheck::getLangOpts
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
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:69
clang::tidy::readability::kDefaultTypes
static const char kDefaultTypes[]
Definition: SimplifySubscriptExprCheck.cpp:21
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:19
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
clang::tidy::readability::SimplifySubscriptExprCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SimplifySubscriptExprCheck.cpp:30
SimplifySubscriptExprCheck.h
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:98
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:100
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:51
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: ClangTidy.cpp:438