File: filA3.py

package info (click to toggle)
openmx 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd, stretch
  • size: 325,856 kB
  • ctags: 3,575
  • sloc: ansic: 152,655; f90: 2,080; python: 876; makefile: 675; sh: 25; perl: 18
file content (638 lines) | stat: -rw-r--r-- 13,987 bytes parent folder | download | duplicates (4)
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
#
# Usage : 
#  /lib/cpp (-I include_directory)  filename.c | python thisfile.py 
#
#
# Copyright(C) 2006 
# Hiori Kino 
# All rights reserved. 
#
# log 
#
# filA3.py:  Jan. 11, 2006
#
#

import sys
import string
import re

#---------------------

def variables_clear_at_level(level):
	global gvariables,gthisfile,glinenum
	_name_=", in variables_clear_at_level"
	rmlist=[]
	for x in gvariables:
		if x[0]>=level:
			rmlist.append(x)
			pointer_level= x[2]+x[4]
			alloc_level=x[5]
			flag=0
			for i in range(pointer_level):
				if alloc_level[i]!=0:
					flag=1
			if flag==1:
				if x[0]==0:
					print "_WARNING_",gthisfile,glinenum,"pointer is not deallocated",x,_name_
					print
				elif x[0]%10==0:
					print "_ERROR_",gthisfile,glinenum,"some of pointer is not deallocated",x,_name_
					print
				else:
					print "_WARNING_",gthisfile,glinenum,"pointer is passed to parent function",x ,_name_
					print
	for x in rmlist:
		try:
			gvariables.remove(x)
		except:
			print "_SERIOUS_ERROR_",gthisfile,glinenum,"can not remove",x,_name_
			print

#-------------------

def variables_add_a(indentlevel,kind,astlevel,name,blevel,funcname="",iarg=0):
        global gvariables,glinenum,gthisfile
	_name_=", in variables_add_a"

	if name==")":
		return

        for x in gvariables:
                if x[3]==name:
                        print "_WARINIG_","another definition",name, gvariables.index(x),x,_name_

        y = []
        for i in range(astlevel+blevel):
                y.append(0)
        x= [indentlevel,kind,astlevel,name,blevel,y,glinenum,gthisfile,funcname,iarg]
        print "variable definition ", x,len(gvariables)
        gvariables.append(x)

#------------------

def process_function_definition(s):
	global gindentlevel,gthisfile,glinenum
	_name_=", in process_function_definition"
	if s[0]=="extern":
		return

	if gindentlevel!=10:
		return

	print "function=",s

	ipos=-1
	for i in range(len(s)):
		if s[i]=="(":
			ipos=i
			break
	if ipos<0:
		return


	ast=0
	type=""
	funcname=""
	for i in range(ipos):
		if s[i]=="static":
			continue
		elif s[i]=="*":
			ast=ast+1
		else:
			if type=="":
				type=s[i]
			else:
				funcname=s[i]

#	print "type,ast,funcname=",type,"-",ast,"-",funcname
	if type!="" and funcname=="":
		funcname=type
		type="void"
	if type=="" or funcname=="":
		print "_SERIOUS_ERROR_",gthisfile,glinenum,"error in process_function_definition 1",_name_
		print
		abort(0)

	arglist=[]
	arg=[]
	for  i in range(len(s)):
		if i<=ipos:
			continue
		if s[i]==")":
			continue
		if s[i]=="{":
			continue
		if s[i]==",":
			if arg!=[]:
				arglist.append(arg)
				arg=[]
				continue
		arg.append(s[i])
	if arg!=[]:
		arglist.append(arg)
		
	print "arglist=",arglist	

	iarg=0
	for x in arglist:
		type=""
		name=""
		ast=0
		blevel=0
		blevel0=0
		for y in x:
			if y=="*":
				ast=ast+1
			if y=="[":
				blevel=blevel+1
				blevel0=blevel0+1
			if y=="]":
				blevel=blevel-1
			else:
				if blevel==0:
					if type=="":
						type=y
					else:
						name=y
		if type=="void" and name=="":
			continue
		if type=="" or name=="":
			print "_SERIOUS_ERROR","error in process_function_definition 2",_name
			print
			abort(2)
		variables_add_a(gindentlevel+1,type,ast,name,blevel0,funcname,iarg)
		iarg=iarg+1
	
	

#------------------

def process_analyze_eq(s):
	global gthisfile, gthisline,glinenum
	_name_=", in process_analyze_eq"
	if len(s)==1:
		return
	for i in range(len(s)-1):
		if s[i]=="=" and s[i+1]=="+":
			print s
			print gthisfile, glinenum,gthisline
			print "_WARNING_",gthisfile,glinenum,"possible += ?"
			print
		if s[i]=="=" and s[i+1]=="-":
			print s
			print gthisfile, glinenum,gthisline
	                print "_WARNING_",gthisfile,glinenum,"possible -= ?"
			print

#-----------------------

def process_vardef(s):
	global gindentlevel


	if s[0]=="if":
		return 1
	if s[0]=="for":
		return 1
	if s[0]=="else":
		return 1

	i=0
	for x in s:
		if x=="(":
			i=1
		elif x==")":
			if i==1:
				i=2
		elif x=="{":
			if i==2:
				i=3
	if i==3:
		# this is a function definition, maybe
		process_function_definition(s)
		return 2

	i=0
        for x in s:
                if x=="=":
                        i=1
                elif x=="+":
                        i=1
                elif x=="-":
                        i=1
                elif x=="(":
                        i=1
                elif x=="{":
                        i=1
                elif x=="}":
                        i=1
                elif x=="<":
                        i=1
                elif x==">":
                        i=1
        if i==1:
                return 1

        astlevel=0
        blevel=0
        lastkey=[]
        
	if s[0]=="case":
		return 1
        if s[0]=="typedef":
                return 1
        if s[0]=="return":
                return 1
        if s[0]=="static":
                x = s[0]
                s.remove(x)
        if s[0]=="extern":
                x = s[0]
                s.remove(x)

        if s[0]=="struct":
                print "connect struct",s
                x1=s[0]
                x2=s[1]
                s.remove(x1)
                s.remove(x2)
                x12 = string.join([x1,x2],".")
                s.insert(0,x12)

        if s[0]=="unsigned":
                print "connect unsigned",s
                x1=s[0]
                x2=s[1]
                s.remove(x1)
                s.remove(x2)
                x12 =string.join([x1,x2],".")
                s.insert(0,x12)

        if len(s)<=2:
                return 1
        i=0
        
        for j in range(len(s)):
                if j==0:
                        kind=s[j]
                        lastkey=s[j]
                if s[j]=="*":
                        astlevel=astlevel+1
                elif s[j]==";":
                        variables_add_a(gindentlevel,kind,astlevel,name,blevel)
                        i=2
                        astlevel=0
                        blevel=0
                elif s[j]==",":
                        variables_add_a(gindentlevel,kind,astlevel,name,blevel)
                        i=2
                        astlevel=0
                        blevel=0
                elif s[j]=="[":
                        blevel=blevel+1
                elif s[j]=="]":
                        nothing=0
                else:
                        if blevel==0:
                                name=s[j]

        return i

#------------------------

def malloc_check_pointerlevel_consistency(s):
	global gthisfile,glinenum
	if s==[]:
		return
	s1 = s[1]
	s2 = s[2]
	cast_pointer=s1[1]
	sizeof_pointer=s2[1]
	if cast_pointer-1 != sizeof_pointer:
	#	print gthisfile,glinenum,gthisline
		print "_ERROR_",gthisfile,glinenum,"cast_pointer-1 != sizeof_pointer"
		print

#---------------------------

def variables_find_defname(name):
	global gvariables,gthisfile,glinenum
	_name_=", in variables_find_defname"
	y=[]
	ylist=[]
	for x in gvariables:
		gname = x[3]
		if gname==name:
			y=x
			ylist.append(y)
	if len(ylist)>1:
		print "_WARNING_",gthisfile,glinenum,"more than 1 definition",name
		print
		for z in ylist:
			print z
	if y==[]:
		print "_ERROR_",gthisfile,glinenum,"no definition",name,_name_
		print

	return y	

#----------------------------

def malloc_check_var_consistency(s):
	global gvariables,gthisfile,glinenum
	_name_=", in malloc_check_var_consistency"
	if s==[]:
		return

	s0=s[0]
	varname=s0[0]
	defst=variables_find_defname(varname)
	if defst==[]:
		return
	def_pointerlevel=defst[2]+defst[4]	
	var_pointerlevel=s0[1]+s0[2]
	s1=s[1]
	cast_pointerlevel=s1[1]
	#print "pointerlevel=",def_pointerlevel,var_pointerlevel,cast_pointerlevel
	if def_pointerlevel != var_pointerlevel+cast_pointerlevel:
		print "_ERROR_",gthisfile,glinenum,"ecast level",varname,_name_
		print

	allocst=defst[5]
	if allocst[var_pointerlevel]==1:
		print "_ERROR_",gthisfile,glinenum,"pointer is already allocated",varname,_name_
		print
	allocst[var_pointerlevel]=1
	i = gvariables.index(defst)
	defst[5]=allocst
	gvariables[i]=defst
	#print "new=",gvariables[i]

#---------------------------------------

def process_malloc(s):

	mallocst=[]

	print s
	blevel=0
	astlevel=0
	havex=0
	for x in s:
		if x=="[":
			blevel=blevel+1
		elif x=="=":
			havex=1
			break
		elif x=="*":
			astlevel=astlevel+1
		elif x=="(":
			nothing=0
		elif x==")":
			nothing=0
		else:
			if blevel==0:
				name =x
		
	if havex==0:
		return mallocst

	left = [name,astlevel,blevel]
#	print "left=",left

	mallocst.append(left)

	astlevel=0
	start=0
	bstart=0
	name=""
	for x in s:
		if start==0:
			if x=="=":
				start=1
		elif start==1:
			if x=="(":
				bstart=bstart+1
			elif x=="*":
				if bstart==1:
					astlevel=astlevel+1
			elif x==")":
				bstart=bstart-1
				if name!="" and bstart==0:
					break
			else:
				name=x
#	print "cast=",[name,astlevel]

	mallocst.append([name,astlevel])

	mallocstart=0
	sizeofstart=0
	blevel=0
	astlevel=0
	name=""
	for x in s:
		if x=="malloc":
			mallocstart=1
		if mallocstart==1:
			if x=="sizeof":
				sizeofstart=1
		if mallocstart==1 and sizeofstart==1:
			if x=="(":
				blevel=blevel+1
			elif x==")":
				blevel=blevel-1
				if blevel==0:
					break
			elif x=="*":
				astlevel=astlevel+1
			else:
				name = x
				
#	print "sizeof=",[name,astlevel]

	mallocst.append([name,astlevel])

	return  mallocst

#---------------------------------------
def process_free(s):
        global gvariables,glinenum,gthisline,gthisfile
	_name_=", in process_free"
	print "s=",s
        start=0
        bstart=0
        ast=0
        name=""
        blevel=0
        for x in s:
                if x=="free":
                        start=1
                elif x=="(":
                        if start==1:
                                bstart=1
                if bstart>=1:
                        if x=="*":
                                ast=ast+1
                        elif x=="[":
                                blevel=blevel+1
                        elif x=="(":
                                nothing=0
                        elif x==")":
                                nothing=0
                        else:
                                if bstart==1:
                                        name=x
                                        bstart=2

	#print 1,name,ast+blevel
        status=[]
	x=variables_find_defname(name)
	if x==[]:
		print "_ERROR_",gthisfile,glinenum,"no definition",name,_name_
		print
		return []

	#print "found",x
        status=x[5]
	def_max_pointerlevel=x[2]+x[4]
	if def_max_pointerlevel<=ast+blevel:
		print "_ERROR_",gthisfile,glinenum,"more than pointer level",s,_name_
		print
		return []
        if status[ast+blevel]==1:
                status[ast+blevel]=0
                y=x
        elif status[ast+blevel]==0:
                print "_ERROR_",gthisfile,glinenum, "already deallocated", s,_name_
		print
                y=x
	#print "status=",status

        if status!=[]:
	#	print status
                i=gvariables.index(x)
                y[5]=status
                gvariables[i]=y
                #print "new status=",gvariables[i]

        return [ast,name,blevel]

	

#-----------------------------------------------

def processIt(s):
	global glinenum,gthisline,gthisfile

	process_analyze_eq(s)

	i = process_vardef(s)

	# i=2 is variable definition
	if i==2:
		return

	for x in s:
		if x=="malloc":
	#		print "malloc"
			print gthisfile,glinenum,gthisline
			mallocst=process_malloc(s)
			malloc_check_pointerlevel_consistency(mallocst)
			malloc_check_var_consistency(mallocst)
		elif x=="free":
	#		print "free"
			print gthisfile,glinenum,gthisline
			freest=process_free(s)

#---------------------------------------------------------------
#           main 
#---------------------------------------------------------------

try:
	global glinenum, gthisline,gthisfile,gindentlevel,gvariables
	gindentlevel=0
	glinenum=0
	gvariables=[]
	x=[]
	stringmode=0
	gthisfile=""
	while 1:
		s=sys.stdin.readline()
		gthisline=re.sub('\n','',s)
		if s=="":
			print "leave",0,"at",glinenum,gthisfile,len(gvariables)
			variables_clear_at_level(0)
			print "File",gthisfile,": processed successfully"
			break
		glinenum=glinenum+1
		#print "linenum=",glinenum,s
		if s[0]=="#":
			s1=string.split(s)
			#glinenum=string.atoi(s1[1])-1
			glinenum=int(s1[1])-1
			gthisfile=s1[2]
			#print "linenum,file=",glinenum,gthisfile
			continue
                s = re.sub("\{"," { ",s)
                s = re.sub("\}"," } ",s)
                s = re.sub("\("," ( ",s)
                s = re.sub("\)"," ) ",s)
                s = re.sub("="," = ",s)
                s = re.sub("\+"," + ",s)
                s = re.sub("\*"," * " ,s)
                s = re.sub("\["," [ ",s)
                s = re.sub("\]"," ] ",s)
                s = re.sub(","," , ",s)
                s = re.sub(";"," ; ", s)
                s = re.sub(">"," > ",s)
                s = re.sub("<"," < ",s)

		s1=string.split(s)

		#exception
		if len(s1)>6:
			# extern void free ( ... ) ; 
			if s1[0]=="extern" and s1[1]=="void":
				continue

		for y in s1:
			if y[0]=='\"' and stringmode==0:
			        stringmode=1
			if stringmode==1:
				sm=[]
				sm.append(y)
				if len(y)>1 and y[len(y)-1]=='\"':
					stringmode=0
					x.append(y)
					sm=[]
				else:
					stringmode=2
			elif stringmode==2:
				sm.append(y)
				if y[len(y)-1]=='\"':
					stringmode=0
					smj = string.join(sm)
					x.append(smj)
			else: 
				x.append(y)
				if y==";":
					processIt(x)
					x=[]
				elif y=="{":
					gindentlevel=gindentlevel+10
					#print "enter",gindentlevel,"at",glinenum,gthisfile,len(gvariables)
					processIt(x)
					x=[]
				elif y=="}":
					processIt(x)
					#print "leave",gindentlevel,"at",glinenum,gthisfile,len(gvariables)
					variables_clear_at_level(gindentlevel)
					x=[]
					gindentlevel=gindentlevel-10

except:
	_name_=", in main"
	print "_SERIOUS_ERROR_ aborted at", glinenum,gthisfile,_name_