clang-tools 19.0.0git
RedundantStringCStrCheck.cpp
Go to the documentation of this file.
1//===- RedundantStringCStrCheck.cpp - Check for redundant c_str calls -----===//
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// This file implements a check for redundant calls of c_str() on strings.
10//
11//===----------------------------------------------------------------------===//
12
14#include "../utils/FixItHintUtils.h"
15#include "../utils/Matchers.h"
16#include "../utils/OptionsUtils.h"
17#include "clang/Lex/Lexer.h"
18#include "clang/Tooling/FixIt.h"
19
20using namespace clang::ast_matchers;
21
23
24namespace {
25
26AST_MATCHER(MaterializeTemporaryExpr, isBoundToLValue) {
27 return Node.isBoundToLvalueReference();
28}
29
30} // end namespace
31
33 ClangTidyContext *Context)
34 : ClangTidyCheck(Name, Context),
35 StringParameterFunctions(utils::options::parseStringList(
36 Options.get("StringParameterFunctions", ""))) {
37 if (getLangOpts().CPlusPlus20)
38 StringParameterFunctions.emplace_back("::std::format");
39 if (getLangOpts().CPlusPlus23)
40 StringParameterFunctions.emplace_back("::std::print");
41}
42
44 ast_matchers::MatchFinder *Finder) {
45 // Match expressions of type 'string' or 'string*'.
46 const auto StringDecl = type(hasUnqualifiedDesugaredType(recordType(
47 hasDeclaration(cxxRecordDecl(hasName("::std::basic_string"))))));
48 const auto StringExpr =
49 expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl)))));
50
51 // Match string constructor.
52 const auto StringConstructorExpr = expr(anyOf(
53 cxxConstructExpr(argumentCountIs(1),
54 hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
55 cxxConstructExpr(
56 argumentCountIs(2),
57 hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
58 // If present, the second argument is the alloc object which must not
59 // be present explicitly.
60 hasArgument(1, cxxDefaultArgExpr()))));
61
62 // Match string constructor.
63 const auto StringViewConstructorExpr = cxxConstructExpr(
64 argumentCountIs(1),
65 hasDeclaration(cxxMethodDecl(hasName("basic_string_view"))));
66
67 // Match a call to the string 'c_str()' method.
68 const auto StringCStrCallExpr =
69 cxxMemberCallExpr(on(StringExpr.bind("arg")),
70 callee(memberExpr().bind("member")),
71 callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
72 .bind("call");
73 const auto HasRValueTempParent =
74 hasParent(materializeTemporaryExpr(unless(isBoundToLValue())));
75 // Detect redundant 'c_str()' calls through a string constructor.
76 // If CxxConstructExpr is the part of some CallExpr we need to
77 // check that matched ParamDecl of the ancestor CallExpr is not rvalue.
78 Finder->addMatcher(
79 traverse(
80 TK_AsIs,
81 cxxConstructExpr(
82 anyOf(StringConstructorExpr, StringViewConstructorExpr),
83 hasArgument(0, StringCStrCallExpr),
84 unless(anyOf(HasRValueTempParent, hasParent(cxxBindTemporaryExpr(
85 HasRValueTempParent)))))),
86 this);
87
88 // Detect: 's == str.c_str()' -> 's == str'
89 Finder->addMatcher(
90 cxxOperatorCallExpr(
91 hasAnyOverloadedOperatorName("<", ">", ">=", "<=", "!=", "==", "+"),
92 anyOf(allOf(hasArgument(0, StringExpr),
93 hasArgument(1, StringCStrCallExpr)),
94 allOf(hasArgument(0, StringCStrCallExpr),
95 hasArgument(1, StringExpr)))),
96 this);
97
98 // Detect: 'dst += str.c_str()' -> 'dst += str'
99 // Detect: 's = str.c_str()' -> 's = str'
100 Finder->addMatcher(
101 cxxOperatorCallExpr(hasAnyOverloadedOperatorName("=", "+="),
102 hasArgument(0, StringExpr),
103 hasArgument(1, StringCStrCallExpr)),
104 this);
105
106 // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'
107 Finder->addMatcher(
108 cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName(
109 "append", "assign", "compare")))),
110 argumentCountIs(1), hasArgument(0, StringCStrCallExpr)),
111 this);
112
113 // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)'
114 Finder->addMatcher(
115 cxxMemberCallExpr(on(StringExpr),
116 callee(decl(cxxMethodDecl(hasName("compare")))),
117 argumentCountIs(3), hasArgument(2, StringCStrCallExpr)),
118 this);
119
120 // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)'
121 Finder->addMatcher(
122 cxxMemberCallExpr(on(StringExpr),
123 callee(decl(cxxMethodDecl(hasAnyName(
124 "find", "find_first_not_of", "find_first_of",
125 "find_last_not_of", "find_last_of", "rfind")))),
126 anyOf(argumentCountIs(1), argumentCountIs(2)),
127 hasArgument(0, StringCStrCallExpr)),
128 this);
129
130 // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)'
131 Finder->addMatcher(
132 cxxMemberCallExpr(on(StringExpr),
133 callee(decl(cxxMethodDecl(hasName("insert")))),
134 argumentCountIs(2), hasArgument(1, StringCStrCallExpr)),
135 this);
136
137 // Detect redundant 'c_str()' calls through a StringRef constructor.
138 Finder->addMatcher(
139 traverse(
140 TK_AsIs,
141 cxxConstructExpr(
142 // Implicit constructors of these classes are overloaded
143 // wrt. string types and they internally make a StringRef
144 // referring to the argument. Passing a string directly to
145 // them is preferred to passing a char pointer.
146 hasDeclaration(cxxMethodDecl(hasAnyName(
147 "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))),
148 argumentCountIs(1),
149 // The only argument must have the form x.c_str() or p->c_str()
150 // where the method is string::c_str(). StringRef also has
151 // a constructor from string which is more efficient (avoids
152 // strlen), so we can construct StringRef from the string
153 // directly.
154 hasArgument(0, StringCStrCallExpr))),
155 this);
156
157 if (!StringParameterFunctions.empty()) {
158 // Detect redundant 'c_str()' calls in parameters passed to std::format in
159 // C++20 onwards and std::print in C++23 onwards.
160 Finder->addMatcher(
161 traverse(TK_AsIs,
162 callExpr(callee(functionDecl(matchers::matchesAnyListedName(
163 StringParameterFunctions))),
164 forEachArgumentWithParam(StringCStrCallExpr,
165 parmVarDecl()))),
166 this);
167 }
168}
169
170void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
171 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
172 const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
173 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
174 bool Arrow = Member->isArrow();
175 // Replace the "call" node with the "arg" node, prefixed with '*'
176 // if the call was using '->' rather than '.'.
177 std::string ArgText =
178 Arrow ? utils::fixit::formatDereference(*Arg, *Result.Context)
179 : tooling::fixit::getText(*Arg, *Result.Context).str();
180 if (ArgText.empty())
181 return;
182
183 diag(Call->getBeginLoc(), "redundant call to %0")
184 << Member->getMemberDecl()
185 << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
186}
187
188} // namespace clang::tidy::readability
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
RedundantStringCStrCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string formatDereference(const Expr &ExprNode, const ASTContext &Context)