[sysrepo-devel] ?==?utf-8?q? New idea - let compiler check the types of variadic arguments
Michal Vaško
mvasko at cesnet.cz
Mon Dec 10 12:05:07 UTC 2018
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 <Pavol.Hanzel at pantheon.tech> wrote:
> Hello sysrepo team,
>
> I am Pavol Hanzel, a software engineer from Pantheon Technologies. Currently we're contributing to the sweetcomb<https://wiki.fd.io/view/Project_Proposals/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
>
More information about the sysrepo-devel
mailing list