clang-tools 10.0.0

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
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 {
16 namespace tidy {
17 namespace fuchsia {
18
19 namespace {
20 AST_MATCHER(CXXRecordDecl, hasDirectVirtualBaseClass) {
21 if (!Node.hasDefinition()) return false;
22 if (!Node.getNumVBases()) return false;
23 for (const CXXBaseSpecifier &Base : Node.bases())
24 if (Base.isVirtual()) return true;
25 return false;
26 }
27 } // namespace
28
29 void VirtualInheritanceCheck::registerMatchers(MatchFinder *Finder) {
30 // Defining classes using direct virtual inheritance is disallowed.
31 Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"),
32 this);
33 }
34
35 void 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 fuchsia
41 } // namespace tidy
42 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
VirtualInheritanceCheck.h
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:18
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Base
std::unique_ptr< GlobalCompilationDatabase > Base
Definition: GlobalCompilationDatabaseTests.cpp:83