summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/test
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2020-09-08 09:48:17 +0200
committerStephan Bergmann <sbergman@redhat.com>2020-09-16 23:02:09 +0200
commite6dfaf9f44f9939abc338c83b3024108431d0f69 (patch)
tree2e220510dc589fa4ce9b205696b9d0b58d38b60a /compilerplugins/clang/test
parenteb55bc5eae82873492cfd7577fb553b1c5abff6b (diff)
Turn OUStringLiteral into a consteval'ed, static-refcound rtl_uString
...from which an OUString can cheaply be instantiated. This is the OUString equivalent of 4b9e440c51be3e40326bc90c33ae69885bfb51e4 "Turn OStringLiteral into a consteval'ed, static-refcound rtl_String". Most remarks about that commit apply here too (this commit is just substantially bigger and a bit more complicated because there were so much more uses of OUStringLiteral than of OStringLiteral): The one downside is that OUStringLiteral now needs to be a template abstracting over the string length. But any uses for which that is a problem (e.g., as the element type of a container that would no longer be homogeneous, or in the signature of a function that shall not be turned into a template for one reason or another) can be replaced with std::u16string_view, without loss of efficiency compared to the original OUStringLiteral, and without loss of expressivity. The new OUStringLiteral ctor code would probably not be very efficient if it were ever executed at runtime, but it is intended to be only executed at compile time. Where available, C++20 "consteval" is used to statically ensure that. The intended use of the new OUStringLiteral is in all cases where an object that shall itself not be an OUString (e.g., because it shall be a global static variable for which the OUString ctor/dtor would be detrimental at library load/unload) must be converted to an OUString instance in at least one place. Other string literal abstractions could use std::u16string_view (or just plain char16_t const[N]), but interestingly OUStringLiteral might be more efficient than constexpr std::u16string_view even for such cases, as it should not need any relocations at library load time. For now, no existing uses of OUStringLiteral have been changed to some other abstraction (unless technically necessary as discussed above), and no additional places that would benefit from OUStringLiteral have been changed to use it. Global constexpr OUStringLiteral variables defined in an included file would be somewhat suboptimal, as each translation unit that uses them would create its own, unshared instance. The envisioned solution is to turn them into static data members of some class (and there may be a loplugin coming to find and fix affected places). Another approach that has been taken here in a few cases where such variables were only used in one .cxx anyway is to move their definitions from the .hxx into that one .cxx (in turn causing some files to become empty and get removed completely)---which also silenced some GCC -Werror=unused-variable if a variable from a .hxx was not used in some .cxx including it. To keep individual commits reasonably manageable, some consumers of OUStringLiteral in rtl/ustrbuf.hxx and rtl/ustring.hxx are left in a somewhat odd state for now, where they don't take advantage of OUStringLiteral's equivalence to rtl_uString, but just keep extracting its contents and copy it elsewhere. In follow-up commits, those consumers should be changed appropriately, making them treat OUStringLiteral like an rtl_uString or dropping the OUStringLiteral overload in favor of an existing (and cheap to use now) OUString overload, etc. In a similar vein, comparison operators between OUString and std::u16string_view have been added to the existing plethora of comparison operator overloads. It would be nice to eventually consolidate them, esp. with the overloads taking OUStringLiteral and/or char16_t const[N] string literals, but that appears tricky to get right without introducing new ambiguities. Also, a handful of places across the code base use comparisons between OUString and OUStringNumber, which are now ambiguous (converting the OUStringNumber to either OUString or std::u16string_view). For simplicity, those few places have manually been fixed for now by adding explicit conversion to std::u16string_view. Also some compilerplugins code needed to be adapted, and some of the compilerplugins/test cases have become irrelevant (and have been removed), as the tested code would no longer compile in the first place. sal/qa/rtl/strings/test_oustring_concat.cxx documents a workaround for GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96878> "Failed class template argument deduction in unevaluated, parenthesized context". That place, as well as uses of OUStringLiteral in extensions/source/abpilot/fieldmappingimpl.cxx and i18npool/source/localedata/localedata.cxx, which have been replaced with OUString::Concat (and which is arguably a better choice, anyway), also caused failures with at least Clang 5.0.2 (but would not have caused failures with at least recent Clang 12 trunk, so appear to be bugs in Clang that have meanwhile been fixed). Change-Id: I34174462a28f2000cfeb2d219ffd533a767920b8 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102222 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins/clang/test')
-rw-r--r--compilerplugins/clang/test/conditionalstring.cxx11
-rw-r--r--compilerplugins/clang/test/elidestringvar.cxx3
-rw-r--r--compilerplugins/clang/test/staticvar.cxx6
-rw-r--r--compilerplugins/clang/test/stringadd.cxx2
-rw-r--r--compilerplugins/clang/test/stringconcatliterals.cxx5
5 files changed, 12 insertions, 15 deletions
diff --git a/compilerplugins/clang/test/conditionalstring.cxx b/compilerplugins/clang/test/conditionalstring.cxx
index f38bc1d2436f..c044ee324b50 100644
--- a/compilerplugins/clang/test/conditionalstring.cxx
+++ b/compilerplugins/clang/test/conditionalstring.cxx
@@ -13,17 +13,14 @@
void f(OUString s, bool b)
{
- // expected-error@+2 {{replace 2nd operand of conditional expression with `rtl::OUStringLiteral` [loplugin:conditionalstring]}}
+ // expected-error@+2 {{replace 2nd operand of conditional expression with `std::u16string_view` [loplugin:conditionalstring]}}
// expected-note@+1 {{conditional expression is here [loplugin:conditionalstring]}}
s += (b ? OUString("a") : throw 0);
- // expected-error@+2 {{replace 2nd operand of conditional expression with `rtl::OUStringLiteral` [loplugin:conditionalstring]}}
- // expected-note@+1 {{conditional expression is here [loplugin:conditionalstring]}}
- s += (b ? OUString("a") : OUStringLiteral(u"b"));
- // expected-error@+1 {{replace both 2nd and 3rd operands of conditional expression with `rtl::OUStringLiteral` [loplugin:conditionalstring]}}
+ // expected-error@+1 {{replace both 2nd and 3rd operands of conditional expression with `std::u16string_view` [loplugin:conditionalstring]}}
b = (b ? ("x") : (OUString(("y")))) == s;
- // expected-error@+1 {{replace both 2nd and 3rd operands of conditional expression with `rtl::OUStringLiteral` [loplugin:conditionalstring]}}
+ // expected-error@+1 {{replace both 2nd and 3rd operands of conditional expression with `std::u16string_view` [loplugin:conditionalstring]}}
b = operator==(s, b ? OUString("x") : OUString("y"));
- // expected-error@+1 {{replace both 2nd and 3rd operands of conditional expression with `rtl::OUStringLiteral` [loplugin:conditionalstring]}}
+ // expected-error@+1 {{replace both 2nd and 3rd operands of conditional expression with `std::u16string_view` [loplugin:conditionalstring]}}
s.operator+=(b ? OUString("x") : OUString("y"));
}
diff --git a/compilerplugins/clang/test/elidestringvar.cxx b/compilerplugins/clang/test/elidestringvar.cxx
index bc0de05c999e..1835c183bb39 100644
--- a/compilerplugins/clang/test/elidestringvar.cxx
+++ b/compilerplugins/clang/test/elidestringvar.cxx
@@ -18,8 +18,9 @@ OUString f(sal_Unicode c, int n)
OUString s2('a');
// expected-note@+1 {{literal OUString variable defined here [loplugin:elidestringvar]}}
OUString s3(u'a');
+ static constexpr OUStringLiteral s4lit(u"a");
// expected-note@+1 {{literal OUString variable defined here [loplugin:elidestringvar]}}
- OUString s4 = OUStringLiteral(u"a");
+ OUString s4 = s4lit;
switch (n)
{
case 1:
diff --git a/compilerplugins/clang/test/staticvar.cxx b/compilerplugins/clang/test/staticvar.cxx
index f527df6a521a..5c0a86fc420f 100644
--- a/compilerplugins/clang/test/staticvar.cxx
+++ b/compilerplugins/clang/test/staticvar.cxx
@@ -11,6 +11,8 @@
#include <sal/config.h>
#include <rtl/ustring.hxx>
+#include <string_view>
+
struct S1
{
int x, y;
@@ -52,13 +54,13 @@ S2 const& f3()
// no warning expected
struct S4
{
- OUStringLiteral const cName;
+ std::u16string_view const cName;
bool const bCanBeVisible;
};
S4 const& f4()
{
static const S4 s1[] = {
- { OUStringLiteral(u"/DocColor"), false },
+ { std::u16string_view(u"/DocColor"), false },
};
return s1[0];
}
diff --git a/compilerplugins/clang/test/stringadd.cxx b/compilerplugins/clang/test/stringadd.cxx
index 0879805d3449..e17b207fcb64 100644
--- a/compilerplugins/clang/test/stringadd.cxx
+++ b/compilerplugins/clang/test/stringadd.cxx
@@ -18,7 +18,7 @@
namespace test1
{
static const char XXX1[] = "xxx";
-static const char16_t XXX1u[] = u"xxx";
+static constexpr char16_t XXX1u[] = u"xxx";
static const char XXX2[] = "xxx";
void f1(OUString s1, int i, OString o)
{
diff --git a/compilerplugins/clang/test/stringconcatliterals.cxx b/compilerplugins/clang/test/stringconcatliterals.cxx
index 348440f0ec4c..d19ebb97c1c3 100644
--- a/compilerplugins/clang/test/stringconcatliterals.cxx
+++ b/compilerplugins/clang/test/stringconcatliterals.cxx
@@ -20,7 +20,7 @@
void f(std::ostream& s1)
{
static constexpr char foo[] = "foo";
- static char16_t const foou[] = u"foo";
+ static constexpr char16_t foou[] = u"foo";
s1 << "foo"
<< "foo";
// expected-error@-1 {{replace '<<' between string literals with juxtaposition}}
@@ -37,9 +37,6 @@ void f(std::ostream& s1)
s1 << "foo" << OUString(FOO);
// expected-error@-1 {{replace '<<' between string literals with juxtaposition}}
s1 << "foo" << OUString(foo);
- s1 << "foo" << OUStringLiteral(u"foo"); //TODO: warn too, OUStringLiteral wrapped in OUString
- s1 << "foo" << OUStringLiteral(FOOu); //TODO: warn too, OUStringLiteral wrapped in OUString
- s1 << "foo" << OUStringLiteral(foou);
OString s2;
s2 = "foo" + OString("foo");
// expected-error@-1 {{replace '+' between string literals with juxtaposition}}