mirror of
https://github.com/cp2k/cp2k.git
synced 2026-07-29 06:35:28 -04:00
* only nullified pointers now removed
* moduleN, routineN and routineP now automatically set * replacer now activated by default and replaces routine_name and module_name with routineN and moduleN svn-origin-rev: 4660
This commit is contained in:
parent
f3f6e11535
commit
bfb6d2b0d3
3 changed files with 105 additions and 25 deletions
|
|
@ -27,7 +27,7 @@ def readFortranLine(infile):
|
|||
lines=[]
|
||||
continuation=0
|
||||
while 1:
|
||||
line=infile.readline()
|
||||
line=infile.readline().replace("\t",8*" ")
|
||||
if not line: break
|
||||
m=lineRe.match(line)
|
||||
if not m or m.span()[1]!=len(line):
|
||||
|
|
@ -421,6 +421,7 @@ def cleanDeclarations(routine,logFile=sys.stdout):
|
|||
removes unused variables"""
|
||||
global rVar
|
||||
commentToRemoveRe=re.compile(r" *! *(?:interface|arguments|parameters|locals?|\** *local +variables *\**|\** *local +parameters *\**) *$",re.IGNORECASE)
|
||||
nullifyRe=re.compile(r" *nullify *\(([^()]+)\) *\n?",re.IGNORECASE|re.MULTILINE)
|
||||
|
||||
if not routine['kind']: return
|
||||
if (routine['core'] and
|
||||
|
|
@ -433,6 +434,8 @@ def cleanDeclarations(routine,logFile=sys.stdout):
|
|||
return
|
||||
try:
|
||||
rest="".join(routine['strippedCore']).lower()
|
||||
nullifys="".join(nullifyRe.findall(rest))
|
||||
rest=nullifyRe.sub("",rest)
|
||||
paramDecl=[]
|
||||
decls=[]
|
||||
for d in routine['parsedDeclarations']:
|
||||
|
|
@ -447,9 +450,19 @@ def cleanDeclarations(routine,logFile=sys.stdout):
|
|||
paramDecl.append(d)
|
||||
else:
|
||||
decls.append(d)
|
||||
|
||||
|
||||
sortDeclarations(paramDecl)
|
||||
sortDeclarations(decls)
|
||||
for d in paramDecl:
|
||||
for i in xrange(len(d['vars'])):
|
||||
v=d['vars'][i]
|
||||
m=varRe.match(v)
|
||||
lowerV=m.group("var").lower()
|
||||
if lowerV=="routinen":
|
||||
d['vars'][i]="routineN = '"+routine['name']+"'"
|
||||
elif lowerV=="routinep":
|
||||
d['vars'][i]="routineP = moduleN//':'//routineN"
|
||||
|
||||
|
||||
if routine['arguments']:
|
||||
routine['lowercaseArguments']=map(lambda x:x.lower(),routine['arguments'])
|
||||
|
|
@ -464,7 +477,8 @@ def cleanDeclarations(routine,logFile=sys.stdout):
|
|||
localD['vars']=[]
|
||||
argD=None
|
||||
for v in d['vars']:
|
||||
lowerV=varRe.match(v).group("var").lower()
|
||||
m=varRe.match(v)
|
||||
lowerV=m.group("var").lower()
|
||||
if lowerV in routine['lowercaseArguments']:
|
||||
argD={}
|
||||
argD.update(d)
|
||||
|
|
@ -479,6 +493,11 @@ def cleanDeclarations(routine,logFile=sys.stdout):
|
|||
if (pos!=-1):
|
||||
localD['vars'].append(v)
|
||||
else:
|
||||
if findWord(lowerV,nullifys)!=-1:
|
||||
if not rmNullify(lowerV,routine['core']):
|
||||
raise SyntaxError(
|
||||
"could not remove nullify of "+lowerV+
|
||||
" as expected, routine="+routine['name'])
|
||||
logFile.write("removed var %s in routine %s\n" %
|
||||
(lowerV,routine['name']))
|
||||
rVar+=1
|
||||
|
|
@ -530,7 +549,65 @@ def cleanDeclarations(routine,logFile=sys.stdout):
|
|||
logFile.write("**** exception cleaning routine "+routine['name']+" ****")
|
||||
logFile.write("parsedDeclartions="+str(routine['parsedDeclarations']))
|
||||
raise
|
||||
|
||||
def rmNullify(var,strings):
|
||||
removed=0
|
||||
var=var.lower()
|
||||
nullifyRe=re.compile(r" *nullify *\(", re.IGNORECASE)
|
||||
nullify2Re=re.compile(r"(?P<nullif> *nullify *\()(?P<vars>[^()!&]+)\)",re.IGNORECASE)
|
||||
|
||||
for i in xrange(len(strings)-1,-1,-1):
|
||||
line=strings[i]
|
||||
comments=[]
|
||||
if nullifyRe.match(line) and findWord(var,line)!=-1:
|
||||
core=""
|
||||
comments=[]
|
||||
for l in line.splitlines():
|
||||
pos=l.find("&")
|
||||
pos2=l.find("!")
|
||||
if pos==-1:
|
||||
if pos2==-1:
|
||||
core+=l
|
||||
else:
|
||||
core+=l[:pos2]
|
||||
comments.append(l[pos2:]+"\n")
|
||||
else:
|
||||
core+=l[:pos]
|
||||
if pos2!=-1:
|
||||
comments.append(l[pos2:]+"\n")
|
||||
m=nullify2Re.match(core)
|
||||
if not m:
|
||||
raise SyntaxError("could not match nullify to "+repr(core)+
|
||||
"in"+repr(line))
|
||||
allVars=[]
|
||||
vars=m.group("vars")
|
||||
v= map(string.strip,vars.split(","))
|
||||
removedNow=0
|
||||
for j in xrange(len(v)-1,-1,-1):
|
||||
if v[j].lower()==var:
|
||||
del v[j]
|
||||
removedNow=1
|
||||
if removedNow:
|
||||
if len(v)==0:
|
||||
if not comments:
|
||||
del strings[i]
|
||||
else:
|
||||
strings[i]="".join(comments)
|
||||
else:
|
||||
for j in xrange(len(v)-1):
|
||||
v[j]+=", "
|
||||
v[-1]+=")"
|
||||
newS=StringIO()
|
||||
v.insert(0,m.group("nullif"))
|
||||
writeInCols(v,len(v[0])-len(v[0].lstrip())+5,77,0,newS)
|
||||
newS.write("\n")
|
||||
if comments:
|
||||
for c in comments:
|
||||
newS.write(c)
|
||||
strings[i]=newS.getvalue()
|
||||
removed+=1
|
||||
return removed
|
||||
|
||||
def parseUse(inFile):
|
||||
"""Parses the use statements in inFile
|
||||
The parsing stops at the first non use statement.
|
||||
|
|
@ -713,7 +790,16 @@ def cleanUse(modulesDict,rest,implicitUses=None,logFile=sys.stdout):
|
|||
map(lambda x:x+"\n",modules[i]['comments']))
|
||||
del modules[i]
|
||||
|
||||
def rewriteFortranFile(inFile,outFile,logFile=sys.stdout):
|
||||
def resetModuleN(moduleName,lines):
|
||||
"resets the moduleN variable to the module name in the lines lines"
|
||||
moduleNRe=re.compile(r".*:: *moduleN *= *(['\"])[a-zA-Z_0-9]+\1",
|
||||
flags=re.IGNORECASE)
|
||||
for i in xrange(len(lines)):
|
||||
lines[i]=moduleNRe.sub(
|
||||
" CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = '"+moduleName+"'",
|
||||
lines[i])
|
||||
|
||||
def rewriteFortranFile(inFile,outFile,logFile=sys.stdout,orig_filename=None):
|
||||
"""rewrites the use statements and declarations of inFile to outFile.
|
||||
It sorts them and removes the repetitions."""
|
||||
import os.path
|
||||
|
|
@ -730,7 +816,8 @@ def rewriteFortranFile(inFile,outFile,logFile=sys.stdout):
|
|||
outFile.write(line)
|
||||
m=moduleRe.match(line)
|
||||
if m:
|
||||
if (m.group('moduleName')!=os.path.basename(inFile.name)[0:-2]) :
|
||||
if not orig_filename: orig_filename=inFile.name
|
||||
if (m.group('moduleName')!=os.path.basename(orig_filename)[0:-2]) :
|
||||
raise SyntaxError("Module name is different from filename ("+
|
||||
m.group('moduleName')+
|
||||
"!="+os.path.basename(inFile.name)[0:-2]+")")
|
||||
|
|
@ -741,6 +828,8 @@ def rewriteFortranFile(inFile,outFile,logFile=sys.stdout):
|
|||
coreLines.append(modulesDict['postLine'])
|
||||
routine=parseRoutine(inFile)
|
||||
coreLines.extend(routine['preRoutine'])
|
||||
if m:
|
||||
resetModuleN(m.group('moduleName'),routine['preRoutine'])
|
||||
routines.append(routine)
|
||||
while routine['kind']:
|
||||
routine=parseRoutine(inFile)
|
||||
|
|
|
|||
|
|
@ -64,19 +64,21 @@ def prettifyFile(infile,normalize_use=1, upcase_keywords=1,
|
|||
|
||||
does not close the input file"""
|
||||
ifile=infile
|
||||
orig_filename=infile.name
|
||||
tmpfile=None
|
||||
try:
|
||||
if normalize_use:
|
||||
if replace:
|
||||
tmpfile2=os.tmpfile()
|
||||
normalizeFortranFile.rewriteFortranFile(ifile,tmpfile2,logFile)
|
||||
replacer.replaceWords(ifile,tmpfile2,logFile=logFile)
|
||||
tmpfile2.seek(0)
|
||||
if tmpfile:
|
||||
tmpfile.close()
|
||||
tmpfile=tmpfile2
|
||||
ifile=tmpfile
|
||||
if replace:
|
||||
if normalize_use:
|
||||
tmpfile2=os.tmpfile()
|
||||
replacer.replaceWords(ifile,tmpfile2,logFile)
|
||||
normalizeFortranFile.rewriteFortranFile(ifile,tmpfile2,logFile,
|
||||
orig_filename=orig_filename)
|
||||
tmpfile2.seek(0)
|
||||
if tmpfile:
|
||||
tmpfile.close()
|
||||
|
|
@ -166,7 +168,7 @@ def prettfyInplace(fileName,bkDir="preprettify",normalize_use=1,
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
defaultsDict={'upcase':1,'normalize-use':1,'replace':0,
|
||||
defaultsDict={'upcase':1,'normalize-use':1,'replace':1,
|
||||
'interface-dir':None,
|
||||
'backup-dir':'preprettify'}
|
||||
usageDesc=("usage:\n"+sys.argv[0]+ """
|
||||
|
|
|
|||
|
|
@ -4,23 +4,11 @@ import re
|
|||
import sys
|
||||
|
||||
repl={
|
||||
'force_control':'force_env_types',
|
||||
'fragment':'subsys',
|
||||
'cp_fragment_types':'cp_subsystem_types',
|
||||
'cp_fragment_type':'cp_subsystem_type',
|
||||
'cp_fragment_p_type':'cp_subsystem_p_type',
|
||||
'fragment_create':'cp_subsys_create',
|
||||
'fragment_retain':'cp_subsys_retain',
|
||||
'fragment_release':'cp_subsys_release',
|
||||
'fragment_get':'cp_subsys_get',
|
||||
'fragment_set':'cp_subsys_set',
|
||||
'qs_geoopt':'geo_opt',
|
||||
'qs_md':'md_run',
|
||||
'md_qs_energies':'md_energies',
|
||||
'routine_name':'routineN',
|
||||
'module_name':'moduleN'
|
||||
}
|
||||
|
||||
specialRepl=None
|
||||
# { re.compile(r"%fragment",flags=re.IGNORECASE):r"%subsys" }
|
||||
# { re.compile(r"(.*:: *moduleN) *= *(['\"])[a-zA-Z_0-9]+\2",flags=re.IGNORECASE):r"character(len=*), parameter :: moduleN = '__MODULE_NAME__'" }
|
||||
|
||||
def replaceWords(infile,outfile,replacements=repl,
|
||||
specialReplacements=specialRepl,
|
||||
|
|
@ -32,6 +20,7 @@ def replaceWords(infile,outfile,replacements=repl,
|
|||
"""
|
||||
lineNr=0
|
||||
nonWordRe=re.compile(r"(\W+)")
|
||||
|
||||
while 1:
|
||||
line= infile.readline()
|
||||
lineNr=lineNr+1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue