1 //===--- UnnecessaryValueParamCheck.cpp - clang-tidy-----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "UnnecessaryValueParamCheck.h"
12 #include "../utils/DeclRefExprUtils.h"
13 #include "../utils/FixItHintUtils.h"
14 #include "../utils/Matchers.h"
15 #include "../utils/TypeTraits.h"
16 #include "clang/Frontend/CompilerInstance.h"
17 #include "clang/Lex/Lexer.h"
18 #include "clang/Lex/Preprocessor.h"
20 using namespace clang::ast_matchers;
24 namespace performance {
28 std::string paramNameOrIndex(StringRef Name, size_t Index) {
29 return (Name.empty() ? llvm::Twine('#') + llvm::Twine(Index + 1)
30 : llvm::Twine('\'') + Name + llvm::Twine('\''))
35 bool isSubset(const S &SubsetCandidate, const S &SupersetCandidate) {
36 for (const auto &E : SubsetCandidate)
37 if (SupersetCandidate.count(E) == 0)
42 bool isReferencedOutsideOfCallExpr(const FunctionDecl &Function,
43 ASTContext &Context) {
44 auto Matches = match(declRefExpr(to(functionDecl(equalsNode(&Function))),
45 unless(hasAncestor(callExpr()))),
47 return !Matches.empty();
50 bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Decl &Decl,
51 ASTContext &Context) {
53 match(decl(forEachDescendant(declRefExpr(
55 unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(),
56 whileStmt(), doStmt()))))))),
58 return Matches.empty();
61 bool isExplicitTemplateSpecialization(const FunctionDecl &Function) {
62 if (const auto *SpecializationInfo = Function.getTemplateSpecializationInfo())
63 if (SpecializationInfo->getTemplateSpecializationKind() ==
64 TSK_ExplicitSpecialization)
66 if (const auto *Method = llvm::dyn_cast<CXXMethodDecl>(&Function))
67 if (Method->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization &&
68 Method->getMemberSpecializationInfo()->isExplicitSpecialization())
75 UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
76 StringRef Name, ClangTidyContext *Context)
77 : ClangTidyCheck(Name, Context),
78 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
79 Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
81 void UnnecessaryValueParamCheck::registerMatchers(MatchFinder *Finder) {
82 // This check is specific to C++ and doesn't apply to languages like
84 if (!getLangOpts().CPlusPlus)
86 const auto ExpensiveValueParamDecl =
87 parmVarDecl(hasType(hasCanonicalType(allOf(
88 unless(referenceType()), matchers::isExpensiveToCopy()))),
89 decl().bind("param"));
91 functionDecl(hasBody(stmt()), isDefinition(), unless(isImplicit()),
92 unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))),
93 has(typeLoc(forEach(ExpensiveValueParamDecl))),
94 unless(isInstantiated()), decl().bind("functionDecl")),
98 void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
99 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
100 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl");
101 const size_t Index = std::find(Function->parameters().begin(),
102 Function->parameters().end(), Param) -
103 Function->parameters().begin();
104 bool IsConstQualified =
105 Param->getType().getCanonicalType().isConstQualified();
107 auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs(
108 *Param, *Function, *Result.Context);
109 auto ConstDeclRefExprs = utils::decl_ref_expr::constReferenceDeclRefExprs(
110 *Param, *Function, *Result.Context);
112 // Do not trigger on non-const value parameters when they are not only used as
114 if (!isSubset(AllDeclRefExprs, ConstDeclRefExprs))
117 // If the parameter is non-const, check if it has a move constructor and is
118 // only referenced once to copy-construct another object or whether it has a
119 // move assignment operator and is only referenced once when copy-assigned.
120 // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary
122 if (!IsConstQualified && AllDeclRefExprs.size() == 1) {
123 auto CanonicalType = Param->getType().getCanonicalType();
124 const auto &DeclRefExpr = **AllDeclRefExprs.begin();
126 if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
127 ((utils::type_traits::hasNonTrivialMoveConstructor(CanonicalType) &&
128 utils::decl_ref_expr::isCopyConstructorArgument(
129 DeclRefExpr, *Function, *Result.Context)) ||
130 (utils::type_traits::hasNonTrivialMoveAssignment(CanonicalType) &&
131 utils::decl_ref_expr::isCopyAssignmentArgument(
132 DeclRefExpr, *Function, *Result.Context)))) {
133 handleMoveFix(*Param, DeclRefExpr, *Result.Context);
139 diag(Param->getLocation(),
140 IsConstQualified ? "the const qualified parameter %0 is "
141 "copied for each invocation; consider "
142 "making it a reference"
143 : "the parameter %0 is copied for each "
144 "invocation but only used as a const reference; "
145 "consider making it a const reference")
146 << paramNameOrIndex(Param->getName(), Index);
147 // Do not propose fixes when:
148 // 1. the ParmVarDecl is in a macro, since we cannot place them correctly
149 // 2. the function is virtual as it might break overrides
150 // 3. the function is referenced outside of a call expression within the
151 // compilation unit as the signature change could introduce build errors.
152 // 4. the function is an explicit template specialization.
153 const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
154 if (Param->getLocStart().isMacroID() || (Method && Method->isVirtual()) ||
155 isReferencedOutsideOfCallExpr(*Function, *Result.Context) ||
156 isExplicitTemplateSpecialization(*Function))
158 for (const auto *FunctionDecl = Function; FunctionDecl != nullptr;
159 FunctionDecl = FunctionDecl->getPreviousDecl()) {
160 const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
161 Diag << utils::fixit::changeVarDeclToReference(CurrentParam,
163 // The parameter of each declaration needs to be checked individually as to
164 // whether it is const or not as constness can differ between definition and
166 if (!CurrentParam.getType().getCanonicalType().isConstQualified())
167 Diag << utils::fixit::changeVarDeclToConst(CurrentParam);
171 void UnnecessaryValueParamCheck::registerPPCallbacks(
172 CompilerInstance &Compiler) {
173 Inserter.reset(new utils::IncludeInserter(
174 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
175 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
178 void UnnecessaryValueParamCheck::storeOptions(
179 ClangTidyOptions::OptionMap &Opts) {
180 Options.store(Opts, "IncludeStyle",
181 utils::IncludeSorter::toString(IncludeStyle));
184 void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
185 const DeclRefExpr &CopyArgument,
186 const ASTContext &Context) {
187 auto Diag = diag(CopyArgument.getLocStart(),
188 "parameter %0 is passed by value and only copied once; "
189 "consider moving it to avoid unnecessary copies")
191 // Do not propose fixes in macros since we cannot place them correctly.
192 if (CopyArgument.getLocStart().isMacroID())
194 const auto &SM = Context.getSourceManager();
195 auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
196 Context.getLangOpts());
197 Diag << FixItHint::CreateInsertion(CopyArgument.getLocStart(), "std::move(")
198 << FixItHint::CreateInsertion(EndLoc, ")");
199 if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
200 SM.getFileID(CopyArgument.getLocStart()), "utility",
202 Diag << *IncludeFixit;
205 } // namespace performance