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"
27std::string paramNameOrIndex(StringRef
Name,
size_t Index) {
28 return (
Name.empty() ? llvm::Twine(
'#') + llvm::Twine(Index + 1)
29 : llvm::Twine(
'\'') +
Name + llvm::Twine(
'\''))
33bool isReferencedOutsideOfCallExpr(
const FunctionDecl &Function,
34 ASTContext &Context) {
35 auto Matches =
match(declRefExpr(to(functionDecl(equalsNode(&Function))),
36 unless(hasAncestor(callExpr()))),
38 return !Matches.empty();
42 ASTContext &Context) {
45 decl(forEachDescendant(declRefExpr(
47 unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(),
48 whileStmt(), doStmt())))))))),
50 return Matches.empty();
58 Inserter(Options.getLocalOrGlobal(
"IncludeStyle",
59 utils::IncludeSorter::IS_LLVM),
60 areDiagsSelfContained()),
62 utils::options::parseStringList(Options.get(
"AllowedTypes",
""))) {}
65 const auto ExpensiveValueParamDecl = parmVarDecl(
67 hasCanonicalType(matchers::isExpensiveToCopy()),
68 unless(anyOf(hasCanonicalType(referenceType()),
69 hasDeclaration(namedDecl(
71 decl().bind(
"param"));
75 functionDecl(hasBody(stmt()), isDefinition(), unless(isImplicit()),
76 unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))),
77 has(typeLoc(forEach(ExpensiveValueParamDecl))),
78 decl().bind(
"functionDecl"))),
83 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
84 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"functionDecl");
86 TraversalKindScope RAII(*Result.Context, TK_AsIs);
88 FunctionParmMutationAnalyzer *Analyzer =
89 FunctionParmMutationAnalyzer::getFunctionParmMutationAnalyzer(
90 *Function, *Result.Context, MutationAnalyzerCache);
91 if (Analyzer->isMutated(Param))
94 const bool IsConstQualified =
95 Param->getType().getCanonicalType().isConstQualified();
102 if (!IsConstQualified) {
104 *Param, *Function, *Result.Context);
105 if (AllDeclRefExprs.size() == 1) {
106 auto CanonicalType = Param->getType().getCanonicalType();
107 const auto &DeclRefExpr = **AllDeclRefExprs.begin();
109 if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
112 DeclRefExpr, *Function, *Result.Context)) ||
115 DeclRefExpr, *Function, *Result.Context)))) {
126 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
138 MutationAnalyzerCache.clear();
142 const ParmVarDecl &Param,
143 ASTContext &Context) {
145 llvm::find(Function.parameters(), &Param) - Function.parameters().begin();
146 const bool IsConstQualified =
147 Param.getType().getCanonicalType().isConstQualified();
150 diag(Param.getLocation(),
151 "the %select{|const qualified }0parameter %1 is copied for each "
152 "invocation%select{ but only used as a const reference|}0; consider "
153 "making it a %select{const |}0reference")
154 << IsConstQualified << paramNameOrIndex(Param.getName(), Index);
161 const auto *Method = llvm::dyn_cast<CXXMethodDecl>(&Function);
162 if (Param.getBeginLoc().isMacroID() || (Method && Method->isVirtual()) ||
163 isReferencedOutsideOfCallExpr(Function, Context) ||
164 Function.getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
166 for (
const auto *FunctionDecl = &Function; FunctionDecl !=
nullptr;
167 FunctionDecl = FunctionDecl->getPreviousDecl()) {
168 const auto &CurrentParam = *FunctionDecl->getParamDecl(Index);
173 if (!CurrentParam.getType().getCanonicalType().isConstQualified()) {
175 CurrentParam, Context, DeclSpec::TQ::TQ_const))
182 const DeclRefExpr &CopyArgument,
183 ASTContext &Context) {
184 auto Diag =
diag(CopyArgument.getBeginLoc(),
185 "parameter %0 is passed by value and only copied once; "
186 "consider moving it to avoid unnecessary copies")
189 if (CopyArgument.getBeginLoc().isMacroID())
191 const auto &SM = Context.getSourceManager();
192 auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
193 Context.getLangOpts());
194 Diag << FixItHint::CreateInsertion(CopyArgument.getBeginLoc(),
"std::move(")
195 << FixItHint::CreateInsertion(EndLoc,
")")
197 SM.getFileID(CopyArgument.getBeginLoc()),
"<utility>");
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
static cl::opt< bool > Fix("fix", 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))
const DeclRefExpr * DeclRef
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
std::optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
IncludeSorter::IncludeStyle getStyle() const
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
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.
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.
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.
std::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.
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
bool hasNonTrivialMoveAssignment(QualType Type)
Return true if Type has a non-trivial move assignment operator.
bool hasNonTrivialMoveConstructor(QualType Type)
Returns true if Type has a non-trivial move constructor.
llvm::StringMap< ClangTidyValue > OptionMap