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" 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(
'\''))
34 bool isReferencedOutsideOfCallExpr(
const FunctionDecl &Function,
35 ASTContext &Context) {
36 auto Matches =
match(declRefExpr(to(functionDecl(equalsNode(&Function))),
37 unless(hasAncestor(callExpr()))),
39 return !Matches.empty();
42 bool hasLoopStmtAncestor(
const DeclRefExpr &
DeclRef,
const Decl &Decl,
43 ASTContext &Context) {
45 match(decl(forEachDescendant(declRefExpr(
47 unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(),
48 whileStmt(), doStmt()))))))),
50 return Matches.empty();
53 bool isExplicitTemplateSpecialization(
const FunctionDecl &Function) {
54 if (
const auto *SpecializationInfo = Function.getTemplateSpecializationInfo())
55 if (SpecializationInfo->getTemplateSpecializationKind() ==
56 TSK_ExplicitSpecialization)
58 if (
const auto *Method = llvm::dyn_cast<CXXMethodDecl>(&Function))
59 if (
Method->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization &&
60 Method->getMemberSpecializationInfo()->isExplicitSpecialization())
67 UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
70 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
71 Options.getLocalOrGlobal(
"IncludeStyle",
"llvm"))),
80 const auto ExpensiveValueParamDecl = parmVarDecl(
83 unless(anyOf(hasCanonicalType(referenceType()),
84 hasDeclaration(namedDecl(
85 matchers::matchesAnyListedName(AllowedTypes))))))),
86 decl().bind(
"param"));
88 functionDecl(hasBody(stmt()), isDefinition(), unless(isImplicit()),
89 unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))),
90 has(typeLoc(forEach(ExpensiveValueParamDecl))),
91 unless(isInstantiated()), decl().bind(
"functionDecl")),
96 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
97 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"functionDecl");
99 FunctionParmMutationAnalyzer &Analyzer =
100 MutationAnalyzers.try_emplace(Function, *Function, *Result.Context)
102 if (Analyzer.isMutated(Param))
105 const bool IsConstQualified =
106 Param->getType().getCanonicalType().isConstQualified();
113 if (!IsConstQualified) {
115 *Param, *Function, *Result.Context);
116 if (AllDeclRefExprs.size() == 1) {
117 auto CanonicalType = Param->getType().getCanonicalType();
118 const auto &DeclRefExpr = **AllDeclRefExprs.begin();
120 if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
123 DeclRefExpr, *Function, *Result.Context)) ||
126 DeclRefExpr, *Function, *Result.Context)))) {
127 handleMoveFix(*Param, DeclRefExpr, *Result.Context);
133 const size_t Index = std::find(Function->parameters().begin(),
134 Function->parameters().end(), Param) -
135 Function->parameters().begin();
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);
152 const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
153 if (Param->getBeginLoc().isMacroID() || (Method && Method->isVirtual()) ||
154 isReferencedOutsideOfCallExpr(*Function, *Result.Context) ||
155 isExplicitTemplateSpecialization(*Function))
157 for (
const auto *FunctionDecl = Function; FunctionDecl !=
nullptr;
158 FunctionDecl = FunctionDecl->getPreviousDecl()) {
159 const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
165 if (!CurrentParam.getType().getCanonicalType().isConstQualified())
171 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
172 Inserter = llvm::make_unique<utils::IncludeInserter>(SM,
getLangOpts(),
174 PP->addPPCallbacks(Inserter->CreatePPCallbacks());
186 MutationAnalyzers.clear();
189 void UnnecessaryValueParamCheck::handleMoveFix(
const ParmVarDecl &Var,
190 const DeclRefExpr &CopyArgument,
191 const ASTContext &Context) {
192 auto Diag =
diag(CopyArgument.getBeginLoc(),
193 "parameter %0 is passed by value and only copied once; " 194 "consider moving it to avoid unnecessary copies")
197 if (CopyArgument.getBeginLoc().isMacroID())
199 const auto &SM = Context.getSourceManager();
200 auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
201 Context.getLangOpts());
202 Diag << FixItHint::CreateInsertion(CopyArgument.getBeginLoc(),
"std::move(")
203 << FixItHint::CreateInsertion(EndLoc,
")");
204 if (
auto IncludeFixit = Inserter->CreateIncludeInsertion(
205 SM.getFileID(CopyArgument.getBeginLoc()),
"utility",
207 Diag << *IncludeFixit;
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.
Base class for all clang-tidy checks.
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.
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.
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
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
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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'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.
const SymbolIndex * Index
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.