summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2021-10-01 20:55:05 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-10-02 08:00:28 +0200
commit7934085eb95cc0ff39e948525f0cc2b80edc0169 (patch)
treedb728dddcd2f77103213bf3d46979aa3375768e7 /compilerplugins
parent7b31f7428a1469fa823538fe5cf9cf36bbe2df68 (diff)
new loplugin:moveparam
Look for places where we can pass Primitive2DContainer by move reference and so avoid unnecessary copies. Change-Id: I1db167feba6d1a616ca6fc39778118ae20106bd1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122964 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/moveparam.cxx127
-rw-r--r--compilerplugins/clang/test/moveparam.cxx43
2 files changed, 170 insertions, 0 deletions
diff --git a/compilerplugins/clang/moveparam.cxx b/compilerplugins/clang/moveparam.cxx
new file mode 100644
index 000000000000..46816184071f
--- /dev/null
+++ b/compilerplugins/clang/moveparam.cxx
@@ -0,0 +1,127 @@
+/* -*- 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.
+ *
+ */
+
+#include <cassert>
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <set>
+#include <unordered_set>
+#include "plugin.hxx"
+#include "check.hxx"
+
+/*
+Look for places where we can pass by Primitive2DContainer param and so avoid
+unnecessary copies.
+*/
+
+namespace
+{
+class MoveParam : public loplugin::FilteringPlugin<MoveParam>
+{
+public:
+ explicit MoveParam(loplugin::InstantiationData const& data)
+ : FilteringPlugin(data)
+ {
+ }
+
+ virtual bool preRun() override { return true; }
+
+ virtual void run() override
+ {
+ if (preRun())
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+ bool PreTraverseConstructorInitializer(CXXCtorInitializer*);
+ bool PostTraverseConstructorInitializer(CXXCtorInitializer*, bool);
+ bool TraverseConstructorInitializer(CXXCtorInitializer*);
+ bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr*);
+};
+
+bool MoveParam::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* callExpr)
+{
+ if (ignoreLocation(callExpr))
+ return true;
+ if (!callExpr->isAssignmentOp())
+ return true;
+ if (!loplugin::TypeCheck(callExpr->getType())
+ .Class("Primitive2DContainer")
+ .Namespace("primitive2d"))
+ return true;
+ auto declRef = dyn_cast<DeclRefExpr>(callExpr->getArg(1)->IgnoreParenImpCasts());
+ if (!declRef)
+ return true;
+
+ auto parmVarDecl = dyn_cast_or_null<ParmVarDecl>(declRef->getDecl());
+ if (!parmVarDecl)
+ return true;
+
+ if (!loplugin::TypeCheck(parmVarDecl->getType()).LvalueReference().Const())
+ return true;
+
+ report(DiagnosticsEngine::Warning, "rather use move && param", compat::getBeginLoc(callExpr));
+
+ return true;
+}
+
+bool MoveParam::PreTraverseConstructorInitializer(CXXCtorInitializer* init)
+{
+ if (ignoreLocation(init->getSourceLocation()))
+ return true;
+ const FieldDecl* fieldDecl = init->getAnyMember();
+ if (!fieldDecl)
+ return true;
+
+ auto dc = loplugin::TypeCheck(fieldDecl->getType())
+ .Class("Primitive2DContainer")
+ .Namespace("primitive2d")
+ .Namespace("drawinglayer")
+ .GlobalNamespace();
+ if (!dc)
+ return true;
+
+ auto constructExpr = dyn_cast_or_null<CXXConstructExpr>(init->getInit());
+ if (!constructExpr || constructExpr->getNumArgs() != 1)
+ return true;
+
+ auto declRef = dyn_cast<DeclRefExpr>(constructExpr->getArg(0)->IgnoreParenImpCasts());
+ if (!declRef)
+ return true;
+
+ auto parmVarDecl = dyn_cast_or_null<ParmVarDecl>(declRef->getDecl());
+ if (!parmVarDecl)
+ return true;
+
+ if (!loplugin::TypeCheck(parmVarDecl->getType()).LvalueReference().Const())
+ return true;
+
+ report(DiagnosticsEngine::Warning, "rather use move && param", init->getSourceLocation());
+
+ return true;
+}
+bool MoveParam::PostTraverseConstructorInitializer(CXXCtorInitializer*, bool) { return true; }
+bool MoveParam::TraverseConstructorInitializer(CXXCtorInitializer* init)
+{
+ bool ret = true;
+ if (PreTraverseConstructorInitializer(init))
+ {
+ ret = FilteringPlugin<MoveParam>::TraverseConstructorInitializer(init);
+ PostTraverseConstructorInitializer(init, ret);
+ }
+ return ret;
+}
+
+loplugin::Plugin::Registration<MoveParam> moveparam("moveparam", true);
+
+} // namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/test/moveparam.cxx b/compilerplugins/clang/test/moveparam.cxx
new file mode 100644
index 000000000000..c90e8ae4bbd4
--- /dev/null
+++ b/compilerplugins/clang/test/moveparam.cxx
@@ -0,0 +1,43 @@
+/* -*- 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 "config_clang.h"
+#include "o3tl/cow_wrapper.hxx"
+
+namespace drawinglayer::primitive2d
+{
+class Primitive2DContainer
+{
+};
+}
+
+struct Foo
+{
+ drawinglayer::primitive2d::Primitive2DContainer maMine;
+
+ // expected-error@+2 {{rather use move && param [loplugin:moveparam]}}
+ Foo(drawinglayer::primitive2d::Primitive2DContainer const& rContainer)
+ : maMine(rContainer)
+ {
+ }
+
+ // no warning expected
+ Foo(drawinglayer::primitive2d::Primitive2DContainer&& rContainer)
+ : maMine(rContainer)
+ {
+ }
+
+ void foo1(const drawinglayer::primitive2d::Primitive2DContainer& rContainer)
+ {
+ // expected-error@+1 {{rather use move && param [loplugin:moveparam]}}
+ maMine = rContainer;
+ }
+};
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */