1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
|
#!/usr/bin/env python
# Copyright (c) 2021, Alliance for Open Media. All rights reserved
#
# This source code is subject to the terms of the BSD 2 Clause License and
# the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
# was not distributed with this source code in the LICENSE file, you can
# obtain it at www.aomedia.org/license/software. If the Alliance for Open
# Media Patent License 1.0 was not distributed with this source code in the
# PATENTS file, you can obtain it at www.aomedia.org/license/patent.
#
import pprint
import re
import os, sys
import io
import unittest as googletest
sys.path[0:0] = ['.', '..']
from pycparser import c_parser, parse_file
from pycparser.c_ast import *
from pycparser.c_parser import CParser, Coord, ParseError
from auto_refactor import *
def get_c_file_path(filename):
return os.path.join('c_files', filename)
class TestStructInfo(googletest.TestCase):
def setUp(self):
filename = get_c_file_path('struct_code.c')
self.ast = parse_file(filename)
def test_build_struct_info(self):
struct_info = build_struct_info(self.ast)
typedef_name_dic = struct_info.typedef_name_dic
self.assertEqual('T1' in typedef_name_dic, True)
self.assertEqual('T4' in typedef_name_dic, True)
self.assertEqual('T5' in typedef_name_dic, True)
struct_name_dic = struct_info.struct_name_dic
struct_name = 'S1'
self.assertEqual(struct_name in struct_name_dic, True)
struct_item = struct_name_dic[struct_name]
self.assertEqual(struct_item.is_union, False)
struct_name = 'S3'
self.assertEqual(struct_name in struct_name_dic, True)
struct_item = struct_name_dic[struct_name]
self.assertEqual(struct_item.is_union, False)
struct_name = 'U5'
self.assertEqual(struct_name in struct_name_dic, True)
struct_item = struct_name_dic[struct_name]
self.assertEqual(struct_item.is_union, True)
self.assertEqual(len(struct_info.struct_item_list), 6)
def test_get_child_decl_status(self):
struct_info = build_struct_info(self.ast)
struct_item = struct_info.typedef_name_dic['T4']
decl_status = struct_item.child_decl_map['x']
self.assertEqual(decl_status.struct_item, None)
self.assertEqual(decl_status.is_ptr_decl, False)
decl_status = struct_item.child_decl_map['s3']
self.assertEqual(decl_status.struct_item.struct_name, 'S3')
self.assertEqual(decl_status.is_ptr_decl, False)
struct_item = struct_info.typedef_name_dic['T6']
decl_status = struct_item.child_decl_map['x']
self.assertEqual(decl_status.struct_item, None)
self.assertEqual(decl_status.is_ptr_decl, False)
decl_status = struct_item.child_decl_map['y']
self.assertEqual(decl_status.struct_item, None)
self.assertEqual(decl_status.is_ptr_decl, False)
decl_status = struct_item.child_decl_map['z']
self.assertEqual(decl_status.struct_item, None)
self.assertEqual(decl_status.is_ptr_decl, False)
struct_item = struct_info.typedef_name_dic['T7']
decl_status = struct_item.child_decl_map['y']
self.assertEqual('x' in decl_status.struct_item.child_decl_map, True)
struct_item = struct_info.typedef_name_dic['T7']
decl_status = struct_item.child_decl_map['z']
self.assertEqual('w' in decl_status.struct_item.child_decl_map, True)
class TestParseLvalue(googletest.TestCase):
def setUp(self):
filename = get_c_file_path('parse_lvalue.c')
self.ast = parse_file(filename)
self.func_dictionary = build_func_dictionary(self.ast)
def test_parse_lvalue(self):
func_node = self.func_dictionary['func']
func_body_items = func_node.body.block_items
id_list = parse_lvalue(func_body_items[0].lvalue)
ref_id_list = ['cpi', 'rd', 'u']
self.assertEqual(id_list, ref_id_list)
id_list = parse_lvalue(func_body_items[2].lvalue)
ref_id_list = ['y']
self.assertEqual(id_list, ref_id_list)
id_list = parse_lvalue(func_body_items[3].lvalue)
ref_id_list = ['cpi', 'rd2', 'v']
self.assertEqual(id_list, ref_id_list)
id_list = parse_lvalue(func_body_items[4].lvalue)
ref_id_list = ['cpi', 'rd', 'arr']
self.assertEqual(id_list, ref_id_list)
id_list = parse_lvalue(func_body_items[5].lvalue)
ref_id_list = ['cpi', 'rd3', 'arr']
self.assertEqual(id_list, ref_id_list)
def test_parse_lvalue_2(self):
func_node = self.func_dictionary['parse_lvalue_2']
func_body_items = func_node.body.block_items
id_list = parse_lvalue(func_body_items[0].init)
ref_id_list = ['cpi', 'rd2']
self.assertEqual(id_list, ref_id_list)
class TestIDStatusNode(googletest.TestCase):
def test_add_descendant(self):
root = IDStatusNode('root')
id_chain1 = ['cpi', 'rd', 'u']
id_chain2 = ['cpi', 'rd', 'v']
root.add_descendant(id_chain1)
root.add_descendant(id_chain2)
ref_children_list1 = ['cpi']
children_list1 = list(root.children.keys())
self.assertEqual(children_list1, ref_children_list1)
ref_children_list2 = ['rd']
children_list2 = list(root.children['cpi'].children.keys())
self.assertEqual(children_list2, ref_children_list2)
ref_children_list3 = ['u', 'v']
children_list3 = list(root.children['cpi'].children['rd'].children.keys())
self.assertEqual(children_list3, ref_children_list3)
def test_get_descendant(self):
root = IDStatusNode('root')
id_chain1 = ['cpi', 'rd', 'u']
id_chain2 = ['cpi', 'rd', 'v']
ref_descendant_1 = root.add_descendant(id_chain1)
ref_descendant_2 = root.add_descendant(id_chain2)
descendant_1 = root.get_descendant(id_chain1)
self.assertEqual(descendant_1 is ref_descendant_1, True)
descendant_2 = root.get_descendant(id_chain2)
self.assertEqual(descendant_2 is ref_descendant_2, True)
id_chain3 = ['cpi', 'rd', 'h']
descendant_3 = root.get_descendant(id_chain3)
self.assertEqual(descendant_3, None)
class TestFuncInOut(googletest.TestCase):
def setUp(self):
c_filename = get_c_file_path('func_in_out.c')
self.ast = parse_file(c_filename)
self.func_dictionary = build_func_dictionary(self.ast)
self.struct_info = build_struct_info(self.ast)
def test_get_func_param_id_map(self):
func_def_node = self.func_dictionary['func']
param_id_map = get_func_param_id_map(func_def_node)
ref_param_id_map_keys = ['cpi', 'x']
self.assertEqual(list(param_id_map.keys()), ref_param_id_map_keys)
def test_assign_refer_status_1(self):
func_def_node = self.func_dictionary['func_assign_refer_status_1']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
body_id_tree = visitor.body_id_tree
id_chain = ['rd']
descendant = body_id_tree.get_descendant(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), False)
ref_link_id_chain = ['cpi', 'rd']
self.assertEqual(ref_link_id_chain, descendant.get_link_id_chain())
id_chain = ['cpi', 'rd']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), False)
self.assertEqual(None, descendant.get_link_id_chain())
def test_assign_refer_status_2(self):
func_def_node = self.func_dictionary['func_assign_refer_status_2']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
body_id_tree = visitor.body_id_tree
id_chain = ['rd2']
descendant = body_id_tree.get_descendant(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), False)
ref_link_id_chain = ['cpi', 'rd']
self.assertEqual(ref_link_id_chain, descendant.get_link_id_chain())
id_chain = ['cpi', 'rd']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), False)
self.assertEqual(None, descendant.get_link_id_chain())
def test_assign_refer_status_3(self):
func_def_node = self.func_dictionary['func_assign_refer_status_3']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
body_id_tree = visitor.body_id_tree
id_chain = ['a']
descendant = body_id_tree.get_descendant(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
self.assertEqual(None, descendant.get_link_id_chain())
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), True)
self.assertEqual(None, descendant.get_link_id_chain())
def test_assign_refer_status_4(self):
func_def_node = self.func_dictionary['func_assign_refer_status_4']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
body_id_tree = visitor.body_id_tree
id_chain = ['b']
descendant = body_id_tree.get_descendant(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
self.assertEqual(None, descendant.get_link_id_chain())
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), True)
self.assertEqual(None, descendant.get_link_id_chain())
def test_assign_refer_status_5(self):
func_def_node = self.func_dictionary['func_assign_refer_status_5']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
body_id_tree = visitor.body_id_tree
id_chain = ['rd5']
descendant = body_id_tree.get_descendant(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), False)
id_chain = ['cpi', 'rd2']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), False)
self.assertEqual(None, descendant.get_link_id_chain())
def test_assign_refer_status_6(self):
func_def_node = self.func_dictionary['func_assign_refer_status_6']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
self.assertEqual(None, descendant.get_link_id_chain())
id_chain = ['cpi2', 'rd']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), True)
self.assertEqual(None, descendant.get_link_id_chain())
def test_assign_refer_status_7(self):
func_def_node = self.func_dictionary['func_assign_refer_status_7']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'arr']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_assign_refer_status_8(self):
func_def_node = self.func_dictionary['func_assign_refer_status_8']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'arr']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), True)
def test_assign_refer_status_9(self):
func_def_node = self.func_dictionary['func_assign_refer_status_9']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_assign_refer_status_10(self):
func_def_node = self.func_dictionary['func_assign_refer_status_10']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), True)
id_chain = ['cpi', 'arr']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_assign_refer_status_11(self):
func_def_node = self.func_dictionary['func_assign_refer_status_11']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd2', 'v']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_assign_refer_status_12(self):
func_def_node = self.func_dictionary['func_assign_refer_status_12']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
id_chain = ['cpi2', 'rd']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), True)
def test_assign_refer_status_13(self):
func_def_node = self.func_dictionary['func_assign_refer_status_13']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'z']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
id_chain = ['cpi', 'w']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_id_status_forrest_1(self):
func_def_node = self.func_dictionary['func']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack().top()
children_names = set(root.get_children().keys())
ref_children_names = set(['cpi', 'x'])
self.assertEqual(children_names, ref_children_names)
root = visitor.body_id_tree
children_names = set(root.get_children().keys())
ref_children_names = set(['a', 'ref_rd', 'ref_rd2', 'ref_rd3', 'b'])
self.assertEqual(children_names, ref_children_names)
def test_id_status_forrest_show(self):
func_def_node = self.func_dictionary['func_id_forrest_show']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
visitor.get_id_tree_stack().top().show()
def test_id_status_forrest_2(self):
func_def_node = self.func_dictionary['func_id_forrest_show']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack().top()
self.assertEqual(root, root.root)
id_chain = ['cpi', 'rd']
descendant = root.get_descendant(id_chain)
self.assertEqual(root, descendant.root)
id_chain = ['b']
descendant = root.get_descendant(id_chain)
self.assertEqual(root, descendant.root)
def test_link_id_chain_1(self):
func_def_node = self.func_dictionary['func_link_id_chain_1']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
def test_link_id_chain_2(self):
func_def_node = self.func_dictionary['func_link_id_chain_2']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd', 'xd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
def test_func_call_1(self):
func_def_node = self.func_dictionary['func_call_1']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
id_chain = ['y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), True)
def test_func_call_2(self):
func_def_node = self.func_dictionary['func_call_2']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
id_chain = ['cpi', 'rd']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), False)
def test_func_call_3(self):
func_def_node = self.func_dictionary['func_call_3']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), True)
def test_func_call_4(self):
func_def_node = self.func_dictionary['func_call_4']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
id_chain = ['cpi', 'rd', 'xd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_func_call_5(self):
func_def_node = self.func_dictionary['func_call_5']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_func_compound_1(self):
func_def_node = self.func_dictionary['func_compound_1']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), True)
def test_func_compound_2(self):
func_def_node = self.func_dictionary['func_compound_2']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), False)
self.assertEqual(descendant.get_refer(), True)
id_chain = ['cpi', 'rd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_func_compound_3(self):
func_def_node = self.func_dictionary['func_compound_3']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'rd', 'u']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), False)
def test_func_compound_4(self):
func_def_node = self.func_dictionary['func_compound_4']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), True)
def test_func_compound_5(self):
func_def_node = self.func_dictionary['func_compound_5']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), True)
def test_func_compound_6(self):
func_def_node = self.func_dictionary['func_compound_6']
visitor = FuncInOutVisitor(func_def_node, self.struct_info,
self.func_dictionary)
visitor.visit(func_def_node.body)
root = visitor.get_id_tree_stack()
id_chain = ['cpi', 'y']
descendant = root.get_id_node(id_chain)
self.assertEqual(descendant.get_assign(), True)
self.assertEqual(descendant.get_refer(), True)
class TestDeclStatus(googletest.TestCase):
def setUp(self):
filename = get_c_file_path('decl_status_code.c')
self.ast = parse_file(filename)
self.func_dictionary = build_func_dictionary(self.ast)
self.struct_info = build_struct_info(self.ast)
def test_parse_decl_node(self):
func_def_node = self.func_dictionary['main']
decl_list = func_def_node.body.block_items
decl_status = parse_decl_node(self.struct_info, decl_list[0])
self.assertEqual(decl_status.name, 'a')
self.assertEqual(decl_status.is_ptr_decl, False)
decl_status = parse_decl_node(self.struct_info, decl_list[1])
self.assertEqual(decl_status.name, 't1')
self.assertEqual(decl_status.is_ptr_decl, False)
decl_status = parse_decl_node(self.struct_info, decl_list[2])
self.assertEqual(decl_status.name, 's1')
self.assertEqual(decl_status.is_ptr_decl, False)
decl_status = parse_decl_node(self.struct_info, decl_list[3])
self.assertEqual(decl_status.name, 't2')
self.assertEqual(decl_status.is_ptr_decl, True)
def test_parse_decl_node_2(self):
func_def_node = self.func_dictionary['parse_decl_node_2']
decl_list = func_def_node.body.block_items
decl_status = parse_decl_node(self.struct_info, decl_list[0])
self.assertEqual(decl_status.name, 'arr')
self.assertEqual(decl_status.is_ptr_decl, True)
self.assertEqual(decl_status.struct_item, None)
def test_parse_decl_node_3(self):
func_def_node = self.func_dictionary['parse_decl_node_3']
decl_list = func_def_node.body.block_items
decl_status = parse_decl_node(self.struct_info, decl_list[0])
self.assertEqual(decl_status.name, 'a')
self.assertEqual(decl_status.is_ptr_decl, True)
self.assertEqual(decl_status.struct_item, None)
def test_parse_decl_node_4(self):
func_def_node = self.func_dictionary['parse_decl_node_4']
decl_list = func_def_node.body.block_items
decl_status = parse_decl_node(self.struct_info, decl_list[0])
self.assertEqual(decl_status.name, 't1')
self.assertEqual(decl_status.is_ptr_decl, True)
self.assertEqual(decl_status.struct_item.typedef_name, 'T1')
self.assertEqual(decl_status.struct_item.struct_name, 'S1')
def test_parse_decl_node_5(self):
func_def_node = self.func_dictionary['parse_decl_node_5']
decl_list = func_def_node.body.block_items
decl_status = parse_decl_node(self.struct_info, decl_list[0])
self.assertEqual(decl_status.name, 't2')
self.assertEqual(decl_status.is_ptr_decl, True)
self.assertEqual(decl_status.struct_item.typedef_name, 'T1')
self.assertEqual(decl_status.struct_item.struct_name, 'S1')
def test_parse_decl_node_6(self):
func_def_node = self.func_dictionary['parse_decl_node_6']
decl_list = func_def_node.body.block_items
decl_status = parse_decl_node(self.struct_info, decl_list[0])
self.assertEqual(decl_status.name, 't3')
self.assertEqual(decl_status.is_ptr_decl, True)
self.assertEqual(decl_status.struct_item.typedef_name, 'T1')
self.assertEqual(decl_status.struct_item.struct_name, 'S1')
if __name__ == '__main__':
googletest.main()
|