bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / test / writeonlyvars.cxx
blob740befb6f1729fc93916c12163d3855fb2c6d47b
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 namespace Bar
21 void test()
23 // check that we DON'T see reads here
24 // expected-error@+1 {{write m_bar3 [loplugin:writeonlyvars]}}
25 int* m_bar3;
26 // expected-error@+1 {{write m_bar3b [loplugin:writeonlyvars]}}
27 int* m_bar3b;
28 m_bar3 = nullptr;
29 m_bar3b = m_bar3 = nullptr;
31 // check that we see reads of field when passed to a function pointer
32 // check that we see read of a field that is a function pointer
33 // expected-error@+2 {{write m_bar4 [loplugin:writeonlyvars]}}
34 // expected-error@+1 {{read m_bar4 [loplugin:writeonlyvars]}}
35 int m_bar4;
36 // expected-error@+1 {{read m_barfunctionpointer [loplugin:writeonlyvars]}}
37 void (*m_barfunctionpointer)(int&) = nullptr;
38 m_barfunctionpointer(m_bar4);
40 // check that we see reads of a field when used in variable init
41 // expected-error@+1 {{read m_bar5 [loplugin:writeonlyvars]}}
42 int m_bar5 = 1;
43 int x = m_bar5;
44 (void)x;
46 // check that we see reads of a field when used in ranged-for
47 // expected-error@+1 {{read m_bar6 [loplugin:writeonlyvars]}}
48 std::vector<int> m_bar6;
49 for (auto i : m_bar6)
51 (void)i;
54 // check that we see writes of array fields
55 // expected-error@+1 {{write m_bar7 [loplugin:writeonlyvars]}}
56 int m_bar7[5];
57 m_bar7[3] = 1;
59 // check that we see reads when a field is used in an array expression
60 // expected-error@+1 {{read m_bar8 [loplugin:writeonlyvars]}}
61 int m_bar8 = 1;
62 // expected-error@+1 {{read tmp [loplugin:writeonlyvars]}}
63 char tmp[5];
64 auto x2 = tmp[m_bar8];
65 (void)x2;
67 // check that we don't see reads when calling operator>>=
68 // expected-error@+1 {{write m_bar9 [loplugin:writeonlyvars]}}
69 sal_Int32 m_bar9;
70 // expected-error@+1 {{read any [loplugin:writeonlyvars]}}
71 css::uno::Any any;
72 any >>= m_bar9;
74 // check that we see don't see writes when calling operator<<=
75 // expected-error@+1 {{read m_bar10 [loplugin:writeonlyvars]}}
76 sal_Int32 m_bar10;
77 // expected-error@+2 {{write any2 [loplugin:writeonlyvars]}}
78 // expected-error@+1 {{read any2 [loplugin:writeonlyvars]}}
79 css::uno::Any any2;
80 any2 <<= m_bar10;
84 struct ReadOnly1
86 ReadOnly1(int&);
89 namespace ReadOnlyAnalysis
91 void method1(int&);
93 void test()
95 // check that we see a write when we pass by non-const ref
96 // expected-error@+2 {{read m_f2 [loplugin:writeonlyvars]}}
97 // expected-error@+1 {{write m_f2 [loplugin:writeonlyvars]}}
98 int m_f2;
99 method1(m_f2);
101 // expected-error@+1 {{write m_f4 [loplugin:writeonlyvars]}}
102 std::vector<int> m_f4;
103 m_f4.push_back(1);
105 // check that we see a write when we pass by non-const ref
106 // expected-error@+2 {{read m_f5 [loplugin:writeonlyvars]}}
107 // expected-error@+1 {{write m_f5 [loplugin:writeonlyvars]}}
108 int m_f5;
109 ReadOnly1 a(m_f5);
111 // check that we see a write when we pass by non-const ref
112 // expected-error@+2 {{read m_f6 [loplugin:writeonlyvars]}}
113 // expected-error@+1 {{write m_f6 [loplugin:writeonlyvars]}}
114 int m_f6;
115 // expected-error@+1 {{write r [loplugin:writeonlyvars]}}
116 int& r = m_f6;
117 r = 1;
121 void ReadOnlyAnalysis3()
123 // expected-error@+1 {{read m_f1 [loplugin:writeonlyvars]}}
124 int m_f1 = 0;
126 if (m_f1)
127 m_f1 = 1;
130 // Verify the special logic for container fields that only contains mutations that
131 // add elements.
132 void ReadOnlyAnalysis4()
134 // expected-error@+1 {{read m_readonly [loplugin:writeonlyvars]}}
135 std::vector<int> m_readonly;
136 // expected-error@+1 {{write m_writeonly [loplugin:writeonlyvars]}}
137 std::vector<int> m_writeonly;
138 // expected-error@+1 {{read m_readonlyCss [loplugin:writeonlyvars]}}
139 css::uno::Sequence<sal_Int32> m_readonlyCss;
141 // expected-error@+1 {{write x [loplugin:writeonlyvars]}}
142 int x = m_readonly[0];
143 (void)x;
144 *m_readonly.begin() = 1; // TODO?
146 m_writeonly.push_back(0);
148 x = m_readonlyCss.getArray()[0];
151 #endif
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */