Skip to content

Commit

Permalink
ci: generalise job size_t to type_t (acts-project#3302)
Browse files Browse the repository at this point in the history
blocked by:
- acts-project#3301
  • Loading branch information
AJPfleger authored and Tim Adye committed Jun 27, 2024
1 parent 971dd2e commit 05040cf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
- name: Check
run: >
CI/check_end_of_file.py . --exclude "thirdparty/*" --reject-multiple-newlines --github
size_t:
type_t:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -86,7 +86,7 @@ jobs:
python-version: '3.12'
- name: Check
run: >
CI/check_size_t.py . --exclude "thirdparty/*"
CI/check_type_t.py . --exclude "thirdparty/*"
boost_test_macro:
runs-on: ubuntu-latest
steps:
Expand Down
90 changes: 53 additions & 37 deletions CI/check_size_t.py → CI/check_type_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,56 @@
import re
import sys

ex = re.compile(r"(\b(?<!std::)size_t)\b")

type_list = [
"size_t",
"ptrdiff_t",
"nullptr_t",
"int8_t",
"int16_t",
"int32_t",
"int64_t",
"uint8_t",
"uint16_t",
"uint32_t",
"uint64_t",
"max_align_t",
]

github = "GITHUB_ACTIONS" in os.environ


def handle_file(file: Path, fix: bool, c_type: str) -> list[tuple[int, str]]:
ex = re.compile(rf"\b(?<!std::){c_type}\b")

content = file.read_text()
lines = content.splitlines()

changed_lines = []

for i, oline in enumerate(lines):
line, n_subs = ex.subn(rf"std::{c_type}", oline)
lines[i] = line
if n_subs > 0:
changed_lines.append((i, oline))

if fix and len(changed_lines) > 0:
file.write_text("\n".join(lines) + "\n")

return changed_lines


def main():
p = argparse.ArgumentParser()
p.add_argument("input")
p.add_argument(
"--fix", action="store_true", help="Attempt to fix any license issues found."
)
p.add_argument("--fix", action="store_true", help="Attempt to fix C-style types.")
p.add_argument("--exclude", "-e", action="append", default=[])

args = p.parse_args()

exit_code = 0

# walk over all files
exit = 0
for root, _, files in os.walk("."):
root = Path(root)
for filename in files:
Expand All @@ -44,38 +77,21 @@ def main():
if any([fnmatch(str(filepath), e) for e in args.exclude]):
continue

changed_lines = handle_file(filepath, fix=args.fix)
if len(changed_lines) > 0:
exit = 1
print()
print(filepath)
for i, oline in changed_lines:
print(f"{i}: {oline}")

if github:
print(
f"::error file={filepath},line={i+1},title=Do not use C-style size_t::Replace size_t with std::size_t"
)

return exit


def handle_file(file: Path, fix: bool) -> list[tuple[int, str]]:
content = file.read_text()
lines = content.splitlines()

changed_lines = []

for i, oline in enumerate(lines):
line, n_subs = ex.subn(r"std::size_t", oline)
lines[i] = line
if n_subs > 0:
changed_lines.append((i, oline))

if fix and len(changed_lines) > 0:
file.write_text("\n".join(lines) + "\n")

return changed_lines
for c_type in type_list:
changed_lines = handle_file(file=filepath, fix=args.fix, c_type=c_type)
if len(changed_lines) > 0:
exit_code = 1
print()
print(filepath)
for i, oline in changed_lines:
print(f"{i}: {oline}")

if github:
print(
f"::error file={filepath},line={i+1},title=Do not use C-style {c_type}::Replace {c_type} with std::{c_type}"
)

return exit_code


if "__main__" == __name__:
Expand Down

0 comments on commit 05040cf

Please sign in to comment.