blob: e4e7e2ce41e7b89e8d5ec1c3cf3c7d4d64afc2af [file] [log] [blame]
Chandler Carruth06a5dd62015-01-12 04:43:18 +00001#!/usr/bin/env python2.7
2
3"""A test case update script.
4
5This script is a utility to update LLVM X86 'llc' based test cases with new
6FileCheck patterns. It can either update all of the tests in the file or
7a single test function.
8"""
9
10import argparse
Sanjay Patel506fd0d2016-03-24 17:30:38 +000011import os # Used to advertise this file's name ("autogenerated_note").
Chandler Carruth06a5dd62015-01-12 04:43:18 +000012import string
13import subprocess
14import sys
Chandler Carruth06a5dd62015-01-12 04:43:18 +000015import re
16
Sanjay Patelbf623012016-03-23 21:40:53 +000017# Invoke the tool that is being tested.
Chandler Carruth06a5dd62015-01-12 04:43:18 +000018def llc(args, cmd_args, ir):
19 with open(ir) as ir_file:
20 stdout = subprocess.check_output(args.llc_binary + ' ' + cmd_args,
21 shell=True, stdin=ir_file)
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +000022 # Fix line endings to unix CR style.
23 stdout = stdout.replace('\r\n', '\n')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000024 return stdout
25
26
Sanjay Patelbf623012016-03-23 21:40:53 +000027# RegEx: this is where the magic happens.
28
Eli Friedman1a9a8872016-12-19 23:09:51 +000029ASM_FUNCTION_X86_RE = re.compile(
30 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
31 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
Zvi Rackoverd635eeb2017-09-06 23:04:28 +000032 r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
Eli Friedman1a9a8872016-12-19 23:09:51 +000033 flags=(re.M | re.S))
Eli Friedman1a9a8872016-12-19 23:09:51 +000034
35ASM_FUNCTION_ARM_RE = re.compile(
36 r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
37 r'\s+\.fnstart\n' # .fnstart
38 r'(?P<body>.*?)\n' # (body of the function)
Chandler Carruth5c69dac2017-08-25 02:32:48 +000039 r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
Eli Friedman1a9a8872016-12-19 23:09:51 +000040 flags=(re.M | re.S))
Sanjay Patelbf623012016-03-23 21:40:53 +000041
Sanjay Patelb2f62a9e2017-08-25 19:33:18 +000042ASM_FUNCTION_AARCH64_RE = re.compile(
43 r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@(?P=func)\n'
44 r'[ \t]+.cfi_startproc\n'
45 r'(?P<body>.*?)\n'
46 # This list is incomplete
47 r'.Lfunc_end[0-9]+:\n',
48 flags=(re.M | re.S))
Chandler Carruth06a5dd62015-01-12 04:43:18 +000049
Tim Shen53ddc1d2016-12-22 20:59:39 +000050ASM_FUNCTION_PPC_RE = re.compile(
51 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
52 r'\.Lfunc_begin[0-9]+:\n'
53 r'[ \t]+.cfi_startproc\n'
54 r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*'
55 r'(?P<body>.*?)\n'
56 # This list is incomplete
57 r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*'
58 r'.Lfunc_end[0-9]+:\n',
59 flags=(re.M | re.S))
60
Jonas Paulssonf20386d2017-03-17 07:11:42 +000061ASM_FUNCTION_SYSTEMZ_RE = re.compile(
62 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
63 r'[ \t]+.cfi_startproc\n'
64 r'(?P<body>.*?)\n'
65 r'.Lfunc_end[0-9]+:\n',
66 flags=(re.M | re.S))
67
Chandler Carruth06a5dd62015-01-12 04:43:18 +000068
Sanjay Patelb2f62a9e2017-08-25 19:33:18 +000069SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
70SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
71SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
72SCRUB_LOOP_COMMENT_RE = re.compile(
73 r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
74
75SCRUB_X86_SHUFFLES_RE = (
76 re.compile(
77 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
78 flags=re.M))
79SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
80SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
81SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
Sanjay Patelf1735a52017-10-20 21:55:23 +000082SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
Sanjay Patelb2f62a9e2017-08-25 19:33:18 +000083
84RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$')
85TRIPLE_ARG_RE = re.compile(r'-mtriple=([^ ]+)')
86TRIPLE_IR_RE = re.compile(r'^target\s+triple\s*=\s*"([^"]+)"$')
87IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
88CHECK_PREFIX_RE = re.compile('--?check-prefix(?:es)?=(\S+)')
89CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
90
Sanjay Patel9db5da22017-10-24 14:32:52 +000091def scrub_asm_x86(asm, args):
Chandler Carruth06a5dd62015-01-12 04:43:18 +000092 # Scrub runs of whitespace out of the assembly, but leave the leading
93 # whitespace in place.
Sanjay Patelbf623012016-03-23 21:40:53 +000094 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000095 # Expand the tabs used for indentation.
96 asm = string.expandtabs(asm, 2)
97 # Detect shuffle asm comments and hide the operands in favor of the comments.
Sanjay Patelbf623012016-03-23 21:40:53 +000098 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000099 # Generically match the stack offset of a memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +0000100 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000101 # Generically match a RIP-relative memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +0000102 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
Simon Pilgrim2b7c02a2016-06-11 20:39:21 +0000103 # Generically match a LCP symbol.
104 asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
Sanjay Patel9db5da22017-10-24 14:32:52 +0000105 if args.x86_extra_scrub:
106 # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
107 asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000108 # Strip kill operands inserted into the asm.
Sanjay Patelbf623012016-03-23 21:40:53 +0000109 asm = SCRUB_KILL_COMMENT_RE.sub('', asm)
Chandler Carruthe3750952015-02-04 10:46:48 +0000110 # Strip trailing whitespace.
Sanjay Patelbf623012016-03-23 21:40:53 +0000111 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000112 return asm
113
Sanjay Patel9db5da22017-10-24 14:32:52 +0000114def scrub_asm_arm_eabi(asm, args):
Eli Friedman1a9a8872016-12-19 23:09:51 +0000115 # Scrub runs of whitespace out of the assembly, but leave the leading
116 # whitespace in place.
117 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
118 # Expand the tabs used for indentation.
119 asm = string.expandtabs(asm, 2)
120 # Strip kill operands inserted into the asm.
121 asm = SCRUB_KILL_COMMENT_RE.sub('', asm)
122 # Strip trailing whitespace.
123 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
124 return asm
125
Sanjay Patel9db5da22017-10-24 14:32:52 +0000126def scrub_asm_powerpc64(asm, args):
Tim Shen53ddc1d2016-12-22 20:59:39 +0000127 # Scrub runs of whitespace out of the assembly, but leave the leading
128 # whitespace in place.
129 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
130 # Expand the tabs used for indentation.
131 asm = string.expandtabs(asm, 2)
Tim Shence26a452017-03-23 16:02:47 +0000132 # Stripe unimportant comments
133 asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000134 # Strip trailing whitespace.
135 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
136 return asm
137
Sanjay Patel9db5da22017-10-24 14:32:52 +0000138def scrub_asm_systemz(asm, args):
Jonas Paulssonf20386d2017-03-17 07:11:42 +0000139 # Scrub runs of whitespace out of the assembly, but leave the leading
140 # whitespace in place.
141 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
142 # Expand the tabs used for indentation.
143 asm = string.expandtabs(asm, 2)
144 # Strip trailing whitespace.
145 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
146 return asm
147
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000148
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000149# Build up a dictionary of all the function bodies.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000150def build_function_body_dictionary(raw_tool_output, triple, prefixes, func_dict,
Sanjay Patel9db5da22017-10-24 14:32:52 +0000151 args):
Tim Shen53ddc1d2016-12-22 20:59:39 +0000152 target_handlers = {
153 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
154 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
155 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
156 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
Sanjay Patelb2f62a9e2017-08-25 19:33:18 +0000157 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
Tim Shen53ddc1d2016-12-22 20:59:39 +0000158 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Sanjay Patel588e4152017-02-24 21:47:44 +0000159 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
160 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Eli Friedman7e0ce822017-02-24 03:04:11 +0000161 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Fangrui Songdc168722017-10-22 18:43:23 +0000162 'powerpc64': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE),
163 'powerpc64le': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE),
Jonas Paulssonf20386d2017-03-17 07:11:42 +0000164 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
Tim Shen53ddc1d2016-12-22 20:59:39 +0000165 }
166 handlers = None
167 for prefix, s in target_handlers.items():
168 if triple.startswith(prefix):
169 handlers = s
170 break
171 else:
172 raise KeyError('Triple %r is not supported' % (triple))
173
174 scrubber, function_re = handlers
Eli Friedman1a9a8872016-12-19 23:09:51 +0000175 for m in function_re.finditer(raw_tool_output):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000176 if not m:
177 continue
178 func = m.group('func')
Sanjay Patel9db5da22017-10-24 14:32:52 +0000179 scrubbed_body = scrubber(m.group('body'), args)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000180 if func.startswith('stress'):
181 # We only use the last line of the function body for stress tests.
182 scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
Sanjay Patel9db5da22017-10-24 14:32:52 +0000183 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000184 print >>sys.stderr, 'Processing function: ' + func
185 for l in scrubbed_body.splitlines():
186 print >>sys.stderr, ' ' + l
187 for prefix in prefixes:
188 if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body:
189 if prefix == prefixes[-1]:
190 print >>sys.stderr, ('WARNING: Found conflicting asm under the '
191 'same prefix: %r!' % (prefix,))
192 else:
193 func_dict[prefix][func] = None
194 continue
195
196 func_dict[prefix][func] = scrubbed_body
197
198
Tim Shen53ddc1d2016-12-22 20:59:39 +0000199def add_checks(output_lines, run_list, func_dict, func_name):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000200 printed_prefixes = []
Tim Shen53ddc1d2016-12-22 20:59:39 +0000201 for p in run_list:
202 checkprefixes = p[0]
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000203 for checkprefix in checkprefixes:
204 if checkprefix in printed_prefixes:
205 break
206 if not func_dict[checkprefix][func_name]:
207 continue
208 # Add some space between different check prefixes.
209 if len(printed_prefixes) != 0:
210 output_lines.append(';')
211 printed_prefixes.append(checkprefix)
212 output_lines.append('; %s-LABEL: %s:' % (checkprefix, func_name))
213 func_body = func_dict[checkprefix][func_name].splitlines()
214 output_lines.append('; %s: %s' % (checkprefix, func_body[0]))
215 for func_line in func_body[1:]:
216 output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line))
217 # Add space between different check prefixes and the first line of code.
218 # output_lines.append(';')
219 break
220 return output_lines
221
222
223def should_add_line_to_output(input_line, prefix_set):
224 # Skip any blank comment lines in the IR.
225 if input_line.strip() == ';':
226 return False
227 # Skip any blank lines in the IR.
228 #if input_line.strip() == '':
229 # return False
230 # And skip any CHECK lines. We're building our own.
231 m = CHECK_RE.match(input_line)
232 if m and m.group(1) in prefix_set:
233 return False
234
235 return True
236
237
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000238def main():
239 parser = argparse.ArgumentParser(description=__doc__)
240 parser.add_argument('-v', '--verbose', action='store_true',
241 help='Show verbose output')
242 parser.add_argument('--llc-binary', default='llc',
243 help='The "llc" binary to use to generate the test case')
244 parser.add_argument(
245 '--function', help='The function in the test file to update')
Sanjay Patel9db5da22017-10-24 14:32:52 +0000246 parser.add_argument(
247 '--x86_extra_scrub', action='store_true',
248 help='Use more regex for x86 matching to reduce diffs between various subtargets')
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000249 parser.add_argument('tests', nargs='+')
250 args = parser.parse_args()
251
James Y Knight7c905062015-11-23 21:33:58 +0000252 autogenerated_note = ('; NOTE: Assertions have been autogenerated by '
Simon Pilgrim2b7c02a2016-06-11 20:39:21 +0000253 'utils/' + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000254
255 for test in args.tests:
256 if args.verbose:
257 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
258 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000259 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000260
Tim Shen53ddc1d2016-12-22 20:59:39 +0000261 triple_in_ir = None
262 for l in input_lines:
263 m = TRIPLE_IR_RE.match(l)
264 if m:
265 triple_in_ir = m.groups()[0]
266 break
267
Bryant Wong291264b2016-12-29 19:32:34 +0000268 raw_lines = [m.group(1)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000269 for m in [RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +0000270 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
271 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +0000272 if run_lines[-1].endswith("\\"):
273 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
274 else:
275 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +0000276
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000277 if args.verbose:
278 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
279 for l in run_lines:
280 print >>sys.stderr, ' RUN: ' + l
281
Tim Shen53ddc1d2016-12-22 20:59:39 +0000282 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000283 for l in run_lines:
Zvi Rackover35a5acf2016-11-07 17:47:21 +0000284 commands = [cmd.strip() for cmd in l.split('|', 1)]
285 llc_cmd = commands[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +0000286
287 triple_in_cmd = None
288 m = TRIPLE_ARG_RE.search(llc_cmd)
289 if m:
290 triple_in_cmd = m.groups()[0]
291
Zvi Rackover35a5acf2016-11-07 17:47:21 +0000292 filecheck_cmd = ''
293 if len(commands) > 1:
294 filecheck_cmd = commands[1]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000295 if not llc_cmd.startswith('llc '):
296 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
297 continue
298
299 if not filecheck_cmd.startswith('FileCheck '):
300 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
301 continue
302
303 llc_cmd_args = llc_cmd[len('llc'):].strip()
304 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
305
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +0000306 check_prefixes = [item for m in CHECK_PREFIX_RE.finditer(filecheck_cmd)
307 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000308 if not check_prefixes:
309 check_prefixes = ['CHECK']
310
311 # FIXME: We should use multiple check prefixes to common check lines. For
312 # now, we just ignore all but the last.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000313 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000314
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000315 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +0000316 for p in run_list:
317 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000318 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000319 func_dict.update({prefix: dict()})
Tim Shen53ddc1d2016-12-22 20:59:39 +0000320 for prefixes, llc_args, triple_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000321 if args.verbose:
322 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
323 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000324
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000325 raw_tool_output = llc(args, llc_args, test)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000326 if not (triple_in_cmd or triple_in_ir):
327 print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
328
329 build_function_body_dictionary(raw_tool_output,
Sanjay Patel9db5da22017-10-24 14:32:52 +0000330 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict, args)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000331
332 is_in_function = False
333 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000334 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000335 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000336 if args.verbose:
337 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000338 output_lines = []
339 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000340
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000341 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000342 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000343 if input_line == '':
344 continue
345 if input_line.lstrip().startswith(';'):
346 m = CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000347 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000348 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000349 continue
350
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000351 # Print out the various check lines here.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000352 output_lines = add_checks(output_lines, run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000353 is_in_function_start = False
354
355 if is_in_function:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000356 if should_add_line_to_output(input_line, prefix_set) == True:
357 # This input line of the function body will go as-is into the output.
358 output_lines.append(input_line)
359 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000360 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000361 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000362 is_in_function = False
363 continue
364
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000365 if input_line == autogenerated_note:
James Y Knight7c905062015-11-23 21:33:58 +0000366 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000367
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000368 # If it's outside a function, it just gets copied to the output.
369 output_lines.append(input_line)
370
371 m = IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000372 if not m:
373 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000374 func_name = m.group(1)
375 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000376 # When filtering on a specific function, skip all others.
377 continue
378 is_in_function = is_in_function_start = True
379
380 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000381 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
382
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000383 with open(test, 'wb') as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000384 f.writelines([l + '\n' for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000385
386
387if __name__ == '__main__':
388 main()