summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2020-02-25 19:05:10 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2020-02-26 07:06:32 +0100
commit9b18f4b2b064c823ac1500a0c297d113587a9bd4 (patch)
tree0f3b48cf01b397b108567e6160d6c5395003c09c /compilerplugins
parentb90eaab5c51c2b0f2aba66da35a5b494c3211f1d (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>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/test/xmlimport.cxx54
-rw-r--r--compilerplugins/clang/xmlimport.cxx139
2 files changed, 193 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: */