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.grandin@collabora.co.uk>
This commit is contained in:
120
compilerplugins/clang/implinheritancehelper.cxx
Normal file
120
compilerplugins/clang/implinheritancehelper.cxx
Normal file
@@ -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: */
|
26
compilerplugins/clang/test/implinheritancehelper.cxx
Normal file
26
compilerplugins/clang/test/implinheritancehelper.cxx
Normal file
@@ -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: */
|
@@ -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 \
|
||||
|
Reference in New Issue
Block a user