compilerplugins/clang/implinheritancehelper.cxx | 120 +++++++++++++++++++ compilerplugins/clang/test/implinheritancehelper.cxx | 26 ++++ solenv/CompilerTest_compilerplugins_clang.mk | 1 xmlhelp/source/cxxhelp/provider/provider.cxx | 49 ------- xmlhelp/source/cxxhelp/provider/provider.hxx | 17 -- 5 files changed, 150 insertions(+), 63 deletions(-)
New commits: commit 0a5d85e76f12d1819b0ed1d5b531e731e57c201c Author: Noel Grandin <noel.gran...@collabora.co.uk> AuthorDate: Mon Dec 19 08:48:21 2022 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Tue Dec 20 06:05:13 2022 +0000 new loplugin:implinheritancehelper Look for places we should be using ImplInheritanceHelper, which handles various boilerplate for us Change-Id: Icff6babf682c95b60aca86e6d6c2e2181eefc2f4 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/144444 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> diff --git a/compilerplugins/clang/implinheritancehelper.cxx b/compilerplugins/clang/implinheritancehelper.cxx new file mode 100644 index 000000000000..fb24ed96f2dd --- /dev/null +++ b/compilerplugins/clang/implinheritancehelper.cxx @@ -0,0 +1,120 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <string> +#include <iostream> + +#include "check.hxx" +#include "plugin.hxx" +#include "config_clang.h" +#include "clang/AST/CXXInheritance.h" + +/** + +Look for places where we should be using ImplInheritanceHelper + +*/ + +namespace +{ +class ImplInheritanceHelper : public loplugin::FilteringPlugin<ImplInheritanceHelper> +{ +public: + explicit ImplInheritanceHelper(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual bool preRun() override { return true; } + + virtual void run() override + { + if (preRun()) + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitCXXRecordDecl(const CXXRecordDecl*); +}; + +bool ImplInheritanceHelper::VisitCXXRecordDecl(const CXXRecordDecl* cxxRecordDecl) +{ + if (ignoreLocation(cxxRecordDecl)) + return true; + if (!cxxRecordDecl->isThisDeclarationADefinition()) + return true; + if (cxxRecordDecl->isDependentContext()) + return true; + + // ignore the utility template classes + SourceLocation spellingLocation + = compiler.getSourceManager().getSpellingLoc(cxxRecordDecl->getBeginLoc()); + StringRef fileName = getFilenameOfLocation(spellingLocation); + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/include/cppu")) + return true; + if (loplugin::isSamePathname(fileName, SRCDIR "/include/comphelper/compbase.hxx")) + return true; + + // not sure why this extends XPropertyState but does not support it in queryInterface. + if (loplugin::DeclCheck(cxxRecordDecl) + .Class("ChainablePropertySet") + .Namespace("comphelper") + .GlobalNamespace()) + return true; + // in these cases the UNO interface is optional + if (loplugin::DeclCheck(cxxRecordDecl).Class("OFSInputStreamContainer").GlobalNamespace()) + return true; + if (loplugin::DeclCheck(cxxRecordDecl) + .Class("OPropertyBrowserController") + .Namespace("pcr") + .GlobalNamespace()) + return true; + + // check if this class extends cppu::WeakImplHelper + if (!loplugin::isDerivedFrom(cxxRecordDecl, [](Decl const* decl) -> bool { + return bool(loplugin::DeclCheck(decl) + .Class("WeakImplHelper") + .Namespace("cppu") + .GlobalNamespace()); + })) + return true; + // check if this class directly inherits from a UNO interface class + bool foundOne = false; + for (auto const& i : cxxRecordDecl->bases()) + { + auto rt = i.getType()->getAs<RecordType>(); + if (!rt) + continue; + auto const bd = cast<CXXRecordDecl>(rt->getDecl())->getDefinition(); + auto ctx = bd->getDeclContext(); + if (!ctx->isNamespace()) + break; + auto ns = dyn_cast<NamespaceDecl>(ctx); + while (ns) + { + if (ns->getIdentifier() && ns->getName() == "star") + { + foundOne = true; + break; + } + ns = dyn_cast_or_null<NamespaceDecl>(ns->getParent()); + } + } + if (!foundOne) + return true; + report(DiagnosticsEngine::Warning, "can probably use ImplInheritanceHelper here", + cxxRecordDecl->getLocation()) + << cxxRecordDecl->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration<ImplInheritanceHelper> + implinheritancehelper("implinheritancehelper"); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/test/implinheritancehelper.cxx b/compilerplugins/clang/test/implinheritancehelper.cxx new file mode 100644 index 000000000000..4ef976641c7e --- /dev/null +++ b/compilerplugins/clang/test/implinheritancehelper.cxx @@ -0,0 +1,26 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <com/sun/star/uno/XInterface.hpp> +#include <com/sun/star/lang/XUnoTunnel.hpp> +#include <cppuhelper/implbase.hxx> +#include "com/sun/star/beans/XProperty.hpp" + +class VCLXDevice : public cppu::WeakImplHelper<css::lang::XUnoTunnel> +{ +}; + +// expected-error@+1 {{can probably use ImplInheritanceHelper here [loplugin:implinheritancehelper]}} +class VCLXCheckBox : public css::beans::XProperty, public VCLXDevice +{ +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk index 6ea19156854f..08fd7e974e96 100644 --- a/solenv/CompilerTest_compilerplugins_clang.mk +++ b/solenv/CompilerTest_compilerplugins_clang.mk @@ -38,6 +38,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \ compilerplugins/clang/test/fragiledestructor \ compilerplugins/clang/test/getstr \ compilerplugins/clang/test/implicitboolconversion \ + compilerplugins/clang/test/implinheritancehelper \ compilerplugins/clang/test/indentation \ compilerplugins/clang/test/intvsfloat \ compilerplugins/clang/test/logexceptionnicely \ commit 5465a60ec2637707479487f839341da5dc8fd2cd Author: Noel Grandin <noel.gran...@collabora.co.uk> AuthorDate: Mon Dec 19 08:46:00 2022 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Tue Dec 20 06:05:05 2022 +0000 loplugin:implinheritancehelper in xmlhelp use more ImplInheritanceHelper to reduce boilerplate Change-Id: Ie8104d40dd13b02f19bcaf1806255446d17b25a6 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/144443 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> diff --git a/xmlhelp/source/cxxhelp/provider/provider.cxx b/xmlhelp/source/cxxhelp/provider/provider.cxx index c98d3704ceb1..eb28f5a9ca37 100644 --- a/xmlhelp/source/cxxhelp/provider/provider.cxx +++ b/xmlhelp/source/cxxhelp/provider/provider.cxx @@ -43,7 +43,7 @@ using namespace chelp; // ContentProvider Implementation. ContentProvider::ContentProvider( const uno::Reference< uno::XComponentContext >& rxContext ) - : ::ucbhelper::ContentProviderImplHelper( rxContext ) + : ContentProvider_Base( rxContext ) , isInitialized( false ) { } @@ -53,53 +53,6 @@ ContentProvider::~ContentProvider() { } -// XInterface methods. -void SAL_CALL ContentProvider::acquire() - noexcept -{ - OWeakObject::acquire(); -} - -void SAL_CALL ContentProvider::release() - noexcept -{ - OWeakObject::release(); -} - -css::uno::Any SAL_CALL ContentProvider::queryInterface( const css::uno::Type & rType ) -{ - css::uno::Any aRet = cppu::queryInterface( rType, - static_cast< lang::XTypeProvider* >(this), - static_cast< lang::XServiceInfo* >(this), - static_cast< ucb::XContentProvider* >(this), - static_cast< lang::XComponent* >(this), - static_cast< lang::XEventListener* >(this), - static_cast< container::XContainerListener* >(this) - ); - return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); -} - -// XTypeProvider methods. - -css::uno::Sequence< sal_Int8 > SAL_CALL ContentProvider::getImplementationId() -{ - return css::uno::Sequence<sal_Int8>(); -} - -css::uno::Sequence< css::uno::Type > SAL_CALL ContentProvider::getTypes() -{ - static cppu::OTypeCollection ourTypeCollection( - cppu::UnoType<lang::XTypeProvider>::get(), - cppu::UnoType<lang::XServiceInfo>::get(), - cppu::UnoType<ucb::XContentProvider>::get(), - cppu::UnoType<lang::XComponent>::get(), - cppu::UnoType<container::XContainerListener>::get() - ); - - return ourTypeCollection.getTypes(); -} - - // XServiceInfo methods. OUString SAL_CALL ContentProvider::getImplementationName() diff --git a/xmlhelp/source/cxxhelp/provider/provider.hxx b/xmlhelp/source/cxxhelp/provider/provider.hxx index 59ee39d828b3..2989d7f85cfc 100644 --- a/xmlhelp/source/cxxhelp/provider/provider.hxx +++ b/xmlhelp/source/cxxhelp/provider/provider.hxx @@ -39,10 +39,8 @@ inline constexpr OUStringLiteral MYUCP_CONTENT_TYPE = u"application/vnd.sun.star class Databases; - class ContentProvider : - public ::ucbhelper::ContentProviderImplHelper, - public css::container::XContainerListener, - public css::lang::XComponent + typedef cppu::ImplInheritanceHelper< ::ucbhelper::ContentProviderImplHelper, css::container::XContainerListener, css::lang::XComponent> ContentProvider_Base; + class ContentProvider : public ContentProvider_Base { public: explicit ContentProvider( @@ -50,17 +48,6 @@ inline constexpr OUStringLiteral MYUCP_CONTENT_TYPE = u"application/vnd.sun.star virtual ~ContentProvider() override; - // XInterface - virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type & rType ) override; - virtual void SAL_CALL acquire() - noexcept override; - virtual void SAL_CALL release() - noexcept override; - - // XTypeProvider - virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override; - virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override; - // XServiceInfo virtual OUString SAL_CALL getImplementationName() override; virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;