Skip to content

Commit

Permalink
Style: Integrate #pragma once in builders/checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Repiteo committed Feb 4, 2025
1 parent 0b6a717 commit 084c78b
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 130 deletions.
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ repos:
language: python
entry: python misc/scripts/header_guards.py
files: \.(h|hpp|hh|hxx)$
exclude: ^.*/(dummy|thread|platform_config|platform_gl)\.h$
exclude: |
(?x)^(
drivers/(apple|coreaudio|coremidi|metal)/.*|
misc/dist/ios_xcode/.*|
platform/(ios|macos)/(?!platform_(config|gl))[^/]*\.h
)$
- id: file-format
name: file-format
Expand Down
5 changes: 1 addition & 4 deletions core/extension/make_interface_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def run(target, source, env):

g.write(
"""/* THIS FILE IS GENERATED DO NOT EDIT */
#ifndef GDEXTENSION_INTERFACE_DUMP_H
#define GDEXTENSION_INTERFACE_DUMP_H
#pragma once
#ifdef TOOLS_ENABLED
Expand Down Expand Up @@ -49,7 +48,5 @@ class GDExtensionInterfaceDump {
};
#endif // TOOLS_ENABLED
#endif // GDEXTENSION_INTERFACE_DUMP_H
"""
)
7 changes: 1 addition & 6 deletions core/extension/make_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ def generate_ex_version(argcount, const=False, returns=False):
def run(target, source, env):
max_versions = 12

txt = """
#ifndef GDEXTENSION_WRAPPERS_GEN_H
#define GDEXTENSION_WRAPPERS_GEN_H
"""
txt = "#pragma once"

for i in range(max_versions + 1):
txt += "\n/* Extension Wrapper " + str(i) + " Arguments */\n"
Expand All @@ -138,7 +135,5 @@ def run(target, source, env):
txt += generate_mod_version(i, True, False)
txt += generate_mod_version(i, True, True)

txt += "\n#endif\n"

with open(str(target[0]), "w", encoding="utf-8", newline="\n") as f:
f.write(txt)
5 changes: 1 addition & 4 deletions core/object/make_virtuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ def run(target, source, env):
max_versions = 12

txt = """/* THIS FILE IS GENERATED DO NOT EDIT */
#ifndef GDVIRTUAL_GEN_H
#define GDVIRTUAL_GEN_H
#pragma once
#include "core/object/script_instance.h"
Expand Down Expand Up @@ -257,7 +256,5 @@ def run(target, source, env):
txt += generate_version(i, True, False, False, True)
txt += generate_version(i, True, True, False, True)

txt += "#endif // GDVIRTUAL_GEN_H\n"

with open(str(target[0]), "w", encoding="utf-8", newline="\n") as f:
f.write(txt)
7 changes: 2 additions & 5 deletions gles3_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,11 @@ def build_gles3_header(
defvariant = ""

fd.write("/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */\n")
fd.write("#pragma once\n")

out_file_base = out_file
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
out_file_base = out_file_base[out_file_base.rfind("\\") + 1 :]
out_file_ifdef = out_file_base.replace(".", "_").upper()
fd.write("#ifndef " + out_file_ifdef + class_suffix + "_GLES3\n")
fd.write("#define " + out_file_ifdef + class_suffix + "_GLES3\n")

out_file_class = (
out_file_base.replace(".glsl.gen.h", "").title().replace("_", "").replace(".", "") + "Shader" + class_suffix
Expand Down Expand Up @@ -580,8 +578,7 @@ def build_gles3_header(

fd.write("\t}\n\n")

fd.write("};\n\n")
fd.write("#endif\n")
fd.write("};\n")


def build_gles3_headers(target, source, env):
Expand Down
11 changes: 2 additions & 9 deletions glsl_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def build_rd_header(
out_file_base = out_file
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
out_file_base = out_file_base[out_file_base.rfind("\\") + 1 :]
out_file_ifdef = out_file_base.replace(".", "_").upper()
out_file_class = out_file_base.replace(".glsl.gen.h", "").title().replace("_", "").replace(".", "") + "ShaderRD"

if header_data.compute_lines:
Expand All @@ -125,8 +124,7 @@ def build_rd_header(

# Intended curly brackets are doubled so f-string doesn't eat them up.
shader_template = f"""/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
#ifndef {out_file_ifdef}_RD
#define {out_file_ifdef}_RD
#pragma once
#include "servers/rendering/renderer_rd/shader_rd.h"
Expand All @@ -139,8 +137,6 @@ class {out_file_class} : public ShaderRD {{
{body_content}
}}
}};
#endif
"""

with open(out_file, "w", encoding="utf-8", newline="\n") as fd:
Expand Down Expand Up @@ -189,16 +185,13 @@ def build_raw_header(
out_file_base = out_file.replace(".glsl.gen.h", "_shader_glsl")
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
out_file_base = out_file_base[out_file_base.rfind("\\") + 1 :]
out_file_ifdef = out_file_base.replace(".", "_").upper()

shader_template = f"""/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
#ifndef {out_file_ifdef}_RAW_H
#define {out_file_ifdef}_RAW_H
#pragma once
static const char {out_file_base}[] = {{
{to_raw_cstring(header_data.code)}
}};
#endif
"""

with open(out_file, "w", encoding="utf-8", newline="\n") as f:
Expand Down
30 changes: 4 additions & 26 deletions methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,8 +1451,6 @@ def generate_copyright_header(filename: str) -> str:
def generated_wrapper(
path, # FIXME: type with `Union[str, Node, List[Node]]` when pytest conflicts are resolved
guard: Optional[bool] = None,
prefix: str = "",
suffix: str = "",
) -> Generator[TextIOBase, None, None]:
"""
Wrapper class to automatically handle copyright headers and header guards
Expand All @@ -1462,12 +1460,8 @@ def generated_wrapper(
- `path`: The path of the file to be created. Can be passed a raw string, an
isolated SCons target, or a full SCons target list. If a target list contains
multiple entries, produces a warning & only creates the first entry.
- `guard`: Optional bool to determine if a header guard should be added. If
unassigned, header guards are determined by the file extension.
- `prefix`: Custom prefix to prepend to a header guard. Produces a warning if
provided a value when `guard` evaluates to `False`.
- `suffix`: Custom suffix to append to a header guard. Produces a warning if
provided a value when `guard` evaluates to `False`.
- `guard`: Optional bool to determine if `#pragma once` should be added. If
unassigned, the value is determined by file extension.
"""

# Handle unfiltered SCons target[s] passed as path.
Expand All @@ -1484,35 +1478,19 @@ def generated_wrapper(

path = str(path).replace("\\", "/")
if guard is None:
guard = path.endswith((".h", ".hh", ".hpp", ".inc"))
if not guard and (prefix or suffix):
print_warning(f'Trying to assign header guard prefix/suffix while `guard` is disabled: "{path}".')

header_guard = ""
if guard:
if prefix:
prefix += "_"
if suffix:
suffix = f"_{suffix}"
split = path.split("/")[-1].split(".")
header_guard = (f"{prefix}{split[0]}{suffix}.{'.'.join(split[1:])}".upper()
.replace(".", "_").replace("-", "_").replace(" ", "_").replace("__", "_")) # fmt: skip
guard = path.endswith((".h", ".hh", ".hpp", ".hxx", ".inc"))

with open(path, "wt", encoding="utf-8", newline="\n") as file:
file.write(generate_copyright_header(path))
file.write("\n/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */\n\n")

if guard:
file.write(f"#ifndef {header_guard}\n")
file.write(f"#define {header_guard}\n\n")
file.write("#pragma once\n\n")

with StringIO(newline="\n") as str_io:
yield str_io
file.write(str_io.getvalue().strip() or "/* NO CONTENT */")

if guard:
file.write(f"\n\n#endif // {header_guard}")

file.write("\n")


Expand Down
62 changes: 15 additions & 47 deletions misc/scripts/header_guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
and lines[HEADER_BEGIN_OFFSET] == HEADER_BEGIN
and lines[HEADER_END_OFFSET] == HEADER_END
):
lines[HEADER_CHECK_OFFSET] = "#pragma once"
lines[HEADER_BEGIN_OFFSET] = "\n"
lines.pop()
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
continue

# Guards might exist but with the wrong names.
Expand All @@ -88,9 +94,9 @@
and lines[HEADER_BEGIN_OFFSET].startswith("#define")
and lines[HEADER_END_OFFSET].startswith("#endif")
):
lines[HEADER_CHECK_OFFSET] = HEADER_CHECK
lines[HEADER_BEGIN_OFFSET] = HEADER_BEGIN
lines[HEADER_END_OFFSET] = HEADER_END
lines[HEADER_CHECK_OFFSET] = "#pragma once"
lines[HEADER_BEGIN_OFFSET] = "\n"
lines.pop()
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
Expand All @@ -99,70 +105,32 @@
header_check = -1
header_begin = -1
header_end = -1
pragma_once = -1
objc = False
skip = False

for idx, line in enumerate(lines):
if line.startswith("// #import"): # Some dummy obj-c files only have commented out import lines.
objc = True
break
if not line.startswith("#"):
continue
elif line.startswith(("#import", "#pragma once")):
skip = True
break
elif line.startswith("#ifndef") and header_check == -1:
header_check = idx
elif line.startswith("#define") and header_begin == -1:
header_begin = idx
elif line.startswith("#endif") and header_end == -1:
header_end = idx
elif line.startswith("#pragma once"):
pragma_once = idx
break
elif line.startswith("#import"):
objc = True
break

if objc:
continue

if pragma_once != -1:
lines.pop(pragma_once)
lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
lines.append("\n")
lines.append(HEADER_END)
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
if skip:
continue

if header_check == -1 and header_begin == -1 and header_end == -1:
# Guards simply didn't exist
lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
lines.append("\n")
lines.append(HEADER_END)
lines.insert(HEADER_CHECK_OFFSET, "#pragma once\n\n")
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
continue

if header_check != -1 and header_begin != -1 and header_end != -1:
# All prepends "found", see if we can salvage this.
if header_check == header_begin - 1 and header_begin < header_end:
lines.pop(header_check)
lines.pop(header_begin - 1)
lines.pop(header_end - 2)
if lines[header_end - 3] == "\n":
lines.pop(header_end - 3)
lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
lines.append("\n")
lines.append(HEADER_END)
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
continue

invalid.append(file)

if changed:
Expand Down
5 changes: 1 addition & 4 deletions misc/scripts/ucaps_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def generate_ucaps_fetch() -> None:
source: str = generate_copyright_header("ucaps.h")

source += f"""
#ifndef UCAPS_H
#define UCAPS_H
#pragma once
// This file was generated using the `misc/scripts/ucaps_fetch.py` script.
Expand Down Expand Up @@ -105,8 +104,6 @@ def generate_ucaps_fetch() -> None:
\treturn ch;
}
#endif // UCAPS_H
"""

ucaps_path: str = os.path.join(os.path.dirname(__file__), "../../core/string/ucaps.h")
Expand Down
2 changes: 1 addition & 1 deletion platform/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def export_icon_builder(target, source, env):
platform = src_path.parent.parent.stem
with open(str(source[0]), "r") as file:
svg = file.read()
with methods.generated_wrapper(target, prefix=platform) as file:
with methods.generated_wrapper(target) as file:
file.write(
f"""\
static const char *_{platform}_{src_name}_svg = {methods.to_raw_cstring(svg)};
Expand Down
6 changes: 1 addition & 5 deletions tests/create_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def main():
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef TEST_{name_upper_snake_case}_H
#define TEST_{name_upper_snake_case}_H
#pragma once
#include "tests/test_macros.h"
Expand All @@ -90,14 +89,11 @@ def main():
}}
}} // namespace Test{name_pascal_case}
#endif // TEST_{name_upper_snake_case}_H
""".format(
name_snake_case=name_snake_case,
# Capitalize the first letter but keep capitalization for the rest of the string.
# This is done in case the user passes a camelCase string instead of PascalCase.
name_pascal_case=args.name[0].upper() + args.name[1:],
name_upper_snake_case=name_snake_case.upper(),
# The padding length depends on the test name length.
padding=" " * (61 - len(name_snake_case)),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
#ifndef VERTEX_FRAGMENT_GLSL_GEN_HGLES3_GLES3
#define VERTEX_FRAGMENT_GLSL_GEN_HGLES3_GLES3
#pragma once


#include "drivers/gles3/shader_gles3.h"
Expand Down Expand Up @@ -70,5 +69,3 @@ void main() {
}

};

#endif
4 changes: 1 addition & 3 deletions tests/python_build/fixtures/glsl/compute_expected_full.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
#ifndef COMPUTE_SHADER_GLSL_RAW_H
#define COMPUTE_SHADER_GLSL_RAW_H
#pragma once

static const char compute_shader_glsl[] = {
R"<!>(#[compute]
Expand All @@ -17,4 +16,3 @@ void main() {
}
)<!>"
};
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
#ifndef VERTEX_FRAGMENT_SHADER_GLSL_RAW_H
#define VERTEX_FRAGMENT_SHADER_GLSL_RAW_H
#pragma once

static const char vertex_fragment_shader_glsl[] = {
R"<!>(#[versions]
Expand Down Expand Up @@ -37,4 +36,3 @@ void main() {
}
)<!>"
};
#endif
Loading

0 comments on commit 084c78b

Please sign in to comment.