add_argument_names.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. import re, sys
  3. def fix_string(s):
  4. TYPE = re.compile('\s*(i[0-9]+|float|double|x86_fp80|fp128|ppc_fp128|\[\[.*?\]\]|\[2 x \[\[[A-Z_0-9]+\]\]\]|<.*?>|{.*?}|\[[0-9]+ x .*?\]|%["a-z:A-Z0-9._]+({{.*?}})?|%{{.*?}}|{{.*?}}|\[\[.*?\]\])(\s*(\*|addrspace\(.*?\)|dereferenceable\(.*?\)|byval\(.*?\)|sret|zeroext|inreg|returned|signext|nocapture|align \d+|swiftself|swifterror|readonly|noalias|inalloca|nocapture))*\s*')
  5. counter = 0
  6. if 'i32{{.*}}' in s:
  7. counter = 1
  8. at_pos = s.find('@')
  9. if at_pos == -1:
  10. at_pos = 0
  11. annoying_pos = s.find('{{[^(]+}}')
  12. if annoying_pos != -1:
  13. at_pos = annoying_pos + 9
  14. paren_pos = s.find('(', at_pos)
  15. if paren_pos == -1:
  16. return s
  17. res = s[:paren_pos+1]
  18. s = s[paren_pos+1:]
  19. m = TYPE.match(s)
  20. while m:
  21. res += m.group()
  22. s = s[m.end():]
  23. if s.startswith(',') or s.startswith(')'):
  24. res += f' %{counter}'
  25. counter += 1
  26. next_arg = s.find(',')
  27. if next_arg == -1:
  28. break
  29. res += s[:next_arg+1]
  30. s = s[next_arg+1:]
  31. m = TYPE.match(s)
  32. return res+s
  33. def process_file(contents):
  34. PREFIX = re.compile(r'check-prefix(es)?(=|\s+)([a-zA-Z0-9,]+)')
  35. check_prefixes = ['CHECK']
  36. result = ''
  37. for line in contents.split('\n'):
  38. if 'FileCheck' in line:
  39. m = PREFIX.search(line)
  40. if m:
  41. check_prefixes.extend(m.group(3).split(','))
  42. found_check = False
  43. for prefix in check_prefixes:
  44. if prefix in line:
  45. found_check = True
  46. break
  47. if not found_check or 'define' not in line:
  48. result += line + '\n'
  49. continue
  50. # We have a check for a function definition. Number the args.
  51. line = fix_string(line)
  52. result += line + '\n'
  53. return result
  54. def main():
  55. print(f'Processing {sys.argv[1]}')
  56. f = open(sys.argv[1])
  57. content = f.read()
  58. f.close()
  59. content = process_file(content)
  60. f = open(sys.argv[1], 'w')
  61. f.write(content)
  62. f.close()
  63. if __name__ == '__main__':
  64. main()