Jonathan Coe | 77ec263 | 2016-08-02 21:18:37 +0000 | [diff] [blame] | 1 | //===--- SpecialMemberFunctionsCheck.h - clang-tidy--------------*- C++ -*-===// |
Jonathan Coe | 5d304b2 | 2016-07-30 08:58:54 +0000 | [diff] [blame] | 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 | : ClangTidyCheck(Name, Context) {} |
| 30 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 31 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 32 | void onEndOfTranslationUnit() override; |
Kirill Bobyrev | 11cea45 | 2016-08-01 12:06:18 +0000 | [diff] [blame] | 33 | |
Justin Lebar | ae90ad2 | 2016-10-21 20:13:39 +0000 | [diff] [blame^] | 34 | enum class SpecialMemberFunctionKind : uint8_t { |
Jonathan Coe | 5d304b2 | 2016-07-30 08:58:54 +0000 | [diff] [blame] | 35 | Destructor, |
| 36 | CopyConstructor, |
| 37 | CopyAssignment, |
| 38 | MoveConstructor, |
| 39 | MoveAssignment |
| 40 | }; |
| 41 | |
| 42 | using ClassDefId = std::pair<SourceLocation, std::string>; |
| 43 | |
Jonathan Coe | 77ec263 | 2016-08-02 21:18:37 +0000 | [diff] [blame] | 44 | using ClassDefiningSpecialMembersMap = |
| 45 | llvm::DenseMap<ClassDefId, |
Justin Lebar | ae90ad2 | 2016-10-21 20:13:39 +0000 | [diff] [blame^] | 46 | llvm::SmallVector<SpecialMemberFunctionKind, 5>>; |
Jonathan Coe | 5d304b2 | 2016-07-30 08:58:54 +0000 | [diff] [blame] | 47 | |
| 48 | private: |
Jonathan Coe | 5d304b2 | 2016-07-30 08:58:54 +0000 | [diff] [blame] | 49 | ClassDefiningSpecialMembersMap ClassWithSpecialMembers; |
| 50 | }; |
| 51 | |
| 52 | } // namespace cppcoreguidelines |
| 53 | } // namespace tidy |
| 54 | } // namespace clang |
| 55 | |
| 56 | namespace llvm { |
| 57 | /// Specialisation of DenseMapInfo to allow ClassDefId objects in DenseMaps |
| 58 | /// FIXME: Move this to the corresponding cpp file as is done for |
Kirill Bobyrev | 11cea45 | 2016-08-01 12:06:18 +0000 | [diff] [blame] | 59 | /// clang-tidy/readability/IdentifierNamingCheck.cpp. |
Jonathan Coe | 5d304b2 | 2016-07-30 08:58:54 +0000 | [diff] [blame] | 60 | template <> |
| 61 | struct DenseMapInfo< |
| 62 | clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId> { |
| 63 | using ClassDefId = |
Jonathan Coe | 77ec263 | 2016-08-02 21:18:37 +0000 | [diff] [blame] | 64 | clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId; |
Jonathan Coe | 5d304b2 | 2016-07-30 08:58:54 +0000 | [diff] [blame] | 65 | |
| 66 | static inline ClassDefId getEmptyKey() { |
| 67 | return ClassDefId( |
| 68 | clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-1)), |
| 69 | "EMPTY"); |
| 70 | } |
| 71 | |
| 72 | static inline ClassDefId getTombstoneKey() { |
| 73 | return ClassDefId( |
| 74 | clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-2)), |
| 75 | "TOMBSTONE"); |
| 76 | } |
| 77 | |
| 78 | static unsigned getHashValue(ClassDefId Val) { |
| 79 | assert(Val != getEmptyKey() && "Cannot hash the empty key!"); |
| 80 | assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!"); |
| 81 | |
| 82 | std::hash<ClassDefId::second_type> SecondHash; |
| 83 | return Val.first.getRawEncoding() + SecondHash(Val.second); |
| 84 | } |
| 85 | |
| 86 | static bool isEqual(ClassDefId LHS, ClassDefId RHS) { |
| 87 | if (RHS == getEmptyKey()) |
| 88 | return LHS == getEmptyKey(); |
| 89 | if (RHS == getTombstoneKey()) |
| 90 | return LHS == getTombstoneKey(); |
| 91 | return LHS == RHS; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | } // namespace llvm |
| 96 | |
| 97 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_SPECIAL_MEMBER_FUNCTIONS_H |