Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A16-0-1 Use #define for conditional file inclusion?

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?


// 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?
  #define SHOULD_INCLUDE_B              // Violating A16-0-1?

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

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

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.
Posted by and on behalf of
the MISRA C++ Working Group
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.
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.
Posted by and on behalf of
the MISRA C++ Working Group

Forum Jump:

Users browsing this thread: 2 Guest(s)