|
| 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