|
| 1 | +//===--- SpecialMemberFunctionsCheck.cpp - clang-tidy----------------------===// |
| 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 | +#include "SpecialMemberFunctionsCheck.h" |
| 11 | + |
| 12 | +#include "clang/AST/ASTContext.h" |
| 13 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 14 | +#include "llvm/ADT/DenseMapInfo.h" |
| 15 | +#include "llvm/ADT/StringExtras.h" |
| 16 | + |
| 17 | +#define DEBUG_TYPE "clang-tidy" |
| 18 | + |
| 19 | +using namespace clang::ast_matchers; |
| 20 | + |
| 21 | +namespace clang { |
| 22 | +namespace tidy { |
| 23 | +namespace cppcoreguidelines { |
| 24 | + |
| 25 | +void SpecialMemberFunctionsCheck::registerMatchers(MatchFinder *Finder) { |
| 26 | + if (!getLangOpts().CPlusPlus) |
| 27 | + return; |
| 28 | + Finder->addMatcher( |
| 29 | + cxxRecordDecl( |
| 30 | + eachOf( |
| 31 | + has(cxxDestructorDecl(unless(isImplicit())).bind("dtor")), |
| 32 | + has(cxxConstructorDecl(isCopyConstructor(), unless(isImplicit())) |
| 33 | + .bind("copy-ctor")), |
| 34 | + has(cxxMethodDecl(isCopyAssignmentOperator(), |
| 35 | + unless(isImplicit())) |
| 36 | + .bind("copy-assign")), |
| 37 | + has(cxxConstructorDecl(isMoveConstructor(), unless(isImplicit())) |
| 38 | + .bind("move-ctor")), |
| 39 | + has(cxxMethodDecl(isMoveAssignmentOperator(), |
| 40 | + unless(isImplicit())) |
| 41 | + .bind("move-assign")))) |
| 42 | + .bind("class-def"), |
| 43 | + this); |
| 44 | +} |
| 45 | + |
| 46 | +llvm::StringRef SpecialMemberFunctionsCheck::toString( |
| 47 | + SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K) { |
| 48 | + switch (K) { |
| 49 | + case SpecialMemberFunctionKind::Destructor: |
| 50 | + return "a destructor"; |
| 51 | + case SpecialMemberFunctionKind::CopyConstructor: |
| 52 | + return "a copy constructor"; |
| 53 | + case SpecialMemberFunctionKind::CopyAssignment: |
| 54 | + return "a copy assignment operator"; |
| 55 | + case SpecialMemberFunctionKind::MoveConstructor: |
| 56 | + return "a move constructor"; |
| 57 | + case SpecialMemberFunctionKind::MoveAssignment: |
| 58 | + return "a move assignment operator"; |
| 59 | + } |
| 60 | + llvm_unreachable("Unhandled SpecialMemberFunctionKind"); |
| 61 | +} |
| 62 | + |
| 63 | +std::string SpecialMemberFunctionsCheck::join( |
| 64 | + llvm::ArrayRef<SpecialMemberFunctionKind> SMFS, llvm::StringRef AndOr) { |
| 65 | + |
| 66 | + assert(!SMFS.empty() && |
| 67 | + "List of defined or undefined members should never be empty."); |
| 68 | + std::string Buffer; |
| 69 | + llvm::raw_string_ostream Stream(Buffer); |
| 70 | + |
| 71 | + Stream << toString(SMFS[0]); |
| 72 | + size_t LastIndex = SMFS.size() - 1; |
| 73 | + for (size_t i = 1; i < LastIndex; ++i) { |
| 74 | + Stream << ", " << toString(SMFS[i]); |
| 75 | + } |
| 76 | + if (LastIndex != 0) { |
| 77 | + Stream << AndOr << toString(SMFS[LastIndex]); |
| 78 | + } |
| 79 | + return Stream.str(); |
| 80 | +} |
| 81 | + |
| 82 | +void SpecialMemberFunctionsCheck::check( |
| 83 | + const MatchFinder::MatchResult &Result) { |
| 84 | + const CXXRecordDecl *MatchedDecl = |
| 85 | + Result.Nodes.getNodeAs<CXXRecordDecl>("class-def"); |
| 86 | + if (!MatchedDecl) |
| 87 | + return; |
| 88 | + |
| 89 | + ClassDefId ID(MatchedDecl->getLocation(), MatchedDecl->getName()); |
| 90 | + |
| 91 | + std::initializer_list<std::pair<std::string, SpecialMemberFunctionKind>> |
| 92 | + Matchers = {{"dtor", SpecialMemberFunctionKind::Destructor}, |
| 93 | + {"copy-ctor", SpecialMemberFunctionKind::CopyConstructor}, |
| 94 | + {"copy-assign", SpecialMemberFunctionKind::CopyAssignment}, |
| 95 | + {"move-ctor", SpecialMemberFunctionKind::MoveConstructor}, |
| 96 | + {"move-assign", SpecialMemberFunctionKind::MoveAssignment}}; |
| 97 | + |
| 98 | + for (const auto &KV : Matchers) |
| 99 | + if (Result.Nodes.getNodeAs<CXXMethodDecl>(KV.first)) |
| 100 | + ClassWithSpecialMembers[ID].push_back(KV.second); |
| 101 | +} |
| 102 | + |
| 103 | +void SpecialMemberFunctionsCheck::onEndOfTranslationUnit() { |
| 104 | + llvm::SmallVector<SpecialMemberFunctionKind, 5> AllSpecialMembers = { |
| 105 | + SpecialMemberFunctionKind::Destructor, |
| 106 | + SpecialMemberFunctionKind::CopyConstructor, |
| 107 | + SpecialMemberFunctionKind::CopyAssignment}; |
| 108 | + |
| 109 | + if (getLangOpts().CPlusPlus11) { |
| 110 | + AllSpecialMembers.push_back(SpecialMemberFunctionKind::MoveConstructor); |
| 111 | + AllSpecialMembers.push_back(SpecialMemberFunctionKind::MoveAssignment); |
| 112 | + } |
| 113 | + |
| 114 | + for (const auto &C : ClassWithSpecialMembers) { |
| 115 | + ArrayRef<SpecialMemberFunctionKind> DefinedSpecialMembers = C.second; |
| 116 | + |
| 117 | + if (DefinedSpecialMembers.size() == AllSpecialMembers.size()) |
| 118 | + continue; |
| 119 | + |
| 120 | + llvm::SmallVector<SpecialMemberFunctionKind, 5> UndefinedSpecialMembers; |
| 121 | + std::set_difference(AllSpecialMembers.begin(), AllSpecialMembers.end(), |
| 122 | + DefinedSpecialMembers.begin(), |
| 123 | + DefinedSpecialMembers.end(), |
| 124 | + std::back_inserter(UndefinedSpecialMembers)); |
| 125 | + |
| 126 | + diag(C.first.first, "class '%0' defines %1 but does not define %2") |
| 127 | + << C.first.second << join(DefinedSpecialMembers, " and ") |
| 128 | + << join(UndefinedSpecialMembers, " or "); |
| 129 | + } |
| 130 | +} |
| 131 | +} // namespace cppcoreguidelines |
| 132 | +} // namespace tidy |
| 133 | +} // namespace clang |
0 commit comments