summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2023-04-07 10:02:18 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2023-04-09 20:51:44 +0200
commiteaf071397a1ff30536616f2ed76051f77fd38ed6 (patch)
treece55cdc75a0826c54b6b60458a028251f11fcb78 /compilerplugins
parent1e79befa61a08de7a1ddaccb6c435dbb8015c063 (diff)
new loplugin:unnecessarygetstr
which prevents constructing unnecessary temporaries via getStr() Change-Id: I9ca70893a10e954b5ee0e6ad6098660ee24c2bef Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150170 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/test/unnecessarygetstr.cxx55
-rw-r--r--compilerplugins/clang/unnecessarygetstr.cxx122
2 files changed, 177 insertions, 0 deletions
diff --git a/compilerplugins/clang/test/unnecessarygetstr.cxx b/compilerplugins/clang/test/unnecessarygetstr.cxx
new file mode 100644
index 000000000000..68175872a4ea
--- /dev/null
+++ b/compilerplugins/clang/test/unnecessarygetstr.cxx
@@ -0,0 +1,55 @@
+/* -*- 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>
+
+#include <ostream>
+#include <string_view>
+
+#include <rtl/strbuf.hxx>
+#include <rtl/string.hxx>
+#include <rtl/ustrbuf.hxx>
+#include <rtl/ustring.hxx>
+#include <sal/log.hxx>
+
+void f1(bool, const OString& s);
+struct Foo
+{
+ void f1(bool, const OString& s);
+};
+void test1(Foo& foo)
+{
+ OString s;
+ // expected-error@+1 {{unnecessary call to 'getStr' when passing to OString arg [loplugin:unnecessarygetstr]}}
+ f1(true, s.getStr());
+ // expected-error@+1 {{unnecessary call to 'getStr' when passing to OString arg [loplugin:unnecessarygetstr]}}
+ foo.f1(true, s.getStr());
+
+ // avoid false +
+ OString aVal = "xx";
+ OUString aCompText
+ = "xx" + OUString(aVal.getStr(), aVal.getLength(), RTL_TEXTENCODING_ASCII_US);
+ (void)aCompText;
+}
+
+// call to param that takes string_view
+void f2(bool, std::string_view);
+struct Foo2
+{
+ void f2(bool, std::string_view);
+};
+void test2(Foo2& foo)
+{
+ OString s;
+ // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view arg [loplugin:unnecessarygetstr]}}
+ f2(true, s.getStr());
+ // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view arg [loplugin:unnecessarygetstr]}}
+ foo.f2(true, s.getStr());
+}
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/compilerplugins/clang/unnecessarygetstr.cxx b/compilerplugins/clang/unnecessarygetstr.cxx
new file mode 100644
index 000000000000..7ef5de6932d1
--- /dev/null
+++ b/compilerplugins/clang/unnecessarygetstr.cxx
@@ -0,0 +1,122 @@
+/* -*- 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/.
+ */
+
+#ifndef LO_CLANG_SHARED_PLUGINS
+
+#include <cassert>
+#include <stack>
+
+#include "check.hxx"
+#include "plugin.hxx"
+
+// Find matches of
+//
+// foo(s.getStr())
+//
+// (for the rtl string classes) that can be written as just
+//
+// foo(s)
+//
+// and warn about them, which prevents constructing unnecessary temporaries.
+
+namespace
+{
+class UnnecessaryGetStr final : public loplugin::FilteringPlugin<UnnecessaryGetStr>
+{
+public:
+ explicit UnnecessaryGetStr(loplugin::InstantiationData const& data)
+ : FilteringPlugin(data)
+ {
+ }
+
+ bool VisitCallExpr(CallExpr* callExpr)
+ {
+ if (ignoreLocation(callExpr))
+ return true;
+ const FunctionDecl* func = callExpr->getDirectCallee();
+ if (!func)
+ return true;
+ unsigned const n = std::min(func->getNumParams(), callExpr->getNumArgs());
+ for (unsigned i = 0; i != n; ++i)
+ {
+ auto arg = callExpr->getArg(i);
+ if (auto matTemp = dyn_cast<MaterializeTemporaryExpr>(arg))
+ {
+ auto cxxConstruct = dyn_cast<CXXConstructExpr>(matTemp->IgnoreImplicit());
+ if (!cxxConstruct || cxxConstruct->getNumArgs() < 1
+ || cxxConstruct->getNumArgs() > 2)
+ continue;
+ auto const tc1 = loplugin::TypeCheck(cxxConstruct->getConstructor()->getParent());
+ if (!(tc1.Class("OString").Namespace("rtl").GlobalNamespace()
+ || tc1.Class("OUString").Namespace("rtl").GlobalNamespace()))
+ continue;
+ auto e = dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreImplicit());
+ if (!e)
+ continue;
+ auto const t = e->getObjectType();
+ auto const tc2 = loplugin::TypeCheck(t);
+ if (!(tc2.Class("OString").Namespace("rtl").GlobalNamespace()
+ || tc2.Class("OUString").Namespace("rtl").GlobalNamespace()
+ || tc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace()
+ || tc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()))
+ continue;
+ if (!loplugin::DeclCheck(e->getMethodDecl()).Function("getStr"))
+ continue;
+ report(DiagnosticsEngine::Warning,
+ "unnecessary call to 'getStr' when passing to OString arg", e->getExprLoc())
+ << e->getSourceRange();
+ }
+ else if (auto impCast = dyn_cast<ImplicitCastExpr>(arg))
+ {
+ auto cxxConstruct = dyn_cast<CXXConstructExpr>(impCast->getSubExpr());
+ if (!cxxConstruct || cxxConstruct->getNumArgs() < 1
+ || cxxConstruct->getNumArgs() > 2)
+ continue;
+ auto const tc1 = loplugin::TypeCheck(cxxConstruct->getConstructor()->getParent());
+ if (!(tc1.ClassOrStruct("basic_string_view").StdNamespace()))
+ continue;
+ auto e = dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreImplicit());
+ if (!e)
+ continue;
+ auto const t = e->getObjectType();
+ auto const tc2 = loplugin::TypeCheck(t);
+ if (!(tc2.Class("OString").Namespace("rtl").GlobalNamespace()
+ || tc2.Class("OUString").Namespace("rtl").GlobalNamespace()
+ || tc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace()
+ || tc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()))
+ continue;
+ if (!loplugin::DeclCheck(e->getMethodDecl()).Function("getStr"))
+ continue;
+ report(DiagnosticsEngine::Warning,
+ "unnecessary call to 'getStr' when passing to string_view arg",
+ e->getExprLoc())
+ << e->getSourceRange();
+ }
+ }
+ return true;
+ }
+
+ bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
+
+private:
+ void run() override
+ {
+ if (preRun())
+ {
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+ }
+};
+
+loplugin::Plugin::Registration<UnnecessaryGetStr> unnecessarygetstr("unnecessarygetstr");
+}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */