-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Description
Description
When integrating other reporting into catch2, e.g. https://github.com/rollbear/trompeloeil/blob/main/include/catch2/trompeloeil.hpp or a https://doc.qt.io/qt-6/qtlogging.html#QtMessageHandler-typedef, it's often the case that you already have source_location about where the message really originates from. But right now there's no way to call FAIL, FAIL_CHECK, WARN, etc and tell them this actual origin. The best you can do is format it into the message somehow, but that still leaves IDE hints and such taking you to the wrong point - where the macro was expanded (in the plumbing code between libraries).
It's possible (albeit hacky) to acheive this by doing something like
char const * file = ...;
int line = ...;
#pragma push_macro("CATCH_INTERNAL_LINEINFO")
#define CATCH_INTERNAL_LINEINFO ::Catch::SourceLineInfo(file, line)
FAIL(...)
#pragma pop_macro("CATCH_INTERNAL_LINEINFO")But redefining the CATCH_INTERNAL_LINEINFO macro is clearly not an intended public API :-).
It would be nice if there was a scoped macro like CATCH_LINEINFO(file, line, column, function) which operated a bit like INFO - while it was in scope, any assertions that report would be attributed to this provided location instead of the site of the actual actual macro expansion. I've chosen those 4 args to align with std::source_location, but you might not always have all 4 - having only file and line is certainly common.
This way error callbacks from other libraries could be routed into FAIL, INFO, WARN, etc and attributed the source of the assertion, rather than the plumbing code between e.g. trompeloeil and catch2.
Additional context
See rollbear/trompeloeil#350 - basically just being mildly annoyed that otherwise-excellent IDE experience (error list, detailed failure inlays) that catch2 and C++ TestMate deliver doesn't work as well when on mesages that originate from trompeloil mocks, qWarning, or other such sources.