clang-tools  3.9.0
UnnecessaryValueParamCheck.cpp
Go to the documentation of this file.
1 //===--- 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 
11 
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"
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 template <typename S>
35 bool 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 
42 } // namespace
43 
44 UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
45  StringRef Name, ClangTidyContext *Context)
46  : ClangTidyCheck(Name, Context),
47  IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
48  Options.get("IncludeStyle", "llvm"))) {}
49 
51  const auto ExpensiveValueParamDecl =
52  parmVarDecl(hasType(hasCanonicalType(allOf(matchers::isExpensiveToCopy(),
53  unless(referenceType())))),
54  decl().bind("param"));
55  Finder->addMatcher(
56  functionDecl(isDefinition(), unless(cxxMethodDecl(isOverride())),
57  unless(isInstantiated()),
58  has(typeLoc(forEach(ExpensiveValueParamDecl))),
59  decl().bind("functionDecl")),
60  this);
61 }
62 
63 void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
64  const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
65  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl");
66  const size_t Index = std::find(Function->parameters().begin(),
67  Function->parameters().end(), Param) -
68  Function->parameters().begin();
69  bool IsConstQualified =
70  Param->getType().getCanonicalType().isConstQualified();
71 
72  // Skip declarations delayed by late template parsing without a body.
73  if (!Function->getBody())
74  return;
75 
76  // Do not trigger on non-const value parameters when:
77  // 1. they are in a constructor definition since they can likely trigger
78  // misc-move-constructor-init which will suggest to move the argument.
79  if (!IsConstQualified && (llvm::isa<CXXConstructorDecl>(Function) ||
80  !Function->doesThisDeclarationHaveABody()))
81  return;
82 
83  auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs(
84  *Param, *Function->getBody(), *Result.Context);
85  auto ConstDeclRefExprs = utils::decl_ref_expr::constReferenceDeclRefExprs(
86  *Param, *Function->getBody(), *Result.Context);
87  // 2. they are not only used as const.
88  if (!isSubset(AllDeclRefExprs, ConstDeclRefExprs))
89  return;
90 
91  // If the parameter is non-const, check if it has a move constructor and is
92  // only referenced once to copy-construct another object or whether it has a
93  // move assignment operator and is only referenced once when copy-assigned.
94  // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary
95  // copy.
96  if (!IsConstQualified) {
97  auto CanonicalType = Param->getType().getCanonicalType();
98  if (AllDeclRefExprs.size() == 1 &&
101  **AllDeclRefExprs.begin(), *Function->getBody(),
102  *Result.Context)) ||
105  **AllDeclRefExprs.begin(), *Function->getBody(),
106  *Result.Context)))) {
107  handleMoveFix(*Param, **AllDeclRefExprs.begin(), *Result.Context);
108  return;
109  }
110  }
111 
112  auto Diag =
113  diag(Param->getLocation(),
114  IsConstQualified ? "the const qualified parameter %0 is "
115  "copied for each invocation; consider "
116  "making it a reference"
117  : "the parameter %0 is copied for each "
118  "invocation but only used as a const reference; "
119  "consider making it a const reference")
120  << paramNameOrIndex(Param->getName(), Index);
121  // Do not propose fixes in macros since we cannot place them correctly, or if
122  // function is virtual as it might break overrides.
123  const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
124  if (Param->getLocStart().isMacroID() || (Method && Method->isVirtual()))
125  return;
126  for (const auto *FunctionDecl = Function; FunctionDecl != nullptr;
127  FunctionDecl = FunctionDecl->getPreviousDecl()) {
128  const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
129  Diag << utils::fixit::changeVarDeclToReference(CurrentParam,
130  *Result.Context);
131  if (!IsConstQualified)
132  Diag << utils::fixit::changeVarDeclToConst(CurrentParam);
133  }
134 }
135 
137  CompilerInstance &Compiler) {
138  Inserter.reset(new utils::IncludeInserter(
139  Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
140  Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
141 }
142 
145  Options.store(Opts, "IncludeStyle",
146  utils::IncludeSorter::toString(IncludeStyle));
147 }
148 
149 void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
150  const DeclRefExpr &CopyArgument,
151  const ASTContext &Context) {
152  auto Diag = diag(CopyArgument.getLocStart(),
153  "parameter %0 is passed by value and only copied once; "
154  "consider moving it to avoid unnecessary copies")
155  << &Var;
156  // Do not propose fixes in macros since we cannot place them correctly.
157  if (CopyArgument.getLocStart().isMacroID())
158  return;
159  const auto &SM = Context.getSourceManager();
160  auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
161  Context.getLangOpts());
162  Diag << FixItHint::CreateInsertion(CopyArgument.getLocStart(), "std::move(")
163  << FixItHint::CreateInsertion(EndLoc, ")");
164  if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
165  SM.getFileID(CopyArgument.getLocStart()), "utility",
166  /*IsAngled=*/true))
167  Diag << *IncludeFixit;
168 }
169 
170 } // namespace performance
171 } // namespace tidy
172 } // namespace clang
const std::string Name
Definition: USRFinder.cpp:140
SmallPtrSet< const DeclRefExpr *, 16 > allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
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...
bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Stmt &Stmt, ASTContext &Context)
Base class for all clang-tidy checks.
Definition: ClangTidy.h:110
bool hasNonTrivialMoveConstructor(QualType Type)
Returns true if Type has a non-trivial move constructor.
Definition: TypeTraits.cpp:127
SourceManager & SM
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
Definition: TypeTraits.cpp:42
bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Stmt &Stmt, ASTContext &Context)
SmallPtrSet< const DeclRefExpr *, 16 > constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
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: ClangTidy.cpp:385
std::map< std::string, std::string > OptionMap
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
Produces fixes to insert specified includes to source files, if not yet present.
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:352
bool hasNonTrivialMoveAssignment(QualType Type)
Return true if Type has a non-trivial move assignment operator.
Definition: TypeTraits.cpp:133
FixItHint changeVarDeclToConst(const VarDecl &Var)
Creates fix to make VarDecl const qualified.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
const NamedDecl * Result
Definition: USRFinder.cpp:137
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.