blob: 508ce6411cfd18bc154ad5a3719ce91f811e561c [file] [log] [blame]
Jonathan Coe5d304b22016-07-30 08:58:54 +00001//===--- 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
17namespace clang {
18namespace tidy {
19namespace 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
26class SpecialMemberFunctionsCheck : public ClangTidyCheck {
27public:
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 Bobyrev11cea452016-08-01 12:06:18 +000033
Jonathan Coe5d304b22016-07-30 08:58:54 +000034 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
46private:
Kirill Bobyrev11cea452016-08-01 12:06:18 +000047
Jonathan Coe5d304b22016-07-30 08:58:54 +000048 static llvm::StringRef toString(SpecialMemberFunctionKind K);
49
50 static std::string join(llvm::ArrayRef<SpecialMemberFunctionKind> SMFS,
51 llvm::StringRef AndOr);
Kirill Bobyrev11cea452016-08-01 12:06:18 +000052
Jonathan Coe5d304b22016-07-30 08:58:54 +000053 ClassDefiningSpecialMembersMap ClassWithSpecialMembers;
54};
55
56} // namespace cppcoreguidelines
57} // namespace tidy
58} // namespace clang
59
60namespace 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 Bobyrev11cea452016-08-01 12:06:18 +000063/// clang-tidy/readability/IdentifierNamingCheck.cpp.
Jonathan Coe5d304b22016-07-30 08:58:54 +000064template <>
65struct 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