Skip to content

Commit

Permalink
suppress warnings in backwards compatibility unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gchenfc committed Dec 23, 2022
1 parent 581c2d5 commit a3e314f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
39 changes: 35 additions & 4 deletions gtsam/base/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,49 @@
#include <omp.h>
#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 {
Expand Down
23 changes: 23 additions & 0 deletions tests/testNonlinearFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> {
static_assert(std::is_same<Base, NoiseModelFactor>::value, "Base type wrong");
static_assert(std::is_same<This, NoiseModelFactor1<double>>::value,
Expand All @@ -351,6 +358,7 @@ class TestFactor1 : public NoiseModelFactor1<double> {
gtsam::NonlinearFactor::shared_ptr(new TestFactor1(*this)));
}
};
DIAGNOSTIC_POP()

/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactor1) {
Expand Down Expand Up @@ -380,6 +388,7 @@ TEST(NonlinearFactor, NoiseModelFactor1) {
}

/* ************************************************************************* */
IGNORE_DEPRECATED_PUSH
class TestFactor4 : public NoiseModelFactor4<double, double, double, double> {
static_assert(std::is_same<Base, NoiseModelFactor>::value, "Base type wrong");
static_assert(
Expand Down Expand Up @@ -411,6 +420,7 @@ class TestFactor4 : public NoiseModelFactor4<double, double, double, double> {
return boost::static_pointer_cast<gtsam::NonlinearFactor>(
gtsam::NonlinearFactor::shared_ptr(new TestFactor4(*this))); }
};
DIAGNOSTIC_POP()

/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactor4) {
Expand All @@ -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<TestFactor4::X1, double>::value,
"X1 type incorrect");
static_assert(std::is_same<TestFactor4::X2, double>::value,
Expand All @@ -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<N>` and `ValueType<N>` too
static_assert(std::is_same<TestFactor4::ValueType<1>, double>::value,
Expand All @@ -477,6 +489,7 @@ TEST(NonlinearFactor, NoiseModelFactor4) {
}

/* ************************************************************************* */
IGNORE_DEPRECATED_PUSH
class TestFactor5 : public NoiseModelFactor5<double, double, double, double, double> {
public:
typedef NoiseModelFactor5<double, double, double, double, double> Base;
Expand All @@ -500,6 +513,7 @@ class TestFactor5 : public NoiseModelFactor5<double, double, double, double, dou
.finished();
}
};
DIAGNOSTIC_POP()

/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactor5) {
Expand Down Expand Up @@ -527,6 +541,7 @@ TEST(NonlinearFactor, NoiseModelFactor5) {
}

/* ************************************************************************* */
IGNORE_DEPRECATED_PUSH
class TestFactor6 : public NoiseModelFactor6<double, double, double, double, double, double> {
public:
typedef NoiseModelFactor6<double, double, double, double, double, double> Base;
Expand Down Expand Up @@ -554,6 +569,7 @@ class TestFactor6 : public NoiseModelFactor6<double, double, double, double, dou
}

};
DIAGNOSTIC_POP()

/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactor6) {
Expand Down Expand Up @@ -656,6 +672,13 @@ TEST(NonlinearFactor, NoiseModelFactorN) {
EXPECT(assert_equal(H3_expected, H3));
EXPECT(assert_equal(H4_expected, H4));

// Test all functions/types for backwards compatibility
IGNORE_DEPRECATED_PUSH
static_assert(std::is_same<TestFactor4::X1, double>::value,
"X1 type incorrect");
EXPECT(assert_equal(tf.key3(), X(3)));
DIAGNOSTIC_POP()

// Test using `key<N>` and `ValueType<N>`
static_assert(std::is_same<TestFactorN::ValueType<1>, double>::value,
"ValueType<1> type incorrect");
Expand Down

0 comments on commit a3e314f

Please sign in to comment.