summaryrefslogtreecommitdiffstats
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2021-09-13 13:02:14 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-09-13 15:05:56 +0200
commit6c7c7bcdbd3e47cad5c6b94e1ed1c5314dbdbf6b (patch)
tree4ab84fa839582aa6922450429d15f3b10cec74a5 /compilerplugins
parentfix ccache size detection with ccache 4.4 (diff)
downloadcore-6c7c7bcdbd3e47cad5c6b94e1ed1c5314dbdbf6b.tar.gz
core-6c7c7bcdbd3e47cad5c6b94e1ed1c5314dbdbf6b.zip
loplugin:cow_wrapper
check the classes that use o3tl::cow_wrapper so that they don't unnecessarity trigger copies Change-Id: I545e627598217f3e61ba2c384adb8d4f8b404829 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122030 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/cow_wrapper.cxx88
-rw-r--r--compilerplugins/clang/test/cow_wrapper.cxx33
2 files changed, 121 insertions, 0 deletions
diff --git a/compilerplugins/clang/cow_wrapper.cxx b/compilerplugins/clang/cow_wrapper.cxx
new file mode 100644
index 000000000000..4c04674f65c1
--- /dev/null
+++ b/compilerplugins/clang/cow_wrapper.cxx
@@ -0,0 +1,88 @@
+/* -*- 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 <cassert>
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <set>
+#include <unordered_set>
+#include "plugin.hxx"
+#include "check.hxx"
+
+/*
+Look for places where we are using cow_wrapper, but we are calling a const method on the impl object
+with a non-const pointer, which means we will unnnecessarily trigger a copy.
+*/
+
+namespace
+{
+class Cow_Wrapper : public loplugin::FilteringPlugin<Cow_Wrapper>
+{
+public:
+ explicit Cow_Wrapper(loplugin::InstantiationData const& data)
+ : FilteringPlugin(data)
+ {
+ }
+
+ virtual bool preRun() override { return true; }
+
+ virtual void run() override
+ {
+ if (preRun())
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+ bool VisitCXXMemberCallExpr(const CXXMemberCallExpr*);
+};
+
+bool Cow_Wrapper::VisitCXXMemberCallExpr(const CXXMemberCallExpr* memberCallExpr)
+{
+ if (ignoreLocation(memberCallExpr))
+ return true;
+ auto methodDecl = memberCallExpr->getMethodDecl();
+ if (!methodDecl || !methodDecl->isConst())
+ return true;
+
+ auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(
+ compat::IgnoreImplicit(memberCallExpr->getImplicitObjectArgument()));
+ if (!operatorCallExpr)
+ return true;
+ if (operatorCallExpr->getOperator() != OO_Arrow)
+ return true;
+ auto arrowMethodDecl = dyn_cast_or_null<CXXMethodDecl>(operatorCallExpr->getDirectCallee());
+ if (!arrowMethodDecl)
+ return true;
+ if (arrowMethodDecl->isConst())
+ return true;
+ auto dc = loplugin::DeclCheck(arrowMethodDecl->getParent())
+ .Class("cow_wrapper")
+ .Namespace("o3tl")
+ .GlobalNamespace();
+ if (!dc)
+ return true;
+
+ report(DiagnosticsEngine::Warning,
+ "calling const method on o3tl::cow_wrapper impl class via non-const pointer, rather use "
+ "std::as_const to prevent triggering an unnecessary copy",
+ compat::getBeginLoc(memberCallExpr))
+ << memberCallExpr->getSourceRange();
+ return true;
+}
+
+loplugin::Plugin::Registration<Cow_Wrapper> cow_wrapper("cow_wrapper", true);
+
+} // namespace
+
+#endif // LO_CLANG_SHARED_PLUGINS
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/test/cow_wrapper.cxx b/compilerplugins/clang/test/cow_wrapper.cxx
new file mode 100644
index 000000000000..705a4052ef66
--- /dev/null
+++ b/compilerplugins/clang/test/cow_wrapper.cxx
@@ -0,0 +1,33 @@
+/* -*- 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"
+
+struct ImplBitmapPalette
+{
+ void foo() const;
+};
+
+struct BitmapPalette
+{
+ void foo1()
+ {
+ // expected-error@+1 {{calling const method on o3tl::cow_wrapper impl class via non-const pointer, rather use std::as_const to prevent triggering an unnecessary copy [loplugin:cow_wrapper]}}
+ mpImpl->foo();
+ }
+ void foo2() const
+ {
+ // no error expected
+ mpImpl->foo();
+ }
+ o3tl::cow_wrapper<ImplBitmapPalette> mpImpl;
+};
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */