Skip to content

Commit

Permalink
Fix inclusion in translation units with exceptions disabled. (pantor#196
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
chinmaygarde authored May 19, 2021
1 parent 4d5a7d1 commit 2491980
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#else
#include <cstdlib>
#define INJA_THROW(exception) std::abort()
#ifndef INJA_NOEXCEPTION
#define INJA_NOEXCEPTION
#endif
#endif

#include "environment.hpp"
Expand Down
4 changes: 4 additions & 0 deletions include/inja/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#else
#include <cstdlib>
#define INJA_THROW(exception) std::abort()
#ifndef INJA_NOEXCEPTION
#define INJA_NOEXCEPTION
#endif
#endif

// #include "environment.hpp"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2491980

Please sign in to comment.