1 //===--- 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 14 using namespace clang::ast_matchers; 15 16 namespace clang { 17 namespace tidy { 18 namespace fuchsia { 19 20 namespace { 21 AST_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 } 28 } // namespace 29 30 void VirtualInheritanceCheck::registerMatchers(MatchFinder *Finder) { 31 // Defining classes using direct virtual inheritance is disallowed. 32 Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"), 33 this); 34 } 35 36 void 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 44