Skip to content

Commit

Permalink
Improve C++ exception support:
Browse files Browse the repository at this point in the history
 * Allow to define TEST_NO_EXCEPTIONS to disable acutest's support for
   C++ exceptions.
 * Try to detect when C++ are disabled or not availaable (e.g. via gcc's
   command line option -fno-exceptions) and define TEST_NO_EXCEPTIONS
   automatically.

Closes #72.
  • Loading branch information
mity committed Dec 27, 2023
1 parent 78a803a commit a63ebb4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions include/acutest.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
#define ACUTEST_H


/* Try to auto-detect whether we need to disable C++ exception handling.
* If the detection fails, you may always define TEST_NO_EXCEPTIONS before
* including "acutest.h" manually. */
#ifdef __cplusplus
#if (__cplusplus >= 199711L && !defined __cpp_exceptions) || \
((defined(__GNUC__) || defined(__clang__)) && !defined __EXCEPTIONS)
#ifndef TEST_NO_EXCEPTIONS
#define TEST_NO_EXCEPTIONS
#endif
#endif
#endif


/************************
*** Public interface ***
************************/
Expand Down Expand Up @@ -113,6 +126,7 @@


#ifdef __cplusplus
#ifndef TEST_NO_EXCEPTIONS
/* Macros to verify that the code (the 1st argument) throws exception of given
* type (the 2nd argument). (Note these macros are only available in C++.)
*
Expand Down Expand Up @@ -159,6 +173,7 @@
if(msg_ != NULL) \
acutest_message_("%s", msg_); \
} while(0)
#endif /* #ifndef TEST_NO_EXCEPTIONS */
#endif /* #ifdef __cplusplus */


Expand Down Expand Up @@ -338,8 +353,10 @@ void acutest_abort_(void) ACUTEST_ATTRIBUTE_(noreturn);
#endif

#ifdef __cplusplus
#ifndef TEST_NO_EXCEPTIONS
#include <exception>
#endif
#endif

#ifdef __has_include
#if __has_include(<valgrind.h>)
Expand Down Expand Up @@ -1011,7 +1028,9 @@ acutest_do_run_(const struct acutest_test_* test, int index)
acutest_cond_failed_ = 0;

#ifdef __cplusplus
#ifndef TEST_NO_EXCEPTIONS
try {
#endif
#endif
acutest_init_(test->name);
acutest_begin_test_line_(test);
Expand Down Expand Up @@ -1065,6 +1084,7 @@ acutest_do_run_(const struct acutest_test_* test, int index)
status = (acutest_test_failures_ == 0) ? 0 : -1;

#ifdef __cplusplus
#ifndef TEST_NO_EXCEPTIONS
} catch(std::exception& e) {
const char* what = e.what();
acutest_check_(0, NULL, 0, "Threw std::exception");
Expand All @@ -1085,6 +1105,7 @@ acutest_do_run_(const struct acutest_test_* test, int index)
printf("C++ exception.\n\n");
}
}
#endif
#endif

acutest_fini_(test->name);
Expand Down

0 comments on commit a63ebb4

Please sign in to comment.