static_warning.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #ifndef BOOST_STATIC_WARNING_HPP
  2. #define BOOST_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(__BORLANDC__) && (__BORLANDC__ >= 0x600)
  70. # define BOOST_HAS_DESCRIPTIVE_UNREFERENCED_VARIABLE_WARNING
  71. # elif defined(__GNUC__) && !defined(BOOST_INTEL) // && (__GNUC__ * 100 + __GNUC_MINOR__ <= 302)
  72. # define BOOST_HAS_DESCRIPTIVE_DIVIDE_BY_ZERO_WARNING
  73. # elif defined(__DMC__)
  74. # define BOOST_HAS_DESCRIPTIVE_RETURNING_ADDRESS_OF_TEMPORARY_WARNING
  75. # elif defined(BOOST_MSVC) // && (BOOST_MSVC < 1300)
  76. # define BOOST_NO_PREDEFINED_LINE_MACRO
  77. # pragma warning(disable:4094) // C4094: untagged 'stuct' declared no symbols
  78. #endif
  79. //------------------Helper templates------------------------------------------//
  80. namespace boost {
  81. struct STATIC_WARNING;
  82. template<bool>
  83. struct static_warning_impl;
  84. template<>
  85. struct static_warning_impl<false> {
  86. enum { value = 0 };
  87. #if !defined(BOOST_HAS_DESCRIPTIVE_UNREFERENCED_VARIABLE_WARNING) && \
  88. !defined(BOOST_HAS_DESCRIPTIVE_RETURNING_ADDRESS_OF_TEMPORARY_WARNING)
  89. typedef boost::STATIC_WARNING type;
  90. #else
  91. typedef int type;
  92. #endif
  93. #if defined(BOOST_NO_PREDEFINED_LINE_MACRO)
  94. struct STATIC_WARNING { };
  95. #endif
  96. };
  97. template<>
  98. struct static_warning_impl<true> {
  99. enum { value = 1 };
  100. struct type { type() { } int* operator&() { return new int; } };
  101. #if defined(BOOST_NO_PREDEFINED_LINE_MACRO)
  102. class STATIC_WARNING { };
  103. #endif
  104. };
  105. } // namespace boost
  106. //------------------Definition of BOOST_STATIC_WARNING------------------------//
  107. #if defined(BOOST_HAS_DESCRIPTIVE_UNREFERENCED_VARIABLE_WARNING)
  108. # define BOOST_STATIC_WARNING_IMPL(B) \
  109. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  110. void f() { \
  111. ::boost::static_warning_impl<(bool)( B )>::type \
  112. STATIC_WARNING; \
  113. } \
  114. } \
  115. /**/
  116. #elif defined(BOOST_HAS_DESCRIPTIVE_RETURNING_ADDRESS_OF_TEMPORARY_WARNING)
  117. # define BOOST_STATIC_WARNING_IMPL(B) \
  118. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  119. int* f() { \
  120. ::boost::static_warning_impl<(bool)( B )>::type \
  121. STATIC_WARNING; \
  122. return &STATIC_WARNING; \
  123. } \
  124. } \
  125. /**/
  126. #elif defined(BOOST_HAS_DESCRIPTIVE_DIVIDE_BY_ZERO_WARNING)
  127. # define BOOST_STATIC_WARNING_IMPL(B) \
  128. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  129. int f() { int STATIC_WARNING = 1; \
  130. return STATIC_WARNING / \
  131. boost::static_warning_impl<(bool)( B )>::value; } \
  132. } \
  133. /**/
  134. #elif defined(BOOST_NO_PREDEFINED_LINE_MACRO)
  135. // VC6; __LINE__ macro broken when -ZI is used see Q199057, so
  136. // non-conforming workaround is used.
  137. # define BOOST_STATIC_WARNING_IMPL(B) \
  138. struct { \
  139. struct S { \
  140. typedef boost::static_warning_impl<(bool)( B )> f; \
  141. friend class f::STATIC_WARNING; \
  142. }; \
  143. } \
  144. /**/
  145. #else // Deletion of pointer to incomplete type.
  146. # define BOOST_STATIC_WARNING_IMPL(B) \
  147. struct BOOST_JOIN(STATIC_WARNING, __LINE__) { \
  148. ::boost::static_warning_impl<(bool)( B )>::type* p; \
  149. void f() { delete p; } \
  150. } \
  151. /**/
  152. #endif
  153. #ifndef BOOST_DISABLE_STATIC_WARNINGS
  154. # define BOOST_STATIC_WARNING(B) BOOST_STATIC_WARNING_IMPL(B)
  155. #else // #ifdef BOOST_ENABLE_STATIC_WARNINGS //-------------------------------//
  156. # define BOOST_STATIC_WARNING(B) BOOST_STATIC_WARNING_IMPL(true)
  157. #endif
  158. #endif // BOOST_STATIC_WARNING_HPP