MultipleInheritanceCheck.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===--- MultipleInheritanceCheck.cpp - clang-tidy-------------------------===//
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "MultipleInheritanceCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang;
  12. using namespace clang::ast_matchers;
  13. namespace clang::tidy::fuchsia {
  14. namespace {
  15. AST_MATCHER(CXXRecordDecl, hasBases) {
  16. if (Node.hasDefinition())
  17. return Node.getNumBases() > 0;
  18. return false;
  19. }
  20. } // namespace
  21. // Adds a node (by name) to the interface map, if it was not present in the map
  22. // previously.
  23. void MultipleInheritanceCheck::addNodeToInterfaceMap(const CXXRecordDecl *Node,
  24. bool IsInterface) {
  25. assert(Node->getIdentifier());
  26. StringRef Name = Node->getIdentifier()->getName();
  27. InterfaceMap.insert(std::make_pair(Name, IsInterface));
  28. }
  29. // Returns "true" if the boolean "isInterface" has been set to the
  30. // interface status of the current Node. Return "false" if the
  31. // interface status for the current node is not yet known.
  32. bool MultipleInheritanceCheck::getInterfaceStatus(const CXXRecordDecl *Node,
  33. bool &IsInterface) const {
  34. assert(Node->getIdentifier());
  35. StringRef Name = Node->getIdentifier()->getName();
  36. llvm::StringMapConstIterator<bool> Pair = InterfaceMap.find(Name);
  37. if (Pair == InterfaceMap.end())
  38. return false;
  39. IsInterface = Pair->second;
  40. return true;
  41. }
  42. bool MultipleInheritanceCheck::isCurrentClassInterface(
  43. const CXXRecordDecl *Node) const {
  44. // Interfaces should have no fields.
  45. if (!Node->field_empty()) return false;
  46. // Interfaces should have exclusively pure methods.
  47. return llvm::none_of(Node->methods(), [](const CXXMethodDecl *M) {
  48. return M->isUserProvided() && !M->isPure() && !M->isStatic();
  49. });
  50. }
  51. bool MultipleInheritanceCheck::isInterface(const CXXRecordDecl *Node) {
  52. if (!Node->getIdentifier())
  53. return false;
  54. // Short circuit the lookup if we have analyzed this record before.
  55. bool PreviousIsInterfaceResult;
  56. if (getInterfaceStatus(Node, PreviousIsInterfaceResult))
  57. return PreviousIsInterfaceResult;
  58. // To be an interface, all base classes must be interfaces as well.
  59. for (const auto &I : Node->bases()) {
  60. if (I.isVirtual()) continue;
  61. const auto *Ty = I.getType()->getAs<RecordType>();
  62. if (!Ty) continue;
  63. const RecordDecl *D = Ty->getDecl()->getDefinition();
  64. if (!D) continue;
  65. const auto *Base = cast<CXXRecordDecl>(D);
  66. if (!isInterface(Base)) {
  67. addNodeToInterfaceMap(Node, false);
  68. return false;
  69. }
  70. }
  71. bool CurrentClassIsInterface = isCurrentClassInterface(Node);
  72. addNodeToInterfaceMap(Node, CurrentClassIsInterface);
  73. return CurrentClassIsInterface;
  74. }
  75. void MultipleInheritanceCheck::registerMatchers(MatchFinder *Finder) {
  76. // Match declarations which have bases.
  77. Finder->addMatcher(
  78. cxxRecordDecl(allOf(hasBases(), isDefinition())).bind("decl"), this);
  79. }
  80. void MultipleInheritanceCheck::check(const MatchFinder::MatchResult &Result) {
  81. if (const auto *D = Result.Nodes.getNodeAs<CXXRecordDecl>("decl")) {
  82. // Check against map to see if if the class inherits from multiple
  83. // concrete classes
  84. unsigned NumConcrete = 0;
  85. for (const auto &I : D->bases()) {
  86. if (I.isVirtual()) continue;
  87. const auto *Ty = I.getType()->getAs<RecordType>();
  88. if (!Ty) continue;
  89. const auto *Base = cast<CXXRecordDecl>(Ty->getDecl()->getDefinition());
  90. if (!isInterface(Base)) NumConcrete++;
  91. }
  92. // Check virtual bases to see if there is more than one concrete
  93. // non-virtual base.
  94. for (const auto &V : D->vbases()) {
  95. const auto *Ty = V.getType()->getAs<RecordType>();
  96. if (!Ty) continue;
  97. const auto *Base = cast<CXXRecordDecl>(Ty->getDecl()->getDefinition());
  98. if (!isInterface(Base)) NumConcrete++;
  99. }
  100. if (NumConcrete > 1) {
  101. diag(D->getBeginLoc(), "inheriting multiple classes that aren't "
  102. "pure virtual is discouraged");
  103. }
  104. }
  105. }
  106. } // namespace clang::tidy::fuchsia