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