From Pavol.Hanzel at pantheon.tech Mon Dec 10 11:50:41 2018 From: Pavol.Hanzel at pantheon.tech (Pavol Hanzel) Date: Mon, 10 Dec 2018 11:50:41 +0000 Subject: [sysrepo-devel] New idea - let compiler check the types of variadic arguments Message-ID: Hello sysrepo team, I am Pavol Hanzel, a software engineer from Pantheon Technologies. Currently we're contributing to the sweetcomb project, which uses sysrepo as a datastore for VPP. We're generating a lot of code that uses sr_val_build_xpath, and we think it would be great if the compiler could warn us when we supply an incorrect argument, Like this: error: format '%s' expects argument of type 'char *', but argument 3 has type 'u32 {aka unsigned int}' [-Werror=format=] sr_val_build_xpath(&vals[0], "/test-bridge-domain:bridge-domains/bridge-domain[id='%s']/interfaces/interface[name='%s']/state/name", ~^ %d reply->bd_id, interface_name); ~~~~~~~~~~~~ To achieve that, one just needs to change the signature of variadic functions like this: int sr_val_build_xpath(sr_val_t *value, const char *format, ...) __attribute__ ((__format__(printf, 2, 3))); These are the functions that would need the change: ./inc/sysrepo/trees.h:114:int sr_node_build_str_data(sr_node_t *node, sr_type_t type, const char *format, ...); ./inc/sysrepo/values.h:96:int sr_val_build_xpath(sr_val_t *value, const char *format, ...); ./inc/sysrepo/values.h:115:int sr_val_build_str_data(sr_val_t *value, sr_type_t type, const char *format, ...); ./src/common/sr_logger.h:227:void sr_log_to_cb(sr_log_level_t level, const char *format, ...); ./src/common/sr_utils.h:187:int sr_asprintf(char **strp, const char *fmt, ...); ./src/common/sr_utils.h:621: const char *msg_fmt, ...); ./src/common/sr_utils.h:726:int sr_print(sr_print_ctx_t *print_ctx, const char *format, ...); Please let me know your remarks about this idea! Regards, Pavol Hanzel -------------- next part -------------- An HTML attachment was scrubbed... URL: From mvasko at cesnet.cz Mon Dec 10 12:05:07 2018 From: mvasko at cesnet.cz (=?utf-8?q?Michal_Va=C5=A1ko?=) Date: Mon, 10 Dec 2018 13:05:07 +0100 Subject: [sysrepo-devel] =?utf-8?b?Pz09P3V0Zi04P3E/ICBOZXcgaWRlYSAtIGxl?= =?utf-8?q?t_compiler_check_the_types_of_variadic_arguments?= In-Reply-To: Message-ID: <27d8-5c0e5680-79-2180e4c0@99310715> Hi Pavol, I do not think anyone will have anything against this and it should be useful. So, feel free to create a pull request but please put the whole attribute into a macro that will be defined based on the compiler used. Something similar to what is in libyang CMakeLists.txt:55. Thanks. Regards, Michal On Monday, December 10, 2018 12:50 CET, Pavol Hanzel wrote: > Hello sysrepo team, > > I am Pavol Hanzel, a software engineer from Pantheon Technologies. Currently we're contributing to the sweetcomb project, which uses sysrepo as a datastore for VPP. > > We're generating a lot of code that uses sr_val_build_xpath, and we think it would be great if the compiler could warn us when we supply an incorrect argument, Like this: > > error: format '%s' expects argument of type 'char *', but argument 3 has type 'u32 {aka unsigned int}' [-Werror=format=] > sr_val_build_xpath(&vals[0], "/test-bridge-domain:bridge-domains/bridge-domain[id='%s']/interfaces/interface[name='%s']/state/name", > ~^ > %d > reply->bd_id, interface_name); > ~~~~~~~~~~~~ > > > To achieve that, one just needs to change the signature of variadic functions like this: > int sr_val_build_xpath(sr_val_t *value, const char *format, ...) __attribute__ ((__format__(printf, 2, 3))); > > > These are the functions that would need the change: > > ./inc/sysrepo/trees.h:114:int sr_node_build_str_data(sr_node_t *node, sr_type_t type, const char *format, ...); > ./inc/sysrepo/values.h:96:int sr_val_build_xpath(sr_val_t *value, const char *format, ...); > ./inc/sysrepo/values.h:115:int sr_val_build_str_data(sr_val_t *value, sr_type_t type, const char *format, ...); > ./src/common/sr_logger.h:227:void sr_log_to_cb(sr_log_level_t level, const char *format, ...); > ./src/common/sr_utils.h:187:int sr_asprintf(char **strp, const char *fmt, ...); > ./src/common/sr_utils.h:621: const char *msg_fmt, ...); > ./src/common/sr_utils.h:726:int sr_print(sr_print_ctx_t *print_ctx, const char *format, ...); > > > Please let me know your remarks about this idea! > Regards, > Pavol Hanzel >