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"
24 const CXXRecordDecl *Class) {
26 Class->friends(), [Friend](FriendDecl *FriendDecl) ->
bool {
27 if (TypeSourceInfo *FriendTypeSource = FriendDecl->getFriendType()) {
28 const QualType FriendType = FriendTypeSource->getType();
29 return FriendType->getAsCXXRecordDecl() == Friend;
60AST_MATCHER_P(CXXRecordDecl, isMoveConstructibleInBoundCXXRecordDecl, StringRef,
62 return Builder->removeBindings(
64 &Node](
const ast_matchers::internal::BoundNodesMap &Nodes) ->
bool {
65 const auto *BoundClass =
66 Nodes.getNode(this->RecordDeclID).get<CXXRecordDecl>();
67 for (
const CXXConstructorDecl *Ctor : Node.ctors()) {
68 if (Ctor->isMoveConstructor() && !Ctor->isDeleted() &&
69 (Ctor->getAccess() == AS_public ||
70 (BoundClass && isFirstFriendOfSecond(BoundClass, &Node))))
79 return lValueReferenceType(
80 pointee(unless(templateSpecializationType()), isConstQualified()));
84 return qualType(unless(anyOf(referenceType(), isConstQualified())));
91 const ParmVarDecl *ParamDecl) {
96 class ExactlyOneUsageVisitor
97 :
public RecursiveASTVisitor<ExactlyOneUsageVisitor> {
98 friend class RecursiveASTVisitor<ExactlyOneUsageVisitor>;
101 ExactlyOneUsageVisitor(
const ParmVarDecl *ParamDecl)
102 : ParamDecl(ParamDecl) {}
107 bool hasExactlyOneUsageIn(
const CXXConstructorDecl *Ctor) {
109 TraverseDecl(
const_cast<CXXConstructorDecl *
>(Ctor));
117 bool VisitDeclRefExpr(DeclRefExpr *D) {
118 if (
const ParmVarDecl *To = dyn_cast<ParmVarDecl>(D->getDecl())) {
119 if (To == ParamDecl) {
130 const ParmVarDecl *ParamDecl;
134 return ExactlyOneUsageVisitor(ParamDecl).hasExactlyOneUsageIn(Ctor);
161 const ParmVarDecl *Param) {
162 if (!Param->getType().getCanonicalType()->isLValueReferenceType()) {
166 const int ParamIdx = Param->getFunctionScopeIndex();
167 const CXXRecordDecl *Record = Ctor->getParent();
171 const auto IsRValueOverload = [&Ctor, ParamIdx](
const CXXConstructorDecl *C) {
172 if (C == Ctor || C->isDeleted() ||
173 C->getNumParams() != Ctor->getNumParams())
175 for (
int I = 0, E = C->getNumParams(); I < E; ++I) {
176 const clang::QualType CandidateParamType =
177 C->parameters()[I]->getType().getCanonicalType();
178 const clang::QualType CtorParamType =
179 Ctor->parameters()[I]->getType().getCanonicalType();
180 const bool IsLValueRValuePair =
181 CtorParamType->isLValueReferenceType() &&
182 CandidateParamType->isRValueReferenceType() &&
183 CandidateParamType->getPointeeType()->getUnqualifiedDesugaredType() ==
184 CtorParamType->getPointeeType()->getUnqualifiedDesugaredType();
187 if (!IsLValueRValuePair)
191 if (!(CandidateParamType == CtorParamType || IsLValueRValuePair))
198 for (
const auto *Candidate : Record->ctors()) {
199 if (IsRValueOverload(Candidate))
207static SmallVector<const ParmVarDecl *, 2>
209 const ParmVarDecl *ParamDecl) {
210 SmallVector<const ParmVarDecl *, 2> Results;
211 unsigned ParamIdx = ParamDecl->getFunctionScopeIndex();
213 for (
const FunctionDecl *Redecl : Ctor->redecls())
214 Results.push_back(Redecl->getParamDecl(ParamIdx));
220 Inserter(Options.getLocalOrGlobal(
"IncludeStyle",
221 utils::IncludeSorter::IS_LLVM),
222 areDiagsSelfContained()),
223 ValuesOnly(Options.get(
"ValuesOnly", false)) {}
226 Options.store(Opts,
"IncludeStyle", Inserter.getStyle());
227 Options.store(Opts,
"ValuesOnly", ValuesOnly);
235 ofClass(cxxRecordDecl().bind(
"outer")),
236 forEachConstructorInitializer(
238 unless(isBaseInitializer()),
244 withInitializer(cxxConstructExpr(
245 has(ignoringParenImpCasts(declRefExpr(to(
257 hasDeclaration(cxxConstructorDecl(
258 isCopyConstructor(), unless(isDeleted()),
259 hasDeclContext(cxxRecordDecl(
260 isMoveConstructibleInBoundCXXRecordDecl(
262 .bind(
"Initializer")))
269 Preprocessor *ModuleExpanderPP) {
270 Inserter.registerPreprocessor(PP);
274 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"Ctor");
275 const auto *ParamDecl = Result.Nodes.getNodeAs<ParmVarDecl>(
"Param");
276 const auto *Initializer =
277 Result.Nodes.getNodeAs<CXXCtorInitializer>(
"Initializer");
278 SourceManager &SM = *Result.SourceManager;
287 if (ParamDecl->getType().getNonReferenceType().isTriviallyCopyableType(
295 auto Diag = diag(ParamDecl->getBeginLoc(),
"pass by value and use std::move");
299 if (ParamDecl->getType()->isLValueReferenceType()) {
302 TypeLoc ParamTL = ParmDecl->getTypeSourceInfo()->getTypeLoc();
303 auto RefTL = ParamTL.getAs<ReferenceTypeLoc>();
304 if (RefTL.isNull()) {
312 TypeLoc ParamTL = ParmDecl->getTypeSourceInfo()->getTypeLoc();
313 auto RefTL = ParamTL.getAs<ReferenceTypeLoc>();
315 TypeLoc ValueTL = RefTL.getPointeeLoc();
316 CharSourceRange TypeRange = CharSourceRange::getTokenRange(
317 ParmDecl->getBeginLoc(), ParamTL.getEndLoc());
318 std::string ValueStr =
319 Lexer::getSourceText(
320 CharSourceRange::getTokenRange(ValueTL.getSourceRange()), SM,
324 Diag << FixItHint::CreateReplacement(TypeRange, ValueStr);
329 Diag << FixItHint::CreateInsertion(Initializer->getRParenLoc(),
")")
330 << FixItHint::CreateInsertion(
331 Initializer->getLParenLoc().getLocWithOffset(1),
"std::move(")
332 << Inserter.createIncludeInsertion(
333 Result.SourceManager->getFileID(Initializer->getSourceLocation()),
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
PassByValueCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
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 isFirstFriendOfSecond(const CXXRecordDecl *Friend, const CXXRecordDecl *Class)
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