summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel <noelgrandin@gmail.com>2020-11-06 20:01:50 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2020-11-11 11:58:37 +0100
commit93c64a61f2c84e684050294a1391cd32425b7837 (patch)
tree00aad2cb8f3ee29ba4ac99e159e26fb8d71d2f33 /compilerplugins
parent1fde62018c8d3344a3408c7b6317120aefc778fb (diff)
loplugin:stringview
Add new methods "subView" to O(U)String to return substring views of the underlying data. Add a clang plugin to warn when replacing existing calls to copy() would be better to use subView(). Change-Id: I03a5732431ce60808946f2ce2c923b22845689ca Reviewed-on: https://gerrit.libreoffice.org/c/core/+/105420 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/stringview.cxx188
-rw-r--r--compilerplugins/clang/test/stringview.cxx96
2 files changed, 284 insertions, 0 deletions
diff --git a/compilerplugins/clang/stringview.cxx b/compilerplugins/clang/stringview.cxx
new file mode 100644
index 000000000000..530cf43d95a0
--- /dev/null
+++ b/compilerplugins/clang/stringview.cxx
@@ -0,0 +1,188 @@
+/* -*- 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/.
+ */
+#ifndef LO_CLANG_SHARED_PLUGINS
+
+#include <cassert>
+#include <string>
+#include <iostream>
+#include <unordered_map>
+#include <unordered_set>
+
+#include "plugin.hxx"
+#include "check.hxx"
+#include "clang/AST/CXXInheritance.h"
+#include "clang/AST/StmtVisitor.h"
+
+/**
+ Look for places where we are making a substring copy of an OUString, and then passing it to a
+ function that takes a u16string_view, in which case it is more efficient to pass a view
+ of the OUString, rather than making a copy.
+
+ TODO currently does not check if there is some other visible overload of the callee, that can take
+ a string_view.
+ TODO handle OUStringBuffer/OStringBuffer similarly
+*/
+
+namespace
+{
+class StringView : public loplugin::FilteringPlugin<StringView>
+{
+public:
+ explicit StringView(loplugin::InstantiationData const& data)
+ : FilteringPlugin(data)
+ {
+ }
+
+ bool preRun() override { return true; }
+
+ virtual void run() override
+ {
+ if (!preRun())
+ return;
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+ bool VisitCallExpr(CallExpr const*);
+ bool VisitCXXConstructExpr(CXXConstructExpr const*);
+ bool VisitFunctionDecl(FunctionDecl const*);
+ bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const*);
+};
+
+bool StringView::VisitCallExpr(CallExpr const* callExpr)
+{
+ if (ignoreLocation(callExpr))
+ return true;
+
+ const FunctionDecl* functionDecl;
+ if (isa<CXXMemberCallExpr>(callExpr))
+ functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
+ else
+ functionDecl = callExpr->getDirectCallee();
+ if (!functionDecl)
+ return true;
+
+ unsigned len = std::min(callExpr->getNumArgs(), functionDecl->getNumParams());
+ for (unsigned i = 0; i < len; ++i)
+ {
+ const Expr* argExpr = callExpr->getArg(i);
+ auto paramDecl = functionDecl->getParamDecl(i);
+ auto paramRecordDecl = dyn_cast_or_null<RecordDecl>(
+ paramDecl->getType()->getUnqualifiedDesugaredType()->getAsTagDecl());
+ if (!paramRecordDecl || !paramRecordDecl->getIdentifier()
+ || paramRecordDecl->getName() != "basic_string_view")
+ continue;
+ // unwrap the operator that converts to std::u16string_view
+ auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(argExpr->IgnoreImpCasts());
+ if (!memberCallExpr)
+ continue;
+ // unwrap the call to copy()
+ auto memberCallExpr2 = dyn_cast<CXXMemberCallExpr>(
+ compat::IgnoreImplicit(memberCallExpr->getImplicitObjectArgument()));
+ if (!memberCallExpr2)
+ continue;
+ auto methodDecl = memberCallExpr2->getMethodDecl();
+ if (!methodDecl->getIdentifier() || methodDecl->getName() != "copy")
+ continue;
+ report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()",
+ compat::getBeginLoc(argExpr))
+ << argExpr->getSourceRange();
+ }
+
+ return true;
+}
+
+bool StringView::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* cxxOperatorCallExpr)
+{
+ if (ignoreLocation(cxxOperatorCallExpr))
+ return true;
+
+ auto check = [&](const Expr* expr) -> void {
+ auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(compat::IgnoreImplicit(expr));
+ if (!memberCallExpr)
+ return;
+ auto methodDecl = memberCallExpr->getMethodDecl();
+ if (!methodDecl->getIdentifier() || methodDecl->getName() != "copy")
+ return;
+ report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()",
+ compat::getBeginLoc(expr))
+ << expr->getSourceRange();
+ };
+ auto op = cxxOperatorCallExpr->getOperator();
+ if (op == OO_Plus && cxxOperatorCallExpr->getNumArgs() == 2)
+ {
+ check(cxxOperatorCallExpr->getArg(0));
+ check(cxxOperatorCallExpr->getArg(1));
+ }
+ if (compat::isComparisonOp(cxxOperatorCallExpr))
+ {
+ check(cxxOperatorCallExpr->getArg(0));
+ check(cxxOperatorCallExpr->getArg(1));
+ }
+ else if (op == OO_PlusEqual)
+ check(cxxOperatorCallExpr->getArg(1));
+ else if (op == OO_Subscript)
+ check(cxxOperatorCallExpr->getArg(0));
+ return true;
+}
+
+bool StringView::VisitFunctionDecl(FunctionDecl const* functionDecl)
+{
+ if (ignoreLocation(functionDecl))
+ return true;
+ // debugging
+ // if (functionDecl->getIdentifier() && functionDecl->getName() == "f1")
+ // functionDecl->dump();
+ return true;
+}
+
+bool StringView::VisitCXXConstructExpr(CXXConstructExpr const* constructExpr)
+{
+ if (ignoreLocation(constructExpr))
+ return true;
+
+ const CXXConstructorDecl* constructorDecl = constructExpr->getConstructor();
+ if (!constructorDecl)
+ return true;
+
+ unsigned len = std::min(constructExpr->getNumArgs(), constructorDecl->getNumParams());
+ for (unsigned i = 0; i < len; ++i)
+ {
+ const Expr* argExpr = constructExpr->getArg(i);
+ auto paramDecl = constructorDecl->getParamDecl(i);
+ auto paramRecordDecl = dyn_cast_or_null<RecordDecl>(
+ paramDecl->getType()->getUnqualifiedDesugaredType()->getAsTagDecl());
+ if (!paramRecordDecl || !paramRecordDecl->getIdentifier()
+ || paramRecordDecl->getName() != "basic_string_view")
+ continue;
+ // unwrap the operator that converts to std::u16string_view
+ auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(argExpr->IgnoreImpCasts());
+ if (!memberCallExpr)
+ continue;
+ // unwrap the call to copy()
+ auto memberCallExpr2 = dyn_cast<CXXMemberCallExpr>(
+ compat::IgnoreImplicit(memberCallExpr->getImplicitObjectArgument()));
+ if (!memberCallExpr2)
+ continue;
+ auto methodDecl = memberCallExpr2->getMethodDecl();
+ if (!methodDecl->getIdentifier() || methodDecl->getName() != "copy")
+ continue;
+ report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()",
+ compat::getBeginLoc(argExpr))
+ << argExpr->getSourceRange();
+ }
+
+ return true;
+}
+
+loplugin::Plugin::Registration<StringView> stringview("stringview");
+}
+
+#endif // LO_CLANG_SHARED_PLUGINS
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/test/stringview.cxx b/compilerplugins/clang/test/stringview.cxx
new file mode 100644
index 000000000000..96d4927e533a
--- /dev/null
+++ b/compilerplugins/clang/test/stringview.cxx
@@ -0,0 +1,96 @@
+/* -*- 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 <rtl/strbuf.hxx>
+#include <rtl/string.hxx>
+#include <rtl/ustrbuf.hxx>
+#include <rtl/ustring.hxx>
+
+void call_view(std::u16string_view) {}
+void call_view(std::string_view) {}
+struct ConstructWithView
+{
+ ConstructWithView(std::u16string_view) {}
+ ConstructWithView(std::string_view) {}
+};
+
+namespace test1
+{
+void f1(std::u16string_view s1)
+{
+ // no warning expected
+ call_view(s1);
+}
+void f1(std::string_view s1)
+{
+ // no warning expected
+ call_view(s1);
+}
+void f1(OUString s1)
+{
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ call_view(s1.copy(1, 2));
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ call_view(s1.copy(1));
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ ConstructWithView(s1.copy(1, 2));
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ ConstructWithView(s1.copy(1));
+}
+void f1(OString s1)
+{
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ call_view(s1.copy(1, 2));
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ call_view(s1.copy(1));
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ ConstructWithView(s1.copy(1, 2));
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ ConstructWithView(s1.copy(1));
+}
+}
+
+namespace test2
+{
+void f3(OUString s1)
+{
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ OUString s2 = s1.copy(1, 2) + "xxx";
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ s2 = s1.copy(1) + "xxx";
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ s2 = "xxx" + s1.copy(1);
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ s2 += s1.copy(1);
+}
+void f3(OString s1)
+{
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ OString s2 = s1.copy(1, 2) + "xxx";
+ (void)s2;
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ OString s3 = s1.copy(1) + "xxx";
+ (void)s3;
+}
+}
+
+namespace test3
+{
+void f4(OUString s1, OUString s2)
+{
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ sal_Unicode x = s2.copy(1, 2)[12];
+ (void)x;
+ // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
+ if (s2.copy(1, 2) < s1)
+ ;
+}
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */