1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-11-14 21:40:03 +00:00

make format.py clean up extra whitespace, also in .h files (#1991)

* make format.py clean up extra whitespace, also in .h files

* fixup

* cleanup whitespace msg

* Update check_format.py to also check non-src files touched by format.py

* format
This commit is contained in:
Dragorn421 2024-07-18 01:32:55 +02:00 committed by GitHub
parent aaad2e1caf
commit a1c7937dba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 74 additions and 50 deletions

View file

@ -4,6 +4,7 @@ import argparse
import glob import glob
import multiprocessing import multiprocessing
import os import os
from pathlib import Path
import re import re
import shutil import shutil
import subprocess import subprocess
@ -94,14 +95,25 @@ def run_clang_apply_replacements(tmp_dir: str):
subprocess.run(exec_str, shell=True) subprocess.run(exec_str, shell=True)
def add_final_new_line(file: str): def cleanup_whitespace(file: str):
# https://backreference.org/2010/05/23/sanitizing-files-with-no-trailing-newline/index.html """
# "gets the last character of the file pipes it into read, which will exit with a nonzero exit Remove whitespace at the end of lines,
# code if it encounters EOF before newline (so, if the last character of the file isn't a newline). ensure the file ends with an empty line.
# If read exits nonzero, then append a newline onto the file using echo (if read exits 0, """
# that satisfies the ||, so the echo command isn't run)." (https://stackoverflow.com/a/34865616) file_p = Path(file)
exec_str = f"tail -c1 {file} | read -r _ || echo >> {file}" contents = file_p.read_text(encoding="UTF-8")
subprocess.run(exec_str, shell=True) modified = False
contents, n_subst = re.subn(r"[^\S\n]+\n", "\n", contents)
if n_subst != 0:
modified = True
if not contents.endswith("\n"):
contents += "\n"
modified = True
if modified:
file_p.write_text(contents, encoding="UTF-8")
def format_files(src_files: List[str], extra_files: List[str], nb_jobs: int): def format_files(src_files: List[str], extra_files: List[str], nb_jobs: int):
@ -134,14 +146,24 @@ def format_files(src_files: List[str], extra_files: List[str], nb_jobs: int):
else: else:
run_clang_tidy(src_files) run_clang_tidy(src_files)
print("Adding missing final new lines...") print("Cleaning up whitespace...")
# Adding final new lines is safe to do in parallel and can be applied to all types of files # Safe to do in parallel and can be applied to all types of files
with multiprocessing.get_context("fork").Pool(nb_jobs) as pool: with multiprocessing.get_context("fork").Pool(nb_jobs) as pool:
pool.map(add_final_new_line, src_files + extra_files) pool.map(cleanup_whitespace, src_files + extra_files)
print("Done formatting files.") print("Done formatting files.")
def list_files_to_format():
files = glob.glob("src/**/*.c", recursive=True)
extra_files = (
glob.glob("assets/**/*.xml", recursive=True)
+ glob.glob("include/**/*.h", recursive=True)
+ glob.glob("src/**/*.h", recursive=True)
)
return files, extra_files
def main(): def main():
parser = argparse.ArgumentParser(description="Format files in the codebase to enforce most style rules") parser = argparse.ArgumentParser(description="Format files in the codebase to enforce most style rules")
parser.add_argument( parser.add_argument(
@ -179,8 +201,7 @@ def main():
files = args.files files = args.files
extra_files = [] extra_files = []
else: else:
files = glob.glob("src/**/*.c", recursive=True) files, extra_files = list_files_to_format()
extra_files = glob.glob("assets/**/*.xml", recursive=True)
format_files(files, extra_files, nb_jobs) format_files(files, extra_files, nb_jobs)

View file

@ -140,22 +140,22 @@ typedef struct {
/* 0x18 */ Vec3f feetPos[2]; // Update by using `Actor_SetFeetPos` in PostLimbDraw /* 0x18 */ Vec3f feetPos[2]; // Update by using `Actor_SetFeetPos` in PostLimbDraw
} ActorShape; // size = 0x30 } ActorShape; // size = 0x30
// //
#define ACTOR_FLAG_0 (1 << 0) #define ACTOR_FLAG_0 (1 << 0)
// //
#define ACTOR_FLAG_2 (1 << 2) #define ACTOR_FLAG_2 (1 << 2)
// //
#define ACTOR_FLAG_3 (1 << 3) #define ACTOR_FLAG_3 (1 << 3)
// //
#define ACTOR_FLAG_4 (1 << 4) #define ACTOR_FLAG_4 (1 << 4)
// //
#define ACTOR_FLAG_5 (1 << 5) #define ACTOR_FLAG_5 (1 << 5)
// //
#define ACTOR_FLAG_6 (1 << 6) #define ACTOR_FLAG_6 (1 << 6)
// hidden or revealed by Lens of Truth (depending on room lensMode) // hidden or revealed by Lens of Truth (depending on room lensMode)
@ -166,64 +166,64 @@ typedef struct {
// Actor will retain this flag until `Actor_TalkOfferAccepted` is called or manually turned off by the actor // Actor will retain this flag until `Actor_TalkOfferAccepted` is called or manually turned off by the actor
#define ACTOR_FLAG_TALK (1 << 8) #define ACTOR_FLAG_TALK (1 << 8)
// //
#define ACTOR_FLAG_9 (1 << 9) #define ACTOR_FLAG_9 (1 << 9)
// //
#define ACTOR_FLAG_10 (1 << 10) #define ACTOR_FLAG_10 (1 << 10)
// //
#define ACTOR_FLAG_ENKUSA_CUT (1 << 11) #define ACTOR_FLAG_ENKUSA_CUT (1 << 11)
// Actor will not shake when a quake occurs // Actor will not shake when a quake occurs
#define ACTOR_FLAG_IGNORE_QUAKE (1 << 12) #define ACTOR_FLAG_IGNORE_QUAKE (1 << 12)
// //
#define ACTOR_FLAG_13 (1 << 13) #define ACTOR_FLAG_13 (1 << 13)
// //
#define ACTOR_FLAG_14 (1 << 14) #define ACTOR_FLAG_14 (1 << 14)
// //
#define ACTOR_FLAG_15 (1 << 15) #define ACTOR_FLAG_15 (1 << 15)
// //
#define ACTOR_FLAG_16 (1 << 16) #define ACTOR_FLAG_16 (1 << 16)
// //
#define ACTOR_FLAG_17 (1 << 17) #define ACTOR_FLAG_17 (1 << 17)
// //
#define ACTOR_FLAG_18 (1 << 18) #define ACTOR_FLAG_18 (1 << 18)
// //
#define ACTOR_FLAG_19 (1 << 19) #define ACTOR_FLAG_19 (1 << 19)
// //
#define ACTOR_FLAG_20 (1 << 20) #define ACTOR_FLAG_20 (1 << 20)
// //
#define ACTOR_FLAG_21 (1 << 21) #define ACTOR_FLAG_21 (1 << 21)
// ignores point lights but not directional lights (such as environment lights) // ignores point lights but not directional lights (such as environment lights)
#define ACTOR_FLAG_IGNORE_POINT_LIGHTS (1 << 22) #define ACTOR_FLAG_IGNORE_POINT_LIGHTS (1 << 22)
// //
#define ACTOR_FLAG_23 (1 << 23) #define ACTOR_FLAG_23 (1 << 23)
// //
#define ACTOR_FLAG_24 (1 << 24) #define ACTOR_FLAG_24 (1 << 24)
// //
#define ACTOR_FLAG_25 (1 << 25) #define ACTOR_FLAG_25 (1 << 25)
// //
#define ACTOR_FLAG_26 (1 << 26) #define ACTOR_FLAG_26 (1 << 26)
// //
#define ACTOR_FLAG_27 (1 << 27) #define ACTOR_FLAG_27 (1 << 27)
// //
#define ACTOR_FLAG_28 (1 << 28) #define ACTOR_FLAG_28 (1 << 28)
#define COLORFILTER_GET_COLORINTENSITY(colorFilterParams) (((colorFilterParams) & 0x1F00) >> 5) #define COLORFILTER_GET_COLORINTENSITY(colorFilterParams) (((colorFilterParams) & 0x1F00) >> 5)

View file

@ -23,14 +23,15 @@ def get_modified_files_to_format(compare_to):
modified_files_str = subprocess.check_output( modified_files_str = subprocess.check_output(
["git", "diff", "--name-only", compare_to], text=True ["git", "diff", "--name-only", compare_to], text=True
) )
modified_files = modified_files_str.splitlines() modified_files = set(modified_files_str.splitlines())
modified_c_files = [
f all_src_files, all_extra_files = format.list_files_to_format()
for f in modified_files # Split modified_files between source files and extra files (see format.py)
if f.startswith("src" + os.path.sep) and f.endswith(".c") # This also filters out deleted files that no logner exist
] modified_src_files_existing = list(modified_files.intersection(all_src_files))
modified_c_files_existing = [f for f in modified_c_files if os.path.exists(f)] modified_extra_files_existing = list(modified_files.intersection(all_extra_files))
return modified_c_files_existing
return modified_src_files_existing, modified_extra_files_existing
def main(): def main():
@ -40,21 +41,23 @@ def main():
args = parser.parse_args() args = parser.parse_args()
if args.compare_to: if args.compare_to:
src_files = get_modified_files_to_format(args.compare_to) src_files, extra_files = get_modified_files_to_format(args.compare_to)
if args.verbose: if args.verbose:
print("Formatting specific files:", len(src_files), src_files) print("Formatting specific files:")
if not src_files: print(len(src_files), src_files)
print(len(extra_files), extra_files)
if not src_files and not extra_files:
if args.verbose: if args.verbose:
print("Nothing to format") print("Nothing to format")
exit(0) exit(0)
else: else:
src_files = glob.glob("src/**/*.c", recursive=True) src_files, extra_files = format.list_files_to_format()
nb_jobs = multiprocessing.cpu_count() nb_jobs = multiprocessing.cpu_count()
git_status_pre = get_git_status() git_status_pre = get_git_status()
format.format_files(src_files, [], nb_jobs) format.format_files(src_files, extra_files, nb_jobs)
git_status_post = get_git_status() git_status_post = get_git_status()