diff --git a/src/assert.cc b/src/assert.cc new file mode 100644 index 00000000..c0b782a1 --- /dev/null +++ b/src/assert.cc @@ -0,0 +1,16 @@ +#include "assert.hh" + +namespace Kakoune +{ + +assert_failed::assert_failed(const std::string& message) +{ + m_message = message; +} + +std::string assert_failed::description() const +{ + return m_message; +} + +} diff --git a/src/assert.hh b/src/assert.hh new file mode 100644 index 00000000..c1bfd66b --- /dev/null +++ b/src/assert.hh @@ -0,0 +1,31 @@ +#ifndef assert_hh_INCLUDED +#define assert_hh_INCLUDED + +#include "exception.hh" + +namespace Kakoune +{ + +struct assert_failed : logic_error +{ + assert_failed(const std::string& message); + std::string description() const; + +private: + std::string m_message; +}; + +} + +#define STRINGIFY(X) #X +#define TOSTRING(X) STRINGIFY(X) + +#ifdef assert +#undef assert +#endif + +#define assert(condition) \ + if (not (condition)) \ + throw assert_failed("assert failed \"" #condition "\" at " __FILE__ ":" TOSTRING(__LINE__)) + +#endif // assert_hh_INCLUDED diff --git a/src/buffer.cc b/src/buffer.cc index 1f9503b0..1997cf81 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -2,8 +2,8 @@ #include "buffer_manager.hh" #include "window.hh" +#include "assert.hh" -#include #include namespace Kakoune diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index db355ae5..eb75c3a7 100644 --- a/src/buffer_manager.cc +++ b/src/buffer_manager.cc @@ -1,7 +1,6 @@ #include "buffer_manager.hh" -#include - +#include "assert.hh" #include "buffer.hh" #include "window.hh" #include "exception.hh" diff --git a/src/file.cc b/src/file.cc index a7907897..8636422c 100644 --- a/src/file.cc +++ b/src/file.cc @@ -2,12 +2,13 @@ #include "buffer.hh" #include "buffer_manager.hh" +#include "assert.hh" #include #include #include #include -#include + namespace Kakoune { diff --git a/src/main.cc b/src/main.cc index 1ec5de6f..d53a059e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,13 +1,13 @@ -#include #include "window.hh" #include "buffer.hh" #include "file.hh" #include "regex_selector.hh" #include "command_manager.hh" #include "buffer_manager.hh" +#include "assert.hh" #include -#include +#include using namespace Kakoune; @@ -331,6 +331,13 @@ int main() } deinit_ncurses(); } + catch (Kakoune::exception& error) + { + deinit_ncurses(); + puts("uncaught exception:\n"); + puts(error.description().c_str()); + return -1; + } catch (...) { deinit_ncurses(); diff --git a/src/utils.hh b/src/utils.hh index 50297f8d..d495afeb 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -1,6 +1,8 @@ #ifndef utils_hh_INCLUDED #define utils_hh_INCLUDED +#include "exception.hh" + #include namespace Kakoune @@ -37,7 +39,6 @@ bool operator== (const std::unique_ptr& lhs, T* rhs) return lhs.get() == rhs; } - } #endif // utils_hh_INCLUDED