clang-tools 10.0.0

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
9 #include "RedundantStringInitCheck.h"
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
29 void RedundantStringInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
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
clang::ast_matchers
Definition: AbseilMatcher.h:14
lit.name
name
Definition: test/lit.cfg.py:6
clang::tidy::matchers
Definition: clang-tidy/utils/Matchers.h:17
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
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:29
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:184
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:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
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:84
clang::tidy::readability::RedundantStringInitCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: RedundantStringInitCheck.cpp:33
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
RedundantStringInitCheck.h
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
clang::tidy::readability::DefaultStringNames
const char DefaultStringNames[]
Definition: RedundantStringInitCheck.cpp:21