blob: a34f7076ea7d1bb56efaad21c085afbb19f689e3 [file] [log] [blame]
Felix Berger3c8edde2016-03-29 02:42:38 +00001//===--- UnnecessaryValueParamCheck.cpp - clang-tidy-----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "UnnecessaryValueParamCheck.h"
11
12#include "../utils/DeclRefExprUtils.h"
13#include "../utils/FixItHintUtils.h"
14#include "../utils/Matchers.h"
Felix Berger7f882752016-07-01 20:12:15 +000015#include "../utils/TypeTraits.h"
16#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Lex/Lexer.h"
18#include "clang/Lex/Preprocessor.h"
Felix Berger3c8edde2016-03-29 02:42:38 +000019
20using namespace clang::ast_matchers;
21
22namespace clang {
23namespace tidy {
24namespace performance {
25
26namespace {
27
28std::string paramNameOrIndex(StringRef Name, size_t Index) {
29 return (Name.empty() ? llvm::Twine('#') + llvm::Twine(Index + 1)
30 : llvm::Twine('\'') + Name + llvm::Twine('\''))
31 .str();
32}
33
Felix Berger7f882752016-07-01 20:12:15 +000034template <typename S>
35bool isSubset(const S &SubsetCandidate, const S &SupersetCandidate) {
36 for (const auto &E : SubsetCandidate)
37 if (SupersetCandidate.count(E) == 0)
38 return false;
39 return true;
40}
41
Felix Berger85f9e8b32016-11-10 01:28:22 +000042bool isReferencedOutsideOfCallExpr(const FunctionDecl &Function,
43 ASTContext &Context) {
44 auto Matches = match(declRefExpr(to(functionDecl(equalsNode(&Function))),
45 unless(hasAncestor(callExpr()))),
46 Context);
47 return !Matches.empty();
48}
49
Malcolm Parsonscfa7d372017-01-03 12:10:44 +000050bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Decl &Decl,
Felix Berger519de4b2016-12-16 02:47:56 +000051 ASTContext &Context) {
52 auto Matches =
Malcolm Parsonscfa7d372017-01-03 12:10:44 +000053 match(decl(forEachDescendant(declRefExpr(
Felix Berger519de4b2016-12-16 02:47:56 +000054 equalsNode(&DeclRef),
55 unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(),
Malcolm Parsonscfa7d372017-01-03 12:10:44 +000056 whileStmt(), doStmt()))))))),
57 Decl, Context);
Felix Berger519de4b2016-12-16 02:47:56 +000058 return Matches.empty();
59}
60
Felix Berger3c8edde2016-03-29 02:42:38 +000061} // namespace
62
Felix Berger7f882752016-07-01 20:12:15 +000063UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
64 StringRef Name, ClangTidyContext *Context)
65 : ClangTidyCheck(Name, Context),
66 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
67 Options.get("IncludeStyle", "llvm"))) {}
68
Felix Berger3c8edde2016-03-29 02:42:38 +000069void UnnecessaryValueParamCheck::registerMatchers(MatchFinder *Finder) {
70 const auto ExpensiveValueParamDecl =
Alexander Kornienkob215d472017-05-16 17:28:17 +000071 parmVarDecl(hasType(hasCanonicalType(allOf(
72 unless(referenceType()), matchers::isExpensiveToCopy()))),
Felix Berger3c8edde2016-03-29 02:42:38 +000073 decl().bind("param"));
74 Finder->addMatcher(
Alexander Kornienkob215d472017-05-16 17:28:17 +000075 functionDecl(hasBody(stmt()), isDefinition(), unless(isImplicit()),
Felix Bergere4ab0602016-12-02 14:44:16 +000076 unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))),
Felix Berger3c8edde2016-03-29 02:42:38 +000077 has(typeLoc(forEach(ExpensiveValueParamDecl))),
Alexander Kornienkob215d472017-05-16 17:28:17 +000078 unless(isInstantiated()), decl().bind("functionDecl")),
Felix Berger3c8edde2016-03-29 02:42:38 +000079 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 const size_t Index = std::find(Function->parameters().begin(),
86 Function->parameters().end(), Param) -
87 Function->parameters().begin();
88 bool IsConstQualified =
89 Param->getType().getCanonicalType().isConstQualified();
90
Felix Berger7f882752016-07-01 20:12:15 +000091 auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs(
Malcolm Parsonscfa7d372017-01-03 12:10:44 +000092 *Param, *Function, *Result.Context);
Felix Berger7f882752016-07-01 20:12:15 +000093 auto ConstDeclRefExprs = utils::decl_ref_expr::constReferenceDeclRefExprs(
Malcolm Parsonscfa7d372017-01-03 12:10:44 +000094 *Param, *Function, *Result.Context);
95
96 // Do not trigger on non-const value parameters when they are not only used as
97 // const.
Felix Berger7f882752016-07-01 20:12:15 +000098 if (!isSubset(AllDeclRefExprs, ConstDeclRefExprs))
99 return;
100
101 // If the parameter is non-const, check if it has a move constructor and is
102 // only referenced once to copy-construct another object or whether it has a
103 // move assignment operator and is only referenced once when copy-assigned.
104 // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary
105 // copy.
Malcolm Parsonscfa7d372017-01-03 12:10:44 +0000106 if (!IsConstQualified && AllDeclRefExprs.size() == 1) {
Felix Berger7f882752016-07-01 20:12:15 +0000107 auto CanonicalType = Param->getType().getCanonicalType();
Malcolm Parsonscfa7d372017-01-03 12:10:44 +0000108 const auto &DeclRefExpr = **AllDeclRefExprs.begin();
109
110 if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
Felix Berger7f882752016-07-01 20:12:15 +0000111 ((utils::type_traits::hasNonTrivialMoveConstructor(CanonicalType) &&
112 utils::decl_ref_expr::isCopyConstructorArgument(
Malcolm Parsonscfa7d372017-01-03 12:10:44 +0000113 DeclRefExpr, *Function, *Result.Context)) ||
Felix Berger7f882752016-07-01 20:12:15 +0000114 (utils::type_traits::hasNonTrivialMoveAssignment(CanonicalType) &&
115 utils::decl_ref_expr::isCopyAssignmentArgument(
Malcolm Parsonscfa7d372017-01-03 12:10:44 +0000116 DeclRefExpr, *Function, *Result.Context)))) {
117 handleMoveFix(*Param, DeclRefExpr, *Result.Context);
Felix Berger7f882752016-07-01 20:12:15 +0000118 return;
119 }
120 }
121
Felix Berger3c8edde2016-03-29 02:42:38 +0000122 auto Diag =
123 diag(Param->getLocation(),
124 IsConstQualified ? "the const qualified parameter %0 is "
125 "copied for each invocation; consider "
126 "making it a reference"
127 : "the parameter %0 is copied for each "
128 "invocation but only used as a const reference; "
129 "consider making it a const reference")
130 << paramNameOrIndex(Param->getName(), Index);
Felix Berger85f9e8b32016-11-10 01:28:22 +0000131 // 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.
Felix Berger17934da2016-07-05 14:40:44 +0000136 const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
Felix Berger85f9e8b32016-11-10 01:28:22 +0000137 if (Param->getLocStart().isMacroID() || (Method && Method->isVirtual()) ||
138 isReferencedOutsideOfCallExpr(*Function, *Result.Context))
Felix Berger3c8edde2016-03-29 02:42:38 +0000139 return;
140 for (const auto *FunctionDecl = Function; FunctionDecl != nullptr;
141 FunctionDecl = FunctionDecl->getPreviousDecl()) {
142 const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +0000143 Diag << utils::fixit::changeVarDeclToReference(CurrentParam,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000144 *Result.Context);
Felix Berger7c6d2892016-11-04 20:51:31 +0000145 // The parameter of each declaration needs to be checked individually as to
146 // whether it is const or not as constness can differ between definition and
147 // declaration.
148 if (!CurrentParam.getType().getCanonicalType().isConstQualified())
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +0000149 Diag << utils::fixit::changeVarDeclToConst(CurrentParam);
Felix Berger3c8edde2016-03-29 02:42:38 +0000150 }
151}
152
Felix Berger7f882752016-07-01 20:12:15 +0000153void UnnecessaryValueParamCheck::registerPPCallbacks(
154 CompilerInstance &Compiler) {
155 Inserter.reset(new utils::IncludeInserter(
156 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
157 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
158}
159
160void UnnecessaryValueParamCheck::storeOptions(
161 ClangTidyOptions::OptionMap &Opts) {
162 Options.store(Opts, "IncludeStyle",
163 utils::IncludeSorter::toString(IncludeStyle));
164}
165
166void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
167 const DeclRefExpr &CopyArgument,
168 const ASTContext &Context) {
169 auto Diag = diag(CopyArgument.getLocStart(),
170 "parameter %0 is passed by value and only copied once; "
171 "consider moving it to avoid unnecessary copies")
172 << &Var;
173 // Do not propose fixes in macros since we cannot place them correctly.
174 if (CopyArgument.getLocStart().isMacroID())
175 return;
176 const auto &SM = Context.getSourceManager();
Kirill Bobyrev11cea452016-08-01 12:06:18 +0000177 auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
178 Context.getLangOpts());
Felix Berger7f882752016-07-01 20:12:15 +0000179 Diag << FixItHint::CreateInsertion(CopyArgument.getLocStart(), "std::move(")
180 << FixItHint::CreateInsertion(EndLoc, ")");
181 if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
182 SM.getFileID(CopyArgument.getLocStart()), "utility",
183 /*IsAngled=*/true))
184 Diag << *IncludeFixit;
185}
186
Felix Berger3c8edde2016-03-29 02:42:38 +0000187} // namespace performance
188} // namespace tidy
189} // namespace clang