10 #include "../utils/Matchers.h" 11 #include "../utils/OptionsUtils.h" 12 #include "clang/ASTMatchers/ASTMatchers.h" 19 namespace readability {
23 RedundantStringInitCheck::RedundantStringInitCheck(StringRef
Name,
27 Options.get(
"StringNames", DefaultStringNames))) {}
36 const auto hasStringTypeName = hasAnyName(
37 SmallVector<StringRef, 3>(StringNames.begin(), StringNames.end()));
40 std::vector<std::string> stringNamesNoNamespace;
41 for (
const std::string &
name : StringNames) {
42 std::string::size_type colonPos =
name.rfind(
':');
43 stringNamesNoNamespace.push_back(
44 name.substr(colonPos == std::string::npos ? 0 : colonPos + 1));
46 const auto hasStringCtorName = hasAnyName(SmallVector<StringRef, 3>(
47 stringNamesNoNamespace.begin(), stringNamesNoNamespace.end()));
50 const auto StringConstructorExpr = expr(
51 anyOf(cxxConstructExpr(argumentCountIs(1),
52 hasDeclaration(cxxMethodDecl(hasStringCtorName))),
55 cxxConstructExpr(argumentCountIs(2),
56 hasDeclaration(cxxMethodDecl(hasStringCtorName)),
57 hasArgument(1, cxxDefaultArgExpr()))));
60 const auto EmptyStringCtorExpr = cxxConstructExpr(
61 StringConstructorExpr,
62 hasArgument(0, ignoringParenImpCasts(stringLiteral(hasSize(0)))));
64 const auto EmptyStringCtorExprWithTemporaries =
65 cxxConstructExpr(StringConstructorExpr,
66 hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));
75 hasType(hasUnqualifiedDesugaredType(recordType(
76 hasDeclaration(cxxRecordDecl(hasStringTypeName))))),
77 hasInitializer(expr(ignoringImplicit(anyOf(
78 EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries)))))
80 unless(parmVarDecl())),
85 const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>(
"vardecl");
88 SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
89 diag(VDecl->getLocation(),
"redundant string initialization")
90 << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const char DefaultStringNames[]