blob: f3da2b6ef1d1944df0cd1a460bf3b001a3978790 [file] [log] [blame]
Julie Hockett63b57db2017-12-15 18:54:28 +00001//===--- VirtualInheritanceCheck.cpp - clang-tidy--------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "VirtualInheritanceCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace fuchsia {
19
Benjamin Kramerf8c99292018-02-18 19:02:35 +000020namespace {
Julie Hockett63b57db2017-12-15 18:54:28 +000021AST_MATCHER(CXXRecordDecl, hasDirectVirtualBaseClass) {
22 if (!Node.hasDefinition()) return false;
23 if (!Node.getNumVBases()) return false;
24 for (const CXXBaseSpecifier &Base : Node.bases())
25 if (Base.isVirtual()) return true;
26 return false;
27}
Benjamin Kramerf8c99292018-02-18 19:02:35 +000028} // namespace
Julie Hockett63b57db2017-12-15 18:54:28 +000029
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->getLocStart(), "direct virtual inheritance is disallowed");
39}
40
41} // namespace fuchsia
42} // namespace tidy
43} // namespace clang