The preprocessor shall only be used for unconditional and conditional file inclusion and include guards, and using specific directives
The preprocessor shall only be used for unconditional and conditional file inclusion and include guards, and using specific directives.
Other than unconditional and conditional file inclusion and include guards, avoid the use of preprocessor directives. Use a safer alternative instead. For instance:
Instead of:
#define MIN(a,b) ((a < b)? (a) : (b))
Instead of:
#define MAX_ARRAY_SIZE 1024U
In these situations, preprocessor directives do not provide the benefits that the alternatives provide, such as linkage, type checking, overloading, and so on.
The rule checker does not allow the use of preprocessor directives. The only exceptions are:
#ifdef, #ifndef, #if,
#if defined, #elif, #else
and #endif, only if used for conditional file inclusion and include
guards.
#define only if used for defining macros to be used in include
guards. For instance, in this example, the macro __FILE_H__
prevents the contents of the header file from being included more than
once:
/* aHeader.h */ #ifndef __FILE_H__ #define __FILE_H__ /* Contents of header file */ #endif
When #ifdef, #define and
#endif are used as include guards in a header file, the entire
content of the header file must be in the include guard.
#include
The checker does not allow the #define directives in other contexts.
If you use #define-s for purposes other than for include guards, do one
of the following:
To define macros when compiling your code, instead of
#define-s, use compilation flags (such as the GCC option
-D). When running a Polyspace® analysis, use the equivalent Polyspace option Preprocessor definitions (-D).
To retain the use of #define in your code, justify the
violation using comments in your results or code. See Address Polyspace Results Through Bug Fixes or Justifications.
If you expect a rule violation but do not see it, refer to Coding Standard Violations Not Displayed.
| Group: Preprocessing directives |
| Category: Required, Automated |