MISRA C++:2023 Rule 19.1.1
Description
Rule Definition
The defined preprocessor operator shall be used appropriately. 1
Rationale
The defined preprocessor operator checks whether an identifier is
defined as a macro. The permissible forms for this operator are:
defined (identifier)defined identifier
Using any other form results in invalid code that compiler might not
report. For instance, if you use expressions as arguments for the
defined operator, the code is invalid. If the compiler does not
report the invalid usage of defined, diagnosing the invalid code is
difficult.
If your #if or similar preprocessor directives expand to create a
defined statement, the code behavior is undefined. For
instance:
#define DEFINED defined #if DEFINED(X)
#if preprocessor directive expands
to form a defined operation. Depending on your environment, the code
might behave in unexpected ways, leading to bugs that are difficult to diagnose.To avoid invalid code, bugs, and undefined behavior, use only the permitted forms
when using the defined operator.
Polyspace Implementation
Polyspace® flags incorrect usages of the defined operator, such as:
The operator
definedis used without an identifier.The operator
definedappears after macro expansion.The operator
definedis used with a complex expression.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
| Group: Preprocessing Directives |
| Category: Required |
Version History
Introduced in R2024b
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.