VirtualInheritanceCheck.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //===--- VirtualInheritanceCheck.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 "VirtualInheritanceCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::fuchsia {
  13. namespace {
  14. AST_MATCHER(CXXRecordDecl, hasDirectVirtualBaseClass) {
  15. if (!Node.hasDefinition()) return false;
  16. if (!Node.getNumVBases()) return false;
  17. for (const CXXBaseSpecifier &Base : Node.bases())
  18. if (Base.isVirtual()) return true;
  19. return false;
  20. }
  21. } // namespace
  22. void VirtualInheritanceCheck::registerMatchers(MatchFinder *Finder) {
  23. // Defining classes using direct virtual inheritance is disallowed.
  24. Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"),
  25. this);
  26. }
  27. void VirtualInheritanceCheck::check(const MatchFinder::MatchResult &Result) {
  28. if (const auto *D = Result.Nodes.getNodeAs<CXXRecordDecl>("decl"))
  29. diag(D->getBeginLoc(), "direct virtual inheritance is disallowed");
  30. }
  31. } // namespace clang::tidy::fuchsia