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