C++ bindings: throw exceptions of correct exception subclass
authorSven Verdoolaege <sven.verdoolaege@gmail.com>
Fri, 3 Aug 2018 13:37:49 +0000 (3 15:37 +0200)
committerSven Verdoolaege <sven.verdoolaege@gmail.com>
Fri, 3 Aug 2018 13:37:49 +0000 (3 15:37 +0200)
The purpose of the subclasses is for the user to be able
to catch different classes of isl exceptions separately,
but this ability somehow got lost while preparing the commit
for upstreaming.  Make it work (again) and make sure it keeps working
by adjusting the test to catch specific subclasses.

Reported-by: Philip Pfaffe <philip.pfaffe@gmail.com>
Signed-off-by: Sven Verdoolaege <sven.verdoolaege@gmail.com>
cpp/cpp.h.pre
isl_test_cpp.cc

index 0e72c2b..bf1fdf8 100644 (file)
@@ -67,9 +67,8 @@ public:
        exception(const char *what_arg) {
                what_str = std::make_shared<std::string>(what_arg);
        }
-       static inline exception create(enum isl_error error, const char *msg,
+       static inline void throw_error(enum isl_error error, const char *msg,
                const char *file, int line);
-       static inline exception create_from_last_error(ctx ctx);
        virtual const char *what() const noexcept {
                return what_str->c_str();
        }
@@ -84,14 +83,9 @@ public:
        /* Wrapper for throwing an exception on NULL input.
         */
        static void throw_NULL_input(const char *file, int line) {
-               throw create(isl_error_invalid, "NULL input", file, line);
-       }
-       /* Wrapper for throwing an exception corresponding to the last
-        * error on "ctx".
-        */
-       static void throw_last_error(ctx ctx) {
-               throw create_from_last_error(ctx);
+               throw_error(isl_error_invalid, "NULL input", file, line);
        }
+       static inline void throw_last_error(ctx ctx);
 };
 
 /* Create an exception of a type described by "what_arg", with
@@ -153,37 +147,37 @@ class exception_unsupported : public exception {
                exception("unsupported operation", msg, file, line) {}
 };
 
-/* Create an exception of the class that corresponds to "error", with
+/* Throw an exception of the class that corresponds to "error", with
  * error message "msg" in line "line" of file "file".
  *
  * isl_error_none is treated as an invalid error type.
  */
-exception exception::create(enum isl_error error, const char *msg,
+void exception::throw_error(enum isl_error error, const char *msg,
        const char *file, int line)
 {
        switch (error) {
        case isl_error_none:
                break;
-       case isl_error_abort: return exception_abort(msg, file, line);
-       case isl_error_alloc: return exception_alloc(msg, file, line);
-       case isl_error_unknown: return exception_unknown(msg, file, line);
-       case isl_error_internal: return exception_internal(msg, file, line);
-       case isl_error_invalid: return exception_invalid(msg, file, line);
-       case isl_error_quota: return exception_quota(msg, file, line);
+       case isl_error_abort: throw exception_abort(msg, file, line);
+       case isl_error_alloc: throw exception_alloc(msg, file, line);
+       case isl_error_unknown: throw exception_unknown(msg, file, line);
+       case isl_error_internal: throw exception_internal(msg, file, line);
+       case isl_error_invalid: throw exception_invalid(msg, file, line);
+       case isl_error_quota: throw exception_quota(msg, file, line);
        case isl_error_unsupported:
-                               return exception_unsupported(msg, file, line);
+                               throw exception_unsupported(msg, file, line);
        }
 
        throw exception_invalid("invalid error type", file, line);
 }
 
-/* Create an exception from the last error that occurred on "ctx" and
+/* Throw an exception corresponding to the last error on "ctx" and
  * reset the error.
  *
  * If "ctx" is NULL or if it is not in an error state at the start,
  * then an invalid argument exception is thrown.
  */
-exception exception::create_from_last_error(ctx ctx)
+void exception::throw_last_error(ctx ctx)
 {
        enum isl_error error;
        const char *msg, *file;
@@ -195,7 +189,7 @@ exception exception::create_from_last_error(ctx ctx)
        line = isl_ctx_last_error_line(ctx.get());
        isl_ctx_reset_error(ctx.get());
 
-       return create(error, msg, file, line);
+       throw_error(error, msg, file, line);
 }
 
 #else
@@ -217,7 +211,7 @@ public:
                fprintf(stderr, "%s:%d: NULL input\n", file, line);
                abort();
        }
-       /* Wrapper for throwing an exception corresponding to the last
+       /* Throw an exception corresponding to the last
         * error on "ctx".
         * isl should already abort when an error condition occurs,
         * so this function should never be called.
index e5580b4..929d69c 100644 (file)
@@ -52,7 +52,7 @@ static void test_return_bool(isl::ctx ctx)
        try {
                null.is_empty();
                die("no exception raised");
-       } catch (const isl::exception &e) {
+       } catch (const isl::exception_invalid &e) {
                caught = true;
        }
 
@@ -131,6 +131,8 @@ static void test_exception(isl::ctx ctx)
 
        try {
                auto umap = isl::union_map::from(mupa);
+       } catch (const isl::exception_unsupported &error) {
+               die("caught wrong exception");
        } catch (const isl::exception &error) {
                assert(strstr(error.what(), "without explicit domain"));
                copy = error;