Skip to content

Commit 36ec6c1

Browse files
committed
feat(filter_include_changes): cleanup change suggestions for Mojom headers
1 parent 9ac8c9e commit 36ec6c1

1 file changed

Lines changed: 57 additions & 1 deletion

File tree

filter_include_changes.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
GENERATED_FILE_REGEX = re.compile(r"^out/[\w-]+/gen/.*$")
19-
MOJOM_HEADER_REGEX = re.compile(r"^.*\.mojom[^\.]*\.h$")
19+
MOJOM_HEADER_REGEX = re.compile(r"^(.*)\.mojom[^\.]*\.h$")
2020
THIRD_PARTY_REGEX = re.compile(r"^(?:third_party\/(?!blink)|v8).*$")
2121

2222

@@ -31,6 +31,7 @@ def filter_changes(
3131
filter_third_party=False,
3232
header_mappings: Dict[str, str] = None,
3333
weight_threshold: float = None,
34+
cleanup_mojom_headers=False,
3435
):
3536
"""Filter changes"""
3637

@@ -39,6 +40,13 @@ def filter_changes(
3940
# mapping, since we know that means clangd is confused on which header to include
4041
pending_changes: DefaultDict[str, Dict[str, Tuple[IncludeChange, int, Optional[str]]]] = defaultdict(dict)
4142

43+
# clangd struggles heavily with Mojom headers since there are several variants
44+
# (mojom-forward.h, mojom-shared.h, mojom.h, etc) and it can't understand the
45+
# conventions and determine which is the canonical header to include. Because
46+
# of that, let's filter out any changes where clangd is suggesting to remove
47+
# one variant and add a different variant since it is more than likely wrong.
48+
pending_mojom_changes: DefaultDict[str, Dict[str, Tuple[IncludeChange, int, Optional[str]]]] = defaultdict(dict)
49+
4250
if header_mappings:
4351
inverse_header_mappings = {v: k for k, v in header_mappings.items()}
4452

@@ -109,6 +117,13 @@ def filter_changes(
109117
pending_changes[filename][header] = (change_type, line, *_)
110118
continue
111119

120+
# If the header is a Mojom header, wait until the end to yield it
121+
if cleanup_mojom_headers and MOJOM_HEADER_REGEX.match(header):
122+
assert header not in pending_mojom_changes[filename]
123+
124+
pending_mojom_changes[filename][header] = (change_type, line, *_)
125+
continue
126+
112127
if change_type_filter and change_type != change_type_filter:
113128
continue
114129

@@ -152,6 +167,41 @@ def filter_changes(
152167

153168
yield (change_type.value, line, filename, header, *_)
154169

170+
if cleanup_mojom_headers:
171+
for filename in pending_mojom_changes:
172+
for header in pending_mojom_changes[filename]:
173+
change_type, line, *_ = pending_mojom_changes[filename][header]
174+
175+
header_prefix = MOJOM_HEADER_REGEX.match(header).group(1)
176+
canceled_out = False
177+
178+
for header_suggestion in pending_mojom_changes[filename]:
179+
match = MOJOM_HEADER_REGEX.match(header_suggestion)
180+
181+
# If the header suggestion is for a different variant of a Mojom header
182+
# which has a pending change of the opposite type, cancel them out
183+
if match and header_prefix == match.group(1):
184+
if (
185+
change_type is IncludeChange.ADD
186+
and pending_mojom_changes[filename][header_suggestion][0] is IncludeChange.REMOVE
187+
):
188+
canceled_out = True
189+
break
190+
elif (
191+
change_type is IncludeChange.REMOVE
192+
and pending_mojom_changes[filename][header_suggestion][0] is IncludeChange.ADD
193+
):
194+
canceled_out = True
195+
break
196+
197+
if canceled_out:
198+
continue
199+
200+
if change_type_filter and change_type != change_type_filter:
201+
continue
202+
203+
yield (change_type.value, line, filename, header, *_)
204+
155205

156206
def main():
157207
parser = argparse.ArgumentParser(description="Filter include changes output")
@@ -172,6 +222,7 @@ def main():
172222
parser.add_argument("--no-filter-generated-files", action="store_true", help="Don't filter out generated files.")
173223
parser.add_argument("--no-filter-mojom-headers", action="store_true", help="Don't filter out mojom headers.")
174224
parser.add_argument("--no-filter-ignores", action="store_true", help="Don't filter out ignores.")
225+
parser.add_argument("--no-cleanup-mojom-headers", action="store_true", help="Don't clean up mojom header changes.")
175226
group = parser.add_mutually_exclusive_group()
176227
group.add_argument("--add-only", action="store_true", default=False, help="Only output includes to add.")
177228
group.add_argument("--remove-only", action="store_true", default=False, help="Only output includes to remove.")
@@ -190,6 +241,10 @@ def main():
190241
print("error: --header-filter is not a valid regex")
191242
return 1
192243

244+
if args.no_cleanup_mojom_headers and not args.no_filter_mojom_headers:
245+
print("error: --no-cleanup-mojom-headers option requires --no-filter-mojom-headers")
246+
return 1
247+
193248
if args.verbose:
194249
logging.basicConfig(level=logging.DEBUG)
195250

@@ -223,6 +278,7 @@ def main():
223278
filter_third_party=args.filter_third_party,
224279
header_mappings=config.headerMappings if config else None,
225280
weight_threshold=args.weight_threshold,
281+
cleanup_mojom_headers=not args.no_cleanup_mojom_headers,
226282
):
227283
csv_writer.writerow(change)
228284

0 commit comments

Comments
 (0)