Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / stringliteralvar.cxx
blobd0fdcedb06688f26bdf943c7f1bd76f954b2c0d3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <iterator>
13 #include <vector>
15 #include <rtl/ustring.hxx>
17 // expected-error-re@+1 {{change type of variable 'literal1' from constant character array ('const char{{ ?}}[4]') to OStringLiteral [loplugin:stringliteralvar]}}
18 char const literal1[] = "foo";
19 OString f1()
21 // expected-note-re@+1 {{first passed into a '{{(rtl::)?}}OString' constructor here [loplugin:stringliteralvar]}}
22 return literal1;
25 void f(OUString const&);
26 void f2()
28 // expected-error-re@+1 {{change type of variable 'literal' from constant character array ('const char{{ ?}}[4]') to OUStringLiteral, and make it static [loplugin:stringliteralvar]}}
29 char const literal[] = "foo";
30 // expected-note-re@+1 {{first passed into a '{{(rtl::)?}}OUString' constructor here [loplugin:stringliteralvar]}}
31 f(literal);
34 struct S3
36 // expected-error-re@+1 {{change type of variable 'literal' from constant character array ('const char16_t{{ ?}}[4]') to OUStringLiteral [loplugin:stringliteralvar]}}
37 static constexpr char16_t literal[] = u"foo";
39 void f3()
41 // expected-note-re@+1 {{first passed into a '{{(rtl::)?}}OUString' constructor here [loplugin:stringliteralvar]}}
42 f(S3::literal);
45 std::vector<OUString> f4()
47 // expected-error-re@+1 {{change type of variable 'literal' from constant character array ('const char16_t{{ ?}}[4]') to OUStringLiteral [loplugin:stringliteralvar]}}
48 static constexpr char16_t literal[] = u"foo";
49 // expected-note@+1 {{first passed into a 'rtl::OUString' constructor here [loplugin:stringliteralvar]}}
50 return { literal };
53 void f5()
55 // expected-error-re@+1 {{variable 'literal' of type 'const {{(rtl::)?}}OUStringLiteral<4>'{{( \(aka 'const rtl::OUStringLiteral<4>'\))?}} with automatic storage duration most likely needs to be static [loplugin:stringliteralvar]}}
56 OUStringLiteral const literal = u"foo";
57 // expected-note-re@+1 {{first converted to '{{(rtl::)?}}OUString' here [loplugin:stringliteralvar]}}
58 f(literal);
61 void f6()
63 // expected-error-re@+1 {{variable 'literal' of type 'const {{(rtl::)?}}OUStringLiteral<4>'{{( \(aka 'const rtl::OUStringLiteral<4>'\))?}} with automatic storage duration most likely needs to be static [loplugin:stringliteralvar]}}
64 constexpr OUStringLiteral literal = u"foo";
65 // expected-note-re@+1 {{first converted to '{{(rtl::)?}}OUString' here [loplugin:stringliteralvar]}}
66 f(literal);
69 void f7()
71 static constexpr OUStringLiteral const literal = u"foo";
72 f(literal);
75 void f8()
77 static constexpr OUStringLiteral const literal = u"foo";
78 // expected-error-re@+1 {{variable 'literal' of type 'const {{(rtl::)?}}OUStringLiteral<4>'{{( \(aka 'const rtl::OUStringLiteral<4>'\))?}} suspiciously used in a sizeof expression [loplugin:stringliteralvar]}}
79 (void)sizeof literal;
82 void f9()
84 // expected-error-re@+1 {{change type of variable 'literal' from constant character array ('const sal_Unicode{{ ?}}[3]'{{( \(aka 'const char16_t\[3\]'\))?}}) to OUStringLiteral [loplugin:stringliteralvar]}}
85 static sal_Unicode const literal[] = { 'f', 'o', 'o' };
86 // expected-note-re@+1 {{first passed into a '{{(rtl::)?}}OUString' constructor here [loplugin:stringliteralvar]}}
87 f(OUString(literal, std::size(literal)));
90 void f10()
92 // expected-error-re@+1 {{change type of variable 'literal' from constant character array ('const sal_Unicode{{ ?}}[3]'{{( \(aka 'const char16_t\[3\]'\))?}}) to OUStringLiteral [loplugin:stringliteralvar]}}
93 static sal_Unicode const literal[] = { 'f', 'o', 'o' };
94 // expected-note-re@+1 {{first passed into a '{{(rtl::)?}}OUString' constructor here [loplugin:stringliteralvar]}}
95 f(OUString(literal, 3));
98 void f11(int nStreamType)
100 // expected-error-re@+1 {{change type of variable 'sDocumentType' from constant character array ('const char{{ ?}}[4]') to OUStringLiteral, and make it static [loplugin:stringliteralvar]}}
101 const char sDocumentType[] = "foo";
102 OUString sStreamType;
103 switch (nStreamType)
105 case 1:
106 // expected-note@+1 {{first assigned here [loplugin:stringliteralvar]}}
107 sStreamType = sDocumentType;
108 break;
110 (void)sStreamType;
113 extern sal_Unicode const extarr[1];
115 sal_Unicode init();
117 void f12()
119 // Suppress warnings if the array contains a malformed sequence of UTF-16 code units...:
120 static sal_Unicode const arr1[] = { 0xD800 };
121 f(OUString(arr1, 1));
122 // ...Or potentially contains a malformed sequence of UTF-16 code units...:
123 f(OUString(extarr, 1));
124 sal_Unicode const arr2[] = { init() };
125 f(OUString(arr2, 1));
126 // ...But generate a warning if the array contains a well-formed sequence of UTF-16 code units
127 // containing surrogates:
128 // expected-error-re@+1 {{change type of variable 'arr3' from constant character array ('const sal_Unicode{{ ?}}[2]'{{( \(aka 'const char16_t\[2\]'\))?}}) to OUStringLiteral [loplugin:stringliteralvar]}}
129 static sal_Unicode const arr3[] = { 0xD800, 0xDC00 };
130 // expected-note-re@+1 {{first passed into a '{{(rtl::)?}}OUString' constructor here [loplugin:stringliteralvar]}}
131 f(OUString(arr3, 2));
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */