c - Misra rule 19.7 : function like macro -
i have warning regarding misra rule 19.7 : function should used in preference function-like macro in below line :
#define goffsetof(type, mem) (goffset)((size_t) ((char *)&((type *) 0)->mem - (char *)((type *) 0)))
how should solve ?
rule 19.7 (advisory): function should used in preference function-like macro. while macros can provide speed advantage on functions, functions provide safer , more robust mechanism. particularly true respect type checking of parameters, , problem of function-like macros potentially evaluating parameters multiple times.
the rule advisory, means "should followed":
note status of "advisory" not mean these items can ignored, should followed far reasonably practical. formal deviations not necessary advisory rules, may raised if considered appropriate.
so have option break rule without making formal deviation.
now, in answer question, "how should solve this?", have 2 choices given macro function can't implemented function.
option 1: deviate rule 19.7
the advisory makes clear functions nicer macro functions, , cites section in c traps , pitfalls, andrew koenig (1988) on comparing macro functions functions, describes preference, , on short macro functions designed "speed advantage".
if believe macro makes code clearer, conciser, , have suitably avoided common pitfalls of macro functions, can deviate rule 19.7 without making formal specific deviation, , without failing compliance.
option 2: remove macro function
if after considering advisory deem appropriate remove macro function. may want write more functions break inline code, and/or avoid needless code duplication.
Comments
Post a Comment