clang-tools 22.0.0git
VirtualInheritanceCheck.cpp
Go to the documentation of this file.
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-exception
6//
7//===----------------------------------------------------------------------===//
8
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 return llvm::any_of(Node.bases(), [](const CXXBaseSpecifier &Base) {
24 return Base.isVirtual();
25 });
26}
27} // namespace
28
30 // Defining classes using direct virtual inheritance is disallowed.
31 Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"),
32 this);
33}
34
35void 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 clang::tidy::fuchsia
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)