10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
21 "::std::basic_string_view;::std::basic_string";
24 std::vector<StringRef> Result;
25 Result.reserve(Names.size());
26 for (StringRef
Name : Names) {
27 StringRef::size_type ColonPos =
Name.rfind(
':');
29 Name.drop_front(ColonPos == StringRef::npos ? 0 : ColonPos + 1));
34static const CXXConstructExpr *
36 const Expr *InitExpr = CtorInit.getInit();
37 if (
const auto *CleanUpExpr = dyn_cast<ExprWithCleanups>(InitExpr))
38 InitExpr = CleanUpExpr->getSubExpr();
39 return dyn_cast<CXXConstructExpr>(InitExpr);
42static std::optional<SourceRange>
45 for (
const Expr *Arg : Construct.arguments()) {
47 B = Arg->getBeginLoc();
48 if (Arg->getEndLoc().isValid())
51 if (B.isInvalid() ||
E.isInvalid())
53 return SourceRange(B,
E);
59 StringNames(utils::options::parseStringList(
67 const auto HasStringTypeName = hasAnyName(StringNames);
71 const auto StringConstructorExpr = expr(
72 anyOf(cxxConstructExpr(argumentCountIs(1),
73 hasDeclaration(cxxMethodDecl(HasStringCtorName))),
76 cxxConstructExpr(argumentCountIs(2),
77 hasDeclaration(cxxMethodDecl(HasStringCtorName)),
78 hasArgument(1, cxxDefaultArgExpr()))));
81 const auto EmptyStringCtorExpr = cxxConstructExpr(
82 StringConstructorExpr,
83 hasArgument(0, ignoringParenImpCasts(stringLiteral(hasSize(0)))));
85 const auto EmptyStringCtorExprWithTemporaries =
86 cxxConstructExpr(StringConstructorExpr,
87 hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));
89 const auto StringType = hasType(hasUnqualifiedDesugaredType(
90 recordType(hasDeclaration(cxxRecordDecl(HasStringTypeName)))));
91 const auto EmptyStringInit = traverse(
92 TK_AsIs, expr(ignoringImplicit(anyOf(
93 EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries))));
101 namedDecl(varDecl(StringType, hasInitializer(EmptyStringInit))
103 unless(parmVarDecl()))),
107 namedDecl(fieldDecl(StringType, hasInClassInitializer(EmptyStringInit))
117 forField(allOf(StringType, optionally(hasInClassInitializer(
118 EmptyStringInit.bind(
"empty_init"))))),
119 withInitializer(EmptyStringInit))
125 if (
const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>(
"vardecl")) {
128 SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
129 diag(VDecl->getLocation(),
"redundant string initialization")
130 << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
132 if (
const auto *FDecl = Result.Nodes.getNodeAs<FieldDecl>(
"fieldDecl")) {
135 SourceRange ReplaceRange(FDecl->getLocation(), FDecl->getEndLoc());
136 diag(FDecl->getLocation(),
"redundant string initialization")
137 << FixItHint::CreateReplacement(ReplaceRange, FDecl->getName());
139 if (
const auto *CtorInit =
140 Result.Nodes.getNodeAs<CXXCtorInitializer>(
"ctorInit")) {
141 if (
const FieldDecl *Member = CtorInit->getMember()) {
142 if (!Member->hasInClassInitializer() ||
143 Result.Nodes.getNodeAs<Expr>(
"empty_init")) {
148 diag(CtorInit->getMemberLocation(),
"redundant string initialization")
149 << FixItHint::CreateRemoval(CtorInit->getSourceRange());
156 if (std::optional<SourceRange> RemovalRange =
158 diag(CtorInit->getMemberLocation(),
"redundant string initialization")
159 << FixItHint::CreateRemoval(*RemovalRange);
llvm::SmallString< 256U > Name
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
RedundantStringInitCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
const char DefaultStringNames[]
static const CXXConstructExpr * getConstructExpr(const CXXCtorInitializer &CtorInit)
static std::vector< StringRef > removeNamespaces(ArrayRef< StringRef > Names)
static std::optional< SourceRange > getConstructExprArgRange(const CXXConstructExpr &Construct)
llvm::StringMap< ClangTidyValue > OptionMap