MISRA.DEFINE.WRONGNAMEUsage of a name from the standard library for naming a macro.
MISRA C 2012 Rule 21.1: #define and #undef shall not be used on a reserved identifier or reserved macro nameC90 [Undefined 54, 57, 58, 62, 71] C99 [Undefined 93, 100, 101, 104, 108, 116, 118, 130] Category: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationThis rule applies to the following:
This rule does not include those identifiers or macro names that are described in the section of the applicable C standard entitled “Future Library Directions”.The Standard states that defining a macro with the same name as:
RationaleReserved identifiers and reserved macro names are intended for use by the implementation. Removing or changing the meaning of a reserved macro may result in undefined behaviour. Example#undef __LINE__ /* Non-compliant - begins with _ */ #define _ GUARD_H 1 /* Non-compliant - begins with _ */ #undef _BUILTIN_sqrt /* Non-compliant - the implementation * may use _BUILTIN_sqrt for other * purposes, e.g. generating a sqrt * instruction */ #define defined /* Non-compliant - reserved identifier */ #define errno my_errno /* Non-compliant - library identifier */ #define isneg( x ) ( ( x ) < 0 ) /* Compliant - rule doesn't include * future library * directions */ See alsoRule 20.4 MISRA-C 2004 Rule 20.1 (required): Reserved identifiers, macros and functions in the standard library, shall not be defined, redefined or undefined.Usage of a name from the standard library for naming a macro. This rule is also covered by MISRA.DEFINE.WRONGNAME.UNDERSCORE, MISRA.DEFINE.EXPANSION.UNSAFE and MISRA.DEFINE.INCL.UNSAFE. [Undefined 54, 57, 58, 62] 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. MISRA-C++ 2008 Rule 17-0-1 (required): Reserved identifiers, macros and functions in the standard library shall not be defined, redefined or undefined.This rule is also covered by MISRA.DEFINE.WRONGNAME.UNDERSCORE. [Undefined 16.8(3)] RationaleIt is generally bad practice to #undef a macro that is defined in the standard library. It is also bad practice to #define a macro name that is a C++ reserved identifier, or 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 that are known to give rise to undefined behaviour if they are redefined or undefined, including defined, _ _LINE_ _, _ _FILE_ _, _ _DATE_ _, _ _TIME_ _, _ _STDC_ _, errno and assert. Refer to ISO/IEC 14882:2003 [1] for a list of the identifiers that are reserved. Generally, all identifiers that begin with the underscore character are reserved. Note that this rule applies regardless of which header files, if any, are actually included. Example#define __TIME__ 11111111 // Non-compliant |