1#!/usr/bin/env python3
2
3import os
4import re
5import sys
6from concurrent.futures import ThreadPoolExecutor, as_completed
7
8def remove_prefix(i, d=0):
9    if d == 100:
10        return 2
11    s = os.popen('llvm-lit -a ' + i).read()
12    r = re.search('no check strings found with (?:prefix|prefixes) \'([^:]+)', s)
13    with open(i, 'r+') as f:
14        s = f.read()
15        if r:
16            p = r.group(1)
17            s = re.sub('=' + p + ',', '=', s)
18            s = re.sub(',' + p + '([, \n])', '\\1', s)
19            s = re.sub('\s+-?-check-prefix=' + p + '([ \n])', '\\1', s)
20        else:
21            s = re.sub('-?-check-prefixes=([\w-]+)(\Z|[ \t\n])', '--check-prefix=\\1\\2', s)
22            t = re.search('-?-check-(?:prefix|prefixes)=([^ ]+)\s+-?-check-(?:prefix|prefixes)=([^ ]+)', s)
23            while t:
24                s = re.sub(t.group(), '--check-prefixes=' + t.group(1) + ',' + t.group(2), s)
25                t = re.search('-?-check-(?:prefix|prefixes)=([^ ]+)\s+-?-check-(?:prefix|prefixes)=([^ ]+)', s)
26            s = re.sub('\s+-?-check-prefix=CHECK[ \t]*\n', '\n', s)
27        f.truncate(0)
28        f.seek(0)
29        f.write(s)
30    if not r:
31        t = re.search('Assertions have been autogenerated by (.*)', s)
32        if t:
33            s = os.popen('llvm/' + t.group(1) + ' ' + i + ' 2>&1').read()
34            if 'Found conflicting' in s:
35                return -1
36            s = os.popen('git diff ' + i).read()
37            if re.search('\n(?:-+)\n', s) or re.search('\n[+-].*(?<!RUN):', s):
38                return 1
39        return 0
40    return remove_prefix(i, d+1)
41
42with ThreadPoolExecutor(max_workers=32) as e:
43    f = []
44    c = []
45    a = []
46    t = { e.submit(remove_prefix, i): i for i in sys.argv[1:] }
47    for i in as_completed(t):
48        if i.result() == 0:
49            print('DONE:', end=' ')
50        elif i.result() == -1:
51            print('FAIL:', end=' ')
52            f.append(t[i])
53        elif i.result() == 1:
54            print('CHANGE:', end=' ')
55            c.append(t[i])
56        else:
57            print('ABORT:', end=' ')
58            a.append(t[i])
59        print(t[i])
60    for i in [ (f, 'Failed'), (c, 'Changed'), (a, 'Aborted') ]:
61        if i[0]:
62            print('********************\n%s Tests (%d):' % (i[1], len(i[0])))
63            for j in i[0]:
64                print('  ' + j)
65