From c6db02a2afe4263e060fe6166db8e5426dd2ecd1 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Wed, 16 Feb 2022 18:15:46 +0100 Subject: [PATCH] Extend loplugin:stringview to OUStringBuffer::copy (Somewhat oddly, there is no OStringBuffer::copy counterpart.) This required some modification to StringView::VisitCXXConstructExpr to avoid > In file included from odk/qa/checkapi/checkapi.cxx:29: > In file included from workdir/CustomTarget/odk/allheaders/allheaders.hxx:351: > In file included from instdir/sdk/include/rtl/math.hxx:31: > instdir/sdk/include/rtl/ustrbuf.hxx:1687:16: error: rather than copy, pass with a view using subView() [loplugin:stringview] > return copy( beginIndex, getLength() - beginIndex ); > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ while building CppunitTest_odk_checkapi as external C++03 code, where the returned value is wrapped in a CXXConstructExpr. And testing for that case required a new CompilerTest_compilerplugins_clang-c++03 that uses gb_CXX03FLAGS and needs to not set LIBO_INTERNAL_ONLY (via gb_CompilerTest_set_external_code), as compiling as C++03 would otherwise generate lots of errors like unknown char16_t at include/sal/types.h:118. (There was a choice whether to name the new test "-c++03" or "-external", but the issue it tests is caused more by the code being compiled with C++03 than by this being external code, see above.) Change-Id: I873a9c5a70d3ea949cf13a169d46920b71282712 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130036 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- compilerplugins/clang/stringview.cxx | 10 +++++++-- .../clang/test/stringview-c++03.cxx | 21 +++++++++++++++++++ compilerplugins/clang/test/stringview.cxx | 8 +++++++ linguistic/source/hyphdsp.cxx | 2 +- sc/source/core/tool/compiler.cxx | 2 +- ...ompilerTest_compilerplugins_clang-c++03.mk | 20 ++++++++++++++++++ solenv/Module_solenv.mk | 1 + solenv/gbuild/CompilerTest.mk | 2 ++ svl/source/numbers/zforlist.cxx | 4 ++-- 9 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 compilerplugins/clang/test/stringview-c++03.cxx create mode 100644 solenv/CompilerTest_compilerplugins_clang-c++03.mk diff --git a/compilerplugins/clang/stringview.cxx b/compilerplugins/clang/stringview.cxx index 4c31c348b357..c81b04ebace5 100644 --- a/compilerplugins/clang/stringview.cxx +++ b/compilerplugins/clang/stringview.cxx @@ -134,7 +134,8 @@ void StringView::handleSubExprThatCouldBeView(Expr const* subExpr) auto const e = e0->IgnoreParens(); auto const tc = loplugin::TypeCheck(e->getType()); if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace() - || tc.Class("OUString").Namespace("rtl").GlobalNamespace())) + || tc.Class("OUString").Namespace("rtl").GlobalNamespace() + || tc.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace())) { return; } @@ -272,7 +273,8 @@ void StringView::handleCXXMemberCallExpr(CXXMemberCallExpr const* expr) if (auto const dc2 = dc1.Function("copy")) { if (dc2.Class("OString").Namespace("rtl").GlobalNamespace() - || dc2.Class("OUString").Namespace("rtl").GlobalNamespace()) + || dc2.Class("OUString").Namespace("rtl").GlobalNamespace() + || dc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()) { report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()", expr->getExprLoc()) @@ -343,6 +345,10 @@ bool StringView::VisitCXXConstructExpr(CXXConstructExpr const* expr) { return true; } + if (!compat::CPlusPlus17(compiler.getLangOpts()) && expr->isElidable()) // external C++03 code + { + return true; + } if (expr->getNumArgs() > 0) handleSubExprThatCouldBeView(expr->getArg(0)); return true; diff --git a/compilerplugins/clang/test/stringview-c++03.cxx b/compilerplugins/clang/test/stringview-c++03.cxx new file mode 100644 index 000000000000..1824ac9d02ce --- /dev/null +++ b/compilerplugins/clang/test/stringview-c++03.cxx @@ -0,0 +1,21 @@ +/* -*- 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 + +#include + +// expected-no-diagnostics + +rtl::OUStringBuffer nowarn(rtl::OUStringBuffer const& s, sal_Int32 n) +{ + return s.copy(n, s.getLength() - n); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/test/stringview.cxx b/compilerplugins/clang/test/stringview.cxx index 16e294a6eeb7..a679ec59ea45 100644 --- a/compilerplugins/clang/test/stringview.cxx +++ b/compilerplugins/clang/test/stringview.cxx @@ -61,6 +61,14 @@ void f1(OString s1) } void f1(OUStringBuffer 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)); // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}} call_view(s1.toString()); // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}} diff --git a/linguistic/source/hyphdsp.cxx b/linguistic/source/hyphdsp.cxx index 17c0bff0e872..6113960d4c9e 100644 --- a/linguistic/source/hyphdsp.cxx +++ b/linguistic/source/hyphdsp.cxx @@ -158,7 +158,7 @@ Reference HyphenatorDispatcher::buildHyphWord( OUStringBuffer aTmp2 ( aTmp.copy(0, std::max (nHyphenationPos + 1 - split, 0) ) ); aTmp2.append( aText.subView( nOrigHyphPos + 1, endhyphpat - nOrigHyphPos - 1) ); nHyphenPos = aTmp2.getLength(); - aTmp2.append( aTmp.copy( nHyphenationPos + 1 ) ); + aTmp2.append( aTmp.subView( nHyphenationPos + 1 ) ); //! take care of #i22591# if (rOrigWord[ rOrigWord.getLength() - 1 ] == '.') aTmp2.append( '.' ); diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx index 6b8c4f8b2fad..61fa95ca5f87 100644 --- a/sc/source/core/tool/compiler.cxx +++ b/sc/source/core/tool/compiler.cxx @@ -1574,7 +1574,7 @@ struct ConventionXL_OOX : public ConventionXL_A1 { rBuffer.append('\''); ConventionXL_OOX::makeExternalDocStr( rBuffer, nFileId); - rBuffer.append( aBuf.copy(1)); + rBuffer.append( aBuf.subView(1)); } else { diff --git a/solenv/CompilerTest_compilerplugins_clang-c++03.mk b/solenv/CompilerTest_compilerplugins_clang-c++03.mk new file mode 100644 index 000000000000..8abe032203a0 --- /dev/null +++ b/solenv/CompilerTest_compilerplugins_clang-c++03.mk @@ -0,0 +1,20 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; 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/. +# + +$(eval $(call gb_CompilerTest_CompilerTest,compilerplugins_clang-c++03)) + +$(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang-c++03, \ + compilerplugins/clang/test/stringview-c++03 \ +)) + +$(eval $(call gb_CompilerTest_add_cxxflags,compilerplugins_clang-c++03,$(gb_CXX03FLAGS))) + +$(eval $(call gb_CompilerTest_set_external_code,compilerplugins_clang-c++03)) + +# vim: set noet sw=4 ts=4: diff --git a/solenv/Module_solenv.mk b/solenv/Module_solenv.mk index a26775f71083..6b9f7d0f1a8c 100644 --- a/solenv/Module_solenv.mk +++ b/solenv/Module_solenv.mk @@ -45,6 +45,7 @@ ifneq ($(COMPILER_PLUGINS),) ifeq ($(COMPILER_EXTERNAL_TOOL)$(COMPILER_PLUGIN_TOOL),) $(eval $(call gb_Module_add_check_targets,solenv, \ CompilerTest_compilerplugins_clang \ + CompilerTest_compilerplugins_clang-c++03 \ )) endif endif diff --git a/solenv/gbuild/CompilerTest.mk b/solenv/gbuild/CompilerTest.mk index ad6c55411f19..fb5150f2cdf0 100644 --- a/solenv/gbuild/CompilerTest.mk +++ b/solenv/gbuild/CompilerTest.mk @@ -45,6 +45,8 @@ gb_CompilerTest_add_objcxxobject = $(call gb_CompilerTest__forward_to_Linktarget gb_CompilerTest_add_objcxxobjects = $(call gb_CompilerTest__forward_to_Linktarget,$(0),$(1),$(2),$(3)) gb_CompilerTest_add_cxxclrobject = $(call gb_CompilerTest__forward_to_Linktarget,$(0),$(1),$(2),$(3)) gb_CompilerTest_add_cxxclrobjects = $(call gb_CompilerTest__forward_to_Linktarget,$(0),$(1),$(2),$(3)) +gb_CompilerTest_add_cxxflags = $(call gb_CompilerTest__forward_to_Linktarget,$(0),$(1),$(2),$(3)) +gb_CompilerTest_set_external_code = $(call gb_CompilerTest__forward_to_Linktarget,$(0),$(1),$(2),$(3)) gb_CompilerTest_use_externals = $(call gb_CompilerTest__forward_to_Linktarget,$(0),$(1),$(2),$(3)) gb_CompilerTest_use_udk_api = $(call gb_CompilerTest__forward_to_Linktarget,$(0),$(1),$(2),$(3)) diff --git a/svl/source/numbers/zforlist.cxx b/svl/source/numbers/zforlist.cxx index 98f607e6f07a..8606a4a24226 100644 --- a/svl/source/numbers/zforlist.cxx +++ b/svl/source/numbers/zforlist.cxx @@ -3231,9 +3231,9 @@ OUString SvNumberFormatter::GenerateFormat(sal_uInt32 nIndex, { sal_Int32 nIndexSep = ImpPosToken( sOldFormatString, ';', nIndexE ); if (nIndexSep > nIndexE) - sString.append( sOldFormatString.copy(nIndexE, nIndexSep - nIndexE) ); + sString.append( sOldFormatString.subView(nIndexE, nIndexSep - nIndexE) ); else - sString.append( sOldFormatString.copy(nIndexE) ); + sString.append( sOldFormatString.subView(nIndexE) ); } } else if (eType == SvNumFormatType::CURRENCY)