File indexing completed on 2026-06-16 07:46:18
0001
0002
0003 from pathlib import Path
0004 import os
0005 import argparse
0006 from fnmatch import fnmatch
0007 import re
0008 import sys
0009
0010 type_list = [
0011 "size_t",
0012 "ptrdiff_t",
0013 "nullptr_t",
0014 "int8_t",
0015 "int16_t",
0016 "int32_t",
0017 "int64_t",
0018 "uint8_t",
0019 "uint16_t",
0020 "uint32_t",
0021 "uint64_t",
0022 "max_align_t",
0023 ]
0024
0025 github = "GITHUB_ACTIONS" in os.environ
0026
0027
0028 def handle_file(file: Path, fix: bool, c_type: str) -> list[tuple[int, str]]:
0029 ex = re.compile(rf"\b(?<!std::){c_type}\b")
0030
0031 content = file.read_text()
0032 lines = content.splitlines()
0033
0034 changed_lines = []
0035
0036 for i, oline in enumerate(lines):
0037 line, n_subs = ex.subn(rf"std::{c_type}", oline)
0038 lines[i] = line
0039 if n_subs > 0:
0040 changed_lines.append((i, oline))
0041
0042 if fix and len(changed_lines) > 0:
0043 file.write_text("\n".join(lines) + "\n")
0044
0045 return changed_lines
0046
0047
0048 def main():
0049 p = argparse.ArgumentParser()
0050 p.add_argument("input", nargs="+")
0051 p.add_argument("--fix", action="store_true", help="Attempt to fix C-style types.")
0052 p.add_argument("--exclude", "-e", action="append", default=[])
0053
0054 args = p.parse_args()
0055
0056 exit_code = 0
0057
0058 inputs = []
0059
0060 if len(args.input) == 1 and os.path.isdir(args.input[0]):
0061
0062 for root, _, files in os.walk(args.input[0]):
0063 root = Path(root)
0064 for filename in files:
0065
0066 filepath = root / filename
0067 if filepath.suffix not in (
0068 ".hpp",
0069 ".cpp",
0070 ".ipp",
0071 ".h",
0072 ".C",
0073 ".c",
0074 ".cu",
0075 ".cuh",
0076 ):
0077 continue
0078
0079 if any([fnmatch(str(filepath), e) for e in args.exclude]):
0080 continue
0081
0082 inputs.append(filepath)
0083 else:
0084 for file in args.input:
0085 inputs.append(Path(file))
0086
0087 for filepath in inputs:
0088 for c_type in type_list:
0089 changed_lines = handle_file(file=filepath, fix=args.fix, c_type=c_type)
0090 if len(changed_lines) > 0:
0091 exit_code = 1
0092 print()
0093 print(filepath)
0094 for i, oline in changed_lines:
0095 print(f"{i}: {oline}")
0096
0097 if github:
0098 print(
0099 f"::error file={filepath},line={i+1},title=Do not use C-style {c_type}::Replace {c_type} with std::{c_type}"
0100 )
0101
0102 return exit_code
0103
0104
0105 if "__main__" == __name__:
0106 sys.exit(main())