189 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements a check for redundant calls of c_str() on strings.10//11//===----------------------------------------------------------------------===//12 13#include "RedundantStringCStrCheck.h"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 22namespace clang::tidy::readability {23 24namespace {25 26AST_MATCHER(MaterializeTemporaryExpr, isBoundToLValue) {27 return Node.isBoundToLvalueReference();28}29 30} // end namespace31 32RedundantStringCStrCheck::RedundantStringCStrCheck(StringRef Name,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 43void RedundantStringCStrCheck::registerMatchers(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(argumentCountIs(2),56 hasDeclaration(cxxMethodDecl(hasName("basic_string"))),57 // If present, the second argument is the alloc object58 // which must not be present explicitly.59 hasArgument(1, cxxDefaultArgExpr()))));60 61 // Match string constructor.62 const auto StringViewConstructorExpr = cxxConstructExpr(63 argumentCountIs(1),64 hasDeclaration(cxxMethodDecl(hasName("basic_string_view"))));65 66 // Match a call to the string 'c_str()' method.67 const auto StringCStrCallExpr =68 cxxMemberCallExpr(on(StringExpr.bind("arg")),69 callee(memberExpr().bind("member")),70 callee(cxxMethodDecl(hasAnyName("c_str", "data"))))71 .bind("call");72 const auto HasRValueTempParent =73 hasParent(materializeTemporaryExpr(unless(isBoundToLValue())));74 // Detect redundant 'c_str()' calls through a string constructor.75 // If CxxConstructExpr is the part of some CallExpr we need to76 // check that matched ParamDecl of the ancestor CallExpr is not rvalue.77 Finder->addMatcher(78 traverse(79 TK_AsIs,80 cxxConstructExpr(81 anyOf(StringConstructorExpr, StringViewConstructorExpr),82 hasArgument(0, StringCStrCallExpr),83 unless(anyOf(HasRValueTempParent, hasParent(cxxBindTemporaryExpr(84 HasRValueTempParent)))))),85 this);86 87 // Detect: 's == str.c_str()' -> 's == str'88 Finder->addMatcher(89 cxxOperatorCallExpr(90 hasAnyOverloadedOperatorName("<", ">", ">=", "<=", "!=", "==", "+"),91 anyOf(allOf(hasArgument(0, StringExpr),92 hasArgument(1, StringCStrCallExpr)),93 allOf(hasArgument(0, StringCStrCallExpr),94 hasArgument(1, StringExpr)))),95 this);96 97 // Detect: 'dst += str.c_str()' -> 'dst += str'98 // Detect: 's = str.c_str()' -> 's = str'99 Finder->addMatcher(100 cxxOperatorCallExpr(hasAnyOverloadedOperatorName("=", "+="),101 hasArgument(0, StringExpr),102 hasArgument(1, StringCStrCallExpr)),103 this);104 105 // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'106 Finder->addMatcher(107 cxxMemberCallExpr(on(StringExpr),108 callee(decl(cxxMethodDecl(109 hasAnyName("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 overloaded143 // wrt. string types and they internally make a StringRef144 // referring to the argument. Passing a string directly to145 // 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 has151 // a constructor from string which is more efficient (avoids152 // strlen), so we can construct StringRef from the string153 // directly.154 hasArgument(0, StringCStrCallExpr))),155 this);156 157 if (!StringParameterFunctions.empty()) {158 // Detect redundant 'c_str()' calls in parameters passed to std::format in159 // 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 const bool Arrow = Member->isArrow();175 // Replace the "call" node with the "arg" node, prefixed with '*'176 // if the call was using '->' rather than '.'.177 const 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::readability189