Skip to content

Commit 3e8566d

Browse files
authored
Merge pull request #3991 from gottesmm/stable/20211026/move-function-more-tests
[move-function] Address feedback from previous test commits and add even more tests!
2 parents f11628a + 0587fb7 commit 3e8566d

File tree

2 files changed

+231
-44
lines changed

2 files changed

+231
-44
lines changed

lldb/test/API/lang/swift/variables/move_function/TestSwiftMoveFunction.py

Lines changed: 180 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_swift_move_function(self):
4545
self.process = self.target.LaunchSimple(None, None, os.getcwd())
4646
threads = lldbutil.get_threads_stopped_at_breakpoint(
4747
self.process, self.breakpoints[0])
48-
self.assertTrue(len(threads) == 1)
48+
self.assertEqual(len(threads), 1)
4949
self.thread = threads[0]
5050

5151
self.do_check_copyable_value_test()
@@ -55,6 +55,10 @@ def test_swift_move_function(self):
5555
# ccf is conditional control flow
5656
self.do_check_copyable_value_ccf_true()
5757
self.do_check_copyable_value_ccf_false()
58+
self.do_check_copyable_var_ccf_true_reinit_out_block()
59+
self.do_check_copyable_var_ccf_true_reinit_in_block()
60+
self.do_check_copyable_var_ccf_false_reinit_out_block()
61+
self.do_check_copyable_var_ccf_false_reinit_in_block()
5862

5963
def setUp(self):
6064
TestBase.setUp(self)
@@ -68,9 +72,14 @@ def add_breakpoints(self, name, num_breakpoints):
6872
pat = pattern.format(name, i+1)
6973
brk = self.target.BreakpointCreateBySourceRegex(
7074
pat, self.main_source_spec)
71-
self.assertTrue(brk.GetNumLocations() > 0, VALID_BREAKPOINT)
75+
self.assertGreater(brk.GetNumLocations(), 0, VALID_BREAKPOINT)
7276
yield brk
7377

78+
def get_var(self, name):
79+
frame = self.thread.frames[0]
80+
self.assertTrue(frame.IsValid(), "Couldn't get a frame.")
81+
return frame.FindVariable(name)
82+
7483
def do_setup_breakpoints(self):
7584
self.breakpoints = []
7685

@@ -88,126 +97,255 @@ def do_setup_breakpoints(self):
8897
self.breakpoints.extend(
8998
self.add_breakpoints('copyableValueCCFFalseTest',
9099
3))
100+
self.breakpoints.extend(
101+
self.add_breakpoints('copyableVarTestCCFlowTrueReinitOutOfBlockTest',
102+
5))
103+
self.breakpoints.extend(
104+
self.add_breakpoints('copyableVarTestCCFlowTrueReinitInBlockTest',
105+
5))
106+
self.breakpoints.extend(
107+
self.add_breakpoints('copyableVarTestCCFlowFalseReinitOutOfBlockTest',
108+
4))
109+
self.breakpoints.extend(
110+
self.add_breakpoints('copyableVarTestCCFlowFalseReinitInBlockTest', 3))
91111

92112
def do_check_copyable_value_test(self):
93-
frame = self.thread.frames[0]
94-
self.assertTrue(frame.IsValid(), "Couldn't get a frame.")
95-
96113
# We haven't defined varK yet.
97-
varK = frame.FindVariable('k')
114+
varK = self.get_var('k')
98115

99-
self.assertTrue(varK.value == None, "varK initialized too early?!")
116+
self.assertIsNone(varK.value, "varK initialized too early?!")
100117

101118
# Go to break point 2. k should be valid.
102119
self.runCmd('continue')
103-
self.assertTrue(varK.unsigned > 0, "varK not initialized?!")
120+
self.assertGreater(varK.unsigned, 0, "varK not initialized?!")
104121

105122
# Go to breakpoint 3. k should no longer be valid.
106123
self.runCmd('continue')
107-
self.assertTrue(varK.value == None, "K is live but was moved?!")
124+
self.assertIsNone(varK.value, "K is live but was moved?!")
108125

109126
# Run so we hit the next breakpoint to jump to the next test's
110127
# breakpoint.
111128
self.runCmd('continue')
112129

113130
def do_check_copyable_var_test(self):
114-
frame = self.thread.frames[0]
115-
self.assertTrue(frame.IsValid(), "Couldn't get a frame.")
116-
117131
# We haven't defined varK yet.
118-
varK = frame.FindVariable('k')
119-
self.assertTrue(varK.value == None, "varK initialized too early?!")
132+
varK = self.get_var('k')
133+
self.assertIsNone(varK.value, "varK initialized too early?!")
120134

121135
# Go to break point 2. k should be valid.
122136
self.runCmd('continue')
123-
self.assertTrue(varK.unsigned > 0, "varK not initialized?!")
137+
self.assertGreater(varK.unsigned, 0, "varK not initialized?!")
124138

125139
# Go to breakpoint 3. We invalidated k
126140
self.runCmd('continue')
127-
self.assertTrue(varK.value == None, "K is live but was moved?!")
141+
self.assertIsNone(varK.value, "K is live but was moved?!")
128142

129143
# Go to the last breakpoint and make sure that k is reinitialized
130144
# properly.
131145
self.runCmd('continue')
132-
self.assertTrue(varK.unsigned > 0, "varK not initialized")
146+
self.assertGreater(varK.unsigned, 0, "varK not initialized")
133147

134148
# Run so we hit the next breakpoint to go to the next test.
135149
self.runCmd('continue')
136150

137151
def do_check_addressonly_value_test(self):
138-
frame = self.thread.frames[0]
139-
self.assertTrue(frame.IsValid(), "Couldn't get a frame.")
140-
141-
# We haven't defined varK or varM yet... so we shouldn't have a summary.
142-
varK = frame.FindVariable('k')
152+
# We haven't defined varK yet.
153+
varK = self.get_var('k')
143154

144155
# Go to break point 2. k should be valid and m should not be. Since M is
145156
# a dbg.declare it is hard to test robustly that it is not initialized
146157
# so we don't do so. We have an additional llvm.dbg.addr test where we
147158
# move the other variable and show the correct behavior with
148159
# llvm.dbg.declare.
149160
self.runCmd('continue')
150-
151-
self.assertTrue(varK.unsigned > 0, "var not initialized?!")
161+
self.assertGreater(varK.unsigned, 0, "var not initialized?!")
152162

153163
# Go to breakpoint 3.
154164
self.runCmd('continue')
155-
self.assertTrue(varK.unsigned == 0,
165+
self.assertEqual(varK.unsigned, 0,
156166
"dbg thinks varK is live despite move?!")
157167

158168
# Run so we hit the next breakpoint as part of the next test.
159169
self.runCmd('continue')
160170

161171
def do_check_addressonly_var_test(self):
162-
frame = self.thread.frames[0]
163-
self.assertTrue(frame.IsValid(), "Couldn't get a frame.")
164-
165-
varK = frame.FindVariable('k')
172+
varK = self.get_var('k')
166173

167174
# Go to break point 2. k should be valid.
168175
self.runCmd('continue')
169-
self.assertTrue(varK.unsigned > 0, "varK not initialized?!")
176+
self.assertGreater(varK.unsigned, 0, "varK not initialized?!")
170177

171178
# Go to breakpoint 3. K was invalidated.
172179
self.runCmd('continue')
173-
self.assertTrue(varK.value == None, "K is live but was moved?!")
180+
self.assertIsNone(varK.value, "K is live but was moved?!")
174181

175182
# Go to the last breakpoint and make sure that k is reinitialized
176183
# properly.
177184
self.runCmd('continue')
178-
self.assertTrue(varK.unsigned > 0, "varK not initialized")
185+
self.assertGreater(varK.unsigned, 0, "varK not initialized")
179186

180187
# Run so we hit the next breakpoint as part of the next test.
181188
self.runCmd('continue')
182189

183190
def do_check_copyable_value_ccf_true(self):
184-
frame = self.thread.frames[0]
185-
self.assertTrue(frame.IsValid(), "Couldn't get a frame.")
186-
varK = frame.FindVariable('k')
191+
varK = self.get_var('k')
187192

188193
# Check at our start point that we do not have any state for varK and
189194
# then continue to our next breakpoint.
190-
self.assertTrue(varK.value == None, "varK should not have a value?!")
195+
self.assertIsNone(varK.value, "varK should not have a value?!")
191196
self.runCmd('continue')
192197

193198
# At this breakpoint, k should be defined since we are going to do
194199
# something with it.
195-
self.assertTrue(varK.value != None, "varK should have a value?!")
200+
self.assertIsNotNone(varK.value, "varK should have a value?!")
196201
self.runCmd('continue')
197202

198203
# At this breakpoint, we are now in the conditional control flow part of
199204
# the loop. Make sure that we can see k still.
200-
self.assertTrue(varK.value != None, "varK should have a value?!")
205+
self.assertIsNotNone(varK.value, "varK should have a value?!")
201206
self.runCmd('continue')
202207

203208
# Ok, we just performed the move. k should not be no longer initialized.
204-
self.runCmd('fr v')
205-
self.assertTrue(varK.value == None, "varK should not have a value?!")
209+
self.assertIsNone(varK.value, "varK should not have a value?!")
206210
self.runCmd('continue')
207211

208212
# Finally we left the conditional control flow part of the function. k
209213
# should still be None.
210-
self.assertTrue(varK.value == None, "varK should not have a value!")
214+
self.assertIsNone(varK.value, "varK should not have a value!")
215+
216+
# Run again so we go and run to the next test.
217+
self.runCmd('continue')
211218

212219
def do_check_copyable_value_ccf_false(self):
213-
pass
220+
varK = self.get_var('k')
221+
222+
# Check at our start point that we do not have any state for varK and
223+
# then continue to our next breakpoint.
224+
self.assertIsNone(varK.value, "varK should not have a value?!")
225+
self.runCmd('continue')
226+
227+
# At this breakpoint, k should be defined since we are going to do
228+
# something with it.
229+
self.assertIsNotNone(varK.value, "varK should have a value?!")
230+
self.runCmd('continue')
231+
232+
# At this breakpoint, we are now past the end of the conditional
233+
# statement. We know due to the move checking that k can not have any
234+
# uses that are reachable from the move. So it is safe to always not
235+
# provide the value here.
236+
self.assertIsNone(varK.value, "varK should have a value?!")
237+
238+
# Run again so we go and run to the next test.
239+
self.runCmd('continue')
240+
241+
def do_check_copyable_var_ccf_true_reinit_out_block(self):
242+
varK = self.get_var('k')
243+
244+
# At first we should not have a value for k.
245+
self.assertEqual(varK.unsigned, 0, "varK should be nullptr!")
246+
self.runCmd('continue')
247+
248+
# Now we are in the conditional true block. K should be defined since we
249+
# are on the move itself.
250+
self.assertGreater(varK.unsigned, 0, "varK should not be nullptr!")
251+
self.runCmd('continue')
252+
253+
# Now we have executed the move and we are about to run code using
254+
# m. Make sure that K is not available!
255+
self.assertEqual(varK.unsigned, 0,
256+
"varK was already moved! Should be nullptr")
257+
self.runCmd('continue')
258+
259+
# We are now out of the conditional lexical block on the line of code
260+
# that redefines k. k should still be not available.
261+
self.assertEqual(varK.unsigned, 0,
262+
"varK was already moved! Should be nullptr")
263+
self.runCmd('continue')
264+
265+
# Ok, we have now reinit k and are about to call a method on it. We
266+
# should be valid now.
267+
self.assertGreater(varK.unsigned, 0,
268+
"varK should have be reinitialized?!")
269+
270+
# Run again so we go and run to the next test.
271+
self.runCmd('continue')
272+
273+
def do_check_copyable_var_ccf_true_reinit_in_block(self):
274+
varK = self.get_var('k')
275+
276+
# At first we should not have a value for k.
277+
self.assertEqual(varK.unsigned, 0, "varK should be nullptr!")
278+
self.runCmd('continue')
279+
280+
# Now we are in the conditional true block. K should be defined since we
281+
# are on the move itself.
282+
self.assertGreater(varK.unsigned, 0, "varK should not be nullptr!")
283+
self.runCmd('continue')
284+
285+
# Now we have executed the move and we are about to reinit k but have
286+
# not yet. Make sure we are not available!
287+
self.assertEqual(varK.unsigned, 0,
288+
"varK was already moved! Should be nullptr")
289+
self.runCmd('continue')
290+
291+
# We are now still inside the conditional part of the code, but have
292+
# reinitialized varK.
293+
self.assertGreater(varK.unsigned, 0,
294+
"varK was reinit! Should be valid value!")
295+
self.runCmd('continue')
296+
297+
# We now have left the conditional part of the function. k should still
298+
# be available.
299+
self.assertGreater(varK.unsigned, 0,
300+
"varK should have be reinitialized?!")
301+
302+
# Run again so we go and run to the next test.
303+
self.runCmd('continue')
304+
305+
def do_check_copyable_var_ccf_false_reinit_out_block(self):
306+
varK = self.get_var('k')
307+
308+
# At first we should not have a value for k.
309+
self.assertEqual(varK.unsigned, 0, "varK should be nullptr!")
310+
self.runCmd('continue')
311+
312+
# Now we are right above the beginning of the false check. varK should
313+
# still be valid.
314+
self.assertGreater(varK.unsigned, 0, "varK should not be nullptr!")
315+
self.runCmd('continue')
316+
317+
# Now we are after the conditional part of the code on the reinit
318+
# line. Since this is reachable from the move and we haven't reinit yet,
319+
# k should not be available.
320+
self.assertEqual(varK.unsigned, 0,
321+
"varK was already moved! Should be nullptr")
322+
self.runCmd('continue')
323+
324+
# Ok, we have now reinit k and are about to call a method on it. We
325+
# should be valid now.
326+
self.assertGreater(varK.unsigned, 0,
327+
"varK should have be reinitialized?!")
328+
329+
# Run again so we go and run to the next test.
330+
self.runCmd('continue')
331+
332+
def do_check_copyable_var_ccf_false_reinit_in_block(self):
333+
varK = self.get_var('k')
334+
335+
# At first we should not have a value for k.
336+
self.assertEqual(varK.unsigned, 0, "varK should be nullptr!")
337+
self.runCmd('continue')
338+
339+
# Now we are on the doSomething above the false check. So varK should be
340+
# valid.
341+
self.assertGreater(varK.unsigned, 0, "varK should not be nullptr!")
342+
self.runCmd('continue')
343+
344+
# Now we are after the conditional scope. Since k was reinitialized in
345+
# the conditional scope, along all paths we are valid so varK should
346+
# still be available.
347+
self.assertGreater(varK.unsigned, 0,
348+
"varK should not be nullptr?!")
349+
350+
# Run again so we go and run to the next test.
351+
self.runCmd('continue')

0 commit comments

Comments
 (0)