clang-tools 10.0.0

SpecialMemberFunctionsCheck.h
Go to the documentation of this file.
1 //===--- SpecialMemberFunctionsCheck.h - clang-tidy--------------*- C++ -*-===//
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
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_SPECIAL_MEMBER_FUNCTIONS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_SPECIAL_MEMBER_FUNCTIONS_H
11
12 #include "../ClangTidy.h"
13
14 #include "llvm/ADT/DenseMapInfo.h"
15
16 namespace clang {
17 namespace tidy {
18 namespace cppcoreguidelines {
19
20 /// Checks for classes where some, but not all, of the special member functions
21 /// are defined.
22 ///
23 /// For the user-facing documentation see:
24 /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-special-member-functions.html
25 class SpecialMemberFunctionsCheck : public ClangTidyCheck {
26 public:
27 SpecialMemberFunctionsCheck(StringRef Name, ClangTidyContext *Context);
28 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
29 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31 void onEndOfTranslationUnit() override;
32
33 enum class SpecialMemberFunctionKind : uint8_t {
34 Destructor,
35 DefaultDestructor,
36 NonDefaultDestructor,
37 CopyConstructor,
38 CopyAssignment,
39 MoveConstructor,
40 MoveAssignment
41 };
42
43 using ClassDefId = std::pair<SourceLocation, std::string>;
44
45 using ClassDefiningSpecialMembersMap =
46 llvm::DenseMap<ClassDefId,
47 llvm::SmallVector<SpecialMemberFunctionKind, 5>>;
48
49 private:
50 void checkForMissingMembers(
51 const ClassDefId &ID,
52 llvm::ArrayRef<SpecialMemberFunctionKind> DefinedSpecialMembers);
53
54 const bool AllowMissingMoveFunctions;
55 const bool AllowSoleDefaultDtor;
56 ClassDefiningSpecialMembersMap ClassWithSpecialMembers;
57 };
58
59 } // namespace cppcoreguidelines
60 } // namespace tidy
61 } // namespace clang
62
63 namespace llvm {
64 /// Specialisation of DenseMapInfo to allow ClassDefId objects in DenseMaps
65 /// FIXME: Move this to the corresponding cpp file as is done for
66 /// clang-tidy/readability/IdentifierNamingCheck.cpp.
67 template <>
68 struct DenseMapInfo<
69 clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId> {
70 using ClassDefId =
71 clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId;
72
73 static inline ClassDefId getEmptyKey() {
74 return ClassDefId(
75 clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-1)),
76 "EMPTY");
77 }
78
79 static inline ClassDefId getTombstoneKey() {
80 return ClassDefId(
81 clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-2)),
82 "TOMBSTONE");
83 }
84
85 static unsigned getHashValue(ClassDefId Val) {
86 assert(Val != getEmptyKey() && "Cannot hash the empty key!");
87 assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
88
89 std::hash<ClassDefId::second_type> SecondHash;
90 return Val.first.getRawEncoding() + SecondHash(Val.second);
91 }
92
93 static bool isEqual(const ClassDefId &LHS, const ClassDefId &RHS) {
94 if (RHS == getEmptyKey())
95 return LHS == getEmptyKey();
96 if (RHS == getTombstoneKey())
97 return LHS == getTombstoneKey();
98 return LHS == RHS;
99 }
100 };
101
102 } // namespace llvm
103
104 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_SPECIAL_MEMBER_FUNCTIONS_H
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: SpecialMemberFunctionsCheck.cpp:133
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck
Checks for classes where some, but not all, of the special member functions are defined.
Definition: SpecialMemberFunctionsCheck.h:25
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::CopyConstructor
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SpecialMemberFunctionsCheck.cpp:30
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind
SpecialMemberFunctionKind
Definition: SpecialMemberFunctionsCheck.h:33
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::isEqual
static bool isEqual(const ClassDefId &LHS, const ClassDefId &RHS)
Definition: SpecialMemberFunctionsCheck.h:93
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::ClassDefId
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId ClassDefId
Definition: SpecialMemberFunctionsCheck.h:71
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::Destructor
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::MoveConstructor
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::getHashValue
static unsigned getHashValue(ClassDefId Val)
Definition: SpecialMemberFunctionsCheck.h:85
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SpecialMemberFunctionsCheck.cpp:36
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SpecialMemberFunctionsCheck.cpp:100
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefiningSpecialMembersMap
llvm::DenseMap< ClassDefId, llvm::SmallVector< SpecialMemberFunctionKind, 5 > > ClassDefiningSpecialMembersMap
Definition: SpecialMemberFunctionsCheck.h:47
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::MoveAssignment
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::CopyAssignment
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::getEmptyKey
static ClassDefId getEmptyKey()
Definition: SpecialMemberFunctionsCheck.h:73
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionsCheck
SpecialMemberFunctionsCheck(StringRef Name, ClangTidyContext *Context)
Definition: SpecialMemberFunctionsCheck.cpp:24
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::DefaultDestructor
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::NonDefaultDestructor
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::getTombstoneKey
static ClassDefId getTombstoneKey()
Definition: SpecialMemberFunctionsCheck.h:79
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId
std::pair< SourceLocation, std::string > ClassDefId
Definition: SpecialMemberFunctionsCheck.h:43