clang-tools 19.0.0git
UnnecessaryValueParamCheck.cpp
Go to the documentation of this file.
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
10
11#include "../utils/DeclRefExprUtils.h"
12#include "../utils/FixItHintUtils.h"
13#include "../utils/Matchers.h"
14#include "../utils/OptionsUtils.h"
15#include "../utils/TypeTraits.h"
16#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Lex/Lexer.h"
18#include "clang/Lex/Preprocessor.h"
19#include <optional>
20
21using namespace clang::ast_matchers;
22
24
25namespace {
26
27std::string paramNameOrIndex(StringRef Name, size_t Index) {
28 return (Name.empty() ? llvm::Twine('#') + llvm::Twine(Index + 1)
29 : llvm::Twine('\'') + Name + llvm::Twine('\''))
30 .str();
31}
32
33bool isReferencedOutsideOfCallExpr(const FunctionDecl &Function,
34 ASTContext &Context) {
35 auto Matches = match(declRefExpr(to(functionDecl(equalsNode(&Function))),
36 unless(hasAncestor(callExpr()))),
37 Context);
38 return !Matches.empty();
39}
40
41bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Decl &Decl,
42 ASTContext &Context) {
43 auto Matches = match(
44 traverse(TK_AsIs,
45 decl(forEachDescendant(declRefExpr(
46 equalsNode(&DeclRef),
47 unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(),
48 whileStmt(), doStmt())))))))),
49 Decl, Context);
50 return Matches.empty();
51}
52
53} // namespace
54
56 StringRef Name, ClangTidyContext *Context)
57 : ClangTidyCheck(Name, Context),
58 Inserter(Options.getLocalOrGlobal("IncludeStyle",
59 utils::IncludeSorter::IS_LLVM),
60 areDiagsSelfContained()),
61 AllowedTypes(
62 utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
63
65 const auto ExpensiveValueParamDecl = parmVarDecl(
66 hasType(qualType(
67 hasCanonicalType(matchers::isExpensiveToCopy()),
68 unless(anyOf(hasCanonicalType(referenceType()),
69 hasDeclaration(namedDecl(
70 matchers::matchesAnyListedName(AllowedTypes))))))),
71 decl().bind("param"));
72 Finder->addMatcher(
73 traverse(
74 TK_AsIs,
75 functionDecl(hasBody(stmt()), isDefinition(), unless(isImplicit()),
76 unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))),
77 has(typeLoc(forEach(ExpensiveValueParamDecl))),
78 unless(isInstantiated()), decl().bind("functionDecl"))),
79 this);
80}
81
82void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
83 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
84 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl");
85
86 TraversalKindScope RAII(*Result.Context, TK_AsIs);
87
88 FunctionParmMutationAnalyzer *Analyzer =
89 FunctionParmMutationAnalyzer::getFunctionParmMutationAnalyzer(
90 *Function, *Result.Context, MutationAnalyzerCache);
91 if (Analyzer->isMutated(Param))
92 return;
93
94 const bool IsConstQualified =
95 Param->getType().getCanonicalType().isConstQualified();
96
97 // If the parameter is non-const, check if it has a move constructor and is
98 // only referenced once to copy-construct another object or whether it has a
99 // move assignment operator and is only referenced once when copy-assigned.
100 // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary
101 // copy.
102 if (!IsConstQualified) {
103 auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs(
104 *Param, *Function, *Result.Context);
105 if (AllDeclRefExprs.size() == 1) {
106 auto CanonicalType = Param->getType().getCanonicalType();
107 const auto &DeclRefExpr = **AllDeclRefExprs.begin();
108
109 if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
112 DeclRefExpr, *Function, *Result.Context)) ||
115 DeclRefExpr, *Function, *Result.Context)))) {
116 handleMoveFix(*Param, DeclRefExpr, *Result.Context);
117 return;
118 }
119 }
120 }
121
122 const size_t Index = llvm::find(Function->parameters(), Param) -
123 Function->parameters().begin();
124
125 auto Diag =
126 diag(Param->getLocation(),
127 "the %select{|const qualified }0parameter %1 is copied for each "
128 "invocation%select{ but only used as a const reference|}0; consider "
129 "making it a %select{const |}0reference")
130 << IsConstQualified << paramNameOrIndex(Param->getName(), Index);
131 // Do not propose fixes when:
132 // 1. the ParmVarDecl is in a macro, since we cannot place them correctly
133 // 2. the function is virtual as it might break overrides
134 // 3. the function is referenced outside of a call expression within the
135 // compilation unit as the signature change could introduce build errors.
136 // 4. the function is a primary template or an explicit template
137 // specialization.
138 const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
139 if (Param->getBeginLoc().isMacroID() || (Method && Method->isVirtual()) ||
140 isReferencedOutsideOfCallExpr(*Function, *Result.Context) ||
141 (Function->getTemplatedKind() != FunctionDecl::TK_NonTemplate))
142 return;
143 for (const auto *FunctionDecl = Function; FunctionDecl != nullptr;
144 FunctionDecl = FunctionDecl->getPreviousDecl()) {
145 const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
146 Diag << utils::fixit::changeVarDeclToReference(CurrentParam,
147 *Result.Context);
148 // The parameter of each declaration needs to be checked individually as to
149 // whether it is const or not as constness can differ between definition and
150 // declaration.
151 if (!CurrentParam.getType().getCanonicalType().isConstQualified()) {
152 if (std::optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl(
153 CurrentParam, *Result.Context, DeclSpec::TQ::TQ_const))
154 Diag << *Fix;
155 }
156 }
157}
158
160 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
161 Inserter.registerPreprocessor(PP);
162}
163
166 Options.store(Opts, "IncludeStyle", Inserter.getStyle());
167 Options.store(Opts, "AllowedTypes",
169}
170
172 MutationAnalyzerCache.clear();
173}
174
175void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
176 const DeclRefExpr &CopyArgument,
177 const ASTContext &Context) {
178 auto Diag = diag(CopyArgument.getBeginLoc(),
179 "parameter %0 is passed by value and only copied once; "
180 "consider moving it to avoid unnecessary copies")
181 << &Var;
182 // Do not propose fixes in macros since we cannot place them correctly.
183 if (CopyArgument.getBeginLoc().isMacroID())
184 return;
185 const auto &SM = Context.getSourceManager();
186 auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
187 Context.getLangOpts());
188 Diag << FixItHint::CreateInsertion(CopyArgument.getBeginLoc(), "std::move(")
189 << FixItHint::CreateInsertion(EndLoc, ")")
190 << Inserter.createIncludeInsertion(
191 SM.getFileID(CopyArgument.getBeginLoc()), "<utility>");
192}
193
194} // namespace clang::tidy::performance
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
static cl::opt< bool > Fix("fix", desc(R"( Apply suggested fixes. Without -fix-errors clang-tidy will bail out if any compilation errors were found. )"), cl::init(false), cl::cat(ClangTidyCategory))
const DeclRefExpr * DeclRef
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
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.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
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.
UnnecessaryValueParamCheck(StringRef Name, ClangTidyContext *Context)
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
std::optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
IncludeSorter::IncludeStyle getStyle() const
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:139
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
SmallPtrSet< const DeclRefExpr *, 16 > allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
Returns set of all DeclRefExprs to VarDecl within Stmt.
bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl, ASTContext &Context)
Returns true if DeclRefExpr is the argument of a copy-constructor call expression within Decl.
bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Decl &Decl, ASTContext &Context)
Returns true if DeclRefExpr is the argument of a copy-assignment operator CallExpr within Decl.
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.
std::optional< FixItHint > addQualifierToVarDecl(const VarDecl &Var, const ASTContext &Context, DeclSpec::TQ Qualifier, QualifierTarget QualTarget, QualifierPolicy QualPolicy)
Creates fix to qualify VarDecl with the specified Qualifier.
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
bool hasNonTrivialMoveAssignment(QualType Type)
Return true if Type has a non-trivial move assignment operator.
Definition: TypeTraits.cpp:156
bool hasNonTrivialMoveConstructor(QualType Type)
Returns true if Type has a non-trivial move constructor.
Definition: TypeTraits.cpp:150
llvm::StringMap< ClangTidyValue > OptionMap