clang-tools  14.0.0git
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 "../ClangTidyCheck.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
26 public:
28  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
29  return LangOpts.CPlusPlus;
30  }
31  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
32  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
33  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
34  void onEndOfTranslationUnit() override;
35  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
36  return TK_IgnoreUnlessSpelledInSource;
37  }
38  enum class SpecialMemberFunctionKind : uint8_t {
39  Destructor,
46  };
47 
50  bool IsDeleted;
51 
53  return (Other.FunctionKind == FunctionKind) &&
54  (Other.IsDeleted == IsDeleted);
55  }
56  };
57 
58  using ClassDefId = std::pair<SourceLocation, std::string>;
59 
61  llvm::DenseMap<ClassDefId,
62  llvm::SmallVector<SpecialMemberFunctionData, 5>>;
63 
64 private:
65  void checkForMissingMembers(
66  const ClassDefId &ID,
67  llvm::ArrayRef<SpecialMemberFunctionData> DefinedSpecialMembers);
68 
69  const bool AllowMissingMoveFunctions;
70  const bool AllowSoleDefaultDtor;
71  const bool AllowMissingMoveFunctionsWhenCopyIsDeleted;
72  ClassDefiningSpecialMembersMap ClassWithSpecialMembers;
73 };
74 
75 } // namespace cppcoreguidelines
76 } // namespace tidy
77 } // namespace clang
78 
79 namespace llvm {
80 /// Specialisation of DenseMapInfo to allow ClassDefId objects in DenseMaps
81 /// FIXME: Move this to the corresponding cpp file as is done for
82 /// clang-tidy/readability/IdentifierNamingCheck.cpp.
83 template <>
84 struct DenseMapInfo<
85  clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId> {
86  using ClassDefId =
88 
89  static inline ClassDefId getEmptyKey() {
90  return ClassDefId(DenseMapInfo<clang::SourceLocation>::getEmptyKey(),
91  "EMPTY");
92  }
93 
94  static inline ClassDefId getTombstoneKey() {
95  return ClassDefId(DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
96  "TOMBSTONE");
97  }
98 
99  static unsigned getHashValue(ClassDefId Val) {
100  assert(Val != getEmptyKey() && "Cannot hash the empty key!");
101  assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
102 
103  std::hash<ClassDefId::second_type> SecondHash;
104  return Val.first.getHashValue() + SecondHash(Val.second);
105  }
106 
107  static bool isEqual(const ClassDefId &LHS, const ClassDefId &RHS) {
108  if (RHS == getEmptyKey())
109  return LHS == getEmptyKey();
110  if (RHS == getTombstoneKey())
111  return LHS == getTombstoneKey();
112  return LHS == RHS;
113  }
114 };
115 
116 } // namespace llvm
117 
118 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_SPECIAL_MEMBER_FUNCTIONS_H
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:32
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionData
Definition: SpecialMemberFunctionsCheck.h:48
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId
std::pair< SourceLocation, std::string > ClassDefId
Definition: SpecialMemberFunctionsCheck.h:58
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionData::FunctionKind
SpecialMemberFunctionKind FunctionKind
Definition: SpecialMemberFunctionsCheck.h:49
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: SpecialMemberFunctionsCheck.cpp:132
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::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SpecialMemberFunctionsCheck.cpp:40
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::NonDefaultDestructor
@ NonDefaultDestructor
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:97
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::getTombstoneKey
static ClassDefId getTombstoneKey()
Definition: SpecialMemberFunctionsCheck.h:94
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::getHashValue
static unsigned getHashValue(ClassDefId Val)
Definition: SpecialMemberFunctionsCheck.h:99
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::getEmptyKey
static ClassDefId getEmptyKey()
Definition: SpecialMemberFunctionsCheck.h:89
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::Destructor
@ Destructor
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefiningSpecialMembersMap
llvm::DenseMap< ClassDefId, llvm::SmallVector< SpecialMemberFunctionData, 5 > > ClassDefiningSpecialMembersMap
Definition: SpecialMemberFunctionsCheck.h:62
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::MoveConstructor
@ MoveConstructor
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::CopyConstructor
@ CopyConstructor
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::getCheckTraversalKind
llvm::Optional< TraversalKind > getCheckTraversalKind() const override
Definition: SpecialMemberFunctionsCheck.h:35
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionsCheck
SpecialMemberFunctionsCheck(StringRef Name, ClangTidyContext *Context)
Definition: SpecialMemberFunctionsCheck.cpp:24
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionData::IsDeleted
bool IsDeleted
Definition: SpecialMemberFunctionsCheck.h:50
ID
static char ID
Definition: Logger.cpp:74
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::DefaultDestructor
@ DefaultDestructor
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: SpecialMemberFunctionsCheck.h:28
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::isEqual
static bool isEqual(const ClassDefId &LHS, const ClassDefId &RHS)
Definition: SpecialMemberFunctionsCheck.h:107
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionData::operator==
bool operator==(const SpecialMemberFunctionData &Other)
Definition: SpecialMemberFunctionsCheck.h:52
llvm::DenseMapInfo< clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId >::ClassDefId
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId ClassDefId
Definition: SpecialMemberFunctionsCheck.h:87
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::CopyAssignment
@ CopyAssignment
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind::MoveAssignment
@ MoveAssignment
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::SpecialMemberFunctionKind
SpecialMemberFunctionKind
Definition: SpecialMemberFunctionsCheck.h:38