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" 42 for (
const CXXConstructorDecl *Ctor : Node.ctors()) {
43 if (Ctor->isMoveConstructor() && !Ctor->isDeleted())
51 return lValueReferenceType(pointee(isConstQualified()));
55 return qualType(unless(anyOf(referenceType(), isConstQualified())));
62 const ParmVarDecl *ParamDecl) {
67 class ExactlyOneUsageVisitor
68 :
public RecursiveASTVisitor<ExactlyOneUsageVisitor> {
69 friend class RecursiveASTVisitor<ExactlyOneUsageVisitor>;
72 ExactlyOneUsageVisitor(
const ParmVarDecl *ParamDecl)
73 : ParamDecl(ParamDecl) {}
78 bool hasExactlyOneUsageIn(
const CXXConstructorDecl *Ctor) {
80 TraverseDecl(const_cast<CXXConstructorDecl *>(Ctor));
88 bool VisitDeclRefExpr(DeclRefExpr *
D) {
89 if (
const ParmVarDecl *To = dyn_cast<ParmVarDecl>(D->getDecl())) {
90 if (To == ParamDecl) {
101 const ParmVarDecl *ParamDecl;
105 return ExactlyOneUsageVisitor(ParamDecl).hasExactlyOneUsageIn(Ctor);
110 static SmallVector<const ParmVarDecl *, 2>
112 const ParmVarDecl *ParamDecl) {
113 SmallVector<const ParmVarDecl *, 2>
Results;
114 unsigned ParamIdx = ParamDecl->getFunctionScopeIndex();
116 for (
const FunctionDecl *Redecl : Ctor->redecls())
117 Results.push_back(Redecl->getParamDecl(ParamIdx));
123 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
124 Options.getLocalOrGlobal(
"IncludeStyle",
"llvm"))),
125 ValuesOnly(Options.get(
"ValuesOnly", 0) != 0) {}
141 forEachConstructorInitializer(
143 unless(isBaseInitializer()),
148 withInitializer(cxxConstructExpr(
149 has(ignoringParenImpCasts(declRefExpr(to(
159 hasDeclaration(cxxConstructorDecl(
160 isCopyConstructor(), unless(isDeleted()),
162 cxxRecordDecl(isMoveConstructible())))))))
163 .bind(
"Initializer")))
174 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
175 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
180 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"Ctor");
181 const auto *ParamDecl = Result.Nodes.getNodeAs<ParmVarDecl>(
"Param");
182 const auto *Initializer =
183 Result.Nodes.getNodeAs<CXXCtorInitializer>(
"Initializer");
184 SourceManager &SM = *Result.SourceManager;
193 if (ParamDecl->getType().getNonReferenceType().isTriviallyCopyableType(
197 auto Diag =
diag(ParamDecl->getBeginLoc(),
"pass by value and use std::move");
201 auto ParamTL = ParmDecl->getTypeSourceInfo()->getTypeLoc();
202 auto RefTL = ParamTL.getAs<ReferenceTypeLoc>();
208 TypeLoc ValueTL = RefTL.getPointeeLoc();
209 auto TypeRange = CharSourceRange::getTokenRange(ParmDecl->getBeginLoc(),
210 ParamTL.getEndLoc());
211 std::string ValueStr = Lexer::getSourceText(CharSourceRange::getTokenRange(
212 ValueTL.getSourceRange()),
216 Diag << FixItHint::CreateReplacement(TypeRange, ValueStr);
220 Diag << FixItHint::CreateInsertion(Initializer->getRParenLoc(),
")")
221 << FixItHint::CreateInsertion(
222 Initializer->getLParenLoc().getLocWithOffset(1),
"std::move(");
224 if (
auto IncludeFixit = Inserter->CreateIncludeInsertion(
225 Result.SourceManager->getFileID(Initializer->getSourceLocation()),
228 Diag << *IncludeFixit;
static bool paramReferredExactlyOnce(const CXXConstructorDecl *Ctor, const ParmVarDecl *ParamDecl)
Whether or not ParamDecl is used exactly one time in Ctor.
AST_MATCHER(BinaryOperator, isAssignmentOperator)
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.
Some operations such as code completion produce a set of candidates.
static TypeMatcher constRefType()
LangOptions getLangOpts() const
Returns the language options from the context.
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
static TypeMatcher nonConstValueType()
std::vector< CodeCompletionResult > Results
Base class for all clang-tidy checks.
void registerPPCallbacks(clang::CompilerInstance &Compiler) override
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Produces fixes to insert specified includes to source files, if not yet present.
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.