diff --git a/clang/test/AST/gen_ast_dump_json_test.py b/clang/test/AST/gen_ast_dump_json_test.py index 4f4f37cd26d7..b03e3cb23771 100644 --- a/clang/test/AST/gen_ast_dump_json_test.py +++ b/clang/test/AST/gen_ast_dump_json_test.py @@ -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 -cc1 -ast-dump=json ", 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)