aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2018-03-09 10:00:51 +0100
committerGravatar Gael Guennebaud <g.gael@free.fr>2018-03-09 10:00:51 +0100
commitf6be7289d703ea608db01bfae1fb41cb93465363 (patch)
tree7252178a0aaedcabee8a9d3aca7fb4dc7714c9dc
parentd820ab9edc0b38af4cdb3d545714a0c9083e5a78 (diff)
Implement better static assertion checking to make sure that the first assertion is a static one and not a runtime one.
-rw-r--r--Eigen/src/Core/util/StaticAssert.h3
-rw-r--r--test/main.h37
-rw-r--r--test/selfadjoint.cpp6
3 files changed, 40 insertions, 6 deletions
diff --git a/Eigen/src/Core/util/StaticAssert.h b/Eigen/src/Core/util/StaticAssert.h
index 987d5fce4..500e47792 100644
--- a/Eigen/src/Core/util/StaticAssert.h
+++ b/Eigen/src/Core/util/StaticAssert.h
@@ -24,6 +24,7 @@
*
*/
+#ifndef EIGEN_STATIC_ASSERT
#ifndef EIGEN_NO_STATIC_ASSERT
#if EIGEN_MAX_CPP_VER>=11 && (__has_feature(cxx_static_assert) || (defined(__cplusplus) && __cplusplus >= 201103L) || (EIGEN_COMP_MSVC >= 1600))
@@ -132,7 +133,7 @@
#define EIGEN_STATIC_ASSERT(CONDITION,MSG) eigen_assert((CONDITION) && #MSG);
#endif // EIGEN_NO_STATIC_ASSERT
-
+#endif // EIGEN_STATIC_ASSERT
// static assertion failing if the type \a TYPE is not a vector type
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE) \
diff --git a/test/main.h b/test/main.h
index 429c44f81..6079cbd06 100644
--- a/test/main.h
+++ b/test/main.h
@@ -175,6 +175,12 @@ namespace Eigen
eigen_assert_exception(void) {}
~eigen_assert_exception() { Eigen::no_more_assert = false; }
};
+
+ struct eigen_static_assert_exception
+ {
+ eigen_static_assert_exception(void) {}
+ ~eigen_static_assert_exception() { Eigen::no_more_assert = false; }
+ };
}
// If EIGEN_DEBUG_ASSERTS is defined and if no assertion is triggered while
// one should have been, then the list of excecuted assertions is printed out.
@@ -238,6 +244,7 @@ namespace Eigen
else \
EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
}
+
#ifdef EIGEN_EXCEPTIONS
#define VERIFY_RAISES_ASSERT(a) { \
Eigen::no_more_assert = false; \
@@ -249,13 +256,39 @@ namespace Eigen
catch (Eigen::eigen_assert_exception&) { VERIFY(true); } \
Eigen::report_on_cerr_on_assert_failure = true; \
}
- #endif //EIGEN_EXCEPTIONS
+ #endif // EIGEN_EXCEPTIONS
#endif // EIGEN_DEBUG_ASSERTS
+ #if defined(TEST_CHECK_STATIC_ASSERTIONS) && defined(EIGEN_EXCEPTIONS)
+ #define EIGEN_STATIC_ASSERT(a,MSG) \
+ if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\
+ { \
+ Eigen::no_more_assert = true; \
+ if(report_on_cerr_on_assert_failure) \
+ eigen_plain_assert(a && #MSG); \
+ else \
+ EIGEN_THROW_X(Eigen::eigen_static_assert_exception()); \
+ }
+ #define VERIFY_RAISES_STATIC_ASSERT(a) { \
+ Eigen::no_more_assert = false; \
+ Eigen::report_on_cerr_on_assert_failure = false; \
+ try { \
+ a; \
+ VERIFY(Eigen::should_raise_an_assert && # a); \
+ } \
+ catch (Eigen::eigen_static_assert_exception&) { VERIFY(true); } \
+ Eigen::report_on_cerr_on_assert_failure = true; \
+ }
+ #endif // TEST_CHECK_STATIC_ASSERTIONS
+
#ifndef VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a) \
std::cout << "Can't VERIFY_RAISES_ASSERT( " #a " ) with exceptions disabled\n";
#endif
+#ifndef VERIFY_RAISES_STATIC_ASSERT
+ #define VERIFY_RAISES_STATIC_ASSERT(a) \
+ std::cout << "Can't VERIFY_RAISES_STATIC_ASSERT( " #a " ) with exceptions disabled\n";
+#endif
#if !defined(__CUDACC__)
#define EIGEN_USE_CUSTOM_ASSERT
@@ -264,10 +297,10 @@ namespace Eigen
#else // EIGEN_NO_ASSERTION_CHECKING
#define VERIFY_RAISES_ASSERT(a) {}
+ #define VERIFY_RAISES_STATIC_ASSERT(a) {}
#endif // EIGEN_NO_ASSERTION_CHECKING
-
#define EIGEN_INTERNAL_DEBUGGING
#include <Eigen/QR> // required for createRandomPIMatrixOfRank
diff --git a/test/selfadjoint.cpp b/test/selfadjoint.cpp
index 5ac219352..aaa4888bd 100644
--- a/test/selfadjoint.cpp
+++ b/test/selfadjoint.cpp
@@ -7,7 +7,7 @@
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
-#define EIGEN_NO_STATIC_ASSERT
+#define TEST_CHECK_STATIC_ASSERTIONS
#include "main.h"
// This file tests the basic selfadjointView API,
@@ -47,8 +47,8 @@ template<typename MatrixType> void selfadjoint(const MatrixType& m)
m4 -= m1.template selfadjointView<Lower>();
VERIFY_IS_APPROX(m4, m2-m3);
- VERIFY_RAISES_ASSERT(m2.template selfadjointView<StrictlyUpper>());
- VERIFY_RAISES_ASSERT(m2.template selfadjointView<UnitLower>());
+ VERIFY_RAISES_STATIC_ASSERT(m2.template selfadjointView<StrictlyUpper>());
+ VERIFY_RAISES_STATIC_ASSERT(m2.template selfadjointView<UnitLower>());
}
void bug_159()