diff --git a/tools/pr_overlap.py b/tools/pr_overlap.py index f7612d7214b4..ecc31eb8270e 100644 --- a/tools/pr_overlap.py +++ b/tools/pr_overlap.py @@ -698,12 +698,62 @@ def canonical(name): return NID_DISAMBIG.sub("", nid_names.strip_nid_postfix(name)) +def body_exports(pr, files, names): + paths = sorted(path for path in files if path.endswith(".cpp") and library_for_path(path) in names + and not any(part.lower() in {"test", "tests"} for part in Path(path).parts)) + if not paths: + return set() + base = merge_base(pr["ref"]) + found = set() + for path in paths: + ranges = [(int(start), int(count or 1)) for start, count in HUNK.findall( + git("diff", "-U0", "--no-color", "--no-renames", base, pr["ref"], "--", path))] + if not ranges: + continue + source = git("show", f"{base}:{path}") + source = nid_names.CPP_TOKEN.sub( + lambda match: match.group() if match.group("code") is not None else re.sub(r"[^\n]", " ", match.group()), source) + for definition in nid_names.DEFINITION.finditer(source): + requested = names[library_for_path(path)].get(canonical(definition.group(1))) + if not requested: + continue + depth = 0 + end = None + for brace in re.finditer(r"[{}]", source[definition.end() - 1:]): + depth += 1 if brace.group() == "{" else -1 + if depth == 0: + end = definition.end() - 1 + brace.end() + if not re.match(r"\s*catch\b", source[end:]): + break + if end is None: + raise ValueError(f"cannot locate the end of {definition.group(1)} in {path}") + first = source.count("\n", 0, definition.start()) + 1 + last = source.count("\n", 0, end) + 1 + if any(first <= start < last if count == 0 else start <= last and start + count - 1 >= first + for start, count in ranges): + found.update(requested) + return found + + def pull_requests_with(names, prs, jobs): wanted = {canonical(name) for name in names} nids = {nid_names.compute_nid(name): name for name in wanted} found = {name: [] for name in wanted} + bodies = {} + for (library, nid), defined in base_exports(BASE_REF).items(): + requested = wanted & {canonical(name) for name in defined} + if nid in nids: + requested.add(nids[nid]) + if requested: + for name in defined: + bodies.setdefault(library, {}).setdefault(canonical(name), set()).update(requested) + + def inspect(pr): + files, (exported, added), existing = probe(pr) + return files, (exported | body_exports(pr, existing, bodies), added), existing + with ThreadPoolExecutor(max_workers=jobs) as executor: - for index, (number, (_files, (exported, added), _existing)) in enumerate(zip(prs, executor.map(probe, prs.values())), 1): + for index, (number, (_files, (exported, added), _existing)) in enumerate(zip(prs, executor.map(inspect, prs.values())), 1): touched = {canonical(name) for name in exported} for (_library, nid), defined in added.items(): touched.update(canonical(name) for name in defined) @@ -784,7 +834,7 @@ def main(argv=None): parser.add_argument("--out", type=Path, help="write .md for every open pull request (empty when clean)") parser.add_argument("--branch", help="check a local branch against the open pull requests instead") parser.add_argument("--names", nargs="+", metavar="NAME", - help="before writing code, show whether each function is implemented on the base and which open pull requests add it") + help="before writing code, show whether each function is implemented on the base and which open pull requests add it or edit its body") parser.add_argument("--repo", help="owner/name; defaults to the upstream of the current checkout") parser.add_argument("--jobs", type=int, default=4, help="number of pull request comparisons to run at once") args = parser.parse_args(argv) diff --git a/tools/tests/test_pr_overlap_names.py b/tools/tests/test_pr_overlap_names.py index 7d73561e0bae..b1dea910a800 100644 --- a/tools/tests/test_pr_overlap_names.py +++ b/tools/tests/test_pr_overlap_names.py @@ -69,6 +69,75 @@ def test_finds_the_pull_requests_that_add_each_name(self): found = pr_overlap.pull_requests_with(["pending_nid_postfix", "legacy_target", "done", "absent"], self.prs, 2) self.assertEqual(found, {"pending": [1], "legacy_target": [2], "done": [], "absent": []}) + def test_finds_body_edits_without_reporting_other_exports_as_added(self): + source = ( + 'int APS5_VABI done_nid_postfix(int value) { const char* closing = "}";\n return value;\n}\n' + "int APS5_VABI untouched(int value) {\n return value;\n}\n" + "int APS5_VABI helper_nid_no_patch(int value) {\n return value;\n}\n" + ) + test_source = "int APS5_VABI test_only(int value) {\n return value;\n}\n" + path = "core/libs/prx/libSceExample/Export.cpp" + test_path = "core/libs/prx/libSceExample/tests/Body.cpp" + base = commit_on_base(run("rev-parse", "refs/pr/base"), "multiline exports", { + path: source, test_path: test_source, + }) + run("update-ref", "refs/pr/base", base) + changed = source.replace("return value;", "return value + 1;", 1) + changed = changed.replace("helper_nid_no_patch(int value) {\n return value;", + "helper_nid_no_patch(int value) {\n return value + 2;") + sha = commit_on_base(base, "edit bodies", { + path: changed, test_path: test_source.replace("return value;", "return value + 3;"), + }) + run("update-ref", "refs/pr/4", sha) + pr = {"ref": "refs/pr/4", "sha": sha, "author": "author4", "title": "body edits", "depends": set()} + found = pr_overlap.pull_requests_with(["done", "untouched", "helper_nid_no_patch", "test_only"], {4: pr}, 2) + self.assertEqual(found, {"done": [4], "untouched": [], "helper_nid_no_patch": [], "test_only": []}) + self.assertEqual(pr_overlap.probe(pr)[1][0], set()) + + def test_global_edit_after_an_export_does_not_report_the_export(self): + path = "core/libs/prx/libSceExample/Export.cpp" + source = "int APS5_VABI done(int value) {\n return value;\n}\n\nint setting = 0;\n" + base = commit_on_base(run("rev-parse", "refs/pr/base"), "multiline export and global", {path: source}) + run("update-ref", "refs/pr/base", base) + sha = commit_on_base(base, "edit global", {path: source.replace("setting = 0", "setting = 1")}) + run("update-ref", "refs/pr/4", sha) + pr = {"ref": "refs/pr/4", "sha": sha, "author": "author4", "title": "global edit", "depends": set()} + self.assertEqual(pr_overlap.pull_requests_with(["done"], {4: pr}, 2), {"done": []}) + + def test_insertion_inside_an_export_and_after_it(self): + path = "core/libs/prx/libSceExample/Export.cpp" + source = "int APS5_VABI done(int value) {\n return value;\n}\n" + base = commit_on_base(run("rev-parse", "refs/pr/base"), "multiline export", {path: source}) + run("update-ref", "refs/pr/base", base) + for changed, expected in [(source.replace(" return", " ++value;\n return"), [4]), + (source + "\nint setting = 1;\n", [])]: + with self.subTest(expected=expected): + sha = commit_on_base(base, "insert a line", {path: changed}) + run("update-ref", "refs/pr/4", sha) + pr = {"ref": "refs/pr/4", "sha": sha, "author": "author4", "title": "insertion", "depends": set()} + self.assertEqual(pr_overlap.pull_requests_with(["done"], {4: pr}, 2), {"done": expected}) + + def test_finds_body_edits_through_an_existing_nid_alias(self): + path = "core/libs/prx/libSceExample/Export.cpp" + source = ('int APS5_VABI legacyHelper_nid_no_patch() {\n return 0;\n}\n' + 'APS5_EXPORT("dbwyFdWSTxw", legacyHelper_nid_no_patch);\n') + base = commit_on_base(run("rev-parse", "refs/pr/base"), "multiline alias target", {path: source}) + run("update-ref", "refs/pr/base", base) + sha = commit_on_base(base, "edit alias target body", {path: source.replace("return 0;", "return 1;")}) + run("update-ref", "refs/pr/4", sha) + pr = {"ref": "refs/pr/4", "sha": sha, "author": "author4", "title": "alias body edit", "depends": set()} + self.assertEqual(pr_overlap.pull_requests_with(["legacy_target"], {4: pr}, 2), {"legacy_target": [4]}) + + def test_finds_an_edit_in_a_function_try_block_handler(self): + path = "core/libs/prx/libSceExample/Export.cpp" + source = "int APS5_VABI done(int value) try {\n return value;\n} catch (...) {\n return 0;\n}\n" + base = commit_on_base(run("rev-parse", "refs/pr/base"), "function try block", {path: source}) + run("update-ref", "refs/pr/base", base) + sha = commit_on_base(base, "edit handler", {path: source.replace("return 0;", "return 1;")}) + run("update-ref", "refs/pr/4", sha) + pr = {"ref": "refs/pr/4", "sha": sha, "author": "author4", "title": "handler edit", "depends": set()} + self.assertEqual(pr_overlap.pull_requests_with(["done"], {4: pr}, 2), {"done": [4]}) + def test_reports_the_state_on_the_base_through_names_and_nid_aliases(self): status = pr_overlap.main_status(["done", "pending", "legacy_target", "absent"], "refs/pr/base") library = "core/libs/prx/libSceExample"