[clang-tidy] Adding Fuchsia checker for virtual inheritance

Adds a check to the Fuchsia module to warn if classes are defined
with virtual inheritance.

See https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md for
reference.

Differential Revision: https://reviews.llvm.org/D40813

llvm-svn: 320841
This commit is contained in:
Julie Hockett 2017-12-15 18:54:28 +00:00
parent 76657f81ba
commit 63b57db396
8 changed files with 142 additions and 0 deletions

View File

@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyFuchsiaModule
DefaultArgumentsCheck.cpp
FuchsiaTidyModule.cpp
VirtualInheritanceCheck.cpp
LINK_LIBS
clangAST

View File

@ -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 @@ public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<DefaultArgumentsCheck>(
"fuchsia-default-arguments");
CheckFactories.registerCheck<VirtualInheritanceCheck>(
"fuchsia-virtual-inheritance");
}
};
// Register the FuchsiaTidyModule using this statically initialized variable.

View File

@ -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<CXXRecordDecl>("decl"))
diag(D->getLocStart(), "direct virtual inheritance is disallowed");
}
} // namespace fuchsia
} // namespace tidy
} // namespace clang

View File

@ -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

View File

@ -134,7 +134,12 @@ Improvements to clang-tidy
<http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-default-arguments.html>`_ check
Warns if a function or method is declared or called with default arguments.
- New `fuchsia-virtual-inheritance
<http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-virtual-inheritance.html>`_ check
Warns if classes are defined with virtual inheritance.
- New `google-objc-avoid-throwing-exception
<http://clang.llvm.org/extra/clang-tidy/checks/google-objc-avoid-throwing-exception.html>`_ check

View File

@ -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

View File

@ -69,6 +69,7 @@ Clang-Tidy Checks
cppcoreguidelines-slicing
cppcoreguidelines-special-member-functions
fuchsia-default-arguments
fuchsia-virtual-inheritance
google-build-explicit-make-pair
google-build-namespaces
google-build-using-namespace

View File

@ -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;
}