clang-tools  8.0.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/OptionsUtils.h"
16 #include "../utils/TypeTraits.h"
17 #include "clang/Frontend/CompilerInstance.h"
18 #include "clang/Lex/Lexer.h"
19 #include "clang/Lex/Preprocessor.h"
20 
21 using namespace clang::ast_matchers;
22 
23 namespace clang {
24 namespace tidy {
25 namespace performance {
26 
27 namespace {
28 
29 std::string paramNameOrIndex(StringRef Name, size_t Index) {
30  return (Name.empty() ? llvm::Twine('#') + llvm::Twine(Index + 1)
31  : llvm::Twine('\'') + Name + llvm::Twine('\''))
32  .str();
33 }
34 
35 bool isReferencedOutsideOfCallExpr(const FunctionDecl &Function,
36  ASTContext &Context) {
37  auto Matches = match(declRefExpr(to(functionDecl(equalsNode(&Function))),
38  unless(hasAncestor(callExpr()))),
39  Context);
40  return !Matches.empty();
41 }
42 
43 bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Decl &Decl,
44  ASTContext &Context) {
45  auto Matches =
46  match(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  IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
72  Options.getLocalOrGlobal("IncludeStyle", "llvm"))),
73  AllowedTypes(
74  utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
75 
77  // This check is specific to C++ and doesn't apply to languages like
78  // Objective-C.
79  if (!getLangOpts().CPlusPlus)
80  return;
81  const auto ExpensiveValueParamDecl = parmVarDecl(
82  hasType(qualType(
83  hasCanonicalType(matchers::isExpensiveToCopy()),
84  unless(anyOf(hasCanonicalType(referenceType()),
85  hasDeclaration(namedDecl(
86  matchers::matchesAnyListedName(AllowedTypes))))))),
87  decl().bind("param"));
88  Finder->addMatcher(
89  functionDecl(hasBody(stmt()), isDefinition(), unless(isImplicit()),
90  unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))),
91  has(typeLoc(forEach(ExpensiveValueParamDecl))),
92  unless(isInstantiated()), decl().bind("functionDecl")),
93  this);
94 }
95 
96 void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
97  const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
98  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl");
99 
100  FunctionParmMutationAnalyzer &Analyzer =
101  MutationAnalyzers.try_emplace(Function, *Function, *Result.Context)
102  .first->second;
103  if (Analyzer.isMutated(Param))
104  return;
105 
106  const bool IsConstQualified =
107  Param->getType().getCanonicalType().isConstQualified();
108 
109  // If the parameter is non-const, check if it has a move constructor and is
110  // only referenced once to copy-construct another object or whether it has a
111  // move assignment operator and is only referenced once when copy-assigned.
112  // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary
113  // copy.
114  if (!IsConstQualified) {
115  auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs(
116  *Param, *Function, *Result.Context);
117  if (AllDeclRefExprs.size() == 1) {
118  auto CanonicalType = Param->getType().getCanonicalType();
119  const auto &DeclRefExpr = **AllDeclRefExprs.begin();
120 
121  if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
124  DeclRefExpr, *Function, *Result.Context)) ||
127  DeclRefExpr, *Function, *Result.Context)))) {
128  handleMoveFix(*Param, DeclRefExpr, *Result.Context);
129  return;
130  }
131  }
132  }
133 
134  const size_t Index = std::find(Function->parameters().begin(),
135  Function->parameters().end(), Param) -
136  Function->parameters().begin();
137 
138  auto Diag =
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->getBeginLoc().isMacroID() || (Method && Method->isVirtual()) ||
155  isReferencedOutsideOfCallExpr(*Function, *Result.Context) ||
156  isExplicitTemplateSpecialization(*Function))
157  return;
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,
162  *Result.Context);
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
165  // declaration.
166  if (!CurrentParam.getType().getCanonicalType().isConstQualified())
167  Diag << utils::fixit::changeVarDeclToConst(CurrentParam);
168  }
169 }
170 
172  CompilerInstance &Compiler) {
173  Inserter.reset(new utils::IncludeInserter(
174  Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
175  Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
176 }
177 
180  Options.store(Opts, "IncludeStyle",
181  utils::IncludeSorter::toString(IncludeStyle));
182  Options.store(Opts, "AllowedTypes",
184 }
185 
187  MutationAnalyzers.clear();
188 }
189 
190 void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
191  const DeclRefExpr &CopyArgument,
192  const ASTContext &Context) {
193  auto Diag = diag(CopyArgument.getBeginLoc(),
194  "parameter %0 is passed by value and only copied once; "
195  "consider moving it to avoid unnecessary copies")
196  << &Var;
197  // Do not propose fixes in macros since we cannot place them correctly.
198  if (CopyArgument.getBeginLoc().isMacroID())
199  return;
200  const auto &SM = Context.getSourceManager();
201  auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
202  Context.getLangOpts());
203  Diag << FixItHint::CreateInsertion(CopyArgument.getBeginLoc(), "std::move(")
204  << FixItHint::CreateInsertion(EndLoc, ")");
205  if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
206  SM.getFileID(CopyArgument.getBeginLoc()), "utility",
207  /*IsAngled=*/true))
208  Diag << *IncludeFixit;
209 }
210 
211 } // namespace performance
212 } // namespace tidy
213 } // namespace clang
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:473
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.
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
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.
Definition: ClangTidy.h:127
bool hasNonTrivialMoveConstructor(QualType Type)
Returns true if Type has a non-trivial move constructor.
Definition: TypeTraits.cpp:135
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:42
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.
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...
const DeclRefExpr * DeclRef
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438
bool hasNonTrivialMoveAssignment(QualType Type)
Return true if Type has a non-trivial move assignment operator.
Definition: TypeTraits.cpp:141
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 SymbolIndex * Index
Definition: Dexp.cpp:85
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.