MISRA C++:2023 Rule 19.1.1
Description
Rule Definition
The defined preprocessor operator shall be used appropriately.
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
defined
is used without an identifier.The operator
defined
appears after macro expansion.The operator
defined
is 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