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