From afd5faceec72d9f40aaaf5191da7086d2393f05d Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 7 Nov 2023 11:36:52 +1100 Subject: [PATCH] Tools: add edit to use std min/max instead of MIN2/MAX2 macros --- tools/utils_maintenance/code_clean.py | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tools/utils_maintenance/code_clean.py b/tools/utils_maintenance/code_clean.py index 79083346b56..42b486e5f1d 100755 --- a/tools/utils_maintenance/code_clean.py +++ b/tools/utils_maintenance/code_clean.py @@ -1076,6 +1076,41 @@ class edit_generators: return edits + class use_std_min_max(EditGenerator): + """ + Use `std::min` & `std::max` instead of `MIN2`, `MAX2` macros: + + Replace: + MAX2(a, b) + With: + std::max(a, b) + """ + is_default = True + + @staticmethod + def edit_list_from_file(source: str, data: str, _shared_edit_data: Any) -> List[Edit]: + edits: List[Edit] = [] + + # The user might include C & C++, if they forget, it is better not to operate on C. + if source.lower().endswith((".h", ".c")): + return edits + + for src, dst in ( + ("MIN2", "std::min"), + ("MAX2", "std::max"), + ): + for match in re.finditer( + (r"\b(" + src + r")\("), + data, + ): + edits.append(Edit( + span=match.span(1), + content=dst, + content_fail='__ALWAYS_FAIL__', + )) + + return edits + class use_str_sizeof_macros(EditGenerator): """ Use `STRNCPY` & `SNPRINTF` macros: