clang-tools  14.0.0git
RedundantStringInitCheck.cpp
Go to the documentation of this file.
1 //===- RedundantStringInitCheck.cpp - clang-tidy ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "../utils/Matchers.h"
11 #include "../utils/OptionsUtils.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 
14 using namespace clang::ast_matchers;
15 using namespace clang::tidy::matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace readability {
20 
21 const char DefaultStringNames[] =
22  "::std::basic_string_view;::std::basic_string";
23 
24 static ast_matchers::internal::Matcher<NamedDecl>
25 hasAnyNameStdString(std::vector<std::string> Names) {
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());
34  for (const std::string &Name : Names) {
35  std::string::size_type ColonPos = Name.rfind(':');
36  Result.push_back(
37  Name.substr(ColonPos == std::string::npos ? 0 : ColonPos + 1));
38  }
39  return Result;
40 }
41 
42 static const CXXConstructExpr *
43 getConstructExpr(const CXXCtorInitializer &CtorInit) {
44  const Expr *InitExpr = CtorInit.getInit();
45  if (const auto *CleanUpExpr = dyn_cast<ExprWithCleanups>(InitExpr))
46  InitExpr = CleanUpExpr->getSubExpr();
47  return dyn_cast<CXXConstructExpr>(InitExpr);
48 }
49 
50 static llvm::Optional<SourceRange>
51 getConstructExprArgRange(const CXXConstructExpr &Construct) {
52  SourceLocation B, E;
53  for (const Expr *Arg : Construct.arguments()) {
54  if (B.isInvalid())
55  B = Arg->getBeginLoc();
56  if (Arg->getEndLoc().isValid())
57  E = Arg->getEndLoc();
58  }
59  if (B.isInvalid() || E.isInvalid())
60  return llvm::None;
61  return SourceRange(B, E);
62 }
63 
64 RedundantStringInitCheck::RedundantStringInitCheck(StringRef Name,
65  ClangTidyContext *Context)
66  : ClangTidyCheck(Name, Context),
67  StringNames(utils::options::parseStringList(
68  Options.get("StringNames", DefaultStringNames))) {}
69 
71  Options.store(Opts, "StringNames", DefaultStringNames);
72 }
73 
74 void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
75  const auto HasStringTypeName = hasAnyNameStdString(StringNames);
76  const auto HasStringCtorName =
78 
79  // Match string constructor.
80  const auto StringConstructorExpr = expr(
81  anyOf(cxxConstructExpr(argumentCountIs(1),
82  hasDeclaration(cxxMethodDecl(HasStringCtorName))),
83  // If present, the second argument is the alloc object which must
84  // not be present explicitly.
85  cxxConstructExpr(argumentCountIs(2),
86  hasDeclaration(cxxMethodDecl(HasStringCtorName)),
87  hasArgument(1, cxxDefaultArgExpr()))));
88 
89  // Match a string constructor expression with an empty string literal.
90  const auto EmptyStringCtorExpr = cxxConstructExpr(
91  StringConstructorExpr,
92  hasArgument(0, ignoringParenImpCasts(stringLiteral(hasSize(0)))));
93 
94  const auto EmptyStringCtorExprWithTemporaries =
95  cxxConstructExpr(StringConstructorExpr,
96  hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));
97 
98  const auto StringType = hasType(hasUnqualifiedDesugaredType(
99  recordType(hasDeclaration(cxxRecordDecl(HasStringTypeName)))));
100  const auto EmptyStringInit = traverse(
101  TK_AsIs, expr(ignoringImplicit(anyOf(
102  EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries))));
103 
104  // Match a variable declaration with an empty string literal as initializer.
105  // Examples:
106  // string foo = "";
107  // string bar("");
108  Finder->addMatcher(
109  traverse(TK_AsIs,
110  namedDecl(varDecl(StringType, hasInitializer(EmptyStringInit))
111  .bind("vardecl"),
112  unless(parmVarDecl()))),
113  this);
114  // Match a field declaration with an empty string literal as initializer.
115  Finder->addMatcher(
116  namedDecl(fieldDecl(StringType, hasInClassInitializer(EmptyStringInit))
117  .bind("fieldDecl")),
118  this);
119  // Matches Constructor Initializers with an empty string literal as
120  // initializer.
121  // Examples:
122  // Foo() : SomeString("") {}
123  Finder->addMatcher(
124  cxxCtorInitializer(
125  isWritten(),
126  forField(allOf(StringType, optionally(hasInClassInitializer(
127  EmptyStringInit.bind("empty_init"))))),
128  withInitializer(EmptyStringInit))
129  .bind("ctorInit"),
130  this);
131 }
132 
133 void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) {
134  if (const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl")) {
135  // VarDecl's getSourceRange() spans 'string foo = ""' or 'string bar("")'.
136  // So start at getLocation() to span just 'foo = ""' or 'bar("")'.
137  SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
138  diag(VDecl->getLocation(), "redundant string initialization")
139  << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
140  }
141  if (const auto *FDecl = Result.Nodes.getNodeAs<FieldDecl>("fieldDecl")) {
142  // FieldDecl's getSourceRange() spans 'string foo = ""'.
143  // So start at getLocation() to span just 'foo = ""'.
144  SourceRange ReplaceRange(FDecl->getLocation(), FDecl->getEndLoc());
145  diag(FDecl->getLocation(), "redundant string initialization")
146  << FixItHint::CreateReplacement(ReplaceRange, FDecl->getName());
147  }
148  if (const auto *CtorInit =
149  Result.Nodes.getNodeAs<CXXCtorInitializer>("ctorInit")) {
150  if (const FieldDecl *Member = CtorInit->getMember()) {
151  if (!Member->hasInClassInitializer() ||
152  Result.Nodes.getNodeAs<Expr>("empty_init")) {
153  // The String isn't declared in the class with an initializer or its
154  // declared with a redundant initializer, which will be removed. Either
155  // way the string will be default initialized, therefore we can remove
156  // the constructor initializer entirely.
157  diag(CtorInit->getMemberLocation(), "redundant string initialization")
158  << FixItHint::CreateRemoval(CtorInit->getSourceRange());
159  return;
160  }
161  }
162  const CXXConstructExpr *Construct = getConstructExpr(*CtorInit);
163  if (!Construct)
164  return;
165  if (llvm::Optional<SourceRange> RemovalRange =
166  getConstructExprArgRange(*Construct))
167  diag(CtorInit->getMemberLocation(), "redundant string initialization")
168  << FixItHint::CreateRemoval(*RemovalRange);
169  }
170 }
171 
172 } // namespace readability
173 } // namespace tidy
174 } // namespace clang
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::tidy::readability::getConstructExprArgRange
static llvm::Optional< SourceRange > getConstructExprArgRange(const CXXConstructExpr &Construct)
Definition: RedundantStringInitCheck.cpp:51
clang::tidy::readability::RedundantStringInitCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: RedundantStringInitCheck.cpp:70
clang::tidy::readability::DefaultStringNames
const char DefaultStringNames[]
Definition: RedundantStringInitCheck.cpp:21
RedundantStringInitCheck.h
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::matchers
Definition: clang-tidy/utils/Matchers.h:17
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:18
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:71
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang::tidy::readability::RedundantStringInitCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: RedundantStringInitCheck.cpp:74
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::readability::removeNamespaces
static std::vector< std::string > removeNamespaces(const std::vector< std::string > &Names)
Definition: RedundantStringInitCheck.cpp:31
clang::tidy::readability::hasAnyNameStdString
static ast_matchers::internal::Matcher< NamedDecl > hasAnyNameStdString(std::vector< std::string > Names)
Definition: RedundantStringInitCheck.cpp:25
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:120
ns1::ns2::B
@ B
Definition: CategoricalFeature.h:3
clang::tidy::readability::RedundantStringInitCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: RedundantStringInitCheck.cpp:133
clang::tidy::readability::getConstructExpr
static const CXXConstructExpr * getConstructExpr(const CXXCtorInitializer &CtorInit)
Definition: RedundantStringInitCheck.cpp:43