xref: /llvm-project/clang-tools-extra/clang-tidy/fuchsia/VirtualInheritanceCheck.cpp (revision 63b57db3969109c9f32a7835fdf4da73f8954272)
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 
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 
28 void VirtualInheritanceCheck::registerMatchers(MatchFinder *Finder) {
29   // Defining classes using direct virtual inheritance is disallowed.
30   Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"),
31                      this);
32 }
33 
34 void VirtualInheritanceCheck::check(const MatchFinder::MatchResult &Result) {
35   if (const auto *D = Result.Nodes.getNodeAs<CXXRecordDecl>("decl"))
36     diag(D->getLocStart(), "direct virtual inheritance is disallowed");
37 }
38 
39 }  // namespace fuchsia
40 }  // namespace tidy
41 }  // namespace clang
42