static_warning.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef BOOST_SERIALIZATION_STATIC_WARNING_HPP
  2. #define BOOST_SERIALIZATION_STATIC_WARNING_HPP
  3. // (C) Copyright Robert Ramey 2003. Jonathan Turkanis 2004.
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // MS compatible compilers support #pragma once
  7. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  8. # pragma once
  9. #endif
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. // See http://www.boost.org/libs/static_assert for documentation.
  12. /*
  13. Revision history:
  14. 15 June 2003 - Initial version.
  15. 31 March 2004 - improved diagnostic messages and portability
  16. (Jonathan Turkanis)
  17. 03 April 2004 - works on VC6 at class and namespace scope
  18. - ported to DigitalMars
  19. - static warnings disabled by default; when enabled,
  20. uses pragmas to enable required compiler warnings
  21. on MSVC, Intel, Metrowerks and Borland 5.x.
  22. (Jonathan Turkanis)
  23. 30 May 2004 - tweaked for msvc 7.1 and gcc 3.3
  24. - static warnings ENabled by default; when enabled,
  25. (Robert Ramey)
  26. */
  27. #include <boost/config.hpp>
  28. //
  29. // Implementation
  30. // Makes use of the following warnings:
  31. // 1. GCC prior to 3.3: division by zero.
  32. // 2. BCC 6.0 preview: unreferenced local variable.
  33. // 3. DigitalMars: returning address of local automatic variable.
  34. // 4. VC6: class previously seen as struct (as in 'boost/mpl/print.hpp')
  35. // 5. All others: deletion of pointer to incomplete type.
  36. //
  37. // The trick is to find code which produces warnings containing the name of
  38. // a structure or variable. Details, with same numbering as above:
  39. // 1. static_warning_impl<B>::value is zero iff B is false, so diving an int
  40. // by this value generates a warning iff B is false.
  41. // 2. static_warning_impl<B>::type has a constructor iff B is true, so an
  42. // unreferenced variable of this type generates a warning iff B is false.
  43. // 3. static_warning_impl<B>::type overloads operator& to return a dynamically
  44. // allocated int pointer only is B is true, so returning the address of an
  45. // automatic variable of this type generates a warning iff B is fasle.
  46. // 4. static_warning_impl<B>::STATIC_WARNING is decalred as a struct iff B is
  47. // false.
  48. // 5. static_warning_impl<B>::type is incomplete iff B is false, so deleting a
  49. // pointer to this type generates a warning iff B is false.
  50. //
  51. //------------------Enable selected warnings----------------------------------//
  52. // Enable the warnings relied on by BOOST_STATIC_WARNING, where possible. The
  53. // only pragma which is absolutely necessary here is for Borland 5.x, since
  54. // W8073 is disabled by default. If enabling selected warnings is considered
  55. // unacceptable, this section can be replaced with:
  56. // #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x600)
  57. // pragma warn +stu
  58. // #endif
  59. # if defined(BOOST_MSVC)
  60. # pragma warning(2:4150) // C4150: deletion of pointer to incomplete type 'type'.
  61. # elif defined(BOOST_INTEL) && (defined(__WIN32__) || defined(WIN32))
  62. # pragma warning(2:457) // #457: delete of pointer to incomplete class.
  63. # elif defined(__BORLANDC__) && (__BORLANDC__ <= 0x600)
  64. # pragma warn +stu // W8073: Undefined structure 'structure'.
  65. # elif defined(__MWERKS__)
  66. # pragma extended_errorcheck on // Enable 'extended error checking'.
  67. # endif
  68. //------------------Configure-------------------------------------------------//
  69. # if defined(BOOST_INTEL)
  70. # elif defined(__PGI)
  71. # define BOOST_HAS_DESCRIPTIVE_DIVIDE_BY_ZERO_WARNING
  72. # elif defined(__GNUC__) && !defined(BOOST_INTEL) // && (__GNUC__ * 100 + __GNUC_MINOR__ <= 302)
  73. # define BOOST_HAS_DESCRIPTIVE_DIVIDE_BY_ZERO_WARNING
  74. # elif defined(__DECCXX) // for Tru64
  75. # define BOOST_HAS_DESCRIPTIVE_DIVIDE_BY_ZERO_WARNING
  76. # elif defined(__DMC__)
  77. # define BOOST_HAS_DESCRIPTIVE_RETURNING_ADDRESS_OF_TEMPORARY_WARNING
  78. # elif defined(BOOST_MSVC) // && (BOOST_MSVC < 1300)
  79. # define BOOST_NO_PREDEFINED_LINE_MACRO
  80. # pragma warning(disable:4094) // C4094: untagged 'struct' declared no symbols
  81. # else
  82. # define BOOST_HAS_DESCRIPTIVE_INCOMPLETE_TYPE_WARNING
  83. #endif
  84. //------------------Helper templates------------------------------------------//
  85. namespace boost {
  86. namespace serialization {
  87. struct STATIC_WARNING;
  88. template<bool>
  89. struct static_warning_impl;
  90. template<>
  91. struct static_warning_impl<false> {
  92. enum { value = 0 };
  93. #if !defined(BOOST_HAS_DESCRIPTIVE_UNREFERENCED_VARIABLE_WARNING) && \
  94. !defined(BOOST_HAS_DESCRIPTIVE_RETURNING_ADDRESS_OF_TEMPORARY_WARNING)
  95. typedef boost::serialization::STATIC_WARNING type;
  96. #else
  97. typedef int type;
  98. #endif
  99. #if defined(BOOST_NO_PREDEFINED_LINE_MACRO)
  100. struct STATIC_WARNING { };
  101. #endif
  102. };
  103. template<>
  104. struct static_warning_impl<true> {
  105. enum { value = 1 };
  106. struct type { type() { } int* operator&() { return new int; } };
  107. #if defined(BOOST_NO_PREDEFINED_LINE_MACRO)
  108. class STATIC_WARNING { };
  109. #endif
  110. };
  111. } // namespace serialization
  112. } // namespace boost
  113. //------------------Definition of BOOST_STATIC_WARNING------------------------//
  114. #if defined(BOOST_HAS_DESCRIPTIVE_UNREFERENCED_VARIABLE_WARNING)
  115. # define BOOST_STATIC_WARNING_IMPL(B) \
  116. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  117. void f() { \
  118. ::boost::serialization::static_warning_impl<(bool)( B )>::type \
  119. STATIC_WARNING; \
  120. } \
  121. } \
  122. /**/
  123. #elif defined(BOOST_HAS_DESCRIPTIVE_RETURNING_ADDRESS_OF_TEMPORARY_WARNING)
  124. # define BOOST_STATIC_WARNING_IMPL(B) \
  125. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  126. int* f() { \
  127. ::boost::serialization::static_warning_impl<(bool)( B )>::type \
  128. STATIC_WARNING; \
  129. return &STATIC_WARNING; \
  130. } \
  131. } \
  132. /**/
  133. #elif defined(BOOST_HAS_DESCRIPTIVE_DIVIDE_BY_ZERO_WARNING)
  134. # define BOOST_STATIC_WARNING_IMPL(B) \
  135. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  136. int f() { \
  137. int STATIC_WARNING = 1; \
  138. return STATIC_WARNING / \
  139. boost::serialization::static_warning_impl<(bool)( B )>::value; } \
  140. } \
  141. /**/
  142. #elif defined(BOOST_NO_PREDEFINED_LINE_MACRO)
  143. // VC6; __LINE__ macro broken when -ZI is used see Q199057, so
  144. // non-conforming workaround is used.
  145. # define BOOST_STATIC_WARNING_IMPL(B) \
  146. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  147. struct S { \
  148. typedef boost::serialization::static_warning_impl<(bool)( B )> f; \
  149. friend class f::STATIC_WARNING; \
  150. }; \
  151. }; \
  152. /**/
  153. #elif defined(BOOST_HAS_DESCRIPTIVE_INCOMPLETE_TYPE_WARNING)
  154. # define BOOST_STATIC_WARNING_IMPL(B) \
  155. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  156. ::boost::serialization::static_warning_impl<(bool)( B )>::type* p; \
  157. void f() { delete p; } \
  158. } \
  159. /**/
  160. #else // not defined for this compiler
  161. # define BOOST_STATIC_WARNING_IMPL(B)
  162. #endif
  163. #ifndef BOOST_DISABLE_STATIC_WARNINGS
  164. # define BOOST_STATIC_WARNING(B) BOOST_STATIC_WARNING_IMPL(B)
  165. #else // #ifdef BOOST_ENABLE_STATIC_WARNINGS //-------------------------------//
  166. # define BOOST_STATIC_WARNING(B) BOOST_STATIC_WARNING_IMPL(true)
  167. #endif
  168. #endif // BOOST_SERIALIZATION_STATIC_WARNING_HPP