From 990163534cb5bc63ac500d5204f4caeab056bdf3 Mon Sep 17 00:00:00 2001 From: Luboš Luňák Date: Wed, 6 Mar 2019 14:39:13 +0100 Subject: move isDerivedFrom() from a clang plugin to shared code, for reuse Change-Id: I7b9b41a7081281214a387cdf02080866e9b9dfe7 Reviewed-on: https://gerrit.libreoffice.org/68873 Tested-by: Jenkins Reviewed-by: Noel Grandin --- compilerplugins/clang/check.cxx | 29 +++++++++++++++++++++++++++++ compilerplugins/clang/check.hxx | 6 ++++++ compilerplugins/clang/refcounting.cxx | 33 +-------------------------------- 3 files changed, 36 insertions(+), 32 deletions(-) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/check.cxx b/compilerplugins/clang/check.cxx index acda74adacd6..f2443e44a1f2 100644 --- a/compilerplugins/clang/check.cxx +++ b/compilerplugins/clang/check.cxx @@ -280,6 +280,35 @@ bool isOkToRemoveArithmeticCast( return true; } + +static bool BaseCheckNotSubclass(const clang::CXXRecordDecl *BaseDefinition, void *p) { + if (!BaseDefinition) + return true; + auto const & base = *static_cast(p); + if (base(BaseDefinition)) { + return false; + } + return true; +} + +bool isDerivedFrom(const clang::CXXRecordDecl *decl, DeclChecker base) { + if (!decl) + return false; + if (base(decl)) + return true; + if (!decl->hasDefinition()) { + return false; + } + if (!decl->forallBases( + [&base](const clang::CXXRecordDecl *BaseDefinition) -> bool + { return BaseCheckNotSubclass(BaseDefinition, &base); }, + true)) + { + return true; + } + return false; +} + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/check.hxx b/compilerplugins/clang/check.hxx index 4ae65f139955..0d636636a81a 100644 --- a/compilerplugins/clang/check.hxx +++ b/compilerplugins/clang/check.hxx @@ -140,6 +140,12 @@ private: bool const satisfied_; }; + +typedef std::function DeclChecker; +// Returns true if the class has a base matching the checker, or if the class itself matches. +bool isDerivedFrom(const clang::CXXRecordDecl *decl, DeclChecker base); + + namespace detail { ContextCheck checkRecordDecl( diff --git a/compilerplugins/clang/refcounting.cxx b/compilerplugins/clang/refcounting.cxx index 6ae861cfcad9..535808a3a0ca 100644 --- a/compilerplugins/clang/refcounting.cxx +++ b/compilerplugins/clang/refcounting.cxx @@ -35,7 +35,7 @@ not delete on last 'release'. */ -namespace { +namespace loplugin { class RefCounting: public loplugin::FilteringPlugin @@ -72,37 +72,6 @@ private: bool visitTemporaryObjectExpr(Expr const * expr); }; -typedef std::function DeclChecker; - -bool BaseCheckNotSubclass(const CXXRecordDecl *BaseDefinition, void *p) { - if (!BaseDefinition) - return true; - auto const & base = *static_cast(p); - if (base(BaseDefinition)) { - return false; - } - return true; -} - -bool isDerivedFrom(const CXXRecordDecl *decl, DeclChecker base) { - if (!decl) - return false; - if (base(decl)) - return true; - if (!decl->hasDefinition()) { - return false; - } - if (!decl->forallBases( - [&base](const CXXRecordDecl *BaseDefinition) -> bool - { return BaseCheckNotSubclass(BaseDefinition, &base); }, - true)) - { - return true; - } - return false; -} - - bool containsXInterfaceSubclass(const clang::Type* pType0); bool containsXInterfaceSubclass(const QualType& qType) { -- cgit