Skip to content

Commit ad5d5df

Browse files
committed
Adding a newer version of the refactoring script to Compiler/boot/refactor-fix-notifications.py
- It is now a python script - It now checks that code compiles after performing the operations - Only the matchcontinue-to-match is performed at the moment git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19941 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 31852a8 commit ad5d5df

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

Compiler/boot/CompileFile.mos

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ mkdir("build/tmp");
77
cd("build/tmp");
88
status:=OpenModelica.Scripting.generateSeparateCode(mainClass);
99
(numMessages,numErrors,numWarnings) := countMessages();
10+
messageFile := "../" + typeNameString(mainClass) + ".log";
1011
if numMessages > 0 or not status then
1112
status := false;
12-
print(intString(numErrors+numWarnings) + " errors out of " + intString(numMessages) + " messages:\n" + getErrorString(warningsAsErrors=true));
13-
exit(1);
13+
messages := getErrorString(warningsAsErrors=true);
14+
print(intString(numErrors+numWarnings) + " errors out of " + intString(numMessages) + " messages:\n" + messages);
15+
writeFile(messageFile, messages);
16+
if numErrors+numWarnings > 0 then
17+
exit(1);
18+
end if;
19+
else
20+
system("rm -f " + messageFile);
1421
end if;
1522
files:={typeNameString(mainClass) + ext for ext in {".c",".h","_records.c",".deps"}};getErrorString();
1623
{compareFilesAndMove(file,"../" + file) for file in files};
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python
2+
# Fix warnings in MetaModelica code by refactoring
3+
4+
import sys
5+
import os
6+
import os.path
7+
from pyparsing import *
8+
from optparse import OptionParser
9+
import subprocess
10+
11+
parser = OptionParser()
12+
(options,args) = parser.parse_args()
13+
14+
FILENAME = Regex("[^:]*")
15+
FILEINFO = (FILENAME + Suppress(":") + Word(nums) + Suppress(":") + Word(nums) + Suppress("-") + Word(nums) + Suppress(":") + Word(nums) + Suppress(Regex("[^]]*"))).setParseAction(
16+
lambda s,s2: {'fileName':s2[0],'startLine':int(s2[1]),'startCol':int(s2[2]),'endLine':int(s2[3]),'endCol':int(s2[4])})
17+
UNUSED_LOCAL = (Suppress("Notification: Unused local variable: ") + Word(alphanums + "_") + "." + StringEnd()).setParseAction(
18+
lambda s,s2: {'unused_local':s2[0]})
19+
USE_MATCH = Literal("Notification: This matchcontinue expression has no overlapping patterns and should be using match instead of matchcontinue.").setParseAction(
20+
lambda s,s2: {'mc_to_match':True})
21+
UNKNOWN = Suppress("Notification:")
22+
23+
NOTIFICATION = (Suppress("[") + FILEINFO + Suppress("]") + (UNUSED_LOCAL|USE_MATCH|UNKNOWN))
24+
25+
def runOMC(arg):
26+
try:
27+
res = subprocess.check_output(['../../build/bin/omc','+d=patternmAllInfo',arg],stderr=subprocess.STDOUT)
28+
except subprocess.CalledProcessError as e:
29+
print e.output,e
30+
raise
31+
32+
def infoStr(info):
33+
return "%s:%d:%d" % (info['fileName'],info['startLine'],info['endLine'])
34+
35+
def fixFile(stamp,logFile,moFile):
36+
runOMC(arg)
37+
try:
38+
log = open(logFile, 'r')
39+
except:
40+
return # It's ok; there were no messages
41+
mo = open(moFile, 'r')
42+
moContents = mo.readlines()
43+
moOriginalContents = moContents
44+
lst = [NOTIFICATION.parseString(line.strip()) for line in log.readlines()]
45+
len1 = len(lst)
46+
lst = [n for n in lst if n[0]['fileName'] == moFile]
47+
len2 = len(lst)
48+
lst.sort(key=lambda n: n[0]['fileName'] + str(n[0]['endLine']),reverse=True)
49+
50+
for n in lst:
51+
startLine = n[0]['startLine']
52+
endLine = n[0]['endLine']
53+
if len(n)==2 and n[1].has_key('unused_local'):
54+
pass
55+
#print "Unused local %s" % n
56+
#print moContents[endLine-1]
57+
elif len(n)==2 and n[1].has_key('mc_to_match'):
58+
success1 = False
59+
success2 = False
60+
while startLine < endLine:
61+
if moContents[startLine-1].count("matchcontinue") == 1:
62+
success1 = True
63+
break
64+
elif moContents[startLine-1].count("matchcontinue") == 0:
65+
startLine += 1
66+
else:
67+
msg = "Found 2 matchcontinue on the same line (%d): %s" (startLine,moContents[startLine-1].strip())
68+
break
69+
while success1 and endLine > startLine:
70+
if moContents[endLine-1].count("matchcontinue") == 1:
71+
success2 = True
72+
msg = "Success"
73+
break
74+
elif moContents[endLine-1].count("matchcontinue") == 0:
75+
startLine += 1
76+
else:
77+
msg = "Found 2 matchcontinue on the same line (%d): %s" (endLine,moContents[endLine-1].strip())
78+
break
79+
if success2:
80+
moContents[startLine-1] = moContents[startLine-1].replace("matchcontinue","match")
81+
moContents[endLine-1] = moContents[endLine-1].replace("matchcontinue","match")
82+
else:
83+
startLine = n[0]['startLine']
84+
endLine = n[0]['endLine']
85+
print "%s Matchcontinue to match: %s\n%6d: %s\n%6d: %s" % (infoStr(n[0]),msg,startLine,moContents[startLine-1].strip(),endLine,moContents[endLine-1].strip())
86+
mo.close()
87+
mo = open(moFile, 'w')
88+
mo.writelines(moContents)
89+
mo.close()
90+
try:
91+
runOMC(stamp)
92+
except:
93+
print 'Reverting all operations after failing to compile after performing operations on %s' % stamp
94+
mo.writelines(moOriginalContents)
95+
sys.exit(1)
96+
97+
def runStamp(arg):
98+
if not arg.endswith('.stamp.mos'):
99+
print('Expected all arguments to have suffix .stamp.mos')
100+
sys.exit(1)
101+
f = open(arg)
102+
moFile = os.readlink(f.readline().split('"')[1])
103+
logFile = arg.replace('.stamp.mos','.log')
104+
fixFile(arg,logFile,moFile)
105+
106+
for arg in args:
107+
runStamp(arg)

0 commit comments

Comments
 (0)