clang-tools 19.0.0git
VirtualInheritanceCheck.cpp
Go to the documentation of this file.
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
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()) 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
28 // Defining classes using direct virtual inheritance is disallowed.
29 Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"),
30 this);
31}
32
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
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind