clang-tools 23.0.0git
StringViewConversionsCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "clang/AST/Expr.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13
14using namespace clang::ast_matchers;
16
17static auto getStringTypeMatcher(StringRef CharType) {
18 return hasCanonicalType(hasDeclaration(cxxRecordDecl(hasName(CharType))));
19}
20
22 // Matchers for std::basic_[w|u8|u16|u32]string[_view] families.
23 const auto IsStdString = getStringTypeMatcher("::std::basic_string");
24 const auto IsStdStringView = getStringTypeMatcher("::std::basic_string_view");
25
26 // Matches pointer to any character type (char*, etc.) or array of any
27 // character type (char[], etc.).
28 const auto IsCharPointerOrArray =
29 anyOf(hasType(pointerType(pointee(isAnyCharacter()))),
30 hasType(arrayType(hasElementType(isAnyCharacter()))));
31
32 const auto ImplicitlyConvertibleToStringView =
33 expr(anyOf(hasType(IsStdStringView), IsCharPointerOrArray,
34 hasType(IsStdString)))
35 .bind("originalStringView");
36
37 // Matches std::string construction from a string_view-convertible expression:
38 // - Direct construction: std::string{sv}, std::string{s}
39 // - Copy from existing string: std::string(s) where s is std::string
40 const auto RedundantStringConstruction = cxxConstructExpr(
41 hasType(IsStdString),
42 hasArgument(0, ignoringImplicit(ImplicitlyConvertibleToStringView)),
43 unless(hasArgument(1, unless(cxxDefaultArgExpr()))));
44
45 // Matches functional cast syntax: std::string(expr):
46 // std::string(sv), std::string("literal")
47 const auto RedundantFunctionalCast = cxxFunctionalCastExpr(
48 hasType(IsStdString), hasDescendant(RedundantStringConstruction));
49
50 const auto RedundantTemporaryString =
51 expr(anyOf(RedundantStringConstruction, RedundantFunctionalCast));
52
53 // Matches std::string(...).[c_str()|.data()]
54 const auto RedundantStringWithCStr =
55 cxxMemberCallExpr(callee(cxxMethodDecl(hasAnyName("c_str", "data"))),
56 on(ignoringParenImpCasts(RedundantTemporaryString)));
57
58 // Main matcher: finds cases where an expression convertible to
59 // std::string_view is first converted to std::string unnecessarily.
60 Finder->addMatcher(
61 cxxMemberCallExpr(
62 callee(memberExpr(member(cxxConversionDecl(returns(IsStdStringView))),
63 has(ignoringImplicit(RedundantTemporaryString.bind(
64 "redundantExpr"))))))
65 .bind("stringView"),
66 this);
67
68 Finder->addMatcher(
69 cxxConstructExpr(
70 argumentCountIs(1),
71 hasArgument(0, RedundantStringWithCStr.bind("redundantExpr")))
72 .bind("stringView"),
73 this);
74}
75
76void StringViewConversionsCheck::check(const MatchFinder::MatchResult &Result) {
77 const auto *StringView = Result.Nodes.getNodeAs<Expr>("stringView");
78 const auto *RedundantExpr = Result.Nodes.getNodeAs<Expr>("redundantExpr");
79 const auto *OriginalExpr = Result.Nodes.getNodeAs<Expr>("originalStringView");
80 assert(StringView && RedundantExpr && OriginalExpr);
81
82 bool IsCStrPattern = false;
83 StringRef MethodName;
84 const auto *CStrCall = dyn_cast<CXXMemberCallExpr>(RedundantExpr);
85 if (CStrCall && CStrCall->getMethodDecl()) {
86 MethodName = CStrCall->getMethodDecl()->getName();
87 if (MethodName == "c_str" || MethodName == "data")
88 IsCStrPattern = true;
89 }
90
91 const StringRef OriginalText = Lexer::getSourceText(
92 CharSourceRange::getTokenRange(OriginalExpr->getSourceRange()),
93 *Result.SourceManager, getLangOpts());
94
95 if (OriginalText.empty())
96 return;
97
98 const FixItHint FixRedundantConversion = FixItHint::CreateReplacement(
99 RedundantExpr->getSourceRange(), OriginalText);
100 if (IsCStrPattern && CStrCall) {
101 // Handle std::string(sv).c_str() or std::string(sv).data() pattern
102 diag(RedundantExpr->getBeginLoc(),
103 "redundant conversion to %0 and calling .%1() and then back to %2")
104 << CStrCall->getImplicitObjectArgument()->getType() << MethodName
105 << StringView->getType() << FixRedundantConversion;
106 } else {
107 // Handle direct std::string(sv) pattern
108 diag(RedundantExpr->getBeginLoc(),
109 "redundant conversion to %0 and then back to %1")
110 << RedundantExpr->getType() << StringView->getType()
111 << FixRedundantConversion;
112 }
113}
114
115} // namespace clang::tidy::performance
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static auto getStringTypeMatcher(StringRef CharType)