annotate clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp @ 266:00f31e85ec16 default tip

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