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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang-tools-extra/clang-tidy/fuchsia/VirtualInheritanceCheck.cpp