67322c288dd7183837fb77aac94a79c08de14d5d
[ghc.git] / utils / checkUniques / check-uniques.py
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4 import os.path
5 import sys
6 import re
7 import glob
8 import io
9 from collections import defaultdict
10
11 # keyed on unique type, values are lists of (unique, name) pairs
12 def find_uniques(source_files):
13 uniques = defaultdict(lambda: defaultdict(lambda: set()))
14 unique_re = re.compile(r"([\w\d]+)\s*=\s*mk([\w\d']+)Unique\s+(\d+)")
15 for f in source_files:
16 ms = unique_re.findall(io.open(f, encoding='utf8').read())
17 for m in ms:
18 name = m[0]
19 _type = m[1]
20 n = int(m[2])
21 uniques[_type][n].add(name)
22
23 return uniques
24
25 def print_all(uniques):
26 for _type, uniqs in uniques.items():
27 print('{_type} uniques'.format(**locals()))
28 for n,names in uniqs.items():
29 all_names = ', '.join(names)
30 print(' {n} = {all_names}'.format(**locals()))
31
32 def find_conflicts(uniques):
33 return [ (uniqueType, number, names)
34 for uniqueType, uniqs in uniques.items()
35 for number, names in uniqs.items()
36 if len(names) > 1
37 ]
38
39 top_dir = sys.argv[1]
40 uniques = find_uniques(glob.glob(os.path.join(top_dir, 'compiler', 'prelude', '*.hs')))
41 #print_all(uniques)
42 conflicts = find_conflicts(uniques)
43 if len(conflicts) > 0:
44 print("Error: check-uniques: Found Unique conflict")
45 print()
46 for (ty, n, names) in conflicts:
47 print(' %s unique %d conflict: %s' % (ty, n, ', '.join(names)))
48 print()
49 sys.exit(1)