summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
diff options
context:
space:
mode:
authorMitchell Balan <mitchell@stellarscience.com>2019-11-15 16:42:43 -0500
committerMitchell Balan <mitchell@stellarscience.com>2019-11-15 16:42:54 -0500
commit96fbc32cb9ea23b1e7e3ff6906ec3ccda9500982 (patch)
tree3c4d3be51ca45338ac36ecca6c331a976de70965 /clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
parent51a0a4e355d4fc18b1b00149ea08786de52ccbe9 (diff)
downloadbcm5719-llvm-96fbc32cb9ea23b1e7e3ff6906ec3ccda9500982.tar.gz
bcm5719-llvm-96fbc32cb9ea23b1e7e3ff6906ec3ccda9500982.zip
[clang-tidy] Give readability-redundant-string-init a customizable list of string types to fix
Summary: This patch adds a feature requested in https://reviews.llvm.org/D69238 to enable `readability-redundant-string-init` to take a list of strings to apply the fix to rather than hard-coding `basic_string`. It adds a `StringNames` option of semicolon-delimited names of string classes to which to apply this fix. Tests ensure this works with test class out::TestString as well as std::string and std::wstring as before. It should be applicable to llvm::StringRef, QString, etc. Reviewers: MyDeveloperDay, aaron.ballman, hokein, alexfh, JonasToth, gribozavr2 Patch by: poelmanc Subscribers: gribozavr2, xazax.hun, Eugene.Zelenko, cfe-commits Tags: #clang-tools-extra, #clang Differential Revision: https://reviews.llvm.org/D69548
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp43
1 files changed, 34 insertions, 9 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
index a140e17fabf..6bf0edb7231 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
@@ -8,6 +8,7 @@
#include "RedundantStringInitCheck.h"
#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
@@ -17,19 +18,43 @@ namespace clang {
namespace tidy {
namespace readability {
+const char DefaultStringNames[] = "::std::basic_string";
+
+RedundantStringInitCheck::RedundantStringInitCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ StringNames(utils::options::parseStringList(
+ Options.get("StringNames", DefaultStringNames))) {}
+
+void RedundantStringInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "StringNames", DefaultStringNames);
+}
+
void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus)
return;
+ const auto hasStringTypeName = hasAnyName(
+ SmallVector<StringRef, 3>(StringNames.begin(), StringNames.end()));
+
+ // Version of StringNames with namespaces removed
+ std::vector<std::string> stringNamesNoNamespace;
+ for (const std::string &name : StringNames) {
+ std::string::size_type colonPos = name.rfind(':');
+ stringNamesNoNamespace.push_back(
+ name.substr(colonPos == std::string::npos ? 0 : colonPos + 1));
+ }
+ const auto hasStringCtorName = hasAnyName(SmallVector<StringRef, 3>(
+ stringNamesNoNamespace.begin(), stringNamesNoNamespace.end()));
// Match string constructor.
- const auto StringConstructorExpr = expr(anyOf(
- cxxConstructExpr(argumentCountIs(1),
- hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
- // If present, the second argument is the alloc object which must not
- // be present explicitly.
- cxxConstructExpr(argumentCountIs(2),
- hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
- hasArgument(1, cxxDefaultArgExpr()))));
+ const auto StringConstructorExpr = expr(
+ anyOf(cxxConstructExpr(argumentCountIs(1),
+ hasDeclaration(cxxMethodDecl(hasStringCtorName))),
+ // If present, the second argument is the alloc object which must
+ // not be present explicitly.
+ cxxConstructExpr(argumentCountIs(2),
+ hasDeclaration(cxxMethodDecl(hasStringCtorName)),
+ hasArgument(1, cxxDefaultArgExpr()))));
// Match a string constructor expression with an empty string literal.
const auto EmptyStringCtorExpr = cxxConstructExpr(
@@ -48,7 +73,7 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
namedDecl(
varDecl(
hasType(hasUnqualifiedDesugaredType(recordType(
- hasDeclaration(cxxRecordDecl(hasName("basic_string")))))),
+ hasDeclaration(cxxRecordDecl(hasStringTypeName))))),
hasInitializer(expr(ignoringImplicit(anyOf(
EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries)))))
.bind("vardecl"),
OpenPOWER on IntegriCloud