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.
4  *
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  */
9 
10 #include <sal/config.h>
11 
12 #include <ostream>
13 
14 #include <rtl/strbuf.hxx>
15 #include <rtl/string.hxx>
16 #include <rtl/ustrbuf.hxx>
17 #include <rtl/ustring.hxx>
18 #include <sal/log.hxx>
19 
20 // Determine whether std::ostream operator << for sal_Unicode* (aka char16_t*) is deleted (see
21 // <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1423r3.html> "char8_t backward
22 // compatibility remediation", as implemented now by
23 // <https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=0c5b35933e5b150df0ab487efb2f11ef5685f713>
24 // "libstdc++: P1423R3 char8_t remediation (2/4)" for -std=c++2a; TODO: the checks here and the
25 // relevant code in loplugin:getstr should eventually be removed once support for the deleted
26 // operators is widespread):
27 #if __cplusplus > 201703L && (defined __GLIBCXX__ || defined _MSC_VER)
28 #define HAVE_DELETED_OPERATORS true
29 #else
30 #define HAVE_DELETED_OPERATORS false
31 #endif
32 
33 using S = OString;
34 
f(std::ostream & st,OString const & s1,OStringBuffer const & s2,OUString const & s3,OUStringBuffer const & s4,S const & s5,OString * p1,OStringBuffer * p2,OUString * p3,OUStringBuffer * p4,S * p5,char const * (OString::* pf)()const)35 void f(std::ostream& st, OString const& s1, OStringBuffer const& s2,
36        OUString const& s3[[maybe_unused]], OUStringBuffer const& s4[[maybe_unused]], S const& s5,
37        OString* p1, OStringBuffer* p2, OUString* p3[[maybe_unused]],
38        OUStringBuffer* p4[[maybe_unused]], S* p5, char const* (OString::*pf)() const)
39 {
40     st << s1.getStr() // expected-error {{directly use object of type 'rtl::OString' in a call of 'operator <<', instead of calling 'getStr' first [loplugin:getstr]}}
41        << s2.getStr()
42 #if !HAVE_DELETED_OPERATORS
43        << s3.getStr() // expected-error {{suspicious use of 'getStr' on an object of type 'rtl::OUString'; the result is implicitly cast to a void pointer in a call of 'operator <<' [loplugin:getstr]}}
44        << s4.getStr() // expected-error {{suspicious use of 'getStr' on an object of type 'rtl::OUStringBuffer'; the result is implicitly cast to a void pointer in a call of 'operator <<' [loplugin:getstr]}}
45 #endif
46        << s5.getStr() // expected-error {{directly use object of type 'S' (aka 'rtl::OString') in a call of 'operator <<', instead of calling 'getStr' first [loplugin:getstr]}}
47        << p1->getStr() // expected-error {{directly use object of type 'rtl::OString' in a call of 'operator <<', instead of calling 'getStr' first [loplugin:getstr]}}
48        << p2->getStr()
49 #if !HAVE_DELETED_OPERATORS
50        << p3->getStr() // expected-error {{suspicious use of 'getStr' on an object of type 'rtl::OUString'; the result is implicitly cast to a void pointer in a call of 'operator <<' [loplugin:getstr]}}
51        << p4->getStr() // expected-error {{suspicious use of 'getStr' on an object of type 'rtl::OUStringBuffer'; the result is implicitly cast to a void pointer in a call of 'operator <<' [loplugin:getstr]}}
52 #endif
53        << p5->getStr() // expected-error {{directly use object of type 'rtl::OString' in a call of 'operator <<', instead of calling 'getStr' first [loplugin:getstr]}}
54        << (s1.*pf)();
55     SAL_INFO( // expected-error 1+ {{directly use object of type 'rtl::OString' in a call of 'operator <<', instead of calling 'getStr' first [loplugin:getstr]}}
56         "test", s1.getStr());
57 }
58 
59 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
60