clang-tools  15.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 
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 = match(
45  traverse(TK_AsIs,
46  decl(forEachDescendant(declRefExpr(
47  equalsNode(&DeclRef),
48  unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(),
49  whileStmt(), doStmt())))))))),
50  Decl, Context);
51  return Matches.empty();
52 }
53 
54 bool isExplicitTemplateSpecialization(const FunctionDecl &Function) {
55  if (const auto *SpecializationInfo = Function.getTemplateSpecializationInfo())
56  if (SpecializationInfo->getTemplateSpecializationKind() ==
57  TSK_ExplicitSpecialization)
58  return true;
59  if (const auto *Method = llvm::dyn_cast<CXXMethodDecl>(&Function))
60  if (Method->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization &&
61  Method->getMemberSpecializationInfo()->isExplicitSpecialization())
62  return true;
63  return false;
64 }
65 
66 } // namespace
67 
68 UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
69  StringRef Name, ClangTidyContext *Context)
70  : ClangTidyCheck(Name, Context),
71  Inserter(Options.getLocalOrGlobal("IncludeStyle",
72  utils::IncludeSorter::IS_LLVM),
73  areDiagsSelfContained()),
74  AllowedTypes(
75  utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
76 
78  const auto ExpensiveValueParamDecl = parmVarDecl(
79  hasType(qualType(
80  hasCanonicalType(matchers::isExpensiveToCopy()),
81  unless(anyOf(hasCanonicalType(referenceType()),
82  hasDeclaration(namedDecl(
83  matchers::matchesAnyListedName(AllowedTypes))))))),
84  decl().bind("param"));
85  Finder->addMatcher(
86  traverse(
87  TK_AsIs,
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  TraversalKindScope RAII(*Result.Context, TK_AsIs);
100 
101  FunctionParmMutationAnalyzer &Analyzer =
102  MutationAnalyzers.try_emplace(Function, *Function, *Result.Context)
103  .first->second;
104  if (Analyzer.isMutated(Param))
105  return;
106 
107  const bool IsConstQualified =
108  Param->getType().getCanonicalType().isConstQualified();
109 
110  // If the parameter is non-const, check if it has a move constructor and is
111  // only referenced once to copy-construct another object or whether it has a
112  // move assignment operator and is only referenced once when copy-assigned.
113  // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary
114  // copy.
115  if (!IsConstQualified) {
116  auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs(
117  *Param, *Function, *Result.Context);
118  if (AllDeclRefExprs.size() == 1) {
119  auto CanonicalType = Param->getType().getCanonicalType();
120  const auto &DeclRefExpr = **AllDeclRefExprs.begin();
121 
122  if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
125  DeclRefExpr, *Function, *Result.Context)) ||
128  DeclRefExpr, *Function, *Result.Context)))) {
129  handleMoveFix(*Param, DeclRefExpr, *Result.Context);
130  return;
131  }
132  }
133  }
134 
135  const size_t Index = std::find(Function->parameters().begin(),
136  Function->parameters().end(), Param) -
137  Function->parameters().begin();
138 
139  auto Diag =
140  diag(Param->getLocation(),
141  "the %select{|const qualified }0parameter %1 is copied for each "
142  "invocation%select{ but only used as a const reference|}0; consider "
143  "making it a %select{const |}0reference")
144  << IsConstQualified << paramNameOrIndex(Param->getName(), Index);
145  // Do not propose fixes when:
146  // 1. the ParmVarDecl is in a macro, since we cannot place them correctly
147  // 2. the function is virtual as it might break overrides
148  // 3. the function is referenced outside of a call expression within the
149  // compilation unit as the signature change could introduce build errors.
150  // 4. the function is an explicit template specialization.
151  const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
152  if (Param->getBeginLoc().isMacroID() || (Method && Method->isVirtual()) ||
153  isReferencedOutsideOfCallExpr(*Function, *Result.Context) ||
155  return;
156  for (const auto *FunctionDecl = Function; FunctionDecl != nullptr;
157  FunctionDecl = FunctionDecl->getPreviousDecl()) {
158  const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
159  Diag << utils::fixit::changeVarDeclToReference(CurrentParam,
160  *Result.Context);
161  // The parameter of each declaration needs to be checked individually as to
162  // whether it is const or not as constness can differ between definition and
163  // declaration.
164  if (!CurrentParam.getType().getCanonicalType().isConstQualified()) {
165  if (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl(
166  CurrentParam, *Result.Context, DeclSpec::TQ::TQ_const))
167  Diag << *Fix;
168  }
169  }
170 }
171 
173  const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
174  Inserter.registerPreprocessor(PP);
175 }
176 
179  Options.store(Opts, "IncludeStyle", Inserter.getStyle());
180  Options.store(Opts, "AllowedTypes",
182 }
183 
185  MutationAnalyzers.clear();
186 }
187 
188 void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
189  const DeclRefExpr &CopyArgument,
190  const ASTContext &Context) {
191  auto Diag = diag(CopyArgument.getBeginLoc(),
192  "parameter %0 is passed by value and only copied once; "
193  "consider moving it to avoid unnecessary copies")
194  << &Var;
195  // Do not propose fixes in macros since we cannot place them correctly.
196  if (CopyArgument.getBeginLoc().isMacroID())
197  return;
198  const auto &SM = Context.getSourceManager();
199  auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
200  Context.getLangOpts());
201  Diag << FixItHint::CreateInsertion(CopyArgument.getBeginLoc(), "std::move(")
202  << FixItHint::CreateInsertion(EndLoc, ")")
203  << Inserter.createIncludeInsertion(
204  SM.getFileID(CopyArgument.getBeginLoc()), "<utility>");
205 }
206 
207 } // namespace performance
208 } // namespace tidy
209 } // namespace clang
clang::tidy::utils::decl_ref_expr::allDeclRefExprs
SmallPtrSet< const DeclRefExpr *, 16 > allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
Returns set of all DeclRefExprs to VarDecl within Stmt.
Definition: DeclRefExprUtils.cpp:101
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::utils::fixit::addQualifierToVarDecl
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.
Definition: FixItHintUtils.cpp:184
clang::tidy::performance::UnnecessaryValueParamCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UnnecessaryValueParamCheck.cpp:77
clang::tidy::utils::IncludeInserter::registerPreprocessor
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
Definition: IncludeInserter.cpp:43
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:53
clang::tidy::utils::type_traits::isExpensiveToCopy
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
Definition: TypeTraits.cpp:41
clang::tidy::performance::UnnecessaryValueParamCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: UnnecessaryValueParamCheck.cpp:177
clang::tidy::utils::fixit::changeVarDeclToReference
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.
Definition: FixItHintUtils.cpp:19
Fix
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))
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::clangd::match
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:102
clang::tidy::performance::UnnecessaryValueParamCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: UnnecessaryValueParamCheck.cpp:172
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::clangd::CompletionItemKind::Function
@ Function
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:415
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:66
clang::tidy::utils::type_traits::hasNonTrivialMoveConstructor
bool hasNonTrivialMoveConstructor(QualType Type)
Returns true if Type has a non-trivial move constructor.
Definition: TypeTraits.cpp:152
clang::tidy::utils::decl_ref_expr::isCopyAssignmentArgument
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.
Definition: DeclRefExprUtils.cpp:135
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang::clangd::CompletionItemKind::Method
@ Method
clang::tidy::performance::UnnecessaryValueParamCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: UnnecessaryValueParamCheck.cpp:184
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
Index
const SymbolIndex * Index
Definition: Dexp.cpp:98
clang::tidy::utils::decl_ref_expr::isCopyConstructorArgument
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.
Definition: DeclRefExprUtils.cpp:121
UnnecessaryValueParamCheck.h
DeclRef
const DeclRefExpr * DeclRef
Definition: UseAfterMoveCheck.cpp:50
clang::tidy::matchers::matchesAnyListedName
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
Definition: clang-tidy/utils/Matchers.h:119
clang::tidy::utils::IncludeInserter::getStyle
IncludeSorter::IncludeStyle getStyle() const
Definition: IncludeInserter.h:85
clang::tidy::utils::options::serializeStringList
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Definition: OptionsUtils.cpp:62
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::options::parseStringList
std::vector< StringRef > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:19
clang::tidy::utils::IncludeInserter::createIncludeInsertion
llvm::Optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
Definition: IncludeInserter.cpp:71
clang::clangd::isExplicitTemplateSpecialization
bool isExplicitTemplateSpecialization(const NamedDecl *D)
Indicates if D is an explicit template specialization, e.g.
Definition: AST.cpp:156
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:120
clang::tidy::utils::type_traits::hasNonTrivialMoveAssignment
bool hasNonTrivialMoveAssignment(QualType Type)
Return true if Type has a non-trivial move assignment operator.
Definition: TypeTraits.cpp:158
clang::tidy::performance::UnnecessaryValueParamCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UnnecessaryValueParamCheck.cpp:95