MISRA Discussion Forums
A16-0-1 Use #define for conditional file inclusion? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: AUTOSAR C++:2014 rules (https://forum.misra.org.uk/forumdisplay.php?fid=185)
+--- Thread: A16-0-1 Use #define for conditional file inclusion? (/showthread.php?tid=1609)



A16-0-1 Use #define for conditional file inclusion? - cgpzs - 25-03-2022

Hi,

It's not clear if A16-0-1 allows using #define exclusively for creating header guards, or also for conditional file inclusion. Could you comment on that?

Example:

Code:
// foo.h
#if defined (SOMETHING) && !defined (SOMETHING_ELSE)  // Complex logic that we don't want to repeat everywhere
  #define SHOULD_INCLUDE_A              // Violating A16-0-1?
#else
  #define SHOULD_INCLUDE_B              // Violating A16-0-1?
#endif

// bar.h
#if   defined (SHOULD_INCLUDE_A)
  #include "bar_a.h"
#elif defined (SHOULD_INCLUDE_B)
  #include "bar_b.h"
#endif

// baz.h
#if   defined (SHOULD_INCLUDE_A)
  #include "baz_a.h"
#elif defined (SHOULD_INCLUDE_B)
  #include "baz_b.h"
#endif

Thanks!


RE: A16-0-1 Use #define for conditional file inclusion? - misra cpp - 13-05-2022

Whilst the example doesn't violate the letter of this rule, it certainly is against the spirit and intension.

I'd not be surprised if an analysis tool reported this example as a violation.


RE: A16-0-1 Use #define for conditional file inclusion? - kent.dorfman766 - 25-08-2022

I too have issue with the rules regarding the preprocessor directives in my line of work. Much of what I do is interfacing higher level algorithmic C++ code with POSIX API system functions, and my wrapper classes are about 90% non-compliant. Frequently running into violations surrounding the concept of "system frameworks" and reusable libraries. If I generate a shared utility library .so then I can pretty much guarantee that not ever class/method in it will be used in the target application.

The OP example is extremely relevant when designing platform portable code for multiple embedded environments.


RE: A16-0-1 Use #define for conditional file inclusion? - misra cpp - 07-10-2022

Your concern is noted.

The rules like 'all function definitions shall be used' are being reviewed for the next version of the standard, specifically to assist in the use and creation of libraries.