10#include "clang/AST/ASTContext.h"
11#include "clang/AST/RecursiveASTVisitor.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Lex/Lexer.h"
16#include "clang/Lex/Preprocessor.h"
39 for (
const CXXConstructorDecl *Ctor :
Node.ctors()) {
40 if (Ctor->isMoveConstructor() && !Ctor->isDeleted())
48 return lValueReferenceType(
49 pointee(unless(elaboratedType(namesType(templateSpecializationType()))),
54 return qualType(unless(anyOf(referenceType(), isConstQualified())));
61 const ParmVarDecl *ParamDecl) {
66 class ExactlyOneUsageVisitor
71 ExactlyOneUsageVisitor(
const ParmVarDecl *ParamDecl)
72 : ParamDecl(ParamDecl) {}
77 bool hasExactlyOneUsageIn(
const CXXConstructorDecl *Ctor) {
79 TraverseDecl(
const_cast<CXXConstructorDecl *
>(Ctor));
87 bool VisitDeclRefExpr(DeclRefExpr *D) {
88 if (
const ParmVarDecl *To = dyn_cast<ParmVarDecl>(D->getDecl())) {
89 if (To == ParamDecl) {
100 const ParmVarDecl *ParamDecl;
104 return ExactlyOneUsageVisitor(ParamDecl).hasExactlyOneUsageIn(Ctor);
131 const ParmVarDecl *Param) {
132 if (!Param->getType().getCanonicalType()->isLValueReferenceType()) {
136 const int ParamIdx = Param->getFunctionScopeIndex();
137 const CXXRecordDecl *Record = Ctor->getParent();
141 const auto IsRValueOverload = [&Ctor, ParamIdx](
const CXXConstructorDecl *
C) {
142 if (
C == Ctor ||
C->isDeleted() ||
143 C->getNumParams() != Ctor->getNumParams())
145 for (
int I = 0,
E =
C->getNumParams(); I <
E; ++I) {
146 const clang::QualType CandidateParamType =
147 C->parameters()[I]->getType().getCanonicalType();
148 const clang::QualType CtorParamType =
149 Ctor->parameters()[I]->getType().getCanonicalType();
150 const bool IsLValueRValuePair =
151 CtorParamType->isLValueReferenceType() &&
152 CandidateParamType->isRValueReferenceType() &&
153 CandidateParamType->getPointeeType()->getUnqualifiedDesugaredType() ==
154 CtorParamType->getPointeeType()->getUnqualifiedDesugaredType();
157 if (!IsLValueRValuePair)
161 if (!(CandidateParamType == CtorParamType || IsLValueRValuePair))
168 for (
const auto *
Candidate : Record->ctors()) {
178static SmallVector<const ParmVarDecl *, 2>
180 const ParmVarDecl *ParamDecl) {
181 SmallVector<const ParmVarDecl *, 2>
Results;
182 unsigned ParamIdx = ParamDecl->getFunctionScopeIndex();
184 for (
const FunctionDecl *Redecl : Ctor->redecls())
185 Results.push_back(Redecl->getParamDecl(ParamIdx));
191 Inserter(Options.getLocalOrGlobal(
"IncludeStyle",
192 utils::IncludeSorter::IS_LLVM),
193 areDiagsSelfContained()),
194 ValuesOnly(Options.get(
"ValuesOnly", false)) {}
206 forEachConstructorInitializer(
208 unless(isBaseInitializer()),
214 withInitializer(cxxConstructExpr(
215 has(ignoringParenImpCasts(declRefExpr(to(
227 hasDeclaration(cxxConstructorDecl(
228 isCopyConstructor(), unless(isDeleted()),
230 cxxRecordDecl(isMoveConstructible())))))))
231 .bind(
"Initializer")))
238 Preprocessor *ModuleExpanderPP) {
243 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"Ctor");
244 const auto *ParamDecl = Result.Nodes.getNodeAs<ParmVarDecl>(
"Param");
245 const auto *Initializer =
246 Result.Nodes.getNodeAs<CXXCtorInitializer>(
"Initializer");
247 SourceManager &SM = *Result.SourceManager;
256 if (ParamDecl->getType().getNonReferenceType().isTriviallyCopyableType(
264 auto Diag =
diag(ParamDecl->getBeginLoc(),
"pass by value and use std::move");
268 if (ParamDecl->getType()->isLValueReferenceType()) {
271 TypeLoc ParamTL = ParmDecl->getTypeSourceInfo()->getTypeLoc();
272 auto RefTL = ParamTL.getAs<ReferenceTypeLoc>();
273 if (RefTL.isNull()) {
281 TypeLoc ParamTL = ParmDecl->getTypeSourceInfo()->getTypeLoc();
282 auto RefTL = ParamTL.getAs<ReferenceTypeLoc>();
284 TypeLoc ValueTL = RefTL.getPointeeLoc();
285 CharSourceRange TypeRange = CharSourceRange::getTokenRange(
286 ParmDecl->getBeginLoc(), ParamTL.getEndLoc());
287 std::string ValueStr =
288 Lexer::getSourceText(
289 CharSourceRange::getTokenRange(ValueTL.getSourceRange()), SM,
293 Diag << FixItHint::CreateReplacement(TypeRange, ValueStr);
298 Diag << FixItHint::CreateInsertion(Initializer->getRParenLoc(),
")")
299 << FixItHint::CreateInsertion(
300 Initializer->getLParenLoc().getLocWithOffset(1),
"std::move(")
302 Result.SourceManager->getFileID(Initializer->getSourceLocation()),
llvm::SmallString< 256U > Name
std::vector< CodeCompletionResult > Results
::clang::DynTypedNode Node
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.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
PassByValueCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
static SmallVector< const ParmVarDecl *, 2 > collectParamDecls(const CXXConstructorDecl *Ctor, const ParmVarDecl *ParamDecl)
Find all references to ParamDecl across all of the redeclarations of Ctor.
static bool hasRValueOverload(const CXXConstructorDecl *Ctor, const ParmVarDecl *Param)
Returns true if the given constructor is part of a lvalue/rvalue reference pair, i....
static bool paramReferredExactlyOnce(const CXXConstructorDecl *Ctor, const ParmVarDecl *ParamDecl)
Whether or not ParamDecl is used exactly one time in Ctor.
static TypeMatcher nonConstValueType()
static TypeMatcher notTemplateSpecConstRefType()
Some operations such as code completion produce a set of candidates.
llvm::StringMap< ClangTidyValue > OptionMap