Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:09:26

0001 #!/usr/bin/env python3
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 ex = re.compile(r"(\b(?<!std::)size_t)\b")
0011 
0012 github = "GITHUB_ACTIONS" in os.environ
0013 
0014 
0015 def main():
0016     p = argparse.ArgumentParser()
0017     p.add_argument("input")
0018     p.add_argument(
0019         "--fix", action="store_true", help="Attempt to fix any license issues found."
0020     )
0021     p.add_argument("--exclude", "-e", action="append", default=[])
0022 
0023     args = p.parse_args()
0024 
0025     # walk over all files
0026     exit = 0
0027     for root, _, files in os.walk("."):
0028         root = Path(root)
0029         for filename in files:
0030             # get the full path of the file
0031             filepath = root / filename
0032             if filepath.suffix not in (
0033                 ".hpp",
0034                 ".cpp",
0035                 ".ipp",
0036                 ".h",
0037                 ".C",
0038                 ".c",
0039                 ".cu",
0040                 ".cuh",
0041             ):
0042                 continue
0043 
0044             if any([fnmatch(str(filepath), e) for e in args.exclude]):
0045                 continue
0046 
0047             changed_lines = handle_file(filepath, fix=args.fix)
0048             if len(changed_lines) > 0:
0049                 exit = 1
0050                 print()
0051                 print(filepath)
0052                 for i, oline in changed_lines:
0053                     print(f"{i}: {oline}")
0054 
0055                     if github:
0056                         print(
0057                             f"::error file={filepath},line={i+1},title=Do not use C-style size_t::Replace size_t with std::size_t"
0058                         )
0059 
0060     return exit
0061 
0062 
0063 def handle_file(file: Path, fix: bool) -> list[tuple[int, str]]:
0064     content = file.read_text()
0065     lines = content.splitlines()
0066 
0067     changed_lines = []
0068 
0069     for i, oline in enumerate(lines):
0070         line, n_subs = ex.subn(r"std::size_t", oline)
0071         lines[i] = line
0072         if n_subs > 0:
0073             changed_lines.append((i, oline))
0074 
0075     if fix and len(changed_lines) > 0:
0076         file.write_text("\n".join(lines) + "\n")
0077 
0078     return changed_lines
0079 
0080 
0081 if "__main__" == __name__:
0082     sys.exit(main())