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