Fix inclusion in translation units with exceptions disabled. (#196)

If exceptions are disabled via `-fno-exceptions` or `INJA_NOEXCEPTION`, the use
of try-catch is disallowed by the compiler.

This patch makes does two things:
* Gates the use of try-catch in one translation unit on the definition of
  `INJA_NOEXCEPTION`.
* Make it such that translation units compiled with `-fno-exceptions` but no
  `INJA_NOEXCEPTION` implicitly sets `INJA_NOEXCEPTION`.

In the specific case of `ifstream::open`, setting the exceptions bits without
exceptions enabled should trip an assertion just like INJA_ABORT. The nice
message will not be present however, but that is absent when using INJA_ABORT as
well.

After this patch, inja can be successfully included without issue.
This commit is contained in:
Chinmay Garde
2021-05-19 10:53:55 -07:00
committed by GitHub
parent 4d5a7d1c33
commit 2491980b23
3 changed files with 14 additions and 0 deletions

View File

@@ -10,6 +10,9 @@
#else
#include <cstdlib>
#define INJA_THROW(exception) std::abort()
#ifndef INJA_NOEXCEPTION
#define INJA_NOEXCEPTION
#endif
#endif
#include "environment.hpp"

View File

@@ -15,11 +15,15 @@ namespace inja {
inline void open_file_or_throw(const std::string &path, std::ifstream &file) {
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
#ifndef INJA_NOEXCEPTION
try {
file.open(path);
} catch (const std::ios_base::failure & /*e*/) {
INJA_THROW(FileError("failed accessing file at '" + path + "'"));
}
#else
file.open(path);
#endif
}
namespace string_view {

View File

@@ -10,6 +10,9 @@
#else
#include <cstdlib>
#define INJA_THROW(exception) std::abort()
#ifndef INJA_NOEXCEPTION
#define INJA_NOEXCEPTION
#endif
#endif
// #include "environment.hpp"
@@ -1842,11 +1845,15 @@ namespace inja {
inline void open_file_or_throw(const std::string &path, std::ifstream &file) {
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
#ifndef INJA_NOEXCEPTION
try {
file.open(path);
} catch (const std::ios_base::failure & /*e*/) {
INJA_THROW(FileError("failed accessing file at '" + path + "'"));
}
#else
file.open(path);
#endif
}
namespace string_view {