[Libreoffice-commits] core.git: compilerplugins/clang connectivity/source forms/source
Noel Grandin
noel at peralex.com
Wed Nov 5 06:00:14 PST 2014
compilerplugins/clang/store/findoncontainer.cxx | 77 ++++++++++++++++++++++
connectivity/source/drivers/mork/MQueryHelper.cxx | 2
forms/source/misc/InterfaceContainer.cxx | 5 -
3 files changed, 80 insertions(+), 4 deletions(-)
New commits:
commit 5f15cc01b31ccaed0c6482a36556dece084ce302
Author: Noel Grandin <noel at peralex.com>
Date: Wed Nov 5 14:48:20 2014 +0200
new loplugin: use more efficient find() methods
(Original idea from Kendy)
Look for code that is calling std::find on a sorted container
(set/map/vector) and warn about it - the code should be using
the find method on the container itself, since that is considerably faster.
Change-Id: Ib74e5d3faa836eeb0df16a736d202696626bdfd2
diff --git a/compilerplugins/clang/store/findoncontainer.cxx b/compilerplugins/clang/store/findoncontainer.cxx
new file mode 100644
index 0000000..f7db49d
--- /dev/null
+++ b/compilerplugins/clang/store/findoncontainer.cxx
@@ -0,0 +1,77 @@
+/* -*- 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 <set>
+
+#include "plugin.hxx"
+
+// Look for places calling std::find on a standard container where it should be using the container find method, which
+// is more efficient.
+//
+// This lives in /store because the implementation is a hack and is highly dependant on the inwards
+// of the libc++ library on the machine it runs on.
+//
+
+namespace {
+
+class FindOnContainer:
+ public RecursiveASTVisitor<FindOnContainer>, public loplugin::Plugin
+{
+public:
+ explicit FindOnContainer(InstantiationData const & data): Plugin(data) {}
+
+ virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
+
+ bool VisitCallExpr(const CallExpr * expr);
+};
+
+bool FindOnContainer::VisitCallExpr(const CallExpr * expr) {
+ if (ignoreLocation(expr)) {
+ return true;
+ }
+ FunctionDecl const * fdecl = expr->getDirectCallee();
+ if (fdecl == nullptr) {
+ return true;
+ }
+ std::string qname { fdecl->getQualifiedNameAsString() };
+ if (qname == "std::find")
+ {
+ std::string tname = expr->getArg(0)->getType().getAsString();
+ if (tname.find("std::_List_iterator") != std::string::npos
+ || tname.find("std::_List_const_iterator") != std::string::npos
+ || tname.find("std::vector") != std::string::npos
+ || tname.find("std::_Deque_iterator") != std::string::npos
+ || tname == "const int *"
+ || tname == "struct std::_Bit_const_iterator"
+ || tname == "const rtl::OUString *"
+ || tname == "class rtl::OUString *"
+ || tname == "const class rtl::OUString *"
+ || tname == "const sal_Int8 *"
+ || tname == "const sal_Int32 *"
+ || tname == "sal_Int32 *"
+ || tname == "sal_uInt16 *" )
+ {
+ return true;
+ }
+ expr->dump();
+ report(
+ DiagnosticsEngine::Warning,
+ ("rather use the more specific find method " + tname),
+ expr->getExprLoc());
+ return true;
+ }
+ return true;
+}
+
+loplugin::Plugin::Registration< FindOnContainer > X("findoncontainer");
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/connectivity/source/drivers/mork/MQueryHelper.cxx b/connectivity/source/drivers/mork/MQueryHelper.cxx
index ff1d14e..686af4e 100644
--- a/connectivity/source/drivers/mork/MQueryHelper.cxx
+++ b/connectivity/source/drivers/mork/MQueryHelper.cxx
@@ -228,7 +228,7 @@ sal_Int32 MQueryHelper::executeQuery(OConnection* xConnection, MQueryExpression
{
int rowId = rowIter->first;
// belongs this row id to the list table?
- if (listRecords.end() == std::find(listRecords.begin(), listRecords.end(), rowId))
+ if (listRecords.end() == listRecords.find(rowId))
{
// no, skip it
continue;
diff --git a/forms/source/misc/InterfaceContainer.cxx b/forms/source/misc/InterfaceContainer.cxx
index 5b0efaf..e740c37 100644
--- a/forms/source/misc/InterfaceContainer.cxx
+++ b/forms/source/misc/InterfaceContainer.cxx
@@ -680,9 +680,8 @@ throw (::com::sun::star::uno::RuntimeException, std::exception) {
if (evt.PropertyName == PROPERTY_NAME)
{
::osl::MutexGuard aGuard( m_rMutex );
- OInterfaceMap::iterator i = ::std::find(m_aMap.begin(), m_aMap.end(),
- ::std::pair<const OUString, InterfaceRef >(::comphelper::getString(evt.OldValue),evt.Source));
- if (i != m_aMap.end())
+ OInterfaceMap::iterator i = m_aMap.find(::comphelper::getString(evt.OldValue));
+ if (i != m_aMap.end() && (*i).second != evt.Source)
{
InterfaceRef xCorrectType((*i).second);
m_aMap.erase(i);
More information about the Libreoffice-commits
mailing list