annotate clang-tools-extra/clang-tidy/readability/SimplifySubscriptExprCheck.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- SimplifySubscriptExprCheck.cpp - clang-tidy-----------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "SimplifySubscriptExprCheck.h"
anatofuz
parents:
diff changeset
10 #include "../utils/OptionsUtils.h"
anatofuz
parents:
diff changeset
11 #include "clang/AST/ASTContext.h"
anatofuz
parents:
diff changeset
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 using namespace clang::ast_matchers;
anatofuz
parents:
diff changeset
15
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
16 namespace clang::tidy::readability {
150
anatofuz
parents:
diff changeset
17
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
18 static const char KDefaultTypes[] =
150
anatofuz
parents:
diff changeset
19 "::std::basic_string;::std::basic_string_view;::std::vector;::std::array";
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 SimplifySubscriptExprCheck::SimplifySubscriptExprCheck(
anatofuz
parents:
diff changeset
22 StringRef Name, ClangTidyContext *Context)
anatofuz
parents:
diff changeset
23 : ClangTidyCheck(Name, Context), Types(utils::options::parseStringList(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
24 Options.get("Types", KDefaultTypes))) {
150
anatofuz
parents:
diff changeset
25 }
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 void SimplifySubscriptExprCheck::registerMatchers(MatchFinder *Finder) {
anatofuz
parents:
diff changeset
28 const auto TypesMatcher = hasUnqualifiedDesugaredType(
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
29 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(Types)))));
150
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 Finder->addMatcher(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
32 arraySubscriptExpr(hasBase(
150
anatofuz
parents:
diff changeset
33 cxxMemberCallExpr(
anatofuz
parents:
diff changeset
34 has(memberExpr().bind("member")),
anatofuz
parents:
diff changeset
35 on(hasType(qualType(
anatofuz
parents:
diff changeset
36 unless(anyOf(substTemplateTypeParmType(),
anatofuz
parents:
diff changeset
37 hasDescendant(substTemplateTypeParmType()))),
anatofuz
parents:
diff changeset
38 anyOf(TypesMatcher, pointerType(pointee(TypesMatcher)))))),
anatofuz
parents:
diff changeset
39 callee(namedDecl(hasName("data"))))
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
40 .bind("call"))),
150
anatofuz
parents:
diff changeset
41 this);
anatofuz
parents:
diff changeset
42 }
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 void SimplifySubscriptExprCheck::check(const MatchFinder::MatchResult &Result) {
anatofuz
parents:
diff changeset
45 const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
anatofuz
parents:
diff changeset
46 if (Result.Context->getSourceManager().isMacroBodyExpansion(
anatofuz
parents:
diff changeset
47 Call->getExprLoc()))
anatofuz
parents:
diff changeset
48 return;
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
anatofuz
parents:
diff changeset
51 auto DiagBuilder =
anatofuz
parents:
diff changeset
52 diag(Member->getMemberLoc(),
anatofuz
parents:
diff changeset
53 "accessing an element of the container does not require a call to "
anatofuz
parents:
diff changeset
54 "'data()'; did you mean to use 'operator[]'?");
anatofuz
parents:
diff changeset
55 if (Member->isArrow())
anatofuz
parents:
diff changeset
56 DiagBuilder << FixItHint::CreateInsertion(Member->getBeginLoc(), "(*")
anatofuz
parents:
diff changeset
57 << FixItHint::CreateInsertion(Member->getOperatorLoc(), ")");
anatofuz
parents:
diff changeset
58 DiagBuilder << FixItHint::CreateRemoval(
anatofuz
parents:
diff changeset
59 {Member->getOperatorLoc(), Call->getEndLoc()});
anatofuz
parents:
diff changeset
60 }
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 void SimplifySubscriptExprCheck::storeOptions(
anatofuz
parents:
diff changeset
63 ClangTidyOptions::OptionMap &Opts) {
anatofuz
parents:
diff changeset
64 Options.store(Opts, "Types", utils::options::serializeStringList(Types));
anatofuz
parents:
diff changeset
65 }
anatofuz
parents:
diff changeset
66
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
67 } // namespace clang::tidy::readability