Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.DEFINE.WRONGNAME

Usage 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 name

C90 [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

Amplification

This rule applies to the following:
  • Identifiers or macro names beginning with an underscore;
  • Identifiers in file scope described in Section 7, “Library”, of The Standard;
  • Macro names described in Section 7, “Library”, of The Standard as being defined in a standard header.
This rule also prohibits the use of #define or #undef on the identifier defined as this results in explicitly undefined behaviour.
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:
  • A macro defined in a standard header, or
  • An identifier with file scope declared in a standard header is well-defined provided that the header is not included. This rule does not permit such definitions on the grounds that they are likely to cause confusion.
Note: the macro NDEBUG is not defined in a standard header and may therefore be #define'd.

Rationale

Reserved 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 also

Rule 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)]

Rationale

It 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