Fix typo
[LibreOffice.git] / compilerplugins / clang / test / writeonlyvars.cxx
blobfc521fa7d41725cff4154bcc26421afb86c01995
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 // expected-error@o3tl/safeint.hxx:* {{read res [loplugin:writeonlyvars]}}
28 #if !defined NDEBUG
29 // expected-error@o3tl/runtimetooustring.hxx:* {{read ok [loplugin:writeonlyvars]}}
30 #endif
31 #endif
33 namespace Bar
35 void test()
37 // check that we DON'T see reads here
38 // expected-error@+1 {{write m_bar3 [loplugin:writeonlyvars]}}
39 int* m_bar3;
40 // expected-error@+1 {{write m_bar3b [loplugin:writeonlyvars]}}
41 int* m_bar3b;
42 m_bar3 = nullptr;
43 m_bar3b = m_bar3 = nullptr;
44 (void)m_bar3b;
46 // check that we see reads of field when passed to a function pointer
47 // check that we see read of a field that is a function pointer
48 // expected-error@+2 {{write m_bar4 [loplugin:writeonlyvars]}}
49 // expected-error@+1 {{read m_bar4 [loplugin:writeonlyvars]}}
50 int m_bar4;
51 // expected-error@+1 {{read m_barfunctionpointer [loplugin:writeonlyvars]}}
52 void (*m_barfunctionpointer)(int&) = nullptr;
53 m_barfunctionpointer(m_bar4);
55 // check that we see reads of a field when used in variable init
56 // expected-error@+1 {{read m_bar5 [loplugin:writeonlyvars]}}
57 int m_bar5 = 1;
58 int x = m_bar5;
59 (void)x;
61 // check that we see reads of a field when used in ranged-for
62 // expected-error@+1 {{read m_bar6 [loplugin:writeonlyvars]}}
63 std::vector<int> m_bar6;
64 for (auto i : m_bar6)
66 (void)i;
69 // check that we see writes of array fields
70 // expected-error@+1 {{write m_bar7 [loplugin:writeonlyvars]}}
71 int m_bar7[5];
72 m_bar7[3] = 1;
74 // check that we see reads when a field is used in an array expression
75 // expected-error@+1 {{read m_bar8 [loplugin:writeonlyvars]}}
76 int m_bar8 = 1;
77 // expected-error@+1 {{read tmp [loplugin:writeonlyvars]}}
78 char tmp[5];
79 auto x2 = tmp[m_bar8];
80 (void)x2;
82 // check that we don't see reads when calling operator>>=
83 // expected-error@+1 {{write m_bar9 [loplugin:writeonlyvars]}}
84 sal_Int32 m_bar9;
85 // expected-error@+1 {{read any [loplugin:writeonlyvars]}}
86 css::uno::Any any;
87 any >>= m_bar9;
89 // check that we don't see writes when calling operator<<=
90 // expected-error@+1 {{read m_bar10 [loplugin:writeonlyvars]}}
91 sal_Int32 m_bar10 = 0;
92 // expected-error@+2 {{write any2 [loplugin:writeonlyvars]}}
93 // expected-error@+1 {{read any2 [loplugin:writeonlyvars]}}
94 css::uno::Any any2;
95 any2 <<= m_bar10;
96 (void)any2;
100 struct ReadOnly1
102 ReadOnly1(int&);
105 namespace ReadOnlyAnalysis
107 void method1(int&);
109 void test()
111 // check that we see a write when we pass by non-const ref
112 // expected-error@+2 {{read m_f2 [loplugin:writeonlyvars]}}
113 // expected-error@+1 {{write m_f2 [loplugin:writeonlyvars]}}
114 int m_f2;
115 method1(m_f2);
117 // expected-error@+1 {{write m_f4 [loplugin:writeonlyvars]}}
118 std::vector<int> m_f4;
119 m_f4.push_back(1);
121 // check that we see a write when we pass by non-const ref
122 // expected-error@+2 {{read m_f5 [loplugin:writeonlyvars]}}
123 // expected-error@+1 {{write m_f5 [loplugin:writeonlyvars]}}
124 int m_f5;
125 ReadOnly1 a(m_f5);
127 // check that we see a write when we pass by non-const ref
128 // expected-error@+2 {{read m_f6 [loplugin:writeonlyvars]}}
129 // expected-error@+1 {{write m_f6 [loplugin:writeonlyvars]}}
130 int m_f6;
131 // expected-error@+1 {{write r [loplugin:writeonlyvars]}}
132 int& r = m_f6;
133 r = 1;
137 void ReadOnlyAnalysis3()
139 // expected-error@+1 {{read m_f1 [loplugin:writeonlyvars]}}
140 int m_f1 = 0;
142 if (m_f1)
143 m_f1 = 1;
146 // Verify the special logic for container fields that only contains mutations that
147 // add elements.
148 void ReadOnlyAnalysis4()
150 // expected-error@+1 {{read m_readonly [loplugin:writeonlyvars]}}
151 std::vector<int> m_readonly;
152 // expected-error@+1 {{write m_writeonly [loplugin:writeonlyvars]}}
153 std::vector<int> m_writeonly;
154 // expected-error@+1 {{read m_readonlyCss [loplugin:writeonlyvars]}}
155 css::uno::Sequence<sal_Int32> m_readonlyCss;
157 // expected-error@+1 {{write x [loplugin:writeonlyvars]}}
158 int x = m_readonly[0];
159 (void)x;
160 *m_readonly.begin() = 1; // TODO?
162 m_writeonly.push_back(0);
164 x = m_readonlyCss.getArray()[0];
167 #endif
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */