clang-tools  10.0.0
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 
20 using namespace clang::ast_matchers;
21 
22 namespace clang {
23 namespace tidy {
24 namespace performance {
25 
26 namespace {
27 
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('\''))
31  .str();
32 }
33 
34 bool isReferencedOutsideOfCallExpr(const FunctionDecl &Function,
35  ASTContext &Context) {
36  auto Matches = match(declRefExpr(to(functionDecl(equalsNode(&Function))),
37  unless(hasAncestor(callExpr()))),
38  Context);
39  return !Matches.empty();
40 }
41 
42 bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Decl &Decl,
43  ASTContext &Context) {
44  auto Matches =
45  match(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 bool isExplicitTemplateSpecialization(const FunctionDecl &Function) {
54  if (const auto *SpecializationInfo = Function.getTemplateSpecializationInfo())
55  if (SpecializationInfo->getTemplateSpecializationKind() ==
56  TSK_ExplicitSpecialization)
57  return true;
58  if (const auto *Method = llvm::dyn_cast<CXXMethodDecl>(&Function))
59  if (Method->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization &&
60  Method->getMemberSpecializationInfo()->isExplicitSpecialization())
61  return true;
62  return false;
63 }
64 
65 } // namespace
66 
67 UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
68  StringRef Name, ClangTidyContext *Context)
69  : ClangTidyCheck(Name, Context),
70  IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
71  Options.getLocalOrGlobal("IncludeStyle", "llvm"))),
72  AllowedTypes(
73  utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
74 
76  // This check is specific to C++ and doesn't apply to languages like
77  // Objective-C.
78  if (!getLangOpts().CPlusPlus)
79  return;
80  const auto ExpensiveValueParamDecl = parmVarDecl(
81  hasType(qualType(
82  hasCanonicalType(matchers::isExpensiveToCopy()),
83  unless(anyOf(hasCanonicalType(referenceType()),
84  hasDeclaration(namedDecl(
85  matchers::matchesAnyListedName(AllowedTypes))))))),
86  decl().bind("param"));
87  Finder->addMatcher(
88  functionDecl(hasBody(stmt()), isDefinition(), unless(isImplicit()),
89  unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))),
90  has(typeLoc(forEach(ExpensiveValueParamDecl))),
91  unless(isInstantiated()), decl().bind("functionDecl")),
92  this);
93 }
94 
95 void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
96  const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
97  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl");
98 
99  FunctionParmMutationAnalyzer &Analyzer =
100  MutationAnalyzers.try_emplace(Function, *Function, *Result.Context)
101  .first->second;
102  if (Analyzer.isMutated(Param))
103  return;
104 
105  const bool IsConstQualified =
106  Param->getType().getCanonicalType().isConstQualified();
107 
108  // If the parameter is non-const, check if it has a move constructor and is
109  // only referenced once to copy-construct another object or whether it has a
110  // move assignment operator and is only referenced once when copy-assigned.
111  // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary
112  // copy.
113  if (!IsConstQualified) {
114  auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs(
115  *Param, *Function, *Result.Context);
116  if (AllDeclRefExprs.size() == 1) {
117  auto CanonicalType = Param->getType().getCanonicalType();
118  const auto &DeclRefExpr = **AllDeclRefExprs.begin();
119 
120  if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
123  DeclRefExpr, *Function, *Result.Context)) ||
126  DeclRefExpr, *Function, *Result.Context)))) {
127  handleMoveFix(*Param, DeclRefExpr, *Result.Context);
128  return;
129  }
130  }
131  }
132 
133  const size_t Index = std::find(Function->parameters().begin(),
134  Function->parameters().end(), Param) -
135  Function->parameters().begin();
136 
137  auto Diag =
138  diag(Param->getLocation(),
139  IsConstQualified ? "the const qualified parameter %0 is "
140  "copied for each invocation; consider "
141  "making it a reference"
142  : "the parameter %0 is copied for each "
143  "invocation but only used as a const reference; "
144  "consider making it a const reference")
145  << paramNameOrIndex(Param->getName(), Index);
146  // Do not propose fixes when:
147  // 1. the ParmVarDecl is in a macro, since we cannot place them correctly
148  // 2. the function is virtual as it might break overrides
149  // 3. the function is referenced outside of a call expression within the
150  // compilation unit as the signature change could introduce build errors.
151  // 4. the function is an explicit template specialization.
152  const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
153  if (Param->getBeginLoc().isMacroID() || (Method && Method->isVirtual()) ||
154  isReferencedOutsideOfCallExpr(*Function, *Result.Context) ||
156  return;
157  for (const auto *FunctionDecl = Function; FunctionDecl != nullptr;
158  FunctionDecl = FunctionDecl->getPreviousDecl()) {
159  const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
160  Diag << utils::fixit::changeVarDeclToReference(CurrentParam,
161  *Result.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 (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl(
167  CurrentParam, *Result.Context, DeclSpec::TQ::TQ_const))
168  Diag << *Fix;
169  }
170  }
171 }
172 
174  const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
175  Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
176  IncludeStyle);
177  PP->addPPCallbacks(Inserter->CreatePPCallbacks());
178 }
179 
182  Options.store(Opts, "IncludeStyle",
183  utils::IncludeSorter::toString(IncludeStyle));
184  Options.store(Opts, "AllowedTypes",
186 }
187 
189  MutationAnalyzers.clear();
190 }
191 
192 void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
193  const DeclRefExpr &CopyArgument,
194  const ASTContext &Context) {
195  auto Diag = diag(CopyArgument.getBeginLoc(),
196  "parameter %0 is passed by value and only copied once; "
197  "consider moving it to avoid unnecessary copies")
198  << &Var;
199  // Do not propose fixes in macros since we cannot place them correctly.
200  if (CopyArgument.getBeginLoc().isMacroID())
201  return;
202  const auto &SM = Context.getSourceManager();
203  auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
204  Context.getLangOpts());
205  Diag << FixItHint::CreateInsertion(CopyArgument.getBeginLoc(), "std::move(")
206  << FixItHint::CreateInsertion(EndLoc, ")");
207  if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
208  SM.getFileID(CopyArgument.getBeginLoc()), "utility",
209  /*IsAngled=*/true))
210  Diag << *IncludeFixit;
211 }
212 
213 } // namespace performance
214 } // namespace tidy
215 } // namespace clang
const FunctionDecl * Decl
Some operations such as code completion produce a set of candidates.
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
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...
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Base class for all clang-tidy checks.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
const LangOptions & getLangOpts() const
Returns the language options from the context.
bool hasNonTrivialMoveConstructor(QualType Type)
Returns true if Type has a non-trivial move constructor.
Definition: TypeTraits.cpp:152
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
Definition: TypeTraits.cpp:41
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
bool isExplicitTemplateSpecialization(const NamedDecl *D)
Indicates if D is an explicit template specialization, e.g.
Definition: AST.cpp:153
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
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...
static cl::opt< bool > Fix("fix", cl::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))
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.
const DeclRefExpr * DeclRef
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
bool hasNonTrivialMoveAssignment(QualType Type)
Return true if Type has a non-trivial move assignment operator.
Definition: TypeTraits.cpp:158
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
const SymbolIndex * Index
Definition: Dexp.cpp:84
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.