string-table: Introduce DECLARE_STRING_TABLE_LOOKUP() and friends

Let's introduce these for the following two reasons:
- Using them makes sure the parameter names match between declaration
  and definition (if both use the corresponding macros).
- They make sure developers can't forget the _const_ and _pure_
  attributes for the declarations.

This commit also includes some include sorting fixes
This commit is contained in:
Daan De Meyer
2026-01-02 04:05:45 +09:00
committed by Yu Watanabe
parent daff8ea8bb
commit aec52c1e6e
139 changed files with 351 additions and 589 deletions
+1 -2
View File
@@ -13,8 +13,7 @@ typedef enum ColorMode {
_COLOR_MODE_INVALID = -EINVAL,
} ColorMode;
const char* color_mode_to_string(ColorMode m) _const_;
ColorMode color_mode_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(color_mode, ColorMode);
ColorMode get_color_mode(void);
static inline bool colors_enabled(void) {
+1 -2
View File
@@ -242,5 +242,4 @@ Architecture uname_architecture(void);
# error "Please register your architecture here!"
#endif
const char* architecture_to_string(Architecture a) _const_;
Architecture architecture_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(architecture, Architecture);
+14 -13
View File
@@ -3,20 +3,21 @@
/* IWYU pragma: always_keep */
#include <errno.h> /* IWYU pragma: export */
#include <inttypes.h> /* IWYU pragma: export */
#include <limits.h> /* IWYU pragma: export */
#include <paths.h> /* IWYU pragma: export */
#include <stdarg.h> /* IWYU pragma: export */
#include <stdbool.h> /* IWYU pragma: export */
#include <stddef.h> /* IWYU pragma: export */
#include <stdint.h> /* IWYU pragma: export */
#include <sys/types.h> /* IWYU pragma: export */
#include <uchar.h> /* IWYU pragma: export */
#include <errno.h> /* IWYU pragma: export */
#include <inttypes.h> /* IWYU pragma: export */
#include <limits.h> /* IWYU pragma: export */
#include <paths.h> /* IWYU pragma: export */
#include <stdarg.h> /* IWYU pragma: export */
#include <stdbool.h> /* IWYU pragma: export */
#include <stddef.h> /* IWYU pragma: export */
#include <stdint.h> /* IWYU pragma: export */
#include <sys/types.h> /* IWYU pragma: export */
#include <uchar.h> /* IWYU pragma: export */
#include "assert-util.h" /* IWYU pragma: export */
#include "cleanup-util.h" /* IWYU pragma: export */
#include "macro.h" /* IWYU pragma: export */
#include "assert-util.h" /* IWYU pragma: export */
#include "cleanup-util.h" /* IWYU pragma: export */
#include "macro.h" /* IWYU pragma: export */
#include "string-table-fundamental.h" /* IWYU pragma: export */
/* Generic types */
+4 -8
View File
@@ -95,8 +95,7 @@ typedef enum CGroupIOLimitType {
extern const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX];
const char* cgroup_io_limit_type_to_string(CGroupIOLimitType t) _const_;
CGroupIOLimitType cgroup_io_limit_type_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType);
void cgroup_io_limits_list(void);
/* Special values for the io.bfq.weight attribute */
@@ -243,8 +242,7 @@ int cg_mask_to_string(CGroupMask mask, char **ret);
bool cg_kill_supported(void);
const char* cgroup_controller_to_string(CGroupController c) _const_;
CGroupController cgroup_controller_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);
typedef enum ManagedOOMMode {
MANAGED_OOM_AUTO,
@@ -253,8 +251,7 @@ typedef enum ManagedOOMMode {
_MANAGED_OOM_MODE_INVALID = -EINVAL,
} ManagedOOMMode;
const char* managed_oom_mode_to_string(ManagedOOMMode m) _const_;
ManagedOOMMode managed_oom_mode_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(managed_oom_mode, ManagedOOMMode);
typedef enum ManagedOOMPreference {
MANAGED_OOM_PREFERENCE_NONE = 0,
@@ -264,5 +261,4 @@ typedef enum ManagedOOMPreference {
_MANAGED_OOM_PREFERENCE_INVALID = -EINVAL,
} ManagedOOMPreference;
const char* managed_oom_preference_to_string(ManagedOOMPreference a) _const_;
ManagedOOMPreference managed_oom_preference_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(managed_oom_preference, ManagedOOMPreference);
+2 -4
View File
@@ -12,10 +12,8 @@ typedef enum Compression {
_COMPRESSION_INVALID = -EINVAL,
} Compression;
const char* compression_to_string(Compression compression) _const_;
Compression compression_from_string(const char *s) _pure_;
const char* compression_lowercase_to_string(Compression compression) _const_;
Compression compression_lowercase_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(compression, Compression);
DECLARE_STRING_TABLE_LOOKUP(compression_lowercase, Compression);
bool compression_supported(Compression c);
+1 -2
View File
@@ -20,5 +20,4 @@ typedef enum ConfidentialVirtualization {
ConfidentialVirtualization detect_confidential_virtualization(void);
const char* confidential_virtualization_to_string(ConfidentialVirtualization v) _const_;
ConfidentialVirtualization confidential_virtualization_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(confidential_virtualization, ConfidentialVirtualization);
+1 -2
View File
@@ -36,8 +36,7 @@ int locale_is_installed(const char *name);
bool is_locale_utf8(void);
const char* locale_variable_to_string(LocaleVariable i) _const_;
LocaleVariable locale_variable_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);
static inline void freelocalep(locale_t *p) {
if (*p == (locale_t) 0)
+1 -2
View File
@@ -40,8 +40,7 @@ static inline void clear_log_syntax_callback(dummy_t *dummy) {
set_log_syntax_callback(/* cb= */ NULL, /* userdata= */ NULL);
}
const char* log_target_to_string(LogTarget target) _const_;
LogTarget log_target_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(log_target, LogTarget);
void log_set_target(LogTarget target);
void log_set_target_and_open(LogTarget target);
int log_set_target_from_string(const char *e);
+1 -2
View File
@@ -14,8 +14,7 @@ typedef enum ImageClass {
_IMAGE_CLASS_INVALID = -EINVAL,
} ImageClass;
const char* image_class_to_string(ImageClass cl) _const_;
ImageClass image_class_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(image_class, ImageClass);
/* The *_extension_release flavours will look for /usr/lib/extension-release/extension-release.NAME
* for sysext images and for /etc/extension-release.d/extension-release.NAME for confext images
+2 -5
View File
@@ -107,11 +107,8 @@ const char* personality_to_string(unsigned long);
int safe_personality(unsigned long p);
int opinionated_personality(unsigned long *ret);
const char* sigchld_code_to_string(int i) _const_;
int sigchld_code_from_string(const char *s) _pure_;
int sched_policy_to_string_alloc(int i, char **ret);
int sched_policy_from_string(const char *s);
DECLARE_STRING_TABLE_LOOKUP(sigchld_code, int);
DECLARE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int);
static inline pid_t PTR_TO_PID(const void *p) {
return (pid_t) ((uintptr_t) p);
+1 -2
View File
@@ -7,8 +7,7 @@
#define _RLIMIT_MAX RLIMIT_NLIMITS
const char* rlimit_to_string(int i) _const_;
int rlimit_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(rlimit, int);
int rlimit_from_string_harder(const char *s) _pure_;
void rlimits_list(const char *prefix);
+2 -4
View File
@@ -11,10 +11,8 @@ typedef enum RuntimeScope {
_RUNTIME_SCOPE_INVALID = -EINVAL,
} RuntimeScope;
const char* runtime_scope_to_string(RuntimeScope scope) _const_;
RuntimeScope runtime_scope_from_string(const char *s) _const_;
const char* runtime_scope_cmdline_option_to_string(RuntimeScope scope) _const_;
DECLARE_STRING_TABLE_LOOKUP(runtime_scope, RuntimeScope);
DECLARE_STRING_TABLE_LOOKUP_TO_STRING(runtime_scope_cmdline_option, RuntimeScope);
static inline mode_t runtime_scope_to_socket_mode(RuntimeScope scope) {
/* Returns the right socket mode to use for binding AF_UNIX sockets intended for the specified
+1 -2
View File
@@ -31,8 +31,7 @@ int sigset_add_many_internal(sigset_t *ss, ...);
int sigprocmask_many_internal(int how, sigset_t *ret_old_mask, ...);
#define sigprocmask_many(...) sigprocmask_many_internal(__VA_ARGS__, -1)
const char* signal_to_string(int signo) _const_;
int signal_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(signal, int);
void nop_signal_handler(int sig);
+3 -6
View File
@@ -55,8 +55,7 @@ typedef struct SocketAddress {
#define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
const char* socket_address_type_to_string(int t) _const_;
int socket_address_type_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(socket_address_type, int);
int sockaddr_un_unlink(const struct sockaddr_un *sa);
@@ -87,8 +86,7 @@ int getsockname_pretty(int fd, char **ret);
int socknameinfo_pretty(const struct sockaddr *sa, socklen_t salen, char **_ret);
int netlink_family_to_string_alloc(int i, char **ret);
int netlink_family_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int);
bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b);
@@ -101,8 +99,7 @@ static inline int fd_increase_rxbuf(int fd, size_t n) {
return fd_set_rcvbuf(fd, n, true);
}
int ip_tos_to_string_alloc(int i, char **ret);
int ip_tos_from_string(const char *s);
DECLARE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int);
typedef enum {
IFNAME_VALID_ALTERNATIVE = 1 << 0, /* Allow "altnames" too */
+1 -2
View File
@@ -97,8 +97,7 @@ void inode_hash_func(const struct stat *q, struct siphash *state);
int inode_compare_func(const struct stat *a, const struct stat *b);
extern const struct hash_ops inode_hash_ops;
const char* inode_type_to_string(mode_t m) _const_;
mode_t inode_type_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(inode_type, mode_t);
/* Macros that check whether the stat/statx structures have been initialized already. For "struct stat" we
* use a check for .st_dev being non-zero, since the kernel unconditionally fills that in, mapping the file
+2 -4
View File
@@ -3,12 +3,10 @@
#include "basic-forward.h"
int log_facility_unshifted_to_string_alloc(int i, char **ret);
int log_facility_unshifted_from_string(const char *s);
DECLARE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int);
bool log_facility_unshifted_is_valid(int faciliy);
int log_level_to_string_alloc(int i, char **ret);
int log_level_from_string(const char *s);
DECLARE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int);
bool log_level_is_valid(int level);
int syslog_parse_priority(const char **p, int *priority, bool with_facility);
+1 -2
View File
@@ -232,5 +232,4 @@ int usleep_safe(usec_t usec);
int time_change_fd(void);
const char* timestamp_style_to_string(TimestampStyle t) _const_;
TimestampStyle timestamp_style_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(timestamp_style, TimestampStyle);
+20 -40
View File
@@ -313,67 +313,47 @@ int unit_name_from_dbus_path(const char *path, char **name);
const char* unit_dbus_interface_from_type(UnitType t);
const char* unit_dbus_interface_from_name(const char *name);
const char* unit_type_to_string(UnitType i) _const_;
UnitType unit_type_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(unit_type, UnitType);
void unit_types_list(void);
const char* unit_load_state_to_string(UnitLoadState i) _const_;
UnitLoadState unit_load_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
const char* unit_active_state_to_string(UnitActiveState i) _const_;
UnitActiveState unit_active_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
const char* freezer_state_to_string(FreezerState i) _const_;
FreezerState freezer_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(freezer_state, FreezerState);
FreezerState freezer_state_finish(FreezerState state) _const_;
FreezerState freezer_state_objective(FreezerState state) _const_;
const char* unit_marker_to_string(UnitMarker m) _const_;
UnitMarker unit_marker_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(unit_marker, UnitMarker);
const char* automount_state_to_string(AutomountState i) _const_;
AutomountState automount_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
const char* device_state_to_string(DeviceState i) _const_;
DeviceState device_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(device_state, DeviceState);
const char* mount_state_to_string(MountState i) _const_;
MountState mount_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(mount_state, MountState);
const char* path_state_to_string(PathState i) _const_;
PathState path_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(path_state, PathState);
const char* scope_state_to_string(ScopeState i) _const_;
ScopeState scope_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(scope_state, ScopeState);
const char* service_state_to_string(ServiceState i) _const_;
ServiceState service_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(service_state, ServiceState);
const char* slice_state_to_string(SliceState i) _const_;
SliceState slice_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(slice_state, SliceState);
const char* socket_state_to_string(SocketState i) _const_;
SocketState socket_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(socket_state, SocketState);
const char* swap_state_to_string(SwapState i) _const_;
SwapState swap_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(swap_state, SwapState);
const char* target_state_to_string(TargetState i) _const_;
TargetState target_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(target_state, TargetState);
const char* timer_state_to_string(TimerState i) _const_;
TimerState timer_state_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(timer_state, TimerState);
const char* unit_dependency_to_string(UnitDependency i) _const_;
UnitDependency unit_dependency_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
const char* notify_access_to_string(NotifyAccess i) _const_;
NotifyAccess notify_access_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
const char* job_mode_to_string(JobMode t) _const_;
JobMode job_mode_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(job_mode, JobMode);
const char* exec_directory_type_to_string(ExecDirectoryType i) _const_;
ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(exec_directory_type, ExecDirectoryType);
Glyph unit_active_state_to_glyph(UnitActiveState state);
+1 -2
View File
@@ -62,6 +62,5 @@ Virtualization detect_virtualization(void);
int running_in_userns(void);
int running_in_chroot(void);
const char* virtualization_to_string(Virtualization v) _const_;
Virtualization virtualization_from_string(const char *s) _pure_;
DECLARE_STRING_TABLE_LOOKUP(virtualization, Virtualization);
bool has_cpu_with_flag(const char *flag);