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" 41 for (
const CXXConstructorDecl *Ctor : Node.ctors()) {
42 if (Ctor->isMoveConstructor() && !Ctor->isDeleted())
50 return lValueReferenceType(pointee(isConstQualified()));
54 return qualType(unless(anyOf(referenceType(), isConstQualified())));
61 const ParmVarDecl *ParamDecl) {
66 class ExactlyOneUsageVisitor
67 :
public RecursiveASTVisitor<ExactlyOneUsageVisitor> {
68 friend class RecursiveASTVisitor<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);
109 static SmallVector<const ParmVarDecl *, 2>
111 const ParmVarDecl *ParamDecl) {
112 SmallVector<const ParmVarDecl *, 2>
Results;
113 unsigned ParamIdx = ParamDecl->getFunctionScopeIndex();
115 for (
const FunctionDecl *Redecl : Ctor->redecls())
116 Results.push_back(Redecl->getParamDecl(ParamIdx));
122 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
123 Options.getLocalOrGlobal(
"IncludeStyle",
"llvm"))),
124 ValuesOnly(Options.get(
"ValuesOnly", 0) != 0) {}
140 forEachConstructorInitializer(
142 unless(isBaseInitializer()),
147 withInitializer(cxxConstructExpr(
148 has(ignoringParenImpCasts(declRefExpr(to(
158 hasDeclaration(cxxConstructorDecl(
159 isCopyConstructor(), unless(isDeleted()),
161 cxxRecordDecl(isMoveConstructible())))))))
162 .bind(
"Initializer")))
169 Preprocessor *ModuleExpanderPP) {
174 Inserter = llvm::make_unique<utils::IncludeInserter>(SM,
getLangOpts(),
176 PP->addPPCallbacks(Inserter->CreatePPCallbacks());
181 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"Ctor");
182 const auto *ParamDecl = Result.Nodes.getNodeAs<ParmVarDecl>(
"Param");
183 const auto *Initializer =
184 Result.Nodes.getNodeAs<CXXCtorInitializer>(
"Initializer");
185 SourceManager &SM = *Result.SourceManager;
194 if (ParamDecl->getType().getNonReferenceType().isTriviallyCopyableType(
198 auto Diag =
diag(ParamDecl->getBeginLoc(),
"pass by value and use std::move");
202 auto ParamTL = ParmDecl->getTypeSourceInfo()->getTypeLoc();
203 auto RefTL = ParamTL.getAs<ReferenceTypeLoc>();
209 TypeLoc ValueTL = RefTL.getPointeeLoc();
211 ParamTL.getEndLoc());
213 ValueTL.getSourceRange()),
217 Diag << FixItHint::CreateReplacement(TypeRange, ValueStr);
221 Diag << FixItHint::CreateInsertion(Initializer->getRParenLoc(),
")")
222 << FixItHint::CreateInsertion(
223 Initializer->getLParenLoc().getLocWithOffset(1),
"std::move(");
225 if (
auto IncludeFixit = Inserter->CreateIncludeInsertion(
226 Result.SourceManager->getFileID(Initializer->getSourceLocation()),
229 Diag << *IncludeFixit;
static bool paramReferredExactlyOnce(const CXXConstructorDecl *Ctor, const ParmVarDecl *ParamDecl)
Whether or not ParamDecl is used exactly one time in Ctor.
Some operations such as code completion produce a set of candidates.
static TypeMatcher constRefType()
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
AST_MATCHER(Expr, isMacroID)
static TypeMatcher nonConstValueType()
std::vector< CodeCompletionResult > Results
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
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++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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.