clang-tools 19.0.0git
SpecialMemberFunctionsCheck.cpp
Go to the documentation of this file.
1//===--- SpecialMemberFunctionsCheck.cpp - clang-tidy----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "llvm/ADT/DenseMapInfo.h"
14#include "llvm/ADT/StringExtras.h"
15
16#define DEBUG_TYPE "clang-tidy"
17
18using namespace clang::ast_matchers;
19
21
23 StringRef Name, ClangTidyContext *Context)
24 : ClangTidyCheck(Name, Context), AllowMissingMoveFunctions(Options.get(
25 "AllowMissingMoveFunctions", false)),
26 AllowSoleDefaultDtor(Options.get("AllowSoleDefaultDtor", false)),
27 AllowMissingMoveFunctionsWhenCopyIsDeleted(
28 Options.get("AllowMissingMoveFunctionsWhenCopyIsDeleted", false)) {}
29
32 Options.store(Opts, "AllowMissingMoveFunctions", AllowMissingMoveFunctions);
33 Options.store(Opts, "AllowSoleDefaultDtor", AllowSoleDefaultDtor);
34 Options.store(Opts, "AllowMissingMoveFunctionsWhenCopyIsDeleted",
35 AllowMissingMoveFunctionsWhenCopyIsDeleted);
36}
37
39 Finder->addMatcher(
40 cxxRecordDecl(
41 eachOf(has(cxxDestructorDecl().bind("dtor")),
42 has(cxxConstructorDecl(isCopyConstructor()).bind("copy-ctor")),
43 has(cxxMethodDecl(isCopyAssignmentOperator())
44 .bind("copy-assign")),
45 has(cxxConstructorDecl(isMoveConstructor()).bind("move-ctor")),
46 has(cxxMethodDecl(isMoveAssignmentOperator())
47 .bind("move-assign"))))
48 .bind("class-def"),
49 this);
50}
51
52static llvm::StringRef
54 switch (K) {
56 return "a destructor";
59 return "a default destructor";
62 return "a non-default destructor";
64 return "a copy constructor";
66 return "a copy assignment operator";
68 return "a move constructor";
70 return "a move assignment operator";
71 }
72 llvm_unreachable("Unhandled SpecialMemberFunctionKind");
73}
74
75static std::string
76join(ArrayRef<SpecialMemberFunctionsCheck::SpecialMemberFunctionKind> SMFS,
77 llvm::StringRef AndOr) {
78
79 assert(!SMFS.empty() &&
80 "List of defined or undefined members should never be empty.");
81 std::string Buffer;
82 llvm::raw_string_ostream Stream(Buffer);
83
84 Stream << toString(SMFS[0]);
85 size_t LastIndex = SMFS.size() - 1;
86 for (size_t I = 1; I < LastIndex; ++I) {
87 Stream << ", " << toString(SMFS[I]);
88 }
89 if (LastIndex != 0) {
90 Stream << AndOr << toString(SMFS[LastIndex]);
91 }
92 return Stream.str();
93}
94
96 const MatchFinder::MatchResult &Result) {
97 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXRecordDecl>("class-def");
98 if (!MatchedDecl)
99 return;
100
101 ClassDefId ID(MatchedDecl->getLocation(), std::string(MatchedDecl->getName()));
102
103 auto StoreMember = [this, &ID](SpecialMemberFunctionData Data) {
105 ClassWithSpecialMembers[ID];
106 if (!llvm::is_contained(Members, Data))
107 Members.push_back(std::move(Data));
108 };
109
110 if (const auto *Dtor = Result.Nodes.getNodeAs<CXXMethodDecl>("dtor")) {
111 SpecialMemberFunctionKind DestructorType =
113 if (Dtor->isDefined()) {
114 DestructorType = Dtor->getDefinition()->isDefaulted()
117 }
118 StoreMember({DestructorType, Dtor->isDeleted()});
119 }
120
121 std::initializer_list<std::pair<std::string, SpecialMemberFunctionKind>>
122 Matchers = {{"copy-ctor", SpecialMemberFunctionKind::CopyConstructor},
126
127 for (const auto &KV : Matchers)
128 if (const auto *MethodDecl =
129 Result.Nodes.getNodeAs<CXXMethodDecl>(KV.first)) {
130 StoreMember({KV.second, MethodDecl->isDeleted()});
131 }
132}
133
135 for (const auto &C : ClassWithSpecialMembers) {
136 checkForMissingMembers(C.first, C.second);
137 }
138}
139
140void SpecialMemberFunctionsCheck::checkForMissingMembers(
141 const ClassDefId &ID,
142 llvm::ArrayRef<SpecialMemberFunctionData> DefinedMembers) {
143 llvm::SmallVector<SpecialMemberFunctionKind, 5> MissingMembers;
144
145 auto HasMember = [&](SpecialMemberFunctionKind Kind) {
146 return llvm::any_of(DefinedMembers, [Kind](const auto &Data) {
147 return Data.FunctionKind == Kind;
148 });
149 };
150
151 auto IsDeleted = [&](SpecialMemberFunctionKind Kind) {
152 return llvm::any_of(DefinedMembers, [Kind](const auto &Data) {
153 return Data.FunctionKind == Kind && Data.IsDeleted;
154 });
155 };
156
157 auto RequireMember = [&](SpecialMemberFunctionKind Kind) {
158 if (!HasMember(Kind))
159 MissingMembers.push_back(Kind);
160 };
161
162 bool RequireThree =
164 (!AllowSoleDefaultDtor &&
171
172 bool RequireFive = (!AllowMissingMoveFunctions && RequireThree &&
173 getLangOpts().CPlusPlus11) ||
176
177 if (RequireThree) {
181 MissingMembers.push_back(SpecialMemberFunctionKind::Destructor);
182
185 }
186
187 if (RequireFive &&
188 !(AllowMissingMoveFunctionsWhenCopyIsDeleted &&
191 assert(RequireThree);
194 }
195
196 if (!MissingMembers.empty()) {
197 llvm::SmallVector<SpecialMemberFunctionKind, 5> DefinedMemberKinds;
198 llvm::transform(DefinedMembers, std::back_inserter(DefinedMemberKinds),
199 [](const auto &Data) { return Data.FunctionKind; });
200 diag(ID.first, "class '%0' defines %1 but does not define %2")
201 << ID.second << cppcoreguidelines::join(DefinedMemberKinds, " and ")
202 << cppcoreguidelines::join(MissingMembers, " or ");
203 }
204}
205
206} // namespace clang::tidy::cppcoreguidelines
BindArgumentKind Kind
llvm::SmallString< 256U > Name
const Criteria C
Kind K
Definition: Rename.cpp:474
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the 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.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
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