xref: /llvm-project/llvm/utils/add_argument_names.py (revision b71edfaa4ec3c998aadb35255ce2f60bba2940b0)
1#!/usr/bin/env python3
2import re, sys
3
4
5def fix_string(s):
6    TYPE = re.compile(
7        '\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*'
8    )
9
10    counter = 0
11    if "i32{{.*}}" in s:
12        counter = 1
13
14    at_pos = s.find("@")
15    if at_pos == -1:
16        at_pos = 0
17
18    annoying_pos = s.find("{{[^(]+}}")
19    if annoying_pos != -1:
20        at_pos = annoying_pos + 9
21
22    paren_pos = s.find("(", at_pos)
23    if paren_pos == -1:
24        return s
25
26    res = s[: paren_pos + 1]
27    s = s[paren_pos + 1 :]
28
29    m = TYPE.match(s)
30    while m:
31        res += m.group()
32        s = s[m.end() :]
33        if s.startswith(",") or s.startswith(")"):
34            res += f" %{counter}"
35            counter += 1
36
37        next_arg = s.find(",")
38        if next_arg == -1:
39            break
40
41        res += s[: next_arg + 1]
42        s = s[next_arg + 1 :]
43        m = TYPE.match(s)
44
45    return res + s
46
47
48def process_file(contents):
49    PREFIX = re.compile(r"check-prefix(es)?(=|\s+)([a-zA-Z0-9,]+)")
50    check_prefixes = ["CHECK"]
51    result = ""
52    for line in contents.split("\n"):
53        if "FileCheck" in line:
54            m = PREFIX.search(line)
55            if m:
56                check_prefixes.extend(m.group(3).split(","))
57
58        found_check = False
59        for prefix in check_prefixes:
60            if prefix in line:
61                found_check = True
62                break
63
64        if not found_check or "define" not in line:
65            result += line + "\n"
66            continue
67
68        # We have a check for a function definition. Number the args.
69        line = fix_string(line)
70        result += line + "\n"
71    return result
72
73
74def main():
75    print(f"Processing {sys.argv[1]}")
76    f = open(sys.argv[1])
77    content = f.read()
78    f.close()
79
80    content = process_file(content)
81
82    f = open(sys.argv[1], "w")
83    f.write(content)
84    f.close()
85
86
87if __name__ == "__main__":
88    main()
89