PageRenderTime 79ms CodeModel.GetById 19ms RepoModel.GetById 3ms app.codeStats 0ms

/TOOLS/SrcFragments/ANALYZEnotusednow/analyze.py

http://github.com/tkotani/ecalj
Python | 422 lines | 417 code | 1 blank | 4 comment | 0 complexity | 313b6e7ff53484b5dfc11c33b78d2e13 MD5 | raw file
  1. #!/usr/bin/env python
  2. # This routine checks module-dependency in fortran90 and compile them in right order.
  3. #
  4. import os
  5. import sys
  6. import string
  7. import re
  8. import glob
  9. #---------------------
  10. def rsp(dat):
  11. dat= string.lower(re.sub(' ' ,'',dat))
  12. return dat
  13. def connect(alist):
  14. result=''
  15. for lll in alist:
  16. result = result + ' ' + lll
  17. return result
  18. def uniqlist(input): # uniq
  19. output = []
  20. for i in input:
  21. if not i in output:
  22. output.append(i)
  23. return output
  24. def skipc(flines,fin): # remove comment, treat continuous lines,
  25. # and make list [number
  26. re_nocomm = re.compile('^\ *subroutine', re.IGNORECASE)
  27. re_char = re.compile('("[^"]*")|(\'[^\']*\')')
  28. il=0
  29. fff =[]
  30. init=1
  31. #ixxx=0
  32. for fl in flines:
  33. #ixxx=ixxx+1
  34. #print ixxx, fl
  35. il=il+1
  36. if(not re.search('( |#)',fl[0:1])): continue # skip comment line
  37. flx = re.split('!',fl)[0]
  38. if(re.search('#',fl[0:1]) or re.search(' ',fl[5:6]) ):
  39. if(init==0):
  40. flo =string.lower(flo)
  41. flo = re_char.sub('@@@',flo) #
  42. flo = re.sub('^ *','',flo)
  43. flo = re.sub(' +',' ',flo)
  44. flo = re.sub('end *do','enddo',flo, re.IGNORECASE)
  45. flo = re.sub('end *if','endif',flo, re.IGNORECASE)
  46. flo = re.sub('end *interface','endinterface',flo, re.IGNORECASE)
  47. fff.append([fin,ilo,flo,floo])
  48. else:
  49. init=0
  50. flo = flx #new sentence start
  51. floo= fl +'\n' #new sentence start
  52. ilo= il
  53. else: #continuous line
  54. flo= flo + flx[6:]
  55. floo= floo + fl + '\n'
  56. flo = re.sub('^ *','',flo)
  57. flo = re.sub(' +',' ',flo)
  58. fff.append([fin,ilo,string.lower(flo),floo])
  59. # for x in fff:
  60. # print x[0:3]
  61. # sys.exit()
  62. return fff
  63. re_mod = re.compile( '^\ *module(?!( *procedure))', re.IGNORECASE)
  64. rcall = re.compile( 'call', re.IGNORECASE)
  65. re_function = re.compile( \
  66. '((^\ *(complex|real|double|character|integer|logical)[^!]* *function))', re.IGNORECASE)
  67. re_function2 = re.compile( \
  68. '((^\ *function))', re.IGNORECASE)
  69. re_end = re.compile( '((^\ *end( |\Z)+(?!(if|select))))', re.IGNORECASE)
  70. re_end_mod = re.compile( '((^\ *end *module))', re.IGNORECASE)
  71. re_contains = re.compile( '((^\ *contains))', re.IGNORECASE)
  72. re_subroutine= re.compile( '^\ *subroutine', re.IGNORECASE)
  73. re_blockdata = re.compile( '^\ *blockdata', re.IGNORECASE)
  74. re_type = re.compile( '^\ *type +(?!\()', re.IGNORECASE)
  75. re_call = re.compile( '^\ *call', re.IGNORECASE)
  76. re_use = re.compile( '^\ *use', re.IGNORECASE)
  77. re_program = re.compile( '^\ *program', re.IGNORECASE)
  78. re_entry = re.compile( '^\ *entry', re.IGNORECASE)
  79. reff = re.compile( 'function', re.IGNORECASE)
  80. rcal = re.compile( 'call', re.IGNORECASE)
  81. ruse = re.compile( 'use *', re.IGNORECASE)
  82. rsub = re.compile( 'subroutine', re.IGNORECASE)
  83. rmod = re.compile( 'module', re.IGNORECASE)
  84. rprog = re.compile( 'program', re.IGNORECASE)
  85. rent = re.compile( 'entry', re.IGNORECASE)
  86. rblock = re.compile( 'blockdata', re.IGNORECASE)
  87. rtype = re.compile( 'type', re.IGNORECASE)
  88. nargv = len(sys.argv) -1
  89. if(nargv==0) :
  90. print 'usage: analyze.py {name of top routine} source files ... (order them as you link them).'
  91. sys.exit()
  92. print '######### analyze.py ###### Generate tree for ####### ' ,sys.argv[1]
  93. src = connect(sys.argv[2:])
  94. #print '################## src =', src
  95. #fff = os.system(zzz+ ' > ' + tmp)
  96. #sys.exit()
  97. ffiles=src
  98. # tmp = 'makedat'
  99. # oxx = string.split(open(tmp,'rt').read(),'\n')
  100. # ### Find all fortran files.
  101. # ffiles=[]
  102. # #ix=0
  103. # for iline in oxx:
  104. # # ix=ix+1
  105. # # if(iline[0:3]!='ifc'): continue
  106. # offf = re.split(' +',iline)
  107. # for iff in offf[1:]:
  108. # if(re.search('\.o',iff)):
  109. # ffile = re.sub('\.o','.F',iff)
  110. # ffiles.append(ffile)
  111. # # print ffile
  112. #ffiles = uniqlist(ffiles)
  113. ffiles = re.split(' *',ffiles)[1:]
  114. print ffiles
  115. ####################################################
  116. # test
  117. #ffiles=[]
  118. #ffiles= ['../gwsrc/genallcf_mod.F']
  119. #ffiles= ['../gwsrc/tetwt4.F','../main/h_uumatrix.m.F','../gwsrc/pointops.F','../gwsrc/genallcf_mod.F','../gwsrc/readeigen.F', '../main/hx0fp0.m.F']
  120. #ffiles= ['../gwsrc/tetwt4.F']#,'../gwsrc/pointops.F','../main/h_uumatrix.m.F']#,'../gwsrc/genallcf_mod.F','../gwsrc/readeigen.F', '../main/hx0fp0.m.F']
  121. #ffiles = re.split(' +',ffiles)
  122. #print ffiles
  123. ####################################################
  124. ### skip comments ##############################
  125. #fall=[]
  126. sdef={}
  127. mdef={}
  128. for ffilein in ffiles:
  129. print
  130. print ffilein ," ------------------------"
  131. flines = string.split(open(ffilein,'rt').read(),'\n')
  132. flines = skipc(flines,ffilein) #flines are list
  133. ### find subroutines and range
  134. #for ff in fall: #ff contains line data for each file
  135. level=0
  136. calllist=[]
  137. slines=[]
  138. sstack=[]
  139. fdat= flines[0]
  140. fff = ['main',fdat[0],fdat[1],fdat[3]]
  141. sstack.append(fff)
  142. # fdat[0] FileNM
  143. # fdat[1] line number
  144. # fdat[2] line (comment removed)
  145. # fdat[3] line data
  146. for fdat in flines:
  147. fline =fdat[2]
  148. num = fdat[1]
  149. slines.append([num,fline])
  150. # slines[num]=fline
  151. # print fline
  152. if( re_mod.search(fline)):
  153. level=level+1
  154. subname= re.sub('^ *','',rmod.split(fline,maxsplit=1)[1])
  155. subname= re.split("( |^Z)",subname)[0]
  156. fff= ['mod: '+subname,fdat[0],fdat[1],fdat[3]]
  157. sstack.append(fff)
  158. elif(re_end_mod.search(fline)): #module
  159. level=level-1
  160. fff = sstack.pop()
  161. subname = string.lower(fff[0]) #[5:]
  162. mdef[subname]= fff[1:3] + [fdat[1]] + [fff[3]] +[''] +['']#add range end
  163. elif(re_program.search(fline) ):
  164. level=level+1
  165. subname= string.split( rprog.split(fline,maxsplit=1)[1]," " )[1]
  166. subname= rsp(subname)
  167. fff= ['mai: '+subname,fdat[0],fdat[1],fdat[3]]
  168. sstack.append(fff)
  169. elif(re_subroutine.search(fline) ):
  170. level=level+1
  171. subname= string.split( rsub.split(fline,maxsplit=1)[1],"(" )[0]
  172. subname= rsp(subname)
  173. fff= ['sub: '+subname,fdat[0],fdat[1],fdat[3]]
  174. sstack.append(fff)
  175. elif(re_entry.search(fline) ):
  176. level=level
  177. subname= string.split( rent.split(fline,maxsplit=1)[1],"(" )[0]
  178. subname= rsp(subname)
  179. fff= ['sub: '+subname,fdat[0],fdat[1],fdat[3]]
  180. sstack.append(fff)
  181. elif(re_function.search(fline) or re_function2.search(fline) ):
  182. level=level+1
  183. subname= string.split( reff.split(fline,maxsplit=1)[1],"(" )[0]
  184. subname= rsp(subname)
  185. fff= ['fun: '+subname,fdat[0],fdat[1],fdat[3]]
  186. sstack.append(fff)
  187. elif(re_end.search(fline)):
  188. level=level-1
  189. fff = sstack.pop()
  190. # print ' fline =',fline
  191. # print ' sstack =',fff
  192. if(sdef.has_key(fff[0])):
  193. fff[0]=fff[0]+'+'
  194. while(1) :
  195. if(sdef.has_key(fff[0])):
  196. fff[0]=fff[0]+'+'
  197. continue
  198. break
  199. # print ' subroutine:: ',fff[0],sdef[fff[0]]
  200. # print ' subroutine:: ',[fdat[0],fdat[1],fdat[3]]
  201. # print " ERROR: duplicated subroutine name ..."
  202. # for x in sdef:
  203. # print x
  204. # sys.exit()
  205. subname = string.lower(fff[0]) #[5:]
  206. sdef[subname]= fff[1:3] + [fdat[1]] + [fff[3]] + [calllist] + [slines] #add range end
  207. slines=[]
  208. calllist=[]
  209. elif(re_call.search(fline)):
  210. subname= rsp( string.split( rcall.split(fline,maxsplit=1)[1],"(" )[0] )
  211. fff= [subname,fdat[0],fdat[1],fdat[3]]
  212. calllist.append(fff)
  213. elif(re_use.search(fline)):
  214. subname= re.sub('^ *','',ruse.split(fline,maxsplit=1)[1])
  215. subname= rsp( re.split("(,|^Z| )", subname)[0] )
  216. fff= [subname,fdat[0],fdat[1],fdat[3]]
  217. calllist.append(fff)
  218. print fff
  219. elif(re_blockdata.search(fline) ):
  220. level=level+1
  221. subname= rsp( re.split("(\ |\n)", rblock.split(fline,maxsplit=1)[1])[0] )
  222. fff= ['block: '+subname,fdat[0],fdat[1],fdat[3]]
  223. sstack.append(fff)
  224. elif(re_type.search(fline) ):
  225. level=level+1
  226. subname= rsp( re.split("(\ |\n)", rtype.split(fline,maxsplit=1)[1])[0] )
  227. fff= ['type: '+subname,fdat[0],fdat[1],fdat[3]]
  228. sstack.append(fff)
  229. else:
  230. continue
  231. # print (' %5i ' % fdat[1]) + ( '%2i ' % level),fdat[2]
  232. continue
  233. if( re_contains.search(fline)): level=level+1
  234. ##########################################
  235. # print '################## module ##########################'
  236. # for i in mdef:
  237. # print i+' : ',mdef[i][0:3]
  238. # print mdef[i][3]
  239. # print 'def@ ', i, mdef[i][0], mdef[i][1], mdef[i][2]
  240. sdef.update(mdef)
  241. skeys=sdef.keys()
  242. skeys.sort()
  243. ### find functions #######################
  244. fun=[]
  245. rfun='fjpadpqe341padfeh=>this is for rfun is empty case'
  246. init=1
  247. for k in sdef.keys():
  248. if(k[0:5]=='fun: '):
  249. fun = fun + [k[5:]]
  250. if(init==1):
  251. aa=''
  252. init=0
  253. else:
  254. aa='|'
  255. rrr = re.sub( '\+','',k[5:] )
  256. rfun = rfun + aa + rrr
  257. if(k[0:5]=='mod: '):
  258. fun = fun + [k[5:]]
  259. if(init==1):
  260. aa=''
  261. init=0
  262. else:
  263. aa='|'
  264. rrr = re.sub( '\+','',k[5:] )
  265. rfun = rfun + aa + rrr
  266. if(k[0:6]=='type: '):
  267. fun = fun + [k[6:]]
  268. if(init==1):
  269. aa=''
  270. init=0
  271. else:
  272. aa='|'
  273. rrr = re.sub( '\+','',k[6:] )
  274. rfun = rfun + aa + rrr
  275. if(k[0:7]=='block: '):
  276. fun = fun + [k[7:]]
  277. if(init==1):
  278. aa=''
  279. init=0
  280. else:
  281. aa='|'
  282. rrr = re.sub( '\+','',k[7:] )
  283. rfun = rfun + aa + rrr
  284. rfun = '(?<=\W)('+ rfun +')(?=\W)'
  285. rfff = re.compile(rfun)
  286. print ' rfun=',rfun
  287. #sys.exit()
  288. ###########find function calls ###############################
  289. for i in skeys:
  290. filen= sdef[i][0]
  291. slines=sdef[i][5]
  292. # print 'fff ',filen, slines
  293. # init = sdef[i][1]
  294. # end = sdef[i][2]
  295. # print 'def@ ', i,filen,init,end
  296. # for ifun in fun:
  297. # slines = re.sub(ifun,'~'+ifun+'~',slines)
  298. for ix in slines:
  299. num= ix[0]
  300. # aaa=re.findall(rfun,ix[1])
  301. aaa= rfff.findall(ix[1])
  302. output = []
  303. # print 'aaa',ix
  304. for aaax in aaa:
  305. if(not (aaax==i[5:])):
  306. output.append(aaax)
  307. if(not (output==[])):
  308. for iout in output:
  309. # print num,iout,ix[1]
  310. fff= [iout,filen,num]
  311. sdef[i][4].append(fff)
  312. # This is full data
  313. # for line in sdef[i][5]:
  314. # print line # iline = [line number, line text]
  315. # print sdef[i][3],
  316. # for ic in sdef[i][4]:
  317. # print 'cal@ ', i,' ',ic[0],ic[1],ic[2]
  318. #sys.exit()
  319. ### Write definition and call. ####
  320. print '#############################################'
  321. for i in skeys:
  322. filen= sdef[i][0]
  323. init = sdef[i][1]
  324. end = sdef[i][2]
  325. print
  326. print 'def@ ', i,filen,init,end
  327. # This is full data
  328. # for line in sdef[i][5]:
  329. # print line # iline = [line number, line text]
  330. # print sdef[i][3],
  331. for ic in sdef[i][4]:
  332. print 'cal@ ', i,' ',ic[0],ic[1],ic[2]
  333. ### remove header from sdef ############
  334. for i in skeys:
  335. ii = i[5:]
  336. sss = sdef[i]
  337. del sdef[i]
  338. sdef[ii]= sss
  339. ### tree generation ####################
  340. i=sys.argv[1]
  341. once = 1
  342. print '###### call tree for ', i,' ##########'
  343. calling1 = sdef[i][4]
  344. for ic1 in calling1:
  345. print 'tree0 ', ic1[0],ic1[1],ic1[2]
  346. ir1 = ic1[0]
  347. try:
  348. calling2= sdef[ir1][4]
  349. except:
  350. continue
  351. icalled2=[]
  352. for ic2 in calling2:
  353. ir2 = ic2[0]
  354. if once==1 and ir2 in icalled2: continue
  355. icalled2.append(ir2)
  356. print 'tree1 | ',ic2[0],ic2[1],ic2[2]
  357. try:
  358. calling3= sdef[ir2][4]
  359. except:
  360. continue
  361. icalled3=[]
  362. for ic3 in calling3:
  363. ir3 = ic3[0]
  364. if once==1 and ir3 in icalled3: continue
  365. icalled3.append(ir3)
  366. print 'tree2 | | ',ic3[0],ic3[1],ic3[2]
  367. try:
  368. calling4= sdef[ir3][4]
  369. except:
  370. continue
  371. icalled4 = []
  372. for ic4 in calling4:
  373. ir4 = ic4[0]
  374. if once==1 and ir4 in icalled4: continue
  375. icalled4.append(ir4)
  376. print 'tree3 | | | ',ic4[0],ic4[1],ic4[2]
  377. try:
  378. calling5= sdef[ir4][4]
  379. except:
  380. continue
  381. sys.exit()