https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/176927
>From 3d4bee4370cebb6ff483c547f32d352e7a7b96f8 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Tue, 20 Jan 2026 17:04:43 +0300 Subject: [PATCH 1/8] [clang-tidy][NFC] Run ruff over python scripts --- clang-tools-extra/clang-tidy/add_new_check.py | 3 +-- clang-tools-extra/clang-tidy/rename_check.py | 5 ++--- clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py | 2 +- clang-tools-extra/clang-tidy/tool/run-clang-tidy.py | 8 ++++---- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index 80e1afe1a121c..dfa60ce7f3990 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -176,7 +176,7 @@ def write_implementation( } // namespace clang::tidy::%(namespace)s """ - % {"check_name": check_name_camel, "module": module, "namespace": namespace} + % {"check_name": check_name_camel, "namespace": namespace} ) @@ -578,7 +578,6 @@ def format_link_alias(doc_file: Tuple[str, str]) -> str: "check_name": check_name, "module": module, "check_file": check_file, - "target": target, "title": title, "autofix": autofix, } diff --git a/clang-tools-extra/clang-tidy/rename_check.py b/clang-tools-extra/clang-tidy/rename_check.py index b864bff814485..d5432eadcff6b 100755 --- a/clang-tools-extra/clang-tidy/rename_check.py +++ b/clang-tools-extra/clang-tidy/rename_check.py @@ -65,12 +65,12 @@ def deleteMatchingLines(fileName: str, pattern: str) -> bool: with io.open(fileName, "r", encoding="utf8") as f: lines = f.readlines() - not_matching_lines = [l for l in lines if not re.search(pattern, l)] + not_matching_lines = [line for line in lines if not re.search(pattern, line)] if len(not_matching_lines) == len(lines): return False print("Removing lines matching '%s' in '%s'..." % (pattern, fileName)) - print(" " + " ".join([l for l in lines if re.search(pattern, l)])) + print(" " + " ".join([line for line in lines if re.search(pattern, line)])) with io.open(fileName, "w", encoding="utf8") as f: f.writelines(not_matching_lines) @@ -307,7 +307,6 @@ def main() -> None: ) for filename in getListOfFiles(clang_tidy_path): - originalName = filename filename = fileRename( filename, old_module + "/" + old_name, new_module + "/" + new_name ) diff --git a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py index c090bdc1151df..07f2b911a03a2 100755 --- a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py +++ b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py @@ -427,7 +427,7 @@ def main(): print(f"Writing fixes to {args.export_fixes} ...") try: merge_replacement_files(export_fixes_dir, args.export_fixes) - except: + except Exception: sys.stderr.write("Error exporting fixes.\n") traceback.print_exc() return_code = 1 diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py index 59523fd131185..f4c3d00734389 100755 --- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py +++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py @@ -667,7 +667,7 @@ async def main() -> None: subprocess.check_call( invocation, stdout=subprocess.DEVNULL if args.quiet else None ) - except: + except Exception: print("Unable to run clang-tidy.", file=sys.stderr) sys.exit(1) @@ -681,7 +681,7 @@ async def main() -> None: if args.source_filter: try: source_filter_re = re.compile(args.source_filter) - except: + except Exception: print( "Error: unable to compile regex from arg -source-filter:", file=sys.stderr, @@ -764,7 +764,7 @@ async def main() -> None: try: assert export_fixes_dir merge_replacement_files(export_fixes_dir, args.export_fixes) - except: + except Exception: print("Error exporting fixes.\n", file=sys.stderr) traceback.print_exc() returncode = 1 @@ -775,7 +775,7 @@ async def main() -> None: try: assert export_fixes_dir apply_fixes(args, clang_apply_replacements_binary, export_fixes_dir) - except: + except Exception: print("Error applying fixes.\n", file=sys.stderr) traceback.print_exc() returncode = 1 >From b7141da79518eecbaab4bd018681725743dd5ed3 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Tue, 20 Jan 2026 20:09:39 +0300 Subject: [PATCH 2/8] better --- clang-tools-extra/clang-tidy/add_new_check.py | 30 +++++++------------ clang-tools-extra/clang-tidy/rename_check.py | 2 +- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index dfa60ce7f3990..1e4beac0e9344 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -218,8 +218,7 @@ def adapt_module( while True: line = next(lines_iter) if not header_added: - match = re.search('#include "(.*)"', line) - if match: + if match := re.search('#include "(.*)"', line): header_found = True if match.group(1) > check_name_camel: header_added = True @@ -233,11 +232,10 @@ def adapt_module( check_added = True f.write(check_decl) else: - match = re.search( - r'registerCheck<(.*)> *\( *(?:"([^"]*)")?', line - ) prev_line = None - if match: + if match := re.search( + r'registerCheck<(.*)> *\( *(?:"([^"]*)")?', line + ): current_check_name = match.group(2) if current_check_name is None: # If we didn't find the check name on this line, look on the @@ -410,17 +408,13 @@ def filename_from_module(module_name: str, check_name: str) -> str: with io.open(module_file, "r") as f: code = f.read() full_check_name = module_name + "-" + check_name - name_pos = code.find('"' + full_check_name + '"') - if name_pos == -1: + if (name_pos := code.find('"' + full_check_name + '"')) == -1: return "" - stmt_end_pos = code.find(";", name_pos) - if stmt_end_pos == -1: - return "" - stmt_start_pos = code.rfind(";", 0, name_pos) - if stmt_start_pos == -1: - stmt_start_pos = code.rfind("{", 0, name_pos) - if stmt_start_pos == -1: + if (stmt_end_pos := code.find(";", name_pos)) == -1: return "" + if (stmt_start_pos := code.rfind(";", 0, name_pos)) == -1: + if (stmt_start_pos := code.rfind("{", 0, name_pos)) == -1: + return "" stmt = code[stmt_start_pos + 1 : stmt_end_pos] matches = re.search(r'registerCheck<([^>:]*)>\(\s*"([^"]*)"\s*\)', stmt) if matches and matches[2] == full_check_name: @@ -493,8 +487,7 @@ def has_auto_fix(check_name: str) -> str: if has_fixits(code): return ' "Yes"' - base_class = get_base_class(code, check_file) - if base_class: + if base_class := get_base_class(code, check_file): base_file = os.path.join(clang_tidy_path, dirname, base_class + ".cpp") if os.path.isfile(base_file): with io.open(base_file, encoding="utf8") as f: @@ -752,8 +745,7 @@ def main() -> None: if language == "c": language_restrict = "!%(lang)s.CPlusPlus" - extra = c_language_to_requirements.get(args.standard, None) - if extra: + if extra := c_language_to_requirements.get(args.standard, None): language_restrict += f" && %(lang)s.{extra}" elif language == "c++": language_restrict = ( diff --git a/clang-tools-extra/clang-tidy/rename_check.py b/clang-tools-extra/clang-tidy/rename_check.py index d5432eadcff6b..51759dd12addf 100755 --- a/clang-tools-extra/clang-tidy/rename_check.py +++ b/clang-tools-extra/clang-tidy/rename_check.py @@ -70,7 +70,7 @@ def deleteMatchingLines(fileName: str, pattern: str) -> bool: return False print("Removing lines matching '%s' in '%s'..." % (pattern, fileName)) - print(" " + " ".join([line for line in lines if re.search(pattern, line)])) + print(" " + " ".join(line for line in lines if re.search(pattern, line))) with io.open(fileName, "w", encoding="utf8") as f: f.writelines(not_matching_lines) >From a51b34c7921f600820c8db8cf2af62a6266d486d Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Tue, 20 Jan 2026 20:18:25 +0300 Subject: [PATCH 3/8] better --- clang-tools-extra/clang-tidy/add_new_check.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index 1e4beac0e9344..e38d9509e7bb0 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -15,6 +15,7 @@ import re import sys import textwrap +from operator import methodcaller # FIXME Python 3.9: Replace typing.Tuple with builtins.tuple. from typing import Optional, Tuple, Match @@ -391,7 +392,7 @@ def update_checks_list(clang_tidy_path: str) -> None: lambda s: os.path.isdir(os.path.join(docs_dir, s)), os.listdir(docs_dir) ): for file in filter( - lambda s: s.endswith(".rst"), os.listdir(os.path.join(docs_dir, subdir)) + methodcaller("endswith", ".rst"), os.listdir(os.path.join(docs_dir, subdir)) ): doc_files.append((subdir, file)) doc_files.sort() @@ -412,9 +413,10 @@ def filename_from_module(module_name: str, check_name: str) -> str: return "" if (stmt_end_pos := code.find(";", name_pos)) == -1: return "" - if (stmt_start_pos := code.rfind(";", 0, name_pos)) == -1: - if (stmt_start_pos := code.rfind("{", 0, name_pos)) == -1: - return "" + if (stmt_start_pos := code.rfind(";", 0, name_pos)) == -1 and ( + stmt_start_pos := code.rfind("{", 0, name_pos) + ) == -1: + return "" stmt = code[stmt_start_pos + 1 : stmt_end_pos] matches = re.search(r'registerCheck<([^>:]*)>\(\s*"([^"]*)"\s*\)', stmt) if matches and matches[2] == full_check_name: >From 42b32cb06f4bad29310c47a2cecbdc283216fd0d Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Sun, 25 Jan 2026 15:34:36 +0300 Subject: [PATCH 4/8] fixes --- clang-tools-extra/clang-tidy/add_new_check.py | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index e38d9509e7bb0..8bb5ca486ab40 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -285,18 +285,15 @@ def add_release_notes( for line in lines: if not note_added: - match = lineMatcher.match(line) - match_next = nextSectionMatcher.match(line) - match_check = checkMatcher.match(line) - if match_check: + if match_check := checkMatcher.match(line): last_check = match_check.group(1) if last_check > check_name_dashes: add_note_here = True - if match_next: + if nextSectionMatcher.match(line): add_note_here = True - if match: + if lineMatcher.match(line): header_found = True f.write(line) continue @@ -481,7 +478,7 @@ def has_auto_fix(check_name: str) -> str: if not os.path.isfile(check_file): # Some checks aren't in a file based on the check name. check_file = filename_from_module(dirname, check_name) - if not check_file or not os.path.isfile(check_file): + if not (check_file and os.path.isfile(check_file)): return "" with io.open(check_file, encoding="utf8") as f: @@ -504,9 +501,8 @@ def process_doc(doc_file: Tuple[str, str]) -> Tuple[str, Optional[Match[str]]]: with io.open(os.path.join(docs_dir, *doc_file), "r", encoding="utf8") as doc: content = doc.read() - match = re.search(".*:orphan:.*", content) - if match: + if match := re.search(".*:orphan:.*", content): # Orphan page, don't list it. return "", None @@ -579,9 +575,6 @@ def format_link_alias(doc_file: Tuple[str, str]) -> str: ) return "" - checks = map(format_link, doc_files) - checks_alias = map(format_link_alias, doc_files) - print("Updating %s..." % filename) with io.open(filename, "w", encoding="utf8", newline="\n") as f: for line in lines: @@ -589,12 +582,12 @@ def format_link_alias(doc_file: Tuple[str, str]) -> str: if line.strip() == ".. csv-table::": # We dump the checkers f.write(' :header: "Name", "Offers fixes"\n\n') - f.writelines(checks) + f.writelines(map(format_link, doc_files)) # and the aliases f.write("\nCheck aliases\n-------------\n\n") f.write(".. csv-table::\n") f.write(' :header: "Name", "Redirect", "Offers fixes"\n\n') - f.writelines(checks_alias) + f.writelines(map(format_link_alias, doc_files)) break >From 624807f364ef7fb6d775df4b83c02bdd3ac0bdf5 Mon Sep 17 00:00:00 2001 From: Baranov Victor <[email protected]> Date: Sun, 25 Jan 2026 23:27:50 +0300 Subject: [PATCH 5/8] Apply suggestion from @vbvictor --- clang-tools-extra/clang-tidy/add_new_check.py | 1 - 1 file changed, 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index 8bb5ca486ab40..c3c4e9616e49b 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -17,7 +17,6 @@ import textwrap from operator import methodcaller -# FIXME Python 3.9: Replace typing.Tuple with builtins.tuple. from typing import Optional, Tuple, Match >From 152da53c8860ab80516c24c45ad864bb0405579c Mon Sep 17 00:00:00 2001 From: Baranov Victor <[email protected]> Date: Mon, 26 Jan 2026 18:15:13 +0300 Subject: [PATCH 6/8] Update clang-tools-extra/clang-tidy/add_new_check.py Co-authored-by: EugeneZelenko <[email protected]> --- clang-tools-extra/clang-tidy/add_new_check.py | 1 - 1 file changed, 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index c3c4e9616e49b..44466504c35fd 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -16,7 +16,6 @@ import sys import textwrap from operator import methodcaller - from typing import Optional, Tuple, Match >From e5dbee902fc9df403868821180c12542a4995363 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Mon, 26 Jan 2026 18:45:11 +0300 Subject: [PATCH 7/8] better --- clang-tools-extra/clang-tidy/add_new_check.py | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index 44466504c35fd..5f89f3ebef41b 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -26,9 +26,9 @@ def adapt_cmake(module_path: str, check_name_camel: str) -> bool: # The documentation files are encoded using UTF-8, however on Windows the # default encoding might be different (e.g. CP-1252). To make sure UTF-8 is - # always used, use `io.open(filename, mode, encoding='utf8')` for reading and + # always used, use `open(filename, mode, encoding='utf8')` for reading and # writing files here and elsewhere. - with io.open(filename, "r", encoding="utf8") as f: + with open(filename, "r", encoding="utf8") as f: lines = f.readlines() cpp_file = check_name_camel + ".cpp" @@ -39,7 +39,7 @@ def adapt_cmake(module_path: str, check_name_camel: str) -> bool: return False print("Updating %s..." % filename) - with io.open(filename, "w", encoding="utf8", newline="\n") as f: + with open(filename, "w", encoding="utf8", newline="\n") as f: cpp_found = False file_added = False for line in lines: @@ -80,7 +80,7 @@ def write_header( override_supported = "" filename = os.path.join(module_path, check_name_camel) + ".h" print("Creating %s..." % filename) - with io.open(filename, "w", encoding="utf8", newline="\n") as f: + with open(filename, "w", encoding="utf8", newline="\n") as f: header_guard = ( "LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_" + module.upper() @@ -139,7 +139,7 @@ def write_implementation( ) -> None: filename = os.path.join(module_path, check_name_camel) + ".cpp" print("Creating %s..." % filename) - with io.open(filename, "w", encoding="utf8", newline="\n") as f: + with open(filename, "w", encoding="utf8", newline="\n") as f: f.write( """\ //===----------------------------------------------------------------------===// @@ -195,11 +195,11 @@ def adapt_module( module_path: str, module: str, check_name: str, check_name_camel: str ) -> None: filename = get_module_filename(module_path, module) - with io.open(filename, "r", encoding="utf8") as f: + with open(filename, "r", encoding="utf8") as f: lines = f.readlines() print("Updating %s..." % filename) - with io.open(filename, "w", encoding="utf8", newline="\n") as f: + with open(filename, "w", encoding="utf8", newline="\n") as f: header_added = False header_found = False check_added = False @@ -268,7 +268,7 @@ def add_release_notes( filename = os.path.normpath( os.path.join(module_path, "../../docs/ReleaseNotes.rst") ) - with io.open(filename, "r", encoding="utf8") as f: + with open(filename, "r", encoding="utf8") as f: lines = f.readlines() lineMatcher = re.compile("New checks") @@ -276,7 +276,7 @@ def add_release_notes( checkMatcher = re.compile("- New :doc:`(.*)") print("Updating %s..." % filename) - with io.open(filename, "w", encoding="utf8", newline="\n") as f: + with open(filename, "w", encoding="utf8", newline="\n") as f: note_added = False header_found = False add_note_here = False @@ -339,7 +339,7 @@ def write_test( ) ) print("Creating %s..." % filename) - with io.open(filename, "w", encoding="utf8", newline="\n") as f: + with open(filename, "w", encoding="utf8", newline="\n") as f: f.write( """\ // RUN: %%check_clang_tidy %(standard)s%%s %(check_name_dashes)s %%t @@ -379,7 +379,7 @@ def update_checks_list(clang_tidy_path: str) -> None: docs_dir = os.path.join(clang_tidy_path, "../docs/clang-tidy/checks") filename = os.path.normpath(os.path.join(docs_dir, "list.rst")) # Read the content of the current list.rst file - with io.open(filename, "r", encoding="utf8") as f: + with open(filename, "r", encoding="utf8") as f: lines = f.readlines() # Get all existing docs doc_files = [] @@ -401,7 +401,7 @@ def filename_from_module(module_name: str, check_name: str) -> str: module_file = get_module_filename(module_path, module_name) if not os.path.isfile(module_file): return "" - with io.open(module_file, "r") as f: + with open(module_file, "r") as f: code = f.read() full_check_name = module_name + "-" + check_name if (name_pos := code.find('"' + full_check_name + '"')) == -1: @@ -439,7 +439,7 @@ def get_base_class(code: str, check_file: str) -> str: header_file = os.path.splitext(check_file)[0] + ".h" if not os.path.isfile(header_file): return "" - with io.open(header_file, encoding="utf8") as f: + with open(header_file, encoding="utf8") as f: code = f.read() matches = re.search(" " + ctor_pattern, code) @@ -479,7 +479,7 @@ def has_auto_fix(check_name: str) -> str: if not (check_file and os.path.isfile(check_file)): return "" - with io.open(check_file, encoding="utf8") as f: + with open(check_file, encoding="utf8") as f: code = f.read() if has_fixits(code): return ' "Yes"' @@ -487,7 +487,7 @@ def has_auto_fix(check_name: str) -> str: if base_class := get_base_class(code, check_file): base_file = os.path.join(clang_tidy_path, dirname, base_class + ".cpp") if os.path.isfile(base_file): - with io.open(base_file, encoding="utf8") as f: + with open(base_file, encoding="utf8") as f: code = f.read() if has_fixits(code): return ' "Yes"' @@ -497,7 +497,7 @@ def has_auto_fix(check_name: str) -> str: def process_doc(doc_file: Tuple[str, str]) -> Tuple[str, Optional[Match[str]]]: check_name = doc_file[0] + "-" + doc_file[1].replace(".rst", "") - with io.open(os.path.join(docs_dir, *doc_file), "r", encoding="utf8") as doc: + with open(os.path.join(docs_dir, *doc_file), "r", encoding="utf8") as doc: content = doc.read() if match := re.search(".*:orphan:.*", content): @@ -574,7 +574,7 @@ def format_link_alias(doc_file: Tuple[str, str]) -> str: return "" print("Updating %s..." % filename) - with io.open(filename, "w", encoding="utf8", newline="\n") as f: + with open(filename, "w", encoding="utf8", newline="\n") as f: for line in lines: f.write(line) if line.strip() == ".. csv-table::": @@ -598,7 +598,7 @@ def write_docs(module_path: str, module: str, check_name: str) -> None: ) ) print("Creating %s..." % filename) - with io.open(filename, "w", encoding="utf8", newline="\n") as f: + with open(filename, "w", encoding="utf8", newline="\n") as f: f.write( """.. title:: clang-tidy - %(check_name_dashes)s >From cef99911ac853d3d6b87b87466a059eab11b0943 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Mon, 26 Jan 2026 18:49:24 +0300 Subject: [PATCH 8/8] remove io --- clang-tools-extra/clang-tidy/add_new_check.py | 1 - 1 file changed, 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index 5f89f3ebef41b..2f284a30ab4cf 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -9,7 +9,6 @@ # ===-----------------------------------------------------------------------===# import argparse -import io import itertools import os import re _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
