116 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_SPECIALMEMBERFUNCTIONSCHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_SPECIALMEMBERFUNCTIONSCHECK_H11 12#include "../ClangTidyCheck.h"13 14#include "llvm/ADT/DenseMapInfo.h"15 16namespace clang::tidy::cppcoreguidelines {17 18/// Checks for classes where some, but not all, of the special member functions19/// are defined.20///21/// For the user-facing documentation see:22/// https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/special-member-functions.html23class SpecialMemberFunctionsCheck : public ClangTidyCheck {24public:25 SpecialMemberFunctionsCheck(StringRef Name, ClangTidyContext *Context);26 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {27 return LangOpts.CPlusPlus;28 }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 std::optional<TraversalKind> getCheckTraversalKind() const override;34 35 enum class SpecialMemberFunctionKind : uint8_t {36 Destructor,37 DefaultDestructor,38 NonDefaultDestructor,39 CopyConstructor,40 CopyAssignment,41 MoveConstructor,42 MoveAssignment43 };44 45 struct SpecialMemberFunctionData {46 SpecialMemberFunctionKind FunctionKind;47 bool IsDeleted;48 bool IsImplicit = false;49 50 bool operator==(const SpecialMemberFunctionData &Other) const {51 return (Other.FunctionKind == FunctionKind) &&52 (Other.IsDeleted == IsDeleted);53 }54 };55 56 using ClassDefId = std::pair<SourceLocation, std::string>;57 58 using ClassDefiningSpecialMembersMap =59 llvm::DenseMap<ClassDefId,60 llvm::SmallVector<SpecialMemberFunctionData, 5>>;61 62private:63 void checkForMissingMembers(64 const ClassDefId &ID,65 llvm::ArrayRef<SpecialMemberFunctionData> DefinedMembers);66 67 const bool AllowMissingMoveFunctions;68 const bool AllowSoleDefaultDtor;69 const bool AllowMissingMoveFunctionsWhenCopyIsDeleted;70 const bool AllowImplicitlyDeletedCopyOrMove;71 ClassDefiningSpecialMembersMap ClassWithSpecialMembers;72 const bool IgnoreMacros;73};74 75} // namespace clang::tidy::cppcoreguidelines76 77namespace llvm {78/// Specialization of DenseMapInfo to allow ClassDefId objects in DenseMaps79/// FIXME: Move this to the corresponding cpp file as is done for80/// clang-tidy/readability/IdentifierNamingCheck.cpp.81template <>82struct DenseMapInfo<83 clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId> {84 using ClassDefId =85 clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId;86 87 static ClassDefId getEmptyKey() {88 return {DenseMapInfo<clang::SourceLocation>::getEmptyKey(), "EMPTY"};89 }90 91 static ClassDefId getTombstoneKey() {92 return {DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),93 "TOMBSTONE"};94 }95 96 static unsigned getHashValue(const ClassDefId &Val) {97 assert(Val != getEmptyKey() && "Cannot hash the empty key!");98 assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");99 100 const std::hash<ClassDefId::second_type> SecondHash;101 return Val.first.getHashValue() + SecondHash(Val.second);102 }103 104 static bool isEqual(const ClassDefId &LHS, const ClassDefId &RHS) {105 if (RHS == getEmptyKey())106 return LHS == getEmptyKey();107 if (RHS == getTombstoneKey())108 return LHS == getTombstoneKey();109 return LHS == RHS;110 }111};112 113} // namespace llvm114 115#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_SPECIALMEMBERFUNCTIONSCHECK_H116