Julie Hockett | 63b57db | 2017-12-15 18:54:28 +0000 | [diff] [blame] | 1 | //===--- VirtualInheritanceCheck.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 "VirtualInheritanceCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace fuchsia { |
| 19 | |
Benjamin Kramer | f8c9929 | 2018-02-18 19:02:35 +0000 | [diff] [blame^] | 20 | namespace { |
Julie Hockett | 63b57db | 2017-12-15 18:54:28 +0000 | [diff] [blame] | 21 | AST_MATCHER(CXXRecordDecl, hasDirectVirtualBaseClass) { |
| 22 | if (!Node.hasDefinition()) return false; |
| 23 | if (!Node.getNumVBases()) return false; |
| 24 | for (const CXXBaseSpecifier &Base : Node.bases()) |
| 25 | if (Base.isVirtual()) return true; |
| 26 | return false; |
| 27 | } |
Benjamin Kramer | f8c9929 | 2018-02-18 19:02:35 +0000 | [diff] [blame^] | 28 | } // namespace |
Julie Hockett | 63b57db | 2017-12-15 18:54:28 +0000 | [diff] [blame] | 29 | |
| 30 | void VirtualInheritanceCheck::registerMatchers(MatchFinder *Finder) { |
| 31 | // Defining classes using direct virtual inheritance is disallowed. |
| 32 | Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"), |
| 33 | this); |
| 34 | } |
| 35 | |
| 36 | void VirtualInheritanceCheck::check(const MatchFinder::MatchResult &Result) { |
| 37 | if (const auto *D = Result.Nodes.getNodeAs<CXXRecordDecl>("decl")) |
| 38 | diag(D->getLocStart(), "direct virtual inheritance is disallowed"); |
| 39 | } |
| 40 | |
| 41 | } // namespace fuchsia |
| 42 | } // namespace tidy |
| 43 | } // namespace clang |