diff --git a/gtsam/base/types.h b/gtsam/base/types.h index cb8412cdf4..b9f4b0a3e1 100644 --- a/gtsam/base/types.h +++ b/gtsam/base/types.h @@ -46,18 +46,49 @@ #include #endif +/* Define macros for ignoring compiler warnings. + * Usage Example: + * ``` + * CLANG_DIAGNOSTIC_PUSH_IGNORE("-Wdeprecated-declarations") + * GCC_DIAGNOSTIC_PUSH_IGNORE("-Wdeprecated-declarations") + * MSVC_DIAGNOSTIC_PUSH_IGNORE(4996) + * // ... code you want to suppress deprecation warnings for ... + * DIAGNOSTIC_POP() + * ``` + */ +#define DO_PRAGMA(x) _Pragma (#x) #ifdef __clang__ # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag) \ _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"" diag "\"") + DO_PRAGMA(clang diagnostic ignored diag) #else # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag) #endif -#ifdef __clang__ -# define CLANG_DIAGNOSTIC_POP() _Pragma("clang diagnostic pop") +#ifdef __GNUC__ +# define GCC_DIAGNOSTIC_PUSH_IGNORE(diag) \ + _Pragma("GCC diagnostic push") \ + DO_PRAGMA(GCC diagnostic ignored diag) +#else +# define GCC_DIAGNOSTIC_PUSH_IGNORE(diag) +#endif + +#ifdef _MSC_VER +# define MSVC_DIAGNOSTIC_PUSH_IGNORE(code) \ + _Pragma("warning ( push )") \ + DO_PRAGMA(warning ( disable : code )) +#else +# define MSVC_DIAGNOSTIC_PUSH_IGNORE(code) +#endif + +#if defined(__clang__) +# define DIAGNOSTIC_POP() _Pragma("clang diagnostic pop") +#elif defined(__GNUC__) +# define DIAGNOSTIC_POP() _Pragma("GCC diagnostic pop") +#elif defined(_MSC_VER) +# define DIAGNOSTIC_POP() _Pragma("warning ( pop )") #else -# define CLANG_DIAGNOSTIC_POP() +# define DIAGNOSTIC_POP() #endif namespace gtsam { diff --git a/tests/testNonlinearFactor.cpp b/tests/testNonlinearFactor.cpp index 99ec2f501e..c536a48c3f 100644 --- a/tests/testNonlinearFactor.cpp +++ b/tests/testNonlinearFactor.cpp @@ -330,6 +330,13 @@ TEST( NonlinearFactor, cloneWithNewNoiseModel ) } /* ************************************************************************* */ +// Suppress deprecation warnings while we are testing backwards compatibility +#define IGNORE_DEPRECATED_PUSH \ + CLANG_DIAGNOSTIC_PUSH_IGNORE("-Wdeprecated-declarations") \ + GCC_DIAGNOSTIC_PUSH_IGNORE("-Wdeprecated-declarations") \ + MSVC_DIAGNOSTIC_PUSH_IGNORE(4996) +/* ************************************************************************* */ +IGNORE_DEPRECATED_PUSH class TestFactor1 : public NoiseModelFactor1 { static_assert(std::is_same::value, "Base type wrong"); static_assert(std::is_same>::value, @@ -351,6 +358,7 @@ class TestFactor1 : public NoiseModelFactor1 { gtsam::NonlinearFactor::shared_ptr(new TestFactor1(*this))); } }; +DIAGNOSTIC_POP() /* ************************************ */ TEST(NonlinearFactor, NoiseModelFactor1) { @@ -380,6 +388,7 @@ TEST(NonlinearFactor, NoiseModelFactor1) { } /* ************************************************************************* */ +IGNORE_DEPRECATED_PUSH class TestFactor4 : public NoiseModelFactor4 { static_assert(std::is_same::value, "Base type wrong"); static_assert( @@ -411,6 +420,7 @@ class TestFactor4 : public NoiseModelFactor4 { return boost::static_pointer_cast( gtsam::NonlinearFactor::shared_ptr(new TestFactor4(*this))); } }; +DIAGNOSTIC_POP() /* ************************************ */ TEST(NonlinearFactor, NoiseModelFactor4) { @@ -434,6 +444,7 @@ TEST(NonlinearFactor, NoiseModelFactor4) { EXPECT(assert_equal((Vector)(Vector(1) << 0.5 * -30.).finished(), jf.getb())); // Test all functions/types for backwards compatibility + IGNORE_DEPRECATED_PUSH static_assert(std::is_same::value, "X1 type incorrect"); static_assert(std::is_same::value, @@ -452,6 +463,7 @@ TEST(NonlinearFactor, NoiseModelFactor4) { EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 2.).finished(), H.at(1))); EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 3.).finished(), H.at(2))); EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 4.).finished(), H.at(3))); + DIAGNOSTIC_POP() // And test "forward compatibility" using `key` and `ValueType` too static_assert(std::is_same, double>::value, @@ -477,6 +489,7 @@ TEST(NonlinearFactor, NoiseModelFactor4) { } /* ************************************************************************* */ +IGNORE_DEPRECATED_PUSH class TestFactor5 : public NoiseModelFactor5 { public: typedef NoiseModelFactor5 Base; @@ -500,6 +513,7 @@ class TestFactor5 : public NoiseModelFactor5 { public: typedef NoiseModelFactor6 Base; @@ -554,6 +569,7 @@ class TestFactor6 : public NoiseModelFactor6::value, + "X1 type incorrect"); + EXPECT(assert_equal(tf.key3(), X(3))); + DIAGNOSTIC_POP() + // Test using `key` and `ValueType` static_assert(std::is_same, double>::value, "ValueType<1> type incorrect");