forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathupdate_cc_test_checks.py
executable file
·241 lines (206 loc) · 8.84 KB
/
update_cc_test_checks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
'''A utility to update LLVM IR CHECK lines in C/C++ FileCheck test files.
Example RUN lines in .c/.cc test files:
// RUN: %clang -emit-llvm -S %s -o - -O2 | FileCheck %s
// RUN: %clangxx -emit-llvm -S %s -o - -O2 | FileCheck -check-prefix=CHECK-A %s
Usage:
% utils/update_cc_test_checks.py --llvm-bin=release/bin test/a.cc
% utils/update_cc_test_checks.py --c-index-test=release/bin/c-index-test \
--clang=release/bin/clang /tmp/c/a.cc
'''
importargparse
importcollections
importdistutils.spawn
importos
importshlex
importstring
importsubprocess
importsys
importre
importtempfile
fromUpdateTestChecksimportasm, common
ADVERT='// NOTE: Assertions have been autogenerated by '
CHECK_RE=re.compile(r'^\s*//\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
RUN_LINE_RE=re.compile('^//\s*RUN:\s*(.*)$')
SUBST= {
'%clang': [],
'%clang_cc1': ['-cc1'],
'%clangxx': ['--driver-mode=g++'],
}
defget_line2spell_and_mangled(args, clang_args):
ret= {}
withtempfile.NamedTemporaryFile() asf:
# TODO Make c-index-test print mangled names without circumventing through precompiled headers
status=subprocess.run([args.c_index_test, '-write-pch', f.name, *clang_args],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
ifstatus.returncode:
sys.stderr.write(status.stdout.decode())
sys.exit(2)
output=subprocess.check_output([args.c_index_test,
'-test-print-mangle', f.name])
ifsys.version_info[0] >2:
output=output.decode()
RE=re.compile(r'^FunctionDecl=(\w+):(\d+):\d+ \(Definition\) \[mangled=([^]]+)\]')
forlineinoutput.splitlines():
m=RE.match(line)
ifnotm: continue
spell, line, mangled=m.groups()
ifmangled=='_'+spell:
# HACK for MacOS (where the mangled name includes an _ for C but the IR won't):
mangled=spell
# Note -test-print-mangle does not print file names so if #include is used,
# the line number may come from an included file.
ret[int(line)-1] = (spell, mangled)
ifargs.verbose:
forline, func_nameinsorted(ret.items()):
print('line {}: found function {}'.format(line+1, func_name), file=sys.stderr)
returnret
defconfig():
parser=argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('--llvm-bin', help='llvm $prefix/bin path')
parser.add_argument('--clang',
help='"clang" executable, defaults to $llvm_bin/clang')
parser.add_argument('--clang-args',
help='Space-separated extra args to clang, e.g. --clang-args=-v')
parser.add_argument('--c-index-test',
help='"c-index-test" executable, defaults to $llvm_bin/c-index-test')
parser.add_argument(
'--functions', nargs='+', help='A list of function name regexes. '
'If specified, update CHECK lines for functions matching at least one regex')
parser.add_argument(
'--x86_extra_scrub', action='store_true',
help='Use more regex for x86 matching to reduce diffs between various subtargets')
parser.add_argument('tests', nargs='+')
args=parser.parse_args()
args.clang_args=shlex.split(args.clang_argsor'')
ifargs.clangisNone:
ifargs.llvm_binisNone:
args.clang='clang'
else:
args.clang=os.path.join(args.llvm_bin, 'clang')
ifnotdistutils.spawn.find_executable(args.clang):
print('Please specify --llvm-bin or --clang', file=sys.stderr)
sys.exit(1)
ifargs.c_index_testisNone:
ifargs.llvm_binisNone:
args.c_index_test='c-index-test'
else:
args.c_index_test=os.path.join(args.llvm_bin, 'c-index-test')
ifnotdistutils.spawn.find_executable(args.c_index_test):
print('Please specify --llvm-bin or --c-index-test', file=sys.stderr)
sys.exit(1)
returnargs
defget_function_body(args, filename, clang_args, prefixes, triple_in_cmd, func_dict):
# TODO Clean up duplication of asm/common build_function_body_dictionary
# Invoke external tool and extract function bodies.
raw_tool_output=common.invoke_tool(args.clang, clang_args, filename)
if'-emit-llvm'inclang_args:
common.build_function_body_dictionary(
common.OPT_FUNCTION_RE, common.scrub_body, [],
raw_tool_output, prefixes, func_dict, args.verbose)
else:
print('The clang command line should include -emit-llvm as asm tests '
'are discouraged in Clang testsuite.', file=sys.stderr)
sys.exit(1)
defmain():
args=config()
autogenerated_note= (ADVERT+'utils/'+os.path.basename(__file__))
forfilenameinargs.tests:
withopen(filename) asf:
input_lines= [l.rstrip() forlinf]
# Extract RUN lines.
raw_lines= [m.group(1)
formin [RUN_LINE_RE.match(l) forlininput_lines] ifm]
run_lines= [raw_lines[0]] iflen(raw_lines) >0else []
forlinraw_lines[1:]:
ifrun_lines[-1].endswith("\\"):
run_lines[-1] =run_lines[-1].rstrip("\\") +" "+l
else:
run_lines.append(l)
ifargs.verbose:
print('Found {} RUN lines:'.format(len(run_lines)), file=sys.stderr)
forlinrun_lines:
print(' RUN: '+l, file=sys.stderr)
# Build a list of clang command lines and check prefixes from RUN lines.
run_list= []
line2spell_and_mangled_list=collections.defaultdict(list)
forlinrun_lines:
commands= [cmd.strip() forcmdinl.split('|', 1)]
triple_in_cmd=None
m=common.TRIPLE_ARG_RE.search(commands[0])
ifm:
triple_in_cmd=m.groups()[0]
# Apply %clang substitution rule, replace %s by `filename`, and append args.clang_args
clang_args=shlex.split(commands[0])
ifclang_args[0] notinSUBST:
print('WARNING: Skipping non-clang RUN line: '+l, file=sys.stderr)
continue
clang_args[0:1] =SUBST[clang_args[0]]
clang_args= [filenameifi=='%s'elseiforiinclang_args] +args.clang_args
# Extract -check-prefix in FileCheck args
filecheck_cmd=commands[-1]
ifnotfilecheck_cmd.startswith('FileCheck '):
print('WARNING: Skipping non-FileChecked RUN line: '+l, file=sys.stderr)
continue
check_prefixes= [itemformincommon.CHECK_PREFIX_RE.finditer(filecheck_cmd)
foriteminm.group(1).split(',')]
ifnotcheck_prefixes:
check_prefixes= ['CHECK']
run_list.append((check_prefixes, clang_args, triple_in_cmd))
# Strip CHECK lines which are in `prefix_set`, update test file.
prefix_set=set([prefixforpinrun_listforprefixinp[0]])
input_lines= []
withopen(filename, 'r+') asf:
forlineinf:
m=CHECK_RE.match(line)
ifnot (mandm.group(1) inprefix_set) andline!='//\n':
input_lines.append(line)
f.seek(0)
f.writelines(input_lines)
f.truncate()
# Execute clang, generate LLVM IR, and extract functions.
func_dict= {}
forpinrun_list:
prefixes=p[0]
forprefixinprefixes:
func_dict.update({prefix: dict()})
forprefixes, clang_args, triple_in_cmdinrun_list:
ifargs.verbose:
print('Extracted clang cmd: clang {}'.format(clang_args), file=sys.stderr)
print('Extracted FileCheck prefixes: {}'.format(prefixes), file=sys.stderr)
get_function_body(args, filename, clang_args, prefixes, triple_in_cmd, func_dict)
# Invoke c-index-test to get mapping from start lines to mangled names.
# Forward all clang args for now.
fork, vinget_line2spell_and_mangled(args, clang_args).items():
line2spell_and_mangled_list[k].append(v)
output_lines= [autogenerated_note]
foridx, lineinenumerate(input_lines):
# Discard any previous script advertising.
ifline.startswith(ADVERT):
continue
ifidxinline2spell_and_mangled_list:
added=set()
forspell, mangledinline2spell_and_mangled_list[idx]:
# One line may contain multiple function declarations.
# Skip if the mangled name has been added before.
# The line number may come from an included file,
# we simply require the spelling name to appear on the line
# to exclude functions from other files.
ifmangledinaddedorspellnotinline:
continue
ifargs.functionsisNoneorany(re.search(regex, spell) forregexinargs.functions):
ifadded:
output_lines.append('//')
added.add(mangled)
common.add_ir_checks(output_lines, '//', run_list, func_dict, mangled)
output_lines.append(line.rstrip('\n'))
# Update the test file.
withopen(filename, 'w') asf:
forlineinoutput_lines:
f.write(line+'\n')
return0
if__name__=='__main__':
sys.exit(main())