From 40ecad0ad796f1515c654c127ae498854c9a6120 Mon Sep 17 00:00:00 2001 From: HASUMI Hitoshi Date: Fri, 15 Mar 2024 22:14:57 +0900 Subject: [PATCH] [Universal parser] Fix -Wsuggest-attribute=format warnings Under a configuration including `cppflags=-DUNIVERSAL_PARSER`, warnings listed below show in build time: ``` node.c:396:30: warning: initialization left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format] 396 | bug_report_func rb_bug = ast->node_buffer->config->bug; | ^~~ ``` ``` ruby_parser.c:655:21: warning: initialization left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format] 655 | .compile_warn = rb_compile_warn, | ^~~~~~~~~~~~~~~ ruby_parser.c:656:24: warning: initialization left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format] 656 | .compile_warning = rb_compile_warning, | ^~~~~~~~~~~~~~~~~~ ruby_parser.c:657:12: warning: initialization left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format] 657 | .bug = rb_bug, | ^~~~~~ ruby_parser.c:658:14: warning: initialization left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format] 658 | .fatal = rb_fatal, | ^~~~~~~~ ``` To fix, this patch suggests adding `__attribute__((format(printf, n, m)))` to those function declarations. --- node.h | 3 +-- rubyparser.h | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/node.h b/node.h index 371b33cff6..221c9f4eec 100644 --- a/node.h +++ b/node.h @@ -15,8 +15,7 @@ #include "rubyparser.h" #include "ruby/backward/2/attributes.h" -typedef void (*bug_report_func)(const char *fmt, ...); - +typedef void (*bug_report_func)(const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(1, 2); typedef struct node_buffer_elem_struct { struct node_buffer_elem_struct *next; long len; /* Length of nodes */ diff --git a/rubyparser.h b/rubyparser.h index cf40ad970e..9a809aa059 100644 --- a/rubyparser.h +++ b/rubyparser.h @@ -34,6 +34,18 @@ #endif #endif +#if defined(__GNUC__) +# if defined(__MINGW_PRINTF_FORMAT) +# define RUBYPARSER_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, argument_index))) +# else +# define RUBYPARSER_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index))) +# endif +#elif defined(__clang__) +# define RUBYPARSER_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((__format__(__printf__, string_index, argument_index))) +#else +# define RUBYPARSER_ATTRIBUTE_FORMAT(string_index, argument_index) +#endif + /* * Parser String */ @@ -1403,10 +1415,10 @@ typedef struct rb_parser_config_struct { int (*memcicmp)(const void *x, const void *y, long len); /* Error */ - void (*compile_warn)(const char *file, int line, const char *fmt, ...); - void (*compile_warning)(const char *file, int line, const char *fmt, ...); - void (*bug)(const char *fmt, ...); - void (*fatal)(const char *fmt, ...); + void (*compile_warn)(const char *file, int line, const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(3, 4); + void (*compile_warning)(const char *file, int line, const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(3, 4); + void (*bug)(const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(1, 2); + void (*fatal)(const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(1, 2); VALUE (*verbose)(void); int *(*errno_ptr)(void);