11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "llvm/ADT/StringExtras.h"
15#define DEBUG_TYPE "clang-tidy"
22AST_MATCHER(CXXRecordDecl, isInMacro) {
23 return Node.getBeginLoc().isMacroID() && Node.getEndLoc().isMacroID();
29 :
ClangTidyCheck(Name, Context), AllowMissingMoveFunctions(Options.get(
30 "AllowMissingMoveFunctions", false)),
31 AllowSoleDefaultDtor(Options.get(
"AllowSoleDefaultDtor", false)),
32 AllowMissingMoveFunctionsWhenCopyIsDeleted(
33 Options.get(
"AllowMissingMoveFunctionsWhenCopyIsDeleted", false)),
34 AllowImplicitlyDeletedCopyOrMove(
35 Options.get(
"AllowImplicitlyDeletedCopyOrMove", false)),
36 IgnoreMacros(Options.get(
"IgnoreMacros", true)) {}
40 Options.store(Opts,
"AllowMissingMoveFunctions", AllowMissingMoveFunctions);
41 Options.store(Opts,
"AllowSoleDefaultDtor", AllowSoleDefaultDtor);
42 Options.store(Opts,
"AllowMissingMoveFunctionsWhenCopyIsDeleted",
43 AllowMissingMoveFunctionsWhenCopyIsDeleted);
44 Options.store(Opts,
"AllowImplicitlyDeletedCopyOrMove",
45 AllowImplicitlyDeletedCopyOrMove);
46 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
49std::optional<TraversalKind>
51 return AllowImplicitlyDeletedCopyOrMove ? TK_AsIs
52 : TK_IgnoreUnlessSpelledInSource;
56 const auto IsNotImplicitOrDeleted = anyOf(unless(isImplicit()), isDeleted());
57 const ast_matchers::internal::Matcher<CXXRecordDecl> Anything = anything();
61 unless(isImplicit()), IgnoreMacros ? unless(isInMacro()) : Anything,
62 eachOf(has(cxxDestructorDecl(unless(isImplicit())).bind(
"dtor")),
63 has(cxxConstructorDecl(isCopyConstructor(),
64 IsNotImplicitOrDeleted)
66 has(cxxMethodDecl(isCopyAssignmentOperator(),
67 IsNotImplicitOrDeleted)
68 .bind(
"copy-assign")),
69 has(cxxConstructorDecl(isMoveConstructor(),
70 IsNotImplicitOrDeleted)
72 has(cxxMethodDecl(isMoveAssignmentOperator(),
73 IsNotImplicitOrDeleted)
74 .bind(
"move-assign"))))
83 return "a destructor";
84 case SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::
86 return "a default destructor";
87 case SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::
89 return "a non-default destructor";
91 return "a copy constructor";
93 return "a copy assignment operator";
95 return "a move constructor";
97 return "a move assignment operator";
99 llvm_unreachable(
"Unhandled SpecialMemberFunctionKind");
103join(ArrayRef<SpecialMemberFunctionsCheck::SpecialMemberFunctionKind> SMFS,
104 llvm::StringRef AndOr) {
106 assert(!SMFS.empty() &&
107 "List of defined or undefined members should never be empty.");
109 llvm::raw_string_ostream Stream(Buffer);
112 size_t LastIndex = SMFS.size() - 1;
113 for (
size_t I = 1; I < LastIndex; ++I) {
114 Stream <<
", " <<
toString(SMFS[I]);
116 if (LastIndex != 0) {
117 Stream << AndOr <<
toString(SMFS[LastIndex]);
123 const MatchFinder::MatchResult &Result) {
124 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXRecordDecl>(
"class-def");
129 std::string(MatchedDecl->getName()));
133 ClassWithSpecialMembers[ID];
134 if (!llvm::is_contained(Members, Data))
135 Members.push_back(std::move(Data));
138 if (
const auto *Dtor = Result.Nodes.getNodeAs<CXXMethodDecl>(
"dtor")) {
141 if (Dtor->isDefined()) {
142 DestructorType = Dtor->getDefinition()->isDefaulted()
146 StoreMember({DestructorType, Dtor->isDeleted()});
149 std::initializer_list<std::pair<std::string, SpecialMemberFunctionKind>>
155 for (
const auto &KV : Matchers)
156 if (
const auto *MethodDecl =
157 Result.Nodes.getNodeAs<CXXMethodDecl>(KV.first)) {
159 {KV.second, MethodDecl->isDeleted(), MethodDecl->isImplicit()});
164 for (
const auto &C : ClassWithSpecialMembers) {
165 checkForMissingMembers(C.first, C.second);
169void SpecialMemberFunctionsCheck::checkForMissingMembers(
170 const ClassDefId &ID,
171 llvm::ArrayRef<SpecialMemberFunctionData> DefinedMembers) {
172 llvm::SmallVector<SpecialMemberFunctionKind, 5> MissingMembers;
174 auto HasMember = [&](SpecialMemberFunctionKind Kind) {
175 return llvm::any_of(DefinedMembers, [Kind](
const auto &Data) {
176 return Data.FunctionKind == Kind && !Data.IsImplicit;
180 auto HasImplicitDeletedMember = [&](SpecialMemberFunctionKind Kind) {
181 return llvm::any_of(DefinedMembers, [Kind](
const auto &Data) {
182 return Data.FunctionKind == Kind && Data.IsImplicit && Data.IsDeleted;
187 return llvm::any_of(DefinedMembers, [Kind](
const auto &Data) {
188 return Data.FunctionKind == Kind && Data.IsDeleted;
194 if (AllowImplicitlyDeletedCopyOrMove && HasImplicitDeletedMember(Kind1) &&
195 HasImplicitDeletedMember(Kind2))
198 if (!HasMember(Kind1))
199 MissingMembers.push_back(Kind1);
201 if (!HasMember(Kind2))
202 MissingMembers.push_back(Kind2);
207 (!AllowSoleDefaultDtor &&
215 bool RequireFive = (!AllowMissingMoveFunctions && RequireThree &&
216 getLangOpts().CPlusPlus11) ||
231 !(AllowMissingMoveFunctionsWhenCopyIsDeleted &&
234 assert(RequireThree);
239 if (!MissingMembers.empty()) {
240 llvm::SmallVector<SpecialMemberFunctionKind, 5> DefinedMemberKinds;
241 for (
const auto &Data : DefinedMembers) {
242 if (!Data.IsImplicit)
243 DefinedMemberKinds.push_back(Data.FunctionKind);
245 diag(ID.first,
"class '%0' defines %1 but does not define %2")
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void onEndOfTranslationUnit() override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
std::optional< TraversalKind > getCheckTraversalKind() const override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
std::pair< SourceLocation, std::string > ClassDefId
SpecialMemberFunctionKind
SpecialMemberFunctionsCheck(StringRef Name, ClangTidyContext *Context)
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
llvm::StringMap< ClangTidyValue > OptionMap