diff --git a/CMakeLists.txt b/CMakeLists.txt index bc45593eb2451be9818e2b75671d91341647e4e4..a432f4e8d3cadf2899f90b13ff687018e4fecd0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ -cmake_minimum_required (VERSION 2.8) +cmake_minimum_required(VERSION 2.8) # project info -project (cell_algorithms) +project(cell_algorithms) enable_language(CXX) # save incoming CXX flags for forwarding to modparser external project @@ -19,6 +19,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS "YES") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +# enable assertions? +set(WITH_ASSERTIONS OFF CACHE BOOL "enable EXPECTS() assertions in code") +if(WITH_ASSERTIONS) + add_definitions("-DWITH_ASSERTIONS") +endif() + # TBB support set(WITH_TBB OFF CACHE BOOL "use TBB for on-node threading" ) if(WITH_TBB) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e7bced8530908d637ecec5112fdb5e581e652e9a..7f4a588d933a5ea74342c70dc8b6ac5df95aca8b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ set(BASE_SOURCES parameter_list.cpp profiling/profiler.cpp swcio.cpp + util/debug.cpp ) if(${WITH_MPI}) diff --git a/src/algorithms.hpp b/src/algorithms.hpp index 738f163fa8381363b099c981d57df36923bc921d..a1f6bad69f49fd9c40bc8cdea1cf2a400f8d7285 100644 --- a/src/algorithms.hpp +++ b/src/algorithms.hpp @@ -7,6 +7,7 @@ #include <vector> #include "util.hpp" +#include "util/debug.hpp" /* * Some simple wrappers around stl algorithms to improve readability of code diff --git a/src/cell.cpp b/src/cell.cpp index fb9147ed5e52552da3dc55b3198b5b50d50b7f19..1a70ca3e3034cf25e9238352cb1c5dfdd97a8c49 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -1,5 +1,6 @@ #include "cell.hpp" #include "tree.hpp" +#include "util/debug.hpp" namespace nest { namespace mc { diff --git a/src/cell.hpp b/src/cell.hpp index 1c047e18b2b90cba58a805f66f1579723dd0ccd5..744c9970e26af7f439f9cee7cfb2f88c96014624 100644 --- a/src/cell.hpp +++ b/src/cell.hpp @@ -8,6 +8,7 @@ #include "segment.hpp" #include "cell_tree.hpp" #include "stimulus.hpp" +#include "util/debug.hpp" namespace nest { namespace mc { diff --git a/src/matrix.hpp b/src/matrix.hpp index 9e2c9e7d10d68c2f973cffaaf005ceb939fdcf41..d0ddb816adc5cb9bfb6c3c88562e0a41835920ec 100644 --- a/src/matrix.hpp +++ b/src/matrix.hpp @@ -4,6 +4,7 @@ #include <vector/include/Vector.hpp> #include "util.hpp" +#include "util/debug.hpp" namespace nest { namespace mc { diff --git a/src/profiling/profiler.cpp b/src/profiling/profiler.cpp index c07351e296ba8873ed7064ca4b605d2e9bb0cb8d..247c8c949958e46c2285d0c9c79341c4699a32bb 100644 --- a/src/profiling/profiler.cpp +++ b/src/profiling/profiler.cpp @@ -1,4 +1,5 @@ #include "profiler.hpp" +#include "util/debug.hpp" #include <communication/global_policy.hpp> diff --git a/src/swcio.cpp b/src/swcio.cpp index 203f69eb1bdca8905145edb4edbe86d564682e9b..f1a272df2fb583331e3695964a0416d15868e327 100644 --- a/src/swcio.cpp +++ b/src/swcio.cpp @@ -9,6 +9,7 @@ #include "point.hpp" #include "swcio.hpp" #include "util.hpp" +#include "util/debug.hpp" namespace nest { namespace mc { diff --git a/src/util.hpp b/src/util.hpp index c4362b25d0ad9e54e962b118e84ce974b422a636..29a6b28a9ec09cce13fcced0db20ae30aa3d803f 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -2,12 +2,6 @@ #include <vector/include/Vector.hpp> -#ifdef DEBUG -#define EXPECTS(expression) assert(expression) -#else -#define EXPECTS(expression) -#endif - /* using memory::util::red; using memory::util::yellow; diff --git a/src/util/debug.cpp b/src/util/debug.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dd83f562d3ea15477d7507026a370a87428a36b --- /dev/null +++ b/src/util/debug.cpp @@ -0,0 +1,16 @@ +#include <cstdlib> +#include <iostream> + +#include "util/debug.hpp" + +bool nest::mc::util::failed_assertion(const char *assertion, const char *file, + int line, const char *func) +{ + // Explicit flush, as we can't assume default buffering semantics on stderr/cerr, + // and abort() might not flush streams. + + std::cerr << file << ':' << line << " " << func + << ": Assertion `" << assertion << "' failed." << std::endl; + std::abort(); + return false; +} diff --git a/src/util/debug.hpp b/src/util/debug.hpp new file mode 100644 index 0000000000000000000000000000000000000000..148b3c44db9b03cd396c10db044e6924b99d7104 --- /dev/null +++ b/src/util/debug.hpp @@ -0,0 +1,30 @@ +#pragma once + +namespace nest { +namespace mc { +namespace util { + +bool failed_assertion(const char *assertion, const char *file, int line, const char *func); + +} +} +} + + +#ifdef WITH_ASSERTIONS + +#ifdef __GNUC__ +#define DEBUG_FUNCTION_NAME __PRETTY_FUNCTION__ +#else +#define DEBUG_FUNCTION_NAME __func__ +#endif + +#define EXPECTS(condition) \ +(void)((condition) || \ + nest::mc::util::failed_assertion(#condition, __FILE__, __LINE__, DEBUG_FUNCTION_NAME)) + +#else + +#define EXPECTS(condition) + +#endif // def WITH_ASSERTIONS diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index ec25bd42c0941b8324fbea3288ed5b4f1d1f7a59..d1ffdf11811f44266df10215026a11d907172459 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -1,9 +1,11 @@ #include <vector> #include "gtest.h" + +#include "algorithms.hpp" #include "util.hpp" +#include "util/debug.hpp" -#include "../src/algorithms.hpp" TEST(algorithms, sum) {