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"
18 using namespace clang::ast_matchers;
24 MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef
Name,
27 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
28 Options.get(
"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 const auto *RD = QT->getAsCXXRecordDecl();
61 if (RD && RD->isTriviallyCopyable())
66 const CXXConstructorDecl *Candidate =
nullptr;
67 for (
const auto *Ctor : CopyCtor->getParent()->ctors()) {
68 if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
84 diag(Initializer->getSourceLocation(),
85 "move constructor initializes %0 by calling a copy constructor")
86 << (Initializer->isBaseInitializer() ?
"base class" :
"class member");
87 diag(CopyCtor->getLocation(),
"copy constructor being called",
89 diag(Candidate->getLocation(),
"candidate move constructor here",
96 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
97 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
LangOptions getLangOpts() const
Returns the language options from the context.
std::unique_ptr< ast_matchers::MatchFinder > Finder
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
Base class for all clang-tidy checks.
void registerPPCallbacks(clang::CompilerInstance &Compiler) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.