11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/RecursiveASTVisitor.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/ASTMatchers/ASTMatchers.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Lex/Lexer.h"
17 #include "clang/Lex/Preprocessor.h"
19 using namespace clang::ast_matchers;
41 for (
const CXXConstructorDecl *Ctor : Node.ctors()) {
42 if (Ctor->isMoveConstructor() && !Ctor->isDeleted())
49 return lValueReferenceType(pointee(isConstQualified()));
53 return qualType(unless(anyOf(referenceType(), isConstQualified())));
60 const ParmVarDecl *ParamDecl) {
65 class ExactlyOneUsageVisitor
66 :
public RecursiveASTVisitor<ExactlyOneUsageVisitor> {
67 friend class RecursiveASTVisitor<ExactlyOneUsageVisitor>;
70 ExactlyOneUsageVisitor(
const ParmVarDecl *ParamDecl)
71 : ParamDecl(ParamDecl) {}
76 bool hasExactlyOneUsageIn(
const CXXConstructorDecl *Ctor) {
78 TraverseDecl(const_cast<CXXConstructorDecl *>(Ctor));
86 bool VisitDeclRefExpr(DeclRefExpr *D) {
87 if (
const ParmVarDecl *To = dyn_cast<ParmVarDecl>(D->getDecl())) {
88 if (To == ParamDecl) {
99 const ParmVarDecl *ParamDecl;
103 return ExactlyOneUsageVisitor(ParamDecl).hasExactlyOneUsageIn(Ctor);
108 static SmallVector<const ParmVarDecl *, 2>
110 const ParmVarDecl *ParamDecl) {
111 SmallVector<const ParmVarDecl *, 2> Results;
112 unsigned ParamIdx = ParamDecl->getFunctionScopeIndex();
114 for (
const FunctionDecl *Redecl : Ctor->redecls())
115 Results.push_back(Redecl->getParamDecl(ParamIdx));
121 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
122 Options.get(
"IncludeStyle",
"llvm"))) {}
137 forEachConstructorInitializer(
143 withInitializer(cxxConstructExpr(
144 has(ignoringParenImpCasts(declRefExpr(to(
152 hasDeclaration(cxxConstructorDecl(
153 isCopyConstructor(), unless(isDeleted()),
155 cxxRecordDecl(isMoveConstructible())))))))
156 .bind(
"Initializer")))
167 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
168 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
173 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"Ctor");
174 const auto *ParamDecl = Result.Nodes.getNodeAs<ParmVarDecl>(
"Param");
175 const auto *Initializer =
176 Result.Nodes.getNodeAs<CXXCtorInitializer>(
"Initializer");
177 SourceManager &
SM = *Result.SourceManager;
186 if (ParamDecl->getType().isTriviallyCopyableType(*Result.Context))
189 auto Diag =
diag(ParamDecl->getLocStart(),
"pass by value and use std::move");
193 auto ParamTL = ParmDecl->getTypeSourceInfo()->getTypeLoc();
194 auto RefTL = ParamTL.getAs<ReferenceTypeLoc>();
200 TypeLoc ValueTL = RefTL.getPointeeLoc();
201 auto TypeRange = CharSourceRange::getTokenRange(ParmDecl->getLocStart(),
202 ParamTL.getLocEnd());
203 std::string ValueStr =
204 Lexer::getSourceText(
205 CharSourceRange::getTokenRange(ValueTL.getSourceRange()), SM,
206 Result.Context->getLangOpts())
209 Diag << FixItHint::CreateReplacement(TypeRange, ValueStr);
213 Diag << FixItHint::CreateInsertion(Initializer->getRParenLoc(),
")")
214 << FixItHint::CreateInsertion(
215 Initializer->getLParenLoc().getLocWithOffset(1),
"std::move(");
217 if (
auto IncludeFixit = Inserter->CreateIncludeInsertion(
218 Result.SourceManager->getFileID(Initializer->getSourceLocation()),
221 Diag << *IncludeFixit;
static bool paramReferredExactlyOnce(const CXXConstructorDecl *Ctor, const ParmVarDecl *ParamDecl)
Whether or not ParamDecl is used exactly one time in Ctor.
LangOptions getLangOpts() const
Returns the language options from the context.
static TypeMatcher constRefType()
std::unique_ptr< ast_matchers::MatchFinder > Finder
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
static TypeMatcher nonConstValueType()
AST_MATCHER(CXXRecordDecl, isMoveConstructible)
Matches move-constructible classes.
Base class for all clang-tidy checks.
void registerPPCallbacks(clang::CompilerInstance &Compiler) override
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
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static SmallVector< const ParmVarDecl *, 2 > collectParamDecls(const CXXConstructorDecl *Ctor, const ParmVarDecl *ParamDecl)
Find all references to ParamDecl across all of the redeclarations of Ctor.
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.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.