-
-
Save kritgpt/69bfc7ef78e1784716c693d531a58adf to your computer and use it in GitHub Desktop.
Python script to remove occurences of 'br undef' in LLVM tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# imports for input and regex | |
import sys | |
import re | |
# open each file passed as argument in a loop | |
for filename in sys.argv[1:]: | |
globalCount = 0 # used for numbering replacements in comments | |
filebuffer = [] # stores all lines before writing | |
localbuffer = [] | |
flag1 = 0 | |
brCount = 0 | |
prevLine = '' | |
# open and read the file | |
f = open(filename, 'r') | |
for line in f: | |
if "define" in line: | |
if flag1: | |
# change the function signature of the previous function to include the new arguments | |
pattern = re.compile("define(?P<returnType>.*)@(?P<functionName>.*)\((?P<args>.*)\)") | |
m = pattern.match(prevLine) | |
if(m != None): | |
comma = "," if m.group('args') != "" else "" | |
if(brCount > 0): | |
prevLine = re.sub("define (?P<returnType>.*) @(?P<functionName>.*)\((?P<args>.*)\)", "define \g<returnType> @\g<functionName>(\g<args>"+comma+" %s)" % ', '.join([r'i1 %replaceUndef'+str(x) for x in range(brCount)]),prevLine) | |
filebuffer.append(prevLine) | |
for b in localbuffer: | |
filebuffer.append(b) | |
prevLine = line | |
brCount = 0 | |
localbuffer = [] | |
flag1 = 1 | |
else: | |
# replace undef with a new variable in br statements | |
if re.match('.*br.*undef',line): | |
if re.match('^;',line.strip()): | |
# replace undef in comments | |
line = re.sub('undef', r'[[%replaceUndef'+str(globalCount)+':%.*]]' ,line) | |
globalCount+=1 | |
continue | |
line = re.sub('undef', r'%replaceUndef'+str(brCount) ,line) | |
brCount += 1 | |
localbuffer.append(line) | |
if flag1: | |
# change the function signature of the last function to include the new arguments | |
pattern = re.compile("define(?P<returnType>.*)@(?P<functionName>.*)\((?P<args>.*)\)") | |
m = pattern.match(prevLine) | |
if(m != None): | |
comma = "," if m.group('args') != "" else "" | |
if(brCount > 0): | |
prevLine = re.sub("define (?P<returnType>.*) @(?P<functionName>.*)\((?P<args>.*)\)", "define \g<returnType> @\g<functionName>(\g<args>"+comma+" %s)" % ', '.join([r'i1 %replaceUndef'+str(x) for x in range(brCount)]),prevLine) | |
filebuffer.append(prevLine) | |
for b in localbuffer: | |
filebuffer.append(b) | |
f.close() | |
# write the file | |
f_out = open(filename, 'w') | |
f_out.writelines(filebuffer) | |
f_out.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment