MISRA rule 20.1 and 20.2

6.20 Standard Libraries

Moderators: misra-c, david ward

Post Reply
VRK
Posts: 2
Joined: Thu Mar 13, 2014 1:26 pm
Company: UTC AEROSPACE SYSTEM

MISRA rule 20.1 and 20.2

Post by VRK » Fri Mar 14, 2014 6:53 pm

20.1 (req): %s: Reserved identifiers, macros and functions in the standard library, shall not be defined, redefined or undefined.
20.2 (req): %s: The names of standard library macros, objects and functions shall not be reused.

As per the rule 20.1, Reserved identifiers should not be defined, redefined or undefined.
I am getting the violation of MISRA 20.2 for the below example:

For the declaration,
int for;
'for' is the reserve identifier. It is re-defined as variable declaration and it is not used in any standard library.
Hence it should violates the MISRA rule 20.1 and not MISRA rule 20.2.

Please explain the difference between MISRA 20.1 and 20.2 rules with suitable examples.

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: MISRA rule 20.1 and 20.2

Post by misra-c » Wed Mar 26, 2014 8:31 am

Your example contains a constraint error and therefore violates rule 1.1. Section 6.1.2 of the C90 standard states
In translation phases 7 and 8, an identifier shall not consist of the same sequence of characters as a keyword.
This statement does not apply to the pre-preprocessor, and so rule 20.1 covers macro names with the same names as keywords, but rule 20.2 does not need to include keywords. This was clarified in the MISRA-C:2004 Technical Corrigendum 1 and discussed in the Bulletin Board at http://www.misra-c2.com/forum/viewtopic.php?t=291.

Rule 20.1 restricts the identifier names which may be used with the #define and #undef pre-processor directives. For Example:

Code: Select all

#define sqrt   x   
#define for  x
#undef EOF

Rule 20.2 refers to the declaration of non-preprocessor names. e.g. objects, function names, typedefs. For Example:

Code: Select all

int strlen;
int strlen ( void )
typedef int strlen;

Rule 1.1 covers constraint errors. For Example:

Code: Select all

int for;
---
Posted by and on behalf of
the MISRA C Working Group

VRK
Posts: 2
Joined: Thu Mar 13, 2014 1:26 pm
Company: UTC AEROSPACE SYSTEM

Re: MISRA rule 20.1 and 20.2

Post by VRK » Tue Apr 01, 2014 7:37 am

As per your suggestion, I have used the following examples to violate the MISRA20.1 rule. But it’s violating the MISRA rule 20.2 and 19.4 only and not the MISRA rule 20.1.
#define sqrt x
#define for x

Please clarify.

Post Reply

Return to “6.20 Standard Libraries”