diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2020-02-25 19:05:10 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2020-02-26 07:06:32 +0100 |
commit | 9b18f4b2b064c823ac1500a0c297d113587a9bd4 (patch) | |
tree | 0f3b48cf01b397b108567e6160d6c5395003c09c | |
parent | b90eaab5c51c2b0f2aba66da35a5b494c3211f1d (diff) |
new loplugin:xmlimport
to help me maintain the invariants when updating code to use the
FastParser APIs. One weird invariant is that you need to override
startFastElement or the createFastChildContext will not get called.
Not all of these changes are probably necessary - some of the classes
are never constructured themselves, only their subclasses are
constructed, and their subclasses maintain the invariants, but it is
just easier to scatter a few more startFastElement around
Change-Id: I3f70fb5a1e44c311cf4926fa7b0fcda605709eac
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/89473
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
20 files changed, 251 insertions, 0 deletions
diff --git a/compilerplugins/clang/test/xmlimport.cxx b/compilerplugins/clang/test/xmlimport.cxx new file mode 100644 index 000000000000..965d4936e41c --- /dev/null +++ b/compilerplugins/clang/test/xmlimport.cxx @@ -0,0 +1,54 @@ +/* -*- 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" + +// Cannot include this, makes clang crash +//#include "xmloff/xmlimp.hxx" + +class SvXMLImportContext +{ +public: + virtual ~SvXMLImportContext() {} + + virtual void createFastChildContext() {} + virtual void startFastElement() {} + virtual void endFastElement() {} +}; + +class Test1 : public SvXMLImportContext +{ +public: + // expected-error@+1 {{must override startFastElement too [loplugin:xmlimport]}} + virtual void createFastChildContext() override; +}; + +class Test2 : public SvXMLImportContext +{ +public: + // no warning expected + virtual void createFastChildContext() override; + virtual void startFastElement() override {} +}; + +class Test3 : public Test2 +{ +public: + // no warning expected + virtual void createFastChildContext() override; +}; + +class Test4 : public SvXMLImportContext +{ +public: + // expected-error@+1 {{must override startFastElement too [loplugin:xmlimport]}} + virtual void endFastElement() override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/xmlimport.cxx b/compilerplugins/clang/xmlimport.cxx new file mode 100644 index 000000000000..bdb41f616859 --- /dev/null +++ b/compilerplugins/clang/xmlimport.cxx @@ -0,0 +1,139 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ +#ifndef LO_CLANG_SHARED_PLUGINS + +#include "compat.hxx" +#include "plugin.hxx" +#include "check.hxx" +#include <iostream> +#include "clang/AST/CXXInheritance.h" + +/* + * This is a compile-time checker. + * + * Check that when we override SvXmlImportContext, and we override createFastChildContext, + * we have also overridden startFastElement, or the fast-parser stuff will not work + * correctly. +*/ + +namespace +{ +class XmlImport : public loplugin::FilteringPlugin<XmlImport> +{ +public: + explicit XmlImport(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + bool preRun() override + { + // std::string fn(handler.getMainFileName()); + // loplugin::normalizeDotDotInFilePath(fn); + return true; + } + + void run() override + { + if (preRun()) + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + bool VisitCXXMethodDecl(const CXXMethodDecl*); +}; + +static bool containsStartFastElementMethod(const CXXRecordDecl* cxxRecordDecl, + bool& rbFoundImportContext) +{ + auto dc = loplugin::DeclCheck(cxxRecordDecl); + if (dc.Class("SvXMLImportContext")) + { + rbFoundImportContext = true; + return false; + } + if (dc.Class("XFastContextHandler")) + return false; + for (auto it = cxxRecordDecl->method_begin(); it != cxxRecordDecl->method_end(); ++it) + { + auto i = *it; + if (i->getIdentifier() && i->getName() == "startFastElement") + { + // i->dump(); + return true; + } + } + return false; +} +bool XmlImport::VisitCXXMethodDecl(const CXXMethodDecl* methodDecl) +{ + auto beginLoc = compat::getBeginLoc(methodDecl); + if (!beginLoc.isValid() || ignoreLocation(beginLoc)) + return true; + + auto cxxRecordDecl = methodDecl->getParent(); + if (!cxxRecordDecl || !cxxRecordDecl->getIdentifier()) + return true; + auto className = cxxRecordDecl->getName(); + if (className == "OOXMLFactory") // writerfilter + return true; + if (className == "SvXMLLegacyToFastDocHandler" || className == "ImportDocumentHandler" + || className == "ExportDocumentHandler") // reportdesign + return true; + if (className == "XMLEmbeddedObjectExportFilter" || className == "XMLBasicExportFilter" + || className == "XMLTransformerBase" || className == "SvXMLMetaExport") // xmloff + return true; + if (!methodDecl->getIdentifier()) + return true; + if (!(methodDecl->getName() == "createFastChildContext" || methodDecl->getName() == "characters" + || methodDecl->getName() == "endFastElement")) + return true; + if (loplugin::DeclCheck(cxxRecordDecl).Class("SvXMLImportContext")) + return true; + + bool foundImportContext = false; + if (containsStartFastElementMethod(cxxRecordDecl, foundImportContext)) + return true; + + bool foundStartFastElement = false; + CXXBasePaths aPaths; + cxxRecordDecl->lookupInBases( + [&](const CXXBaseSpecifier* Specifier, CXXBasePath & /*Path*/) -> bool { + if (!Specifier->getType().getTypePtr()) + return false; + const CXXRecordDecl* baseCXXRecordDecl = Specifier->getType()->getAsCXXRecordDecl(); + if (!baseCXXRecordDecl) + return false; + if (baseCXXRecordDecl->isInvalidDecl()) + return false; + foundStartFastElement + |= containsStartFastElementMethod(baseCXXRecordDecl, foundImportContext); + return false; + }, + aPaths); + + if (foundStartFastElement || !foundImportContext) + return true; + + report(DiagnosticsEngine::Warning, "must override startFastElement too", + compat::getBeginLoc(methodDecl)) + << methodDecl->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration<XmlImport> xmlimport("xmlimport"); + +} // namespace + +#endif // LO_CLANG_SHARED_PLUGINS + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dbaccess/source/filter/xml/xmlfilter.cxx b/dbaccess/source/filter/xml/xmlfilter.cxx index eb0e3d505202..4024bec4cfb6 100644 --- a/dbaccess/source/filter/xml/xmlfilter.cxx +++ b/dbaccess/source/filter/xml/xmlfilter.cxx @@ -426,6 +426,10 @@ public: { } + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} + virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& /*xAttrList*/ ) override { diff --git a/editeng/source/misc/SvXMLAutoCorrectImport.hxx b/editeng/source/misc/SvXMLAutoCorrectImport.hxx index f61cd036b526..c45180608390 100644 --- a/editeng/source/misc/SvXMLAutoCorrectImport.hxx +++ b/editeng/source/misc/SvXMLAutoCorrectImport.hxx @@ -97,6 +97,9 @@ private: public: SvXMLExceptionListContext ( SvXMLExceptionListImport& rImport ); + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual css::uno::Reference<XFastContextHandler> SAL_CALL createFastChildContext( sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override; @@ -109,6 +112,9 @@ public: SvXMLExceptionContext ( SvXMLExceptionListImport& rImport, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ); + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual ~SvXMLExceptionContext() override; }; diff --git a/include/xmloff/XMLCharContext.hxx b/include/xmloff/XMLCharContext.hxx index 4883a39f7922..9cdfa63402be 100644 --- a/include/xmloff/XMLCharContext.hxx +++ b/include/xmloff/XMLCharContext.hxx @@ -59,6 +59,9 @@ public: virtual ~XMLCharContext() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} // EndElement is called before a context will be destructed, but // after an elements context has been parsed. It may be used for actions // that require virtual methods. The default is to do nothing. diff --git a/linguistic/source/convdicxml.cxx b/linguistic/source/convdicxml.cxx index ecae4e48548e..fe5ff97105aa 100644 --- a/linguistic/source/convdicxml.cxx +++ b/linguistic/source/convdicxml.cxx @@ -85,6 +85,9 @@ public: } // SvXMLImportContext + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual void SAL_CALL characters( const OUString &rChars ) override; virtual css::uno::Reference<XFastContextHandler> SAL_CALL createFastChildContext( sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override; diff --git a/reportdesign/source/filter/xml/xmlColumn.hxx b/reportdesign/source/filter/xml/xmlColumn.hxx index 16e739e9bca1..cd87a9cbebb9 100644 --- a/reportdesign/source/filter/xml/xmlColumn.hxx +++ b/reportdesign/source/filter/xml/xmlColumn.hxx @@ -42,6 +42,9 @@ namespace rptxml ); virtual ~OXMLRowColumn() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; diff --git a/reportdesign/source/filter/xml/xmlCondPrtExpr.hxx b/reportdesign/source/filter/xml/xmlCondPrtExpr.hxx index 426188efcc63..7842e95c71d7 100644 --- a/reportdesign/source/filter/xml/xmlCondPrtExpr.hxx +++ b/reportdesign/source/filter/xml/xmlCondPrtExpr.hxx @@ -37,6 +37,9 @@ namespace rptxml ,const css::uno::Reference< css::beans::XPropertySet >& _xComponent); virtual ~OXMLCondPrtExpr() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} // This method is called for all characters that are contained in the // current element. The default is to ignore them. virtual void SAL_CALL characters( const OUString& rChars ) override; diff --git a/reportdesign/source/filter/xml/xmlControlProperty.hxx b/reportdesign/source/filter/xml/xmlControlProperty.hxx index 15379d27b720..b1b2c051aba7 100644 --- a/reportdesign/source/filter/xml/xmlControlProperty.hxx +++ b/reportdesign/source/filter/xml/xmlControlProperty.hxx @@ -53,6 +53,10 @@ namespace rptxml virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} + virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; virtual void SAL_CALL characters( const OUString& rChars ) override; diff --git a/reportdesign/source/filter/xml/xmlFormatCondition.hxx b/reportdesign/source/filter/xml/xmlFormatCondition.hxx index 56bd5602fb24..a4594b495c7a 100644 --- a/reportdesign/source/filter/xml/xmlFormatCondition.hxx +++ b/reportdesign/source/filter/xml/xmlFormatCondition.hxx @@ -39,6 +39,9 @@ namespace rptxml ,const css::uno::Reference< css::report::XFormatCondition >& _xComponent ); virtual ~OXMLFormatCondition() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; }; diff --git a/reportdesign/source/filter/xml/xmlFunction.hxx b/reportdesign/source/filter/xml/xmlFunction.hxx index da3f6ac118b6..c58ed9738aab 100644 --- a/reportdesign/source/filter/xml/xmlFunction.hxx +++ b/reportdesign/source/filter/xml/xmlFunction.hxx @@ -47,6 +47,9 @@ namespace rptxml ); virtual ~OXMLFunction() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; }; diff --git a/reportdesign/source/filter/xml/xmlGroup.hxx b/reportdesign/source/filter/xml/xmlGroup.hxx index ea1b97602aaf..70eeb22db992 100644 --- a/reportdesign/source/filter/xml/xmlGroup.hxx +++ b/reportdesign/source/filter/xml/xmlGroup.hxx @@ -42,6 +42,9 @@ namespace rptxml ); virtual ~OXMLGroup() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; diff --git a/reportdesign/source/filter/xml/xmlMasterFields.hxx b/reportdesign/source/filter/xml/xmlMasterFields.hxx index e8e212827e60..5dbb36987bb0 100644 --- a/reportdesign/source/filter/xml/xmlMasterFields.hxx +++ b/reportdesign/source/filter/xml/xmlMasterFields.hxx @@ -36,6 +36,9 @@ namespace rptxml ,IMasterDetailFieds* _pReport); virtual ~OXMLMasterFields() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; }; diff --git a/reportdesign/source/filter/xml/xmlReportElement.hxx b/reportdesign/source/filter/xml/xmlReportElement.hxx index 5f64dbe732e8..dbf5d180c4d0 100644 --- a/reportdesign/source/filter/xml/xmlReportElement.hxx +++ b/reportdesign/source/filter/xml/xmlReportElement.hxx @@ -38,6 +38,9 @@ namespace rptxml ,const css::uno::Reference< css::report::XReportControlModel >& _xComponent); virtual ~OXMLReportElement() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; }; diff --git a/reportdesign/source/filter/xml/xmlSection.hxx b/reportdesign/source/filter/xml/xmlSection.hxx index 26e8517232ff..797cc3df55a9 100644 --- a/reportdesign/source/filter/xml/xmlSection.hxx +++ b/reportdesign/source/filter/xml/xmlSection.hxx @@ -42,6 +42,9 @@ namespace rptxml ,bool _bPageHeader = true); virtual ~OXMLSection() override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; }; diff --git a/reportdesign/source/filter/xml/xmlTable.hxx b/reportdesign/source/filter/xml/xmlTable.hxx index 508ad5087166..b84f7310a59e 100644 --- a/reportdesign/source/filter/xml/xmlTable.hxx +++ b/reportdesign/source/filter/xml/xmlTable.hxx @@ -65,6 +65,8 @@ namespace rptxml virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; void addHeight(sal_Int32 _nHeight) { m_aHeight.push_back(_nHeight); } diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk index dab7501be1a6..80fd67f347a8 100644 --- a/solenv/CompilerTest_compilerplugins_clang.mk +++ b/solenv/CompilerTest_compilerplugins_clang.mk @@ -105,6 +105,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \ compilerplugins/clang/test/vclwidgets \ compilerplugins/clang/test/weakbase \ compilerplugins/clang/test/writeonlyvars \ + compilerplugins/clang/test/xmlimport \ )) $(eval $(call gb_CompilerTest_use_externals,compilerplugins_clang, \ diff --git a/sw/source/filter/xml/xmlimp.cxx b/sw/source/filter/xml/xmlimp.cxx index 01b22fef1c3a..0ea3690eddac 100644 --- a/sw/source/filter/xml/xmlimp.cxx +++ b/sw/source/filter/xml/xmlimp.cxx @@ -131,6 +131,10 @@ public: SwXMLBodyContext_Impl( SwXMLImport& rImport ); + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} + virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override; }; diff --git a/sw/source/uibase/config/StoredChapterNumbering.cxx b/sw/source/uibase/config/StoredChapterNumbering.cxx index d5f69b2fe7c4..5deb102ac024 100644 --- a/sw/source/uibase/config/StoredChapterNumbering.cxx +++ b/sw/source/uibase/config/StoredChapterNumbering.cxx @@ -317,6 +317,10 @@ public: { } + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} + virtual void SAL_CALL endFastElement(sal_Int32 /*Element*/) override { assert(m_Contexts.size() < SwChapterNumRules::nMaxRules); diff --git a/xmloff/source/text/XMLAutoTextContainerEventImport.hxx b/xmloff/source/text/XMLAutoTextContainerEventImport.hxx index 69923aded3ba..686e9b0cd3d6 100644 --- a/xmloff/source/text/XMLAutoTextContainerEventImport.hxx +++ b/xmloff/source/text/XMLAutoTextContainerEventImport.hxx @@ -51,6 +51,9 @@ public: protected: + virtual void SAL_CALL startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override {} virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 /*nElement*/, const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*rxAttribs*/) override |