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