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"
20 using namespace clang::ast_matchers;
24 namespace performance {
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(
'\''))
35 bool isSubset(
const S &SubsetCandidate,
const S &SupersetCandidate) {
36 for (
const auto &E : SubsetCandidate)
37 if (SupersetCandidate.count(E) == 0)
44 UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
47 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
48 Options.get(
"IncludeStyle",
"llvm"))) {}
51 const auto ExpensiveValueParamDecl =
53 unless(referenceType())))),
54 decl().bind(
"param"));
56 functionDecl(isDefinition(), unless(cxxMethodDecl(isOverride())),
57 unless(isInstantiated()),
58 has(typeLoc(forEach(ExpensiveValueParamDecl))),
59 decl().bind(
"functionDecl")),
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();
73 if (!Function->getBody())
79 if (!IsConstQualified && (llvm::isa<CXXConstructorDecl>(Function) ||
80 !Function->doesThisDeclarationHaveABody()))
84 *Param, *Function->getBody(), *Result.Context);
86 *Param, *Function->getBody(), *Result.Context);
88 if (!isSubset(AllDeclRefExprs, ConstDeclRefExprs))
96 if (!IsConstQualified) {
97 auto CanonicalType = Param->getType().getCanonicalType();
98 if (AllDeclRefExprs.size() == 1 &&
101 **AllDeclRefExprs.begin(), *Function->getBody(),
105 **AllDeclRefExprs.begin(), *Function->getBody(),
106 *Result.Context)))) {
107 handleMoveFix(*Param, **AllDeclRefExprs.begin(), *Result.Context);
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);
123 const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
124 if (Param->getLocStart().isMacroID() || (Method && Method->isVirtual()))
126 for (
const auto *FunctionDecl = Function; FunctionDecl !=
nullptr;
127 FunctionDecl = FunctionDecl->getPreviousDecl()) {
128 const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
131 if (!IsConstQualified)
137 CompilerInstance &Compiler) {
139 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
140 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
149 void UnnecessaryValueParamCheck::handleMoveFix(
const ParmVarDecl &Var,
150 const DeclRefExpr &CopyArgument,
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")
157 if (CopyArgument.getLocStart().isMacroID())
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",
167 Diag << *IncludeFixit;
SmallPtrSet< const DeclRefExpr *, 16 > allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
std::unique_ptr< ast_matchers::MatchFinder > Finder
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Stmt &Stmt, ASTContext &Context)
Base class for all clang-tidy checks.
bool hasNonTrivialMoveConstructor(QualType Type)
Returns true if Type has a non-trivial move constructor.
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
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.
std::map< std::string, std::string > OptionMap
Produces fixes to insert specified includes to source files, if not yet present.
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
bool hasNonTrivialMoveAssignment(QualType Type)
Return true if Type has a non-trivial move assignment operator.
FixItHint changeVarDeclToConst(const VarDecl &Var)
Creates fix to make VarDecl const qualified.
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.