catchorg / catchorg/Catch2

Add a means to override CATCH_INTERNAL_LINEINFO and specify a different source location

Open
#2,985 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

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

```c++
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 https://github.com/rollbear/trompeloeil/pull/350 - basically just being mildly annoyed that otherwise-excellent IDE experience (error list, detailed failure inlays) that catch2 and [C++ TestMate](https://marketplace.visualstudio.com/items?itemName=matepek.vscode-catch2-test-adapter) deliver doesn't work as well when on mesages that originate from trompeloil mocks, qWarning, or other such sources.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.