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 { AST_MATCHER(CXXRecordDecl,hasDirectVirtualBaseClass)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 registerMatchers(MatchFinder * Finder)27void VirtualInheritanceCheck::registerMatchers(MatchFinder *Finder) { 28 // Defining classes using direct virtual inheritance is disallowed. 29 Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"), 30 this); 31 } 32 check(const MatchFinder::MatchResult & Result)33void 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"); 36 } 37 38 } // namespace clang::tidy::fuchsia 39