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
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::fuchsia {
16
17namespace {
18AST_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;
23 return false;
24}
25} // namespace
26
27void VirtualInheritanceCheck::registerMatchers(MatchFinder *Finder) {
28 // Defining classes using direct virtual inheritance is disallowed.
29 Finder->addMatcher(NodeMatch: cxxRecordDecl(hasDirectVirtualBaseClass()).bind(ID: "decl"),
30 Action: this);
31}
32
33void VirtualInheritanceCheck::check(const MatchFinder::MatchResult &Result) {
34 if (const auto *D = Result.Nodes.getNodeAs<CXXRecordDecl>(ID: "decl"))
35 diag(D->getBeginLoc(), "direct virtual inheritance is disallowed");
36}
37
38} // namespace clang::tidy::fuchsia
39

source code of clang-tools-extra/clang-tidy/fuchsia/VirtualInheritanceCheck.cpp