comparison clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 79ff65ed7e25
children 1f2b6ac9f198
comparison
equal deleted inserted replaced
232:70dce7da266c 236:c4bab56944e8
19 namespace readability { 19 namespace readability {
20 20
21 const char DefaultStringNames[] = 21 const char DefaultStringNames[] =
22 "::std::basic_string_view;::std::basic_string"; 22 "::std::basic_string_view;::std::basic_string";
23 23
24 static ast_matchers::internal::Matcher<NamedDecl> 24 static std::vector<StringRef> removeNamespaces(ArrayRef<StringRef> Names) {
25 hasAnyNameStdString(std::vector<std::string> Names) { 25 std::vector<StringRef> Result;
26 return ast_matchers::internal::Matcher<NamedDecl>(
27 new ast_matchers::internal::HasNameMatcher(std::move(Names)));
28 }
29
30 static std::vector<std::string>
31 removeNamespaces(const std::vector<std::string> &Names) {
32 std::vector<std::string> Result;
33 Result.reserve(Names.size()); 26 Result.reserve(Names.size());
34 for (const std::string &Name : Names) { 27 for (StringRef Name : Names) {
35 std::string::size_type ColonPos = Name.rfind(':'); 28 StringRef::size_type ColonPos = Name.rfind(':');
36 Result.push_back( 29 Result.push_back(
37 Name.substr(ColonPos == std::string::npos ? 0 : ColonPos + 1)); 30 Name.drop_front(ColonPos == StringRef::npos ? 0 : ColonPos + 1));
38 } 31 }
39 return Result; 32 return Result;
40 } 33 }
41 34
42 static const CXXConstructExpr * 35 static const CXXConstructExpr *
70 void RedundantStringInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 63 void RedundantStringInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
71 Options.store(Opts, "StringNames", DefaultStringNames); 64 Options.store(Opts, "StringNames", DefaultStringNames);
72 } 65 }
73 66
74 void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) { 67 void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
75 const auto HasStringTypeName = hasAnyNameStdString(StringNames); 68 const auto HasStringTypeName = hasAnyName(StringNames);
76 const auto HasStringCtorName = 69 const auto HasStringCtorName = hasAnyName(removeNamespaces(StringNames));
77 hasAnyNameStdString(removeNamespaces(StringNames));
78 70
79 // Match string constructor. 71 // Match string constructor.
80 const auto StringConstructorExpr = expr( 72 const auto StringConstructorExpr = expr(
81 anyOf(cxxConstructExpr(argumentCountIs(1), 73 anyOf(cxxConstructExpr(argumentCountIs(1),
82 hasDeclaration(cxxMethodDecl(HasStringCtorName))), 74 hasDeclaration(cxxMethodDecl(HasStringCtorName))),