diff options
author | Julie Hockett <juliehockett@google.com> | 2017-12-15 18:54:28 +0000 |
---|---|---|
committer | Julie Hockett <juliehockett@google.com> | 2017-12-15 18:54:28 +0000 |
commit | 43d6a1c27721aaabf38e7f78fa368aec001430bf (patch) | |
tree | 1ba8aaa5e589483283dfdb00f5f5004ec11a4638 /clang-tidy/fuchsia | |
parent | 17c91c072e2772b6b728f4a6906dd0f4201395c5 (diff) |
[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
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@320841 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy/fuchsia')
-rw-r--r-- | clang-tidy/fuchsia/CMakeLists.txt | 1 | ||||
-rw-r--r-- | clang-tidy/fuchsia/FuchsiaTidyModule.cpp | 3 | ||||
-rw-r--r-- | clang-tidy/fuchsia/VirtualInheritanceCheck.cpp | 41 | ||||
-rw-r--r-- | clang-tidy/fuchsia/VirtualInheritanceCheck.h | 35 |
4 files changed, 80 insertions, 0 deletions
diff --git a/clang-tidy/fuchsia/CMakeLists.txt b/clang-tidy/fuchsia/CMakeLists.txt index 4a574197..692a3677 100644 --- a/clang-tidy/fuchsia/CMakeLists.txt +++ b/clang-tidy/fuchsia/CMakeLists.txt @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyFuchsiaModule DefaultArgumentsCheck.cpp FuchsiaTidyModule.cpp + VirtualInheritanceCheck.cpp LINK_LIBS clangAST diff --git a/clang-tidy/fuchsia/FuchsiaTidyModule.cpp b/clang-tidy/fuchsia/FuchsiaTidyModule.cpp index 708272f9..5ed834d9 100644 --- a/clang-tidy/fuchsia/FuchsiaTidyModule.cpp +++ b/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 @@ 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. diff --git a/clang-tidy/fuchsia/VirtualInheritanceCheck.cpp b/clang-tidy/fuchsia/VirtualInheritanceCheck.cpp new file mode 100644 index 00000000..6f3b433a --- /dev/null +++ b/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<CXXRecordDecl>("decl")) + diag(D->getLocStart(), "direct virtual inheritance is disallowed"); +} + +} // namespace fuchsia +} // namespace tidy +} // namespace clang diff --git a/clang-tidy/fuchsia/VirtualInheritanceCheck.h b/clang-tidy/fuchsia/VirtualInheritanceCheck.h new file mode 100644 index 00000000..b2c84c41 --- /dev/null +++ b/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 |