static_warning.hpp 3.7 KB

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