annotate clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 79ff65ed7e25
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- RedundantStringInitCheck.cpp - clang-tidy ----------------*- C++ -*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "RedundantStringInitCheck.h"
anatofuz
parents:
diff changeset
10 #include "../utils/Matchers.h"
anatofuz
parents:
diff changeset
11 #include "../utils/OptionsUtils.h"
anatofuz
parents:
diff changeset
12 #include "clang/ASTMatchers/ASTMatchers.h"
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 using namespace clang::ast_matchers;
anatofuz
parents:
diff changeset
15 using namespace clang::tidy::matchers;
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 namespace clang {
anatofuz
parents:
diff changeset
18 namespace tidy {
anatofuz
parents:
diff changeset
19 namespace readability {
anatofuz
parents:
diff changeset
20
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
21 const char DefaultStringNames[] =
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
22 "::std::basic_string_view;::std::basic_string";
150
anatofuz
parents:
diff changeset
23
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
24 static std::vector<StringRef> removeNamespaces(ArrayRef<StringRef> Names) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
25 std::vector<StringRef> Result;
150
anatofuz
parents:
diff changeset
26 Result.reserve(Names.size());
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
27 for (StringRef Name : Names) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
28 StringRef::size_type ColonPos = Name.rfind(':');
150
anatofuz
parents:
diff changeset
29 Result.push_back(
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
30 Name.drop_front(ColonPos == StringRef::npos ? 0 : ColonPos + 1));
150
anatofuz
parents:
diff changeset
31 }
anatofuz
parents:
diff changeset
32 return Result;
anatofuz
parents:
diff changeset
33 }
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 static const CXXConstructExpr *
anatofuz
parents:
diff changeset
36 getConstructExpr(const CXXCtorInitializer &CtorInit) {
anatofuz
parents:
diff changeset
37 const Expr *InitExpr = CtorInit.getInit();
anatofuz
parents:
diff changeset
38 if (const auto *CleanUpExpr = dyn_cast<ExprWithCleanups>(InitExpr))
anatofuz
parents:
diff changeset
39 InitExpr = CleanUpExpr->getSubExpr();
anatofuz
parents:
diff changeset
40 return dyn_cast<CXXConstructExpr>(InitExpr);
anatofuz
parents:
diff changeset
41 }
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 static llvm::Optional<SourceRange>
anatofuz
parents:
diff changeset
44 getConstructExprArgRange(const CXXConstructExpr &Construct) {
anatofuz
parents:
diff changeset
45 SourceLocation B, E;
anatofuz
parents:
diff changeset
46 for (const Expr *Arg : Construct.arguments()) {
anatofuz
parents:
diff changeset
47 if (B.isInvalid())
anatofuz
parents:
diff changeset
48 B = Arg->getBeginLoc();
anatofuz
parents:
diff changeset
49 if (Arg->getEndLoc().isValid())
anatofuz
parents:
diff changeset
50 E = Arg->getEndLoc();
anatofuz
parents:
diff changeset
51 }
anatofuz
parents:
diff changeset
52 if (B.isInvalid() || E.isInvalid())
anatofuz
parents:
diff changeset
53 return llvm::None;
anatofuz
parents:
diff changeset
54 return SourceRange(B, E);
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 RedundantStringInitCheck::RedundantStringInitCheck(StringRef Name,
anatofuz
parents:
diff changeset
58 ClangTidyContext *Context)
anatofuz
parents:
diff changeset
59 : ClangTidyCheck(Name, Context),
anatofuz
parents:
diff changeset
60 StringNames(utils::options::parseStringList(
anatofuz
parents:
diff changeset
61 Options.get("StringNames", DefaultStringNames))) {}
anatofuz
parents:
diff changeset
62
anatofuz
parents:
diff changeset
63 void RedundantStringInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
anatofuz
parents:
diff changeset
64 Options.store(Opts, "StringNames", DefaultStringNames);
anatofuz
parents:
diff changeset
65 }
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
68 const auto HasStringTypeName = hasAnyName(StringNames);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
69 const auto HasStringCtorName = hasAnyName(removeNamespaces(StringNames));
150
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 // Match string constructor.
anatofuz
parents:
diff changeset
72 const auto StringConstructorExpr = expr(
anatofuz
parents:
diff changeset
73 anyOf(cxxConstructExpr(argumentCountIs(1),
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
74 hasDeclaration(cxxMethodDecl(HasStringCtorName))),
150
anatofuz
parents:
diff changeset
75 // If present, the second argument is the alloc object which must
anatofuz
parents:
diff changeset
76 // not be present explicitly.
anatofuz
parents:
diff changeset
77 cxxConstructExpr(argumentCountIs(2),
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
78 hasDeclaration(cxxMethodDecl(HasStringCtorName)),
150
anatofuz
parents:
diff changeset
79 hasArgument(1, cxxDefaultArgExpr()))));
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 // Match a string constructor expression with an empty string literal.
anatofuz
parents:
diff changeset
82 const auto EmptyStringCtorExpr = cxxConstructExpr(
anatofuz
parents:
diff changeset
83 StringConstructorExpr,
anatofuz
parents:
diff changeset
84 hasArgument(0, ignoringParenImpCasts(stringLiteral(hasSize(0)))));
anatofuz
parents:
diff changeset
85
anatofuz
parents:
diff changeset
86 const auto EmptyStringCtorExprWithTemporaries =
anatofuz
parents:
diff changeset
87 cxxConstructExpr(StringConstructorExpr,
anatofuz
parents:
diff changeset
88 hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 const auto StringType = hasType(hasUnqualifiedDesugaredType(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
91 recordType(hasDeclaration(cxxRecordDecl(HasStringTypeName)))));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
92 const auto EmptyStringInit = traverse(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
93 TK_AsIs, expr(ignoringImplicit(anyOf(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
94 EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries))));
150
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 // Match a variable declaration with an empty string literal as initializer.
anatofuz
parents:
diff changeset
97 // Examples:
anatofuz
parents:
diff changeset
98 // string foo = "";
anatofuz
parents:
diff changeset
99 // string bar("");
anatofuz
parents:
diff changeset
100 Finder->addMatcher(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
101 traverse(TK_AsIs,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
102 namedDecl(varDecl(StringType, hasInitializer(EmptyStringInit))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
103 .bind("vardecl"),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
104 unless(parmVarDecl()))),
150
anatofuz
parents:
diff changeset
105 this);
anatofuz
parents:
diff changeset
106 // Match a field declaration with an empty string literal as initializer.
anatofuz
parents:
diff changeset
107 Finder->addMatcher(
anatofuz
parents:
diff changeset
108 namedDecl(fieldDecl(StringType, hasInClassInitializer(EmptyStringInit))
anatofuz
parents:
diff changeset
109 .bind("fieldDecl")),
anatofuz
parents:
diff changeset
110 this);
anatofuz
parents:
diff changeset
111 // Matches Constructor Initializers with an empty string literal as
anatofuz
parents:
diff changeset
112 // initializer.
anatofuz
parents:
diff changeset
113 // Examples:
anatofuz
parents:
diff changeset
114 // Foo() : SomeString("") {}
anatofuz
parents:
diff changeset
115 Finder->addMatcher(
anatofuz
parents:
diff changeset
116 cxxCtorInitializer(
anatofuz
parents:
diff changeset
117 isWritten(),
anatofuz
parents:
diff changeset
118 forField(allOf(StringType, optionally(hasInClassInitializer(
anatofuz
parents:
diff changeset
119 EmptyStringInit.bind("empty_init"))))),
anatofuz
parents:
diff changeset
120 withInitializer(EmptyStringInit))
anatofuz
parents:
diff changeset
121 .bind("ctorInit"),
anatofuz
parents:
diff changeset
122 this);
anatofuz
parents:
diff changeset
123 }
anatofuz
parents:
diff changeset
124
anatofuz
parents:
diff changeset
125 void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) {
anatofuz
parents:
diff changeset
126 if (const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl")) {
anatofuz
parents:
diff changeset
127 // VarDecl's getSourceRange() spans 'string foo = ""' or 'string bar("")'.
anatofuz
parents:
diff changeset
128 // So start at getLocation() to span just 'foo = ""' or 'bar("")'.
anatofuz
parents:
diff changeset
129 SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
anatofuz
parents:
diff changeset
130 diag(VDecl->getLocation(), "redundant string initialization")
anatofuz
parents:
diff changeset
131 << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
anatofuz
parents:
diff changeset
132 }
anatofuz
parents:
diff changeset
133 if (const auto *FDecl = Result.Nodes.getNodeAs<FieldDecl>("fieldDecl")) {
anatofuz
parents:
diff changeset
134 // FieldDecl's getSourceRange() spans 'string foo = ""'.
anatofuz
parents:
diff changeset
135 // So start at getLocation() to span just 'foo = ""'.
anatofuz
parents:
diff changeset
136 SourceRange ReplaceRange(FDecl->getLocation(), FDecl->getEndLoc());
anatofuz
parents:
diff changeset
137 diag(FDecl->getLocation(), "redundant string initialization")
anatofuz
parents:
diff changeset
138 << FixItHint::CreateReplacement(ReplaceRange, FDecl->getName());
anatofuz
parents:
diff changeset
139 }
anatofuz
parents:
diff changeset
140 if (const auto *CtorInit =
anatofuz
parents:
diff changeset
141 Result.Nodes.getNodeAs<CXXCtorInitializer>("ctorInit")) {
anatofuz
parents:
diff changeset
142 if (const FieldDecl *Member = CtorInit->getMember()) {
anatofuz
parents:
diff changeset
143 if (!Member->hasInClassInitializer() ||
anatofuz
parents:
diff changeset
144 Result.Nodes.getNodeAs<Expr>("empty_init")) {
anatofuz
parents:
diff changeset
145 // The String isn't declared in the class with an initializer or its
anatofuz
parents:
diff changeset
146 // declared with a redundant initializer, which will be removed. Either
anatofuz
parents:
diff changeset
147 // way the string will be default initialized, therefore we can remove
anatofuz
parents:
diff changeset
148 // the constructor initializer entirely.
anatofuz
parents:
diff changeset
149 diag(CtorInit->getMemberLocation(), "redundant string initialization")
anatofuz
parents:
diff changeset
150 << FixItHint::CreateRemoval(CtorInit->getSourceRange());
anatofuz
parents:
diff changeset
151 return;
anatofuz
parents:
diff changeset
152 }
anatofuz
parents:
diff changeset
153 }
anatofuz
parents:
diff changeset
154 const CXXConstructExpr *Construct = getConstructExpr(*CtorInit);
anatofuz
parents:
diff changeset
155 if (!Construct)
anatofuz
parents:
diff changeset
156 return;
anatofuz
parents:
diff changeset
157 if (llvm::Optional<SourceRange> RemovalRange =
anatofuz
parents:
diff changeset
158 getConstructExprArgRange(*Construct))
anatofuz
parents:
diff changeset
159 diag(CtorInit->getMemberLocation(), "redundant string initialization")
anatofuz
parents:
diff changeset
160 << FixItHint::CreateRemoval(*RemovalRange);
anatofuz
parents:
diff changeset
161 }
anatofuz
parents:
diff changeset
162 }
anatofuz
parents:
diff changeset
163
anatofuz
parents:
diff changeset
164 } // namespace readability
anatofuz
parents:
diff changeset
165 } // namespace tidy
anatofuz
parents:
diff changeset
166 } // namespace clang