Misra rule 20.1 check is not accurate


(coulet ) #1

Dear support,
We have an internal rule within our SW team that :
internal functions of a module passed as pointer from module itself outside (callbacks, handlers, …) shall have _ prefix and also have their purpose noted in function name by specific keyword (Callback, ISR or Handler) - i. e. _Buttons_DebounceHandler();

For all internal functions we have defined, Sonar checker reports a violation to MISRA rule 20.1 which is not the case because our internal functions are not part of any standard libraries:
MISRA C:2004, 20.1 - Reserved identifiers, macros and functions in the standard library, shall not be defined redefined or undefined.

How can we solve this issue having in mind that we do not want to unselect this MISRA rule?

Many thanks for your feedback,
Jean-Christophe Coulet

(Loïc Joly) #2

A C or C++ user is not allowed to define functions whose name starts with _, because those names are reserved for the standard Library.

In the C++ standard Lexical conventions / Identifiers (5.10§3)

In addition, some identifiers are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.
— Each identifier that contains a double underscore __ or begins with an underscore followed by an
uppercase letter is reserved to the implementation for any use.
— Each identifier that begins with an underscore is reserved to the implementation for use as a name in
the global namespace.

The C standard contains similar clauses (7.1.3).

What Misra rule 20.1 says is that this rule should be enforced, instead of just being a code of good conduct. This is what we do.

Your function _Buttons_DebounceHandler does not comply with this rule. It may not conflict with the standard library you are currently using, but standard library writers are allowed to add such a function at any time, which would then break your code.

I really think the best course of action in your case would be to update your naming convention so that they comply with the C and C++ standards.