Skip to content

Commit 21d0bb7

Browse files
aleks-fmatejk
andauthored
feat(CppUnit): terminate handler #5354 (#5355)
* feat(CppUnit): terminate handler #5354 * fix(CppUnit): suppress Foundation auto-link on all WIN32 CppUnit no longer links Foundation, but Foundation.h's MSVC auto-link pragma fires from CppUnit's own TUs (via TestRunner.h -> Poco/Exception.h), causing LNK1104 for PocoFoundation.lib. Broaden POCO_NO_AUTOMATIC_LIBS from Clang-only to all WIN32. Also replace an em-dash in a comment with --. --------- Co-authored-by: Matej Kenda <matejken@gmail.com>
1 parent bdf9fba commit 21d0bb7

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

CppUnit/CMakeLists.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@ set_target_properties(CppUnit
1414
OUTPUT_NAME CppUnit
1515
DEFINE_SYMBOL CppUnit_EXPORTS
1616
)
17-
target_link_libraries(CppUnit PUBLIC Poco::Foundation)
1817
target_include_directories(CppUnit
1918
PUBLIC
2019
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
2120
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
21+
# Pull Foundation's include dirs in *without* linking Foundation:
22+
# TestRunner.h needs Poco/Exception.h visible (consumed by the
23+
# CppUnitMain / CppUnitPocoExceptionText macros), and CppUnit's own
24+
# sources include TestRunner.h transitively. The macro expansions
25+
# live at consumer call sites, so libCppUnit.so itself never
26+
# references Poco::Exception symbols and doesn't need to link
27+
# Foundation; each consumer brings its own Poco link line.
28+
$<TARGET_PROPERTY:Poco::Foundation,INTERFACE_INCLUDE_DIRECTORIES>
2229
PRIVATE
2330
${CMAKE_CURRENT_SOURCE_DIR}/src
2431
)
2532
if(WIN32)
26-
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
27-
target_compile_definitions(CppUnit PUBLIC POCO_NO_AUTOMATIC_LIBS)
28-
endif()
33+
target_compile_definitions(CppUnit PUBLIC POCO_NO_AUTOMATIC_LIBS)
2934
endif()
3035

3136
if(NOT BUILD_SHARED_LIBS)

CppUnit/include/CppUnit/TestRunner.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <vector>
1313
#include <string>
1414
#include <ostream>
15+
#include <cstdlib>
16+
#include <exception>
17+
#include <iostream>
1518
#if defined(POCO_VXWORKS)
1619
#include <cstdarg>
1720
#endif
@@ -96,9 +99,29 @@ class CppUnit_API TestRunner
9699
return text; \
97100
}
98101

102+
// Installs a std::set_terminate handler that prints the in-flight
103+
// exception's displayText() (for Poco::Exception) or what() (for any
104+
// other std::exception) before std::abort() -- so an unhandled throw
105+
// from a destructor, fixture-static dtor, or worker thread surfaces
106+
// its message instead of just the default
107+
// "terminate called after throwing an instance of '...'" line.
108+
//
109+
// Expands at the consumer's main() (via CppUnitMain), so the
110+
// reference to Poco::Exception lives in the consumer's TU, not in
111+
// CppUnit's own translation units.
112+
#define CppUnitInstallTerminateHandler() \
113+
std::set_terminate([] { \
114+
try { if (auto e = std::current_exception()) std::rethrow_exception(e); } \
115+
catch (const Poco::Exception& ex) { std::cerr << "terminate: " << ex.displayText() << std::endl; } \
116+
catch (const std::exception& ex) { std::cerr << "terminate: " << ex.what() << std::endl; } \
117+
catch (...) { std::cerr << "terminate: unknown exception" << std::endl; } \
118+
std::abort(); \
119+
})
120+
99121
#define CppUnitMain(testCase) \
100122
int main(int ac, char **av) \
101123
{ \
124+
CppUnitInstallTerminateHandler(); \
102125
std::vector<std::string> args; \
103126
for (int i = 0; i < ac; ++i) \
104127
args.push_back(std::string(av[i])); \

0 commit comments

Comments
 (0)