11 #include "../utils/Matchers.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Frontend/CompilerInstance.h" 15 #include "clang/Lex/Lexer.h" 16 #include "clang/Lex/Preprocessor.h" 22 namespace performance {
24 MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef
Name,
27 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
28 Options.getLocalOrGlobal(
"IncludeStyle",
"llvm"))) {}
39 allOf(isMoveConstructor(),
40 hasAnyConstructorInitializer(
42 withInitializer(cxxConstructExpr(hasDeclaration(
43 cxxConstructorDecl(isCopyConstructor())
45 .bind(
"move-init")))),
50 const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
51 const auto *Initializer =
52 Result.Nodes.getNodeAs<CXXCtorInitializer>(
"move-init");
56 QualType QT = Initializer->getInit()->getType();
57 if (QT.isTriviallyCopyableType(*Result.Context))
60 if (QT.isConstQualified())
63 const auto *RD = QT->getAsCXXRecordDecl();
64 if (RD && RD->isTriviallyCopyable())
69 const CXXConstructorDecl *Candidate =
nullptr;
70 for (
const auto *Ctor : CopyCtor->getParent()->ctors()) {
71 if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
87 diag(Initializer->getSourceLocation(),
88 "move constructor initializes %0 by calling a copy constructor")
89 << (Initializer->isBaseInitializer() ?
"base class" :
"class member");
90 diag(CopyCtor->getLocation(),
"copy constructor being called",
92 diag(Candidate->getLocation(),
"candidate move constructor here",
99 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
100 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
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.
LangOptions getLangOpts() const
Returns the language options from the context.
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
Base class for all clang-tidy checks.
std::map< std::string, std::string > OptionMap
Produces fixes to insert specified includes to source files, if not yet present.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.