clang-tools  10.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[] = "::std::basic_string";
22 
23 RedundantStringInitCheck::RedundantStringInitCheck(StringRef Name,
24  ClangTidyContext *Context)
25  : ClangTidyCheck(Name, Context),
26  StringNames(utils::options::parseStringList(
27  Options.get("StringNames", DefaultStringNames))) {}
28 
30  Options.store(Opts, "StringNames", DefaultStringNames);
31 }
32 
33 void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
34  if (!getLangOpts().CPlusPlus)
35  return;
36  const auto hasStringTypeName = hasAnyName(
37  SmallVector<StringRef, 3>(StringNames.begin(), StringNames.end()));
38 
39  // Version of StringNames with namespaces removed
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));
45  }
46  const auto hasStringCtorName = hasAnyName(SmallVector<StringRef, 3>(
47  stringNamesNoNamespace.begin(), stringNamesNoNamespace.end()));
48 
49  // Match string constructor.
50  const auto StringConstructorExpr = expr(
51  anyOf(cxxConstructExpr(argumentCountIs(1),
52  hasDeclaration(cxxMethodDecl(hasStringCtorName))),
53  // If present, the second argument is the alloc object which must
54  // not be present explicitly.
55  cxxConstructExpr(argumentCountIs(2),
56  hasDeclaration(cxxMethodDecl(hasStringCtorName)),
57  hasArgument(1, cxxDefaultArgExpr()))));
58 
59  // Match a string constructor expression with an empty string literal.
60  const auto EmptyStringCtorExpr = cxxConstructExpr(
61  StringConstructorExpr,
62  hasArgument(0, ignoringParenImpCasts(stringLiteral(hasSize(0)))));
63 
64  const auto EmptyStringCtorExprWithTemporaries =
65  cxxConstructExpr(StringConstructorExpr,
66  hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));
67 
68  // Match a variable declaration with an empty string literal as initializer.
69  // Examples:
70  // string foo = "";
71  // string bar("");
72  Finder->addMatcher(
73  namedDecl(
74  varDecl(
75  hasType(hasUnqualifiedDesugaredType(recordType(
76  hasDeclaration(cxxRecordDecl(hasStringTypeName))))),
77  hasInitializer(expr(ignoringImplicit(anyOf(
78  EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries)))))
79  .bind("vardecl"),
80  unless(parmVarDecl())),
81  this);
82 }
83 
84 void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) {
85  const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl");
86  // VarDecl's getSourceRange() spans 'string foo = ""' or 'string bar("")'.
87  // So start at getLocation() to span just 'foo = ""' or 'bar("")'.
88  SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
89  diag(VDecl->getLocation(), "redundant string initialization")
90  << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
91 }
92 
93 } // namespace readability
94 } // namespace tidy
95 } // namespace clang
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&#39;s name.