[gen_ast_dump_json_test.py] Parse RUN: lines with --update

See https://reviews.llvm.org/D70119
This commit is contained in:
Alex Richardson 2019-10-29 12:06:46 +00:00
parent a763d98501
commit 698ea9cb21
1 changed files with 33 additions and 8 deletions

View File

@ -1,4 +1,6 @@
#!/usr/bin/env python
from __future__ import print_function
from collections import OrderedDict
from shutil import copyfile
import argparse
@ -7,8 +9,10 @@ import os
import pprint
import re
import subprocess
import sys
import tempfile
def normalize(dict_var):
for k, v in dict_var.items():
if isinstance(v, OrderedDict):
@ -56,13 +60,14 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument("--clang", help="The clang binary (could be a relative or absolute path)",
action="store", required=True)
parser.add_argument("--opts", help="other options",
action="store", default='', type=str)
parser.add_argument("--source", help="the source file. Command used to generate the json will be of the format <clang> -cc1 -ast-dump=json <opts> <source>",
action="store", required=True)
parser.add_argument("--filters", help="comma separated list of AST filters. Ex: --filters=TypedefDecl,BuiltinType",
action="store", default='')
parser.add_argument("--update", help="Update the file in-place", action="store_true")
update_or_generate_group = parser.add_mutually_exclusive_group()
update_or_generate_group.add_argument("--update", help="Update the file in-place", action="store_true")
update_or_generate_group.add_argument("--opts", help="other options",
action="store", default='', type=str)
args = parser.parse_args()
@ -75,7 +80,6 @@ def main():
print("clang binary specified not present.")
return -1
options = args.opts.split(' ')
filters = set(args.filters.split(',')) if args.filters else set()
note = "// NOTE: CHECK lines have been autogenerated by " \
@ -85,11 +89,32 @@ def main():
note += "\n// using --filters=" + args.filters
cmd = [clang_binary, "-cc1"]
if args.update:
# When updating the first line of the test must be a RUN: line
with open(args.source, "r") as srcf:
first_line = srcf.readline()
if "RUN: %clang_cc1 " not in first_line:
sys.exit("When using --update the first line of the input file must contain RUN: %clang_cc1")
clang_start = first_line.find("%clang_cc1") + len("%clang_cc1")
file_check_idx = first_line.rfind("| FileCheck")
if file_check_idx:
dump_cmd = first_line[clang_start:file_check_idx]
else:
dump_cmd = first_line[clang_start:]
print("Inferred run arguments as '", dump_cmd, "'", sep="")
options = dump_cmd.split()
if "-ast-dump=json" not in options:
sys.exit("ERROR: RUN: line does not contain -ast-dump=json")
if "%s" not in options:
sys.exit("ERROR: RUN: line does not contain %s")
options.remove("%s")
else:
options = args.opts.split()
options.append("-ast-dump=json")
cmd.extend(options)
using_ast_dump_filter = 'ast-dump-filter' in args.opts
cmd.extend(["-ast-dump=json", args.source])
using_ast_dump_filter = any('ast-dump-filter' in arg for arg in cmd)
cmd.append(args.source)
print("Will run", cmd)
try:
json_str = subprocess.check_output(cmd)