Skip to content

Commit 049f4c9

Browse files
committed
[gen_ast_dump_json_test.py] Infer --filters flags when using --update
See https://reviews.llvm.org/D70119
1 parent 698ea9c commit 049f4c9

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

clang/test/AST/gen_ast_dump_json_test.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def main():
6868
update_or_generate_group.add_argument("--update", help="Update the file in-place", action="store_true")
6969
update_or_generate_group.add_argument("--opts", help="other options",
7070
action="store", default='', type=str)
71-
7271
args = parser.parse_args()
7372

7473
if not args.source:
@@ -80,19 +79,31 @@ def main():
8079
print("clang binary specified not present.")
8180
return -1
8281

83-
filters = set(args.filters.split(',')) if args.filters else set()
84-
85-
note = "// NOTE: CHECK lines have been autogenerated by " \
86-
"gen_ast_dump_json_test.py"
87-
88-
if (args.filters):
89-
note += "\n// using --filters=" + args.filters
90-
82+
note_firstline = "// NOTE: CHECK lines have been autogenerated by " \
83+
"gen_ast_dump_json_test.py"
84+
filters_line_prefix = "// using --filters="
85+
note = note_firstline
86+
9187
cmd = [clang_binary, "-cc1"]
9288
if args.update:
9389
# When updating the first line of the test must be a RUN: line
9490
with open(args.source, "r") as srcf:
9591
first_line = srcf.readline()
92+
filters_line_next = False
93+
filters_line = None
94+
for i, line in enumerate(srcf.readlines()):
95+
if filters_line_next:
96+
# print("Filters line: '", line.rstrip(), "'", sep="")
97+
if line.startswith(filters_line_prefix):
98+
filters_line = line[len(filters_line_prefix):].rstrip()
99+
break
100+
if line.startswith(note_firstline):
101+
filters_line_next = True
102+
# print("Found autogenerated disclaimer at line", i + 1)
103+
if not args.filters and filters_line:
104+
args.filters = filters_line
105+
print("Inferred filters as '" + args.filters + "'")
106+
96107
if "RUN: %clang_cc1 " not in first_line:
97108
sys.exit("When using --update the first line of the input file must contain RUN: %clang_cc1")
98109
clang_start = first_line.find("%clang_cc1") + len("%clang_cc1")
@@ -115,6 +126,12 @@ def main():
115126
using_ast_dump_filter = any('ast-dump-filter' in arg for arg in cmd)
116127
cmd.append(args.source)
117128
print("Will run", cmd)
129+
filters = set()
130+
if args.filters:
131+
note += "\n" + filters_line_prefix + args.filters
132+
filters = set(args.filters.split(','))
133+
print("Will use the following filters:", filters)
134+
118135

119136
try:
120137
json_str = subprocess.check_output(cmd)
@@ -147,7 +164,7 @@ def main():
147164
with open(args.source, "r") as srcf:
148165
for line in srcf.readlines():
149166
# copy up to the note:
150-
if line.rstrip() == note:
167+
if line.rstrip() == note_firstline:
151168
break
152169
f.write(line)
153170
f.write(note + "\n")

0 commit comments

Comments
 (0)