1//===--- UnnecessaryValueParamCheck.cpp - clang-tidy-----------------------===//
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#include "UnnecessaryValueParamCheck.h"
10#include "../utils/DeclRefExprUtils.h"
11#include "../utils/FixItHintUtils.h"
12#include "../utils/Matchers.h"
13#include "../utils/OptionsUtils.h"
14#include "../utils/TypeTraits.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Lex/Lexer.h"
17#include "clang/Lex/Preprocessor.h"
18#include <optional>
19
20using namespace clang::ast_matchers;
21
22namespace clang::tidy::performance {
23
24namespace {
25
26std::string paramNameOrIndex(StringRef Name, size_t Index) {
27 return (Name.empty() ? llvm::Twine('#') + llvm::Twine(Index + 1)
28 : llvm::Twine('\'') + Name + llvm::Twine('\''))
29 .str();
30}
31
32bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Decl &Decl,
33 ASTContext &Context) {
34 auto Matches = match(
35 Matcher: traverse(TK: TK_AsIs,
36 InnerMatcher: decl(forEachDescendant(declRefExpr(
37 equalsNode(Other: &DeclRef),
38 unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(),
39 whileStmt(), doStmt())))))))),
40 Node: Decl, Context);
41 return Matches.empty();
42}
43
44} // namespace
45
46UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
47 StringRef Name, ClangTidyContext *Context)
48 : ClangTidyCheck(Name, Context),
49 Inserter(Options.getLocalOrGlobal(LocalName: "IncludeStyle",
50 Default: utils::IncludeSorter::IS_LLVM),
51 areDiagsSelfContained()),
52 AllowedTypes(
53 utils::options::parseStringList(Option: Options.get(LocalName: "AllowedTypes", Default: ""))),
54 IgnoreCoroutines(Options.get(LocalName: "IgnoreCoroutines", Default: true)) {}
55
56void UnnecessaryValueParamCheck::registerMatchers(MatchFinder *Finder) {
57 const auto ExpensiveValueParamDecl = parmVarDecl(
58 hasType(InnerMatcher: qualType(
59 hasCanonicalType(InnerMatcher: matchers::isExpensiveToCopy()),
60 unless(anyOf(hasCanonicalType(InnerMatcher: referenceType()),
61 hasDeclaration(InnerMatcher: namedDecl(
62 matchers::matchesAnyListedName(NameList: AllowedTypes))))))),
63 decl().bind(ID: "param"));
64 Finder->addMatcher(
65 NodeMatch: traverse(TK: TK_AsIs,
66 InnerMatcher: functionDecl(
67 hasBody(InnerMatcher: IgnoreCoroutines ? stmt(unless(coroutineBodyStmt()))
68 : stmt()),
69 isDefinition(), unless(isImplicit()),
70 unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))),
71 has(typeLoc(forEach(ExpensiveValueParamDecl))),
72 decl().bind(ID: "functionDecl"))),
73 Action: this);
74}
75
76void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
77 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(ID: "param");
78 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(ID: "functionDecl");
79
80 TraversalKindScope RAII(*Result.Context, TK_AsIs);
81
82 FunctionParmMutationAnalyzer *Analyzer =
83 FunctionParmMutationAnalyzer::getFunctionParmMutationAnalyzer(
84 Func: *Function, Context&: *Result.Context, Memorized&: MutationAnalyzerCache);
85 if (Analyzer->isMutated(Parm: Param))
86 return;
87
88 const bool IsConstQualified =
89 Param->getType().getCanonicalType().isConstQualified();
90
91 // If the parameter is non-const, check if it has a move constructor and is
92 // only referenced once to copy-construct another object or whether it has a
93 // move assignment operator and is only referenced once when copy-assigned.
94 // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary
95 // copy.
96 if (!IsConstQualified) {
97 auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs(
98 VarDecl: *Param, Decl: *Function, Context&: *Result.Context);
99 if (AllDeclRefExprs.size() == 1) {
100 auto CanonicalType = Param->getType().getCanonicalType();
101 const auto &DeclRefExpr = **AllDeclRefExprs.begin();
102
103 if (!hasLoopStmtAncestor(DeclRef: DeclRefExpr, Decl: *Function, Context&: *Result.Context) &&
104 ((utils::type_traits::hasNonTrivialMoveConstructor(Type: CanonicalType) &&
105 utils::decl_ref_expr::isCopyConstructorArgument(
106 DeclRef: DeclRefExpr, Decl: *Function, Context&: *Result.Context)) ||
107 (utils::type_traits::hasNonTrivialMoveAssignment(Type: CanonicalType) &&
108 utils::decl_ref_expr::isCopyAssignmentArgument(
109 DeclRef: DeclRefExpr, Decl: *Function, Context&: *Result.Context)))) {
110 handleMoveFix(Param: *Param, CopyArgument: DeclRefExpr, Context&: *Result.Context);
111 return;
112 }
113 }
114 }
115
116 handleConstRefFix(Function: *Function, Param: *Param, Context&: *Result.Context);
117}
118
119void UnnecessaryValueParamCheck::registerPPCallbacks(
120 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
121 Inserter.registerPreprocessor(PP);
122}
123
124void UnnecessaryValueParamCheck::storeOptions(
125 ClangTidyOptions::OptionMap &Opts) {
126 Options.store(Options&: Opts, LocalName: "IncludeStyle", Value: Inserter.getStyle());
127 Options.store(Options&: Opts, LocalName: "AllowedTypes",
128 Value: utils::options::serializeStringList(Strings: AllowedTypes));
129 Options.store(Options&: Opts, LocalName: "IgnoreCoroutines", Value: IgnoreCoroutines);
130}
131
132void UnnecessaryValueParamCheck::onEndOfTranslationUnit() {
133 MutationAnalyzerCache.clear();
134}
135
136void UnnecessaryValueParamCheck::handleConstRefFix(const FunctionDecl &Function,
137 const ParmVarDecl &Param,
138 ASTContext &Context) {
139 const size_t Index =
140 llvm::find(Range: Function.parameters(), Val: &Param) - Function.parameters().begin();
141 const bool IsConstQualified =
142 Param.getType().getCanonicalType().isConstQualified();
143
144 auto Diag =
145 diag(Loc: Param.getLocation(),
146 Description: "the %select{|const qualified }0parameter %1 is copied for each "
147 "invocation%select{ but only used as a const reference|}0; consider "
148 "making it a %select{const |}0reference")
149 << IsConstQualified << paramNameOrIndex(Name: Param.getName(), Index);
150 // Do not propose fixes when:
151 // 1. the ParmVarDecl is in a macro, since we cannot place them correctly
152 // 2. the function is virtual as it might break overrides
153 // 3. the function is an explicit template/ specialization.
154 const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Val: &Function);
155 if (Param.getBeginLoc().isMacroID() || (Method && Method->isVirtual()) ||
156 Function.getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
157 return;
158 for (const auto *FunctionDecl = &Function; FunctionDecl != nullptr;
159 FunctionDecl = FunctionDecl->getPreviousDecl()) {
160 const auto &CurrentParam = *FunctionDecl->getParamDecl(i: Index);
161 Diag << utils::fixit::changeVarDeclToReference(Var: CurrentParam, Context);
162 // The parameter of each declaration needs to be checked individually as to
163 // whether it is const or not as constness can differ between definition and
164 // declaration.
165 if (!CurrentParam.getType().getCanonicalType().isConstQualified()) {
166 if (std::optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl(
167 Var: CurrentParam, Context, Qualifier: Qualifiers::Const))
168 Diag << *Fix;
169 }
170 }
171}
172
173void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Param,
174 const DeclRefExpr &CopyArgument,
175 ASTContext &Context) {
176 auto Diag = diag(Loc: CopyArgument.getBeginLoc(),
177 Description: "parameter %0 is passed by value and only copied once; "
178 "consider moving it to avoid unnecessary copies")
179 << &Param;
180 // Do not propose fixes in macros since we cannot place them correctly.
181 if (CopyArgument.getBeginLoc().isMacroID())
182 return;
183 const auto &SM = Context.getSourceManager();
184 auto EndLoc = Lexer::getLocForEndOfToken(Loc: CopyArgument.getLocation(), Offset: 0, SM,
185 LangOpts: Context.getLangOpts());
186 Diag << FixItHint::CreateInsertion(InsertionLoc: CopyArgument.getBeginLoc(), Code: "std::move(")
187 << FixItHint::CreateInsertion(InsertionLoc: EndLoc, Code: ")")
188 << Inserter.createIncludeInsertion(
189 FileID: SM.getFileID(SpellingLoc: CopyArgument.getBeginLoc()), Header: "<utility>");
190}
191
192} // namespace clang::tidy::performance
193

source code of clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp