Skip to content

Commit 7debe51

Browse files
committed
[gen_ast_dump_json_test.py] Allow updating multiple files in one go
With this change it is possible to update all JSON dump tests using the following command: python $LLVM_BINDIR/gen_ast_dump_json_test.py --update --source $LLVM_SRC/clang/test/AST/*-json.* See https://reviews.llvm.org/D70119
1 parent d09e811 commit 7debe51

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

clang/test/AST/gen_ast_dump_json_test.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ def main():
6868
parser = argparse.ArgumentParser()
6969
parser.add_argument("--clang", help="The clang binary (could be a relative or absolute path)",
7070
action="store", default=default_clang_path())
71-
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>",
72-
action="store", required=True)
71+
parser.add_argument("--source", help="the source file(s). Without --update, the command used to generate the JSON "
72+
"will be of the format <clang> -cc1 -ast-dump=json <opts> <source>",
73+
action="store", nargs=argparse.ONE_OR_MORE, required=True)
7374
parser.add_argument("--filters", help="comma separated list of AST filters. Ex: --filters=TypedefDecl,BuiltinType",
7475
action="store", default='')
7576
update_or_generate_group = parser.add_mutually_exclusive_group()
@@ -81,23 +82,29 @@ def main():
8182
args = parser.parse_args()
8283

8384
if not args.source:
84-
print("Specify the source file to give to clang.")
85-
return -1
85+
sys.exit("Specify the source file to give to clang.")
8686

8787
clang_binary = os.path.abspath(args.clang)
8888
if not os.path.isfile(clang_binary):
89-
print("clang binary specified not present.")
90-
return -1
89+
sys.exit("clang binary specified not present.")
90+
91+
for src in args.source:
92+
process_file(src, clang_binary, cmdline_filters=args.filters,
93+
cmdline_opts=args.opts, do_update=args.update,
94+
force_update=args.update_manual)
95+
9196

97+
def process_file(source_file, clang_binary, cmdline_filters, cmdline_opts,
98+
do_update, force_update):
9299
note_firstline = "// NOTE: CHECK lines have been autogenerated by " \
93100
"gen_ast_dump_json_test.py"
94101
filters_line_prefix = "// using --filters="
95102
note = note_firstline
96103

97104
cmd = [clang_binary, "-cc1"]
98-
if args.update:
105+
if do_update:
99106
# When updating the first line of the test must be a RUN: line
100-
with open(args.source, "r") as srcf:
107+
with open(source_file, "r") as srcf:
101108
first_line = srcf.readline()
102109
found_autogenerated_line = False
103110
filters_line = None
@@ -110,12 +117,13 @@ def main():
110117
if line.startswith(note_firstline):
111118
found_autogenerated_line = True
112119
# print("Found autogenerated disclaimer at line", i + 1)
113-
if not found_autogenerated_line and not args.update_manual:
114-
print("Not updating", args.source, "since it is not autogenerated.")
115-
sys.exit(0)
116-
if not args.filters and filters_line:
117-
args.filters = filters_line
118-
print("Inferred filters as '" + args.filters + "'")
120+
if not found_autogenerated_line and not force_update:
121+
print("Not updating", source_file, "since it is not autogenerated.",
122+
file=sys.stderr)
123+
return
124+
if not cmdline_filters and filters_line:
125+
cmdline_filters = filters_line
126+
print("Inferred filters as '" + cmdline_filters + "'")
119127

120128
if "RUN: %clang_cc1 " not in first_line:
121129
sys.exit("When using --update the first line of the input file must contain RUN: %clang_cc1")
@@ -133,19 +141,18 @@ def main():
133141
sys.exit("ERROR: RUN: line does not contain %s")
134142
options.remove("%s")
135143
else:
136-
options = args.opts.split()
144+
options = cmdline_opts.split()
137145
options.append("-ast-dump=json")
138146
cmd.extend(options)
139147
using_ast_dump_filter = any('ast-dump-filter' in arg for arg in cmd)
140-
cmd.append(args.source)
148+
cmd.append(source_file)
141149
print("Will run", cmd)
142150
filters = set()
143-
if args.filters:
144-
note += "\n" + filters_line_prefix + args.filters
145-
filters = set(args.filters.split(','))
151+
if cmdline_filters:
152+
note += "\n" + filters_line_prefix + cmdline_filters
153+
filters = set(cmdline_filters.split(','))
146154
print("Will use the following filters:", filters)
147155

148-
149156
try:
150157
json_str = subprocess.check_output(cmd)
151158
except Exception as ex:
@@ -174,7 +181,7 @@ def main():
174181
filter_json(j, filters, out_asts)
175182

176183
with tempfile.NamedTemporaryFile("w") as f:
177-
with open(args.source, "r") as srcf:
184+
with open(source_file, "r") as srcf:
178185
for line in srcf.readlines():
179186
# copy up to the note:
180187
if line.rstrip() == note_firstline:
@@ -194,15 +201,16 @@ def main():
194201

195202
f.write(out_str)
196203
f.flush()
197-
if args.update:
198-
print("Updating json appended source file to %s." % args.source)
199-
copyfile(f.name, args.source)
204+
if do_update:
205+
print("Updating json appended source file to %s." % source_file)
206+
copyfile(f.name, source_file)
200207
else:
201-
partition = args.source.rpartition('.')
208+
partition = source_file.rpartition('.')
202209
dest_path = '%s-json%s%s' % (partition[0], partition[1], partition[2])
203210
print("Writing json appended source file to %s." % dest_path)
204211
copyfile(f.name, dest_path)
205212
return 0
206-
213+
214+
207215
if __name__ == '__main__':
208216
main()

0 commit comments

Comments
 (0)