Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A16-0-1 Use #define for conditional file inclusion?
#1
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!
Reply
#2
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
Reply
#3
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.
Reply
#4
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)