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