diff --git a/doc/macro_reference.qbk b/doc/macro_reference.qbk index a6d88138..04d8054c 100644 --- a/doc/macro_reference.qbk +++ b/doc/macro_reference.qbk @@ -1152,6 +1152,8 @@ Usage example: handle_error("ptr is NULL"); `` ]] +[[`BOOST_ATTRIBUTE_UNUSED`][Expands to `__attribute__((unused))` when this is available - +can be used to disable compiler warnings relating to unused types or variables.]] ] [endsect] diff --git a/include/boost/config/compiler/clang.hpp b/include/boost/config/compiler/clang.hpp index dd54691c..ab835ddc 100644 --- a/include/boost/config/compiler/clang.hpp +++ b/include/boost/config/compiler/clang.hpp @@ -205,8 +205,10 @@ # define BOOST_NO_CXX11_FINAL #endif -// Clang always supports variadic macros -// Clang always supports extern templates +// Unused attribute: +#if defined(__GNUC__) && (__GNUC__ >= 4) +# define BOOST_ATTRIBUTE_UNUSED __attribute__((unused)) +#endif #ifndef BOOST_COMPILER # define BOOST_COMPILER "Clang version " __clang_version__ diff --git a/include/boost/config/compiler/gcc.hpp b/include/boost/config/compiler/gcc.hpp index f217872f..714b4672 100644 --- a/include/boost/config/compiler/gcc.hpp +++ b/include/boost/config/compiler/gcc.hpp @@ -235,6 +235,12 @@ # define BOOST_NO_CXX11_REF_QUALIFIERS #endif +// +// Unused attribute: +#if __GNUC__ >= 4 +# define BOOST_ATTRIBUTE_UNUSED __attribute__((unused)) +#endif + #ifndef BOOST_COMPILER # define BOOST_COMPILER "GNU C++ version " __VERSION__ #endif diff --git a/include/boost/config/suffix.hpp b/include/boost/config/suffix.hpp index 237e3b87..1720dab7 100644 --- a/include/boost/config/suffix.hpp +++ b/include/boost/config/suffix.hpp @@ -940,6 +940,13 @@ namespace std{ using ::type_info; } #define BOOST_CONSTEXPR_OR_CONST constexpr #endif +// +// Unused variable/typedef workarounds: +// +#ifndef BOOST_ATTRIBUTE_UNUSED +# define BOOST_ATTRIBUTE_UNUSED +#endif + #define BOOST_STATIC_CONSTEXPR static BOOST_CONSTEXPR_OR_CONST // diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d5d768c2..05d27df8 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -89,6 +89,7 @@ test-suite config [ compile-fail threads/test_thread_fail1.cpp ] [ compile-fail threads/test_thread_fail2.cpp ] [ compile boost_fallthrough_test.cpp : [ check-target-builds has_clang_implicit_fallthrough "Clang implicit fallthrough" : clang:"-std=c++11 -Wimplicit-fallthrough" on all ] ] + [ compile helper_macro_test.cpp ] [ run cstdint_test.cpp : : : all gcc:"-Wno-long-long -Wextra" darwin:-Wno-long-long ] [ run cstdint_test2.cpp : : : all gcc:"-Wno-long-long -Wextra" darwin:-Wno-long-long ] [ compile cstdint_include_test.cpp : all gcc:-Wextra ] diff --git a/test/helper_macro_test.cpp b/test/helper_macro_test.cpp new file mode 100644 index 00000000..81737fec --- /dev/null +++ b/test/helper_macro_test.cpp @@ -0,0 +1,60 @@ +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include + +int test_fallthrough(int n) +{ + switch (n) + { + case 0: + n++; + BOOST_FALLTHROUGH; + case 1: + n++; + break; + } + return n; +} + +int test_unreachable(int i) +{ + if(BOOST_LIKELY(i)) return i; + + throw i; + BOOST_UNREACHABLE_RETURN(0); +} + +BOOST_FORCEINLINE int always_inline(int i){ return ++i; } +BOOST_NOINLINE int never_inline(int i){ return ++i; } + +BOOST_NORETURN void always_throw() +{ + throw 0; +} + + +#define test_fallthrough(x) foobar(x) + + +int main() +{ + typedef int unused_type BOOST_ATTRIBUTE_UNUSED; + try + { + int result = test_fallthrough BOOST_PREVENT_MACRO_SUBSTITUTION(0); + BOOST_STATIC_CONSTANT(bool, value = 0); + result += test_unreachable(1); + result += always_inline(2); + result += never_inline(3); + if(BOOST_UNLIKELY(!result)) + always_throw(); + } + catch(int) + { + return 1; + } + return 0; +} +