Index: clang/utils/creduce-clang-crash.py =================================================================== --- clang/utils/creduce-clang-crash.py +++ clang/utils/creduce-clang-crash.py @@ -8,6 +8,7 @@ import stat import sys import subprocess +import pipes def is_exe(fpath): return os.path.isfile(fpath) and os.access(fpath, os.X_OK) @@ -33,11 +34,9 @@ Return as a string. """ # Get clang call from build script - cmd = None - with open(build_script, 'r') as f: - cmd = f.read() - cmd = re.sub("#!.*\n", "", cmd) - cmd = cmd.rstrip('\n\r') + # Assumes the call is the last line of the script + with open(build_script) as f: + cmd = f.readlines()[-1].rstrip('\n\r') # Get crash output p = subprocess.Popen(build_script, @@ -46,21 +45,22 @@ crash_output, _ = p.communicate() output = ['#!/bin/bash'] - output.append('%s --crash %s >& t.log || exit 1' % (llvm_not, cmd)) + output.append('%s --crash %s >& t.log || exit 1' % (pipes.quote(llvm_not), + cmd)) # Add messages from crash output to the test # If there is an Assertion failure, use that; otherwise use the # last five stack trace functions - assertion_re = "Assertion \`([^\']+)\' failed" + assertion_re = r'Assertion `([^\']+)\' failed' assertion_match = re.search(assertion_re, crash_output) if assertion_match: - msg = assertion_match.group(1).replace('"', '\\"') - output.append('grep "%s" t.log || exit 1' % msg) + msg = assertion_match.group(1) + output.append('grep %s t.log || exit 1' % pipes.quote(msg)) else: - stacktrace_re = "#[0-9]+\s+0[xX][0-9a-fA-F]+\s*([^(]+)\(" + stacktrace_re = r'#[0-9]+\s+0[xX][0-9a-fA-F]+\s*([^(]+)\(' matches = re.findall(stacktrace_re, crash_output) - del matches[:len(matches)-5] - output += ['grep "%s" t.log || exit 1' % msg for msg in matches] + del matches[:-5] + output += ['grep %s t.log || exit 1' % pipes.quote(msg) for msg in matches] return output @@ -73,11 +73,11 @@ parser.add_argument('-o', '--output', dest='output', type=str, help='Name of the output file for the reduction. Optional.') parser.add_argument('--llvm-not', dest='llvm_not', type=str, - help="""The path to the llvm-not executable. - Required if 'not' is not in PATH environment."""); + help="The path to the llvm-not executable. " + "Required if 'not' is not in PATH environment."); parser.add_argument('--creduce', dest='creduce', type=str, - help="""The path to the C-Reduce executable. - Required if 'creduce' is not in PATH environment."""); + help="The path to the C-Reduce executable. " + "Required if 'creduce' is not in PATH environment."); args = parser.parse_args() build_script = os.path.abspath(args.build_script[0]) @@ -105,19 +105,11 @@ test_contents = create_test(build_script, llvm_not) testname, _ = os.path.splitext(file_to_reduce) testfile = testname + '.test.sh' - open(testfile, 'w').write('\n'.join(test_contents)) + with open(testfile, 'w') as f: + f.write('\n'.join(test_contents)) os.chmod(testfile, os.stat(testfile).st_mode | stat.S_IEXEC) - # Call C-Reduce - try: - p = subprocess.Popen([creduce, testfile, file_to_reduce]) - p.communicate() - - except KeyboardInterrupt: - print('\n\nctrl-c detected, killed creduce') - p.kill() - - sys.exit(0) + sys.exit(subprocess.call([creduce, testfile, file_to_reduce])) if __name__ == '__main__': main()