[previous] MISRA-C:2004  Rule  20.1:  (Required) [next] Reserved identifiers, macros and functions in the standard library, shall not be defined, redefined or undefined.
It is generally bad practice to #undef a macro which is defined in the standard library. It is also bad practice to #define a macro name which is a C reserved identifier, a C keyword or the name of any macro, object or function in the standard library. For example, there are some specific reserved words and function names which are known to give rise to undefined behaviour if they are redefined or undefined, including defined, _ _LINE_ _, _ _FILE_ _, _ _DATE_ _, _ _TIME_ _, _ _STDC_ _, errno and assert.

See also Rule 19.6 regarding the use of #undef.

Reserved identifiers are defined by ISO/IEC 9899:1990 [2] Sections 7.1.3 "Reserved identifiers" and 6.8.8 "Predefined macro names". Macros in the standard library are examples of reserved identifiers. Functions in the standard library are examples of reserved identifiers. Any identifier in the standard library is considered a reserved identifier in any context i.e. at any scope or regardless of header files.

The defining, redefining or undefining of the reserved identifiers defined in 7.13 "Future library directions" is advisory.

Rule 20.1 applies regardless of which, if any, header files are included.

Example Code:


/*PRQA S 862 ++ */
#include "misra.h"
#include "m2cmex.h"
#include "gen.h"

#define defined ! defined       /* MISRA Violation */  /* 0836 */


#undef __LINE__                 /* MISRA Violation */  /* 0848 */
#undef __FILE__                 /* MISRA Violation */  /* 0848 */
#undef __DATE__                 /* MISRA Violation */  /* 0848 */
#undef __TIME__                 /* MISRA Violation */  /* 0848 */
#undef __STDC__                 /* MISRA Violation */  /* 0848 */

#define __LINE__ 1              /* MISRA Violation */  /* 0854 */
#define __FILE__ "default"      /* MISRA Violation */  /* 0854 */
#define __DATE__ "01-01-2000"   /* MISRA Violation */  /* 0854 */
#define __TIME__ "00:00:00"     /* MISRA Violation */  /* 0854 */
#define __STDC__                /* MISRA Violation */  /* 0854 */

#define NULL (void *)0          /* MISRA Violation */
#define tan 1                   /* MISRA Violation */
#define printf xprintf          /* MISRA Violation */

extern S16 test_2001( void )
{
   return 1;
}


QAC messages that encompass this guideline:

0836 [U] Definition of macro named 'defined'.
0848 [U] Attempting to #undef '%s', which is a predefined macro name.
0854 [U] Attempting to #define '%s', which is a predefined macro name.
3439 Macro redefines a keyword.
4600 The macro '%1s' is also defined in '<%2s>'.
4601 The macro '%1s' is the name of an identifier in '<%2s>'.


Related rules:

Rule  1.2 No reliance shall be placed on undefined or unspecified behaviour.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents