Skip to content

Commit

Permalink
Fix C warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
thomascellerier committed Dec 13, 2024
1 parent 4783a85 commit c3294bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
49 changes: 26 additions & 23 deletions tools/gen_constants.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions tools/gen_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@
"IFF_TUN_EXCL",
]

# Misc constants, not an actual enum
misc = [
"IFNAMSIZ",
]


@dataclass
class TypeSpec:
Expand All @@ -256,7 +261,7 @@ class TypeSpec:
flag: bool = False
hex: bool = False
includes: list[str] = field(default_factory=list)
printf_specifier: typing.Literal["%d", "%ld"] = "%d"
printf_specifier: Callable[[str], str] = lambda _: "%d"


constants = [
Expand Down Expand Up @@ -302,17 +307,24 @@ class TypeSpec:
"TUN",
tun_ioctls,
hex=True,
printf_specifier="%d",
# TODO: Is there a better way to do this?
# Is it possible to find the printf specifier based on the type using a C macro?
printf_specifier=lambda name: "%u" if name == "TUNGETDEVNETNS" else "%ld",
includes=["<sys/ioctl.h>", "<linux/if_tun.h>"],
),
TypeSpec(
"TunIffFlag",
"IFF_",
tun_iff_flags,
flag=True,
printf_specifier="%d",
includes=["<linux/if_tun.h>"],
),
TypeSpec(
"Misc",
"",
misc,
includes=["<linux/if.h>"],
),
]


Expand Down Expand Up @@ -344,7 +356,7 @@ def generate_program(name: str = "gen_constants") -> Path:
if type_spec.is_macro:
f.write(f"#ifdef {constant}\n")
f.write(
f' printf("{type_spec.name} {constant} {type_spec.printf_specifier}\\n", {constant});\n'
f' printf("{type_spec.name} {constant} {type_spec.printf_specifier(constant)}\\n", {constant});\n'
)
if type_spec.is_macro:
f.write("#endif\n")
Expand Down

0 comments on commit c3294bd

Please sign in to comment.