clang-tools 6.0.0

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