MISRA Discussion Forums
Do any other forms of conditional inclusion violate Directive 5.7.2? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188)
+---- Forum: 4.5 Lexical conventions (https://forum.misra.org.uk/forumdisplay.php?fid=191)
+---- Thread: Do any other forms of conditional inclusion violate Directive 5.7.2? (/showthread.php?tid=1706)



Do any other forms of conditional inclusion violate Directive 5.7.2? - rg99 - 27-09-2024

In particular, which of the following violates Directive 5.7.2?

Code:
#if 0
#endif

#if 0U
#endif

#if '\0'
#endif

#if 1 - 1
#endif

#if MACRO * 0
#endif

#if MACRO1 - MACRO2
#endif



RE: Do any other forms of conditional inclusion violate Directive 5.7.2? - misra cpp - 27-09-2024

Firstly, as stated in your examples, all the statements are compliant, as there are no code statements being commented out.

If there were any code statements between the #if and the #endif, then arguably, they are all non-compliant.

This is one of the rare cases where what we are trying to do depends upon the intent of the programmer - which a checking tool cannot know. The issue is that commenting out code makes the program harder to manually review and may lead to confusion. However, there are occasions when different code is required in different builds of a program - for example when compiling for execution on multiple platforms. It is difficult to specify a rule that prevents the 'bad case' but allows the 'good' - hence why its a directive and not a rule.

What a tool chooses to report against this requirement is really a 'quality of implementation' issue.
For example, a tool might report  "#if 0"  but not  "#if macro0"  where macro0 expands to "0"