From 0a5d85e76f12d1819b0ed1d5b531e731e57c201c Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Mon, 19 Dec 2022 08:48:21 +0200 Subject: 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 --- compilerplugins/clang/implinheritancehelper.cxx | 120 +++++++++++++++++++++ .../clang/test/implinheritancehelper.cxx | 26 +++++ 2 files changed, 146 insertions(+) create mode 100644 compilerplugins/clang/implinheritancehelper.cxx create mode 100644 compilerplugins/clang/test/implinheritancehelper.cxx (limited to 'compilerplugins') 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 +#include + +#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 +{ +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(); + if (!rt) + continue; + auto const bd = cast(rt->getDecl())->getDefinition(); + auto ctx = bd->getDeclContext(); + if (!ctx->isNamespace()) + break; + auto ns = dyn_cast(ctx); + while (ns) + { + if (ns->getIdentifier() && ns->getName() == "star") + { + foundOne = true; + break; + } + ns = dyn_cast_or_null(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"); +} + +/* 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 + +#include +#include +#include +#include "com/sun/star/beans/XProperty.hpp" + +class VCLXDevice : public cppu::WeakImplHelper +{ +}; + +// 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: */ -- cgit