Index: clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt =================================================================== --- clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt +++ clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt @@ -3,6 +3,7 @@ add_clang_library(clangTidyFuchsiaModule DefaultArgumentsCheck.cpp FuchsiaTidyModule.cpp + VirtualInheritanceCheck.cpp LINK_LIBS clangAST Index: clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp +++ clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "DefaultArgumentsCheck.h" +#include "VirtualInheritanceCheck.h" using namespace clang::ast_matchers; @@ -24,6 +25,8 @@ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck( "fuchsia-default-arguments"); + CheckFactories.registerCheck( + "fuchsia-virtual-inheritance"); } }; // Register the FuchsiaTidyModule using this statically initialized variable. Index: clang-tools-extra/trunk/clang-tidy/fuchsia/VirtualInheritanceCheck.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/fuchsia/VirtualInheritanceCheck.h +++ clang-tools-extra/trunk/clang-tidy/fuchsia/VirtualInheritanceCheck.h @@ -0,0 +1,35 @@ +//===--- VirtualInheritanceCheck.h - clang-tidy------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_VIRTUAL_INHERITANCE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_VIRTUAL_INHERITANCE_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace fuchsia { + +/// Defining classes with virtual inheritance is disallowed. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-virtual-inheritance.html +class VirtualInheritanceCheck : public ClangTidyCheck { + public: + VirtualInheritanceCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace fuchsia +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_VIRTUAL_INHERITANCE_H Index: clang-tools-extra/trunk/clang-tidy/fuchsia/VirtualInheritanceCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/fuchsia/VirtualInheritanceCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/fuchsia/VirtualInheritanceCheck.cpp @@ -0,0 +1,41 @@ +//===--- VirtualInheritanceCheck.cpp - clang-tidy--------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "VirtualInheritanceCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace fuchsia { + +AST_MATCHER(CXXRecordDecl, hasDirectVirtualBaseClass) { + if (!Node.hasDefinition()) return false; + if (!Node.getNumVBases()) return false; + for (const CXXBaseSpecifier &Base : Node.bases()) + if (Base.isVirtual()) return true; + return false; +} + +void VirtualInheritanceCheck::registerMatchers(MatchFinder *Finder) { + // Defining classes using direct virtual inheritance is disallowed. + Finder->addMatcher(cxxRecordDecl(hasDirectVirtualBaseClass()).bind("decl"), + this); +} + +void VirtualInheritanceCheck::check(const MatchFinder::MatchResult &Result) { + if (const auto *D = Result.Nodes.getNodeAs("decl")) + diag(D->getLocStart(), "direct virtual inheritance is disallowed"); +} + +} // namespace fuchsia +} // namespace tidy +} // namespace clang Index: clang-tools-extra/trunk/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/trunk/docs/ReleaseNotes.rst +++ clang-tools-extra/trunk/docs/ReleaseNotes.rst @@ -134,7 +134,12 @@ `_ check Warns if a function or method is declared or called with default arguments. + +- New `fuchsia-virtual-inheritance + `_ check + Warns if classes are defined with virtual inheritance. + - New `google-objc-avoid-throwing-exception `_ check Index: clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-virtual-inheritance.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-virtual-inheritance.rst +++ clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-virtual-inheritance.rst @@ -0,0 +1,14 @@ +.. title:: clang-tidy - fuchsia-virtual-inheritance + +fuchsia-virtual-inheritance +=========================== + +Warns if classes are defined with virtual inheritance. + +For example, classes should not be defined with virtual inheritance: + +.. code-block:: c++ + + class B : public virtual A {}; // warning + +See the features disallowed in Fuchsia at https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md Index: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst @@ -69,6 +69,7 @@ cppcoreguidelines-slicing cppcoreguidelines-special-member-functions fuchsia-default-arguments + fuchsia-virtual-inheritance google-build-explicit-make-pair google-build-namespaces google-build-using-namespace Index: clang-tools-extra/trunk/test/clang-tidy/fuchsia-virtual-inheritance.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/fuchsia-virtual-inheritance.cpp +++ clang-tools-extra/trunk/test/clang-tidy/fuchsia-virtual-inheritance.cpp @@ -0,0 +1,42 @@ +// RUN: %check_clang_tidy %s fuchsia-virtual-inheritance %t + +class A { +public: + A(int value) : val(value) {} + + int do_A() { return val; } + +private: + int val; +}; + +class B : public virtual A { + // CHECK-MESSAGES: [[@LINE-1]]:1: warning: direct virtual inheritance is disallowed [fuchsia-virtual-inheritance] + // CHECK-NEXT: class B : public virtual A { +public: + B() : A(0) {} + int do_B() { return 1 + do_A(); } +}; + +class C : public virtual A { + // CHECK-MESSAGES: [[@LINE-1]]:1: warning: direct virtual inheritance is disallowed [fuchsia-virtual-inheritance] + // CHECK-NEXT: class C : public virtual A { +public: + C() : A(0) {} + int do_C() { return 2 + do_A(); } +}; + +class D : public B, public C { +public: + D(int value) : A(value), B(), C() {} + + int do_D() { return do_A() + do_B() + do_C(); } +}; + +int main() { + A *a = new A(0); + B *b = new B(); + C *c = new C(); + D *d = new D(0); + return 0; +}