Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / writeonlyvars.cxx
blob0fc141f62dd2b74d6dc1d9c18b35d79abd31e7a7
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 #if defined _WIN32 //TODO, see corresponding TODO in compilerplugins/clang/writeonlyvars.cxx
11 // expected-no-diagnostics
12 #else
14 #include <vector>
15 #include <ostream>
16 #include <com/sun/star/uno/Any.hxx>
17 #include <com/sun/star/uno/Sequence.hxx>
19 // See 1d0bc2139759f087d50432f8a2116060676f34e1 "use std::experimental::source_location in
20 // uno::Exception" modification to
21 // workdir/UnoApiHeadersTarget/udkapi/normal/com/sun/star/uno/Exception.hdl, which is indirectly
22 // included through the above #include directives, in turn causing conditional inclusion of
23 // include/o3tl/runtimetooustring.hxx (and where `ok` is only read in an assert in !NDEBUG builds):
24 #if defined LIBO_USE_SOURCE_LOCATION
25 // expected-error@o3tl/runtimetooustring.hxx:* {{read s [loplugin:writeonlyvars]}}
26 // expected-error@o3tl/runtimetooustring.hxx:* {{write s [loplugin:writeonlyvars]}}
27 #if !defined NDEBUG
28 // expected-error@o3tl/runtimetooustring.hxx:* {{read ok [loplugin:writeonlyvars]}}
29 #endif
30 #endif
32 namespace Bar
34 void test()
36 // check that we DON'T see reads here
37 // expected-error@+1 {{write m_bar3 [loplugin:writeonlyvars]}}
38 int* m_bar3;
39 // expected-error@+1 {{write m_bar3b [loplugin:writeonlyvars]}}
40 int* m_bar3b;
41 m_bar3 = nullptr;
42 m_bar3b = m_bar3 = nullptr;
43 (void)m_bar3b;
45 // check that we see reads of field when passed to a function pointer
46 // check that we see read of a field that is a function pointer
47 // expected-error@+2 {{write m_bar4 [loplugin:writeonlyvars]}}
48 // expected-error@+1 {{read m_bar4 [loplugin:writeonlyvars]}}
49 int m_bar4;
50 // expected-error@+1 {{read m_barfunctionpointer [loplugin:writeonlyvars]}}
51 void (*m_barfunctionpointer)(int&) = nullptr;
52 m_barfunctionpointer(m_bar4);
54 // check that we see reads of a field when used in variable init
55 // expected-error@+1 {{read m_bar5 [loplugin:writeonlyvars]}}
56 int m_bar5 = 1;
57 int x = m_bar5;
58 (void)x;
60 // check that we see reads of a field when used in ranged-for
61 // expected-error@+1 {{read m_bar6 [loplugin:writeonlyvars]}}
62 std::vector<int> m_bar6;
63 for (auto i : m_bar6)
65 (void)i;
68 // check that we see writes of array fields
69 // expected-error@+1 {{write m_bar7 [loplugin:writeonlyvars]}}
70 int m_bar7[5];
71 m_bar7[3] = 1;
73 // check that we see reads when a field is used in an array expression
74 // expected-error@+1 {{read m_bar8 [loplugin:writeonlyvars]}}
75 int m_bar8 = 1;
76 // expected-error@+1 {{read tmp [loplugin:writeonlyvars]}}
77 char tmp[5];
78 auto x2 = tmp[m_bar8];
79 (void)x2;
81 // check that we don't see reads when calling operator>>=
82 // expected-error@+1 {{write m_bar9 [loplugin:writeonlyvars]}}
83 sal_Int32 m_bar9;
84 // expected-error@+1 {{read any [loplugin:writeonlyvars]}}
85 css::uno::Any any;
86 any >>= m_bar9;
88 // check that we don't see writes when calling operator<<=
89 // expected-error@+1 {{read m_bar10 [loplugin:writeonlyvars]}}
90 sal_Int32 m_bar10 = 0;
91 // expected-error@+2 {{write any2 [loplugin:writeonlyvars]}}
92 // expected-error@+1 {{read any2 [loplugin:writeonlyvars]}}
93 css::uno::Any any2;
94 any2 <<= m_bar10;
95 (void)any2;
99 struct ReadOnly1
101 ReadOnly1(int&);
104 namespace ReadOnlyAnalysis
106 void method1(int&);
108 void test()
110 // check that we see a write when we pass by non-const ref
111 // expected-error@+2 {{read m_f2 [loplugin:writeonlyvars]}}
112 // expected-error@+1 {{write m_f2 [loplugin:writeonlyvars]}}
113 int m_f2;
114 method1(m_f2);
116 // expected-error@+1 {{write m_f4 [loplugin:writeonlyvars]}}
117 std::vector<int> m_f4;
118 m_f4.push_back(1);
120 // check that we see a write when we pass by non-const ref
121 // expected-error@+2 {{read m_f5 [loplugin:writeonlyvars]}}
122 // expected-error@+1 {{write m_f5 [loplugin:writeonlyvars]}}
123 int m_f5;
124 ReadOnly1 a(m_f5);
126 // check that we see a write when we pass by non-const ref
127 // expected-error@+2 {{read m_f6 [loplugin:writeonlyvars]}}
128 // expected-error@+1 {{write m_f6 [loplugin:writeonlyvars]}}
129 int m_f6;
130 // expected-error@+1 {{write r [loplugin:writeonlyvars]}}
131 int& r = m_f6;
132 r = 1;
136 void ReadOnlyAnalysis3()
138 // expected-error@+1 {{read m_f1 [loplugin:writeonlyvars]}}
139 int m_f1 = 0;
141 if (m_f1)
142 m_f1 = 1;
145 // Verify the special logic for container fields that only contains mutations that
146 // add elements.
147 void ReadOnlyAnalysis4()
149 // expected-error@+1 {{read m_readonly [loplugin:writeonlyvars]}}
150 std::vector<int> m_readonly;
151 // expected-error@+1 {{write m_writeonly [loplugin:writeonlyvars]}}
152 std::vector<int> m_writeonly;
153 // expected-error@+1 {{read m_readonlyCss [loplugin:writeonlyvars]}}
154 css::uno::Sequence<sal_Int32> m_readonlyCss;
156 // expected-error@+1 {{write x [loplugin:writeonlyvars]}}
157 int x = m_readonly[0];
158 (void)x;
159 *m_readonly.begin() = 1; // TODO?
161 m_writeonly.push_back(0);
163 x = m_readonlyCss.getArray()[0];
166 #endif
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */