[gen_ast_dump_json_test.py] Add a --update flag

This will allow updating the JSON tests for new format changes. Instead of
simply appending the JSON to the input file, the script will now make a
copy of the input file up to the "CHECK lines have been autogenerated"
disclaimer and then append the new JSON.

See https://reviews.llvm.org/D70119
This commit is contained in:
Alex Richardson 2019-10-29 11:50:08 +00:00
parent 0908093977
commit a763d98501
1 changed files with 21 additions and 12 deletions

View File

@ -1,7 +1,5 @@
#!/usr/bin/env python
from collections import OrderedDict
from sets import Set
from shutil import copyfile
import argparse
import json
@ -9,7 +7,8 @@ import os
import pprint
import re
import subprocess
import tempfile
def normalize(dict_var):
for k, v in dict_var.items():
if isinstance(v, OrderedDict):
@ -63,6 +62,7 @@ def main():
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")
args = parser.parse_args()
@ -76,7 +76,7 @@ def main():
return -1
options = args.opts.split(' ')
filters = Set(args.filters.split(',')) if args.filters else Set([])
filters = set(args.filters.split(',')) if args.filters else set()
note = "// NOTE: CHECK lines have been autogenerated by " \
"gen_ast_dump_json_test.py"
@ -118,13 +118,14 @@ def main():
filter_json(j, filters, out_asts)
partition = args.source.rpartition('.')
dest_path = '%s-json%s%s' % (partition[0], partition[1], partition[2])
print("Writing json appended source file to %s." %(dest_path))
copyfile(args.source, dest_path)
with open(dest_path, "a") as f:
f.write("\n" + note + "\n")
with tempfile.NamedTemporaryFile("w") as f:
with open(args.source, "r") as srcf:
for line in srcf.readlines():
# copy up to the note:
if line.rstrip() == note:
break
f.write(line)
f.write(note + "\n")
for out_ast in out_asts:
append_str = json.dumps(out_ast, indent=1, ensure_ascii=False)
out_str = '\n\n'
@ -137,7 +138,15 @@ def main():
out_str += '// CHECK-NEXT: %s\n' %(append_line.rstrip())
f.write(out_str)
f.flush()
if args.update:
print("Updating json appended source file to %s." % args.source)
copyfile(f.name, args.source)
else:
partition = args.source.rpartition('.')
dest_path = '%s-json%s%s' % (partition[0], partition[1], partition[2])
print("Writing json appended source file to %s." % dest_path)
copyfile(f.name, dest_path)
return 0
if __name__ == '__main__':