diff options
-rw-r--r-- | clang/test/AST/gen_ast_dump_json_test.py | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/clang/test/AST/gen_ast_dump_json_test.py b/clang/test/AST/gen_ast_dump_json_test.py index 54a5109aacf..4f4f37cd26d 100644 --- a/clang/test/AST/gen_ast_dump_json_test.py +++ b/clang/test/AST/gen_ast_dump_json_test.py @@ -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__': |