static_warning.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 +st
  58. // #endif
  59. // 6. replaced implementation with one which depends solely on
  60. // mpl::print<>. The previous one was found to fail for functions
  61. // under recent versions of gcc and intel compilers - Robert Ramey
  62. #include <boost/mpl/bool.hpp>
  63. #include <boost/mpl/print.hpp>
  64. #include <boost/mpl/eval_if.hpp>
  65. namespace boost {
  66. namespace serialization {
  67. template<int L>
  68. struct BOOST_SERIALIZATION_STATIC_WARNING_LINE{};
  69. template<bool B, int L>
  70. struct static_warning_test{
  71. typename boost::mpl::eval_if_c<
  72. B,
  73. boost::mpl::true_,
  74. typename boost::mpl::identity<
  75. boost::mpl::print<
  76. BOOST_SERIALIZATION_STATIC_WARNING_LINE<L>
  77. >
  78. >
  79. >::type type;
  80. };
  81. template<int i>
  82. struct BOOST_SERIALIZATION_SS {};
  83. } // serialization
  84. } // boost
  85. #define BOOST_SERIALIZATION_BSW(B, L) \
  86. typedef boost::serialization::BOOST_SERIALIZATION_SS< \
  87. sizeof( boost::serialization::static_warning_test< B, L > ) \
  88. > BOOST_JOIN(STATIC_WARNING_LINE, L);
  89. #define BOOST_STATIC_WARNING(B) BOOST_SERIALIZATION_BSW(B, __LINE__)
  90. #endif // BOOST_SERIALIZATION_STATIC_WARNING_HPP