Commit Graph

897 Commits

Author SHA1 Message Date
Martin Hořeňovský
10aef62f21 Regularize scoped message lifetime to only consider the object's scope
This means that:
1) Scoped messages are always removed at the end of their scope,
   even if the scope ended due to an exception.
2) Scoped messages outlive section end, if that section's scope is
   enclosed in their own.

Previously neither of these were true, which has led to a number
of surprising behaviour, where e.g. this:
```cpp
TEST_CASE() {
    try {
        INFO( "some info" );
        throw std::runtime_error( "ex" );
    } catch (std::exception const&) {}

    REQUIRE( false );
}
```
would print "some info" as the message for the assertion, while this:
```cpp
TEST_CASE() {
    INFO("Hello");
    SECTION("dummy") {}
    REQUIRE(false);
}
```
would not print out "Hello" as the message for the assertion.

This had an underlying reason, in that it was trying to helpfully
keep the messages around in case of unexpected exceptions, so that
code like this:
```cpp
TEST_CASE() {
    auto [input, expected] = GENERATE(...);
    CAPTURE(input);
    auto result = transform(input); // throws
    REQUIRE(result == expected);
}
```
would report the value of `input` when `transform` throws. However,
it was surprising in practice and was causing various issues around
handling of messages in other cases.

Closes #1759
Closes #2019
Closes #2959
2025-07-21 21:50:47 +02:00
Martin Hořeňovský
98b4bbb35e Deprecate comparison operators on MessageInfo 2025-07-21 21:50:45 +02:00
Martin Hořeňovský
8c3ffe05e1 Skeleton for applying deprecation warnings 2025-07-21 21:50:43 +02:00
Martin Hořeňovský
715558fd97 Use the fast path for passing assertions in RunContext::handleNonExpr
`handleNonExpr` is responsible for handling assertions that do not
result in a decomposable expression, e.g. `REQUIRE_THROWS`, or
`REQUIRE_NOTHROW`.

Running benchmark on these two macros specifically, with
`REQUIRE_THROWS([](){ throw 1; }())`, and `REQUIRE_NOTHROW([](){}())`,
we get these speedups:

|         |  Debug | Release |
|---------|--------|---------|
| THROWS  |  3.69x |   2.10x |
| NOTHROW |  1.18x |   1.05x |

Obviously the actual performance improvement is dependent on
how expensive the expression under test is.
2025-07-20 21:01:10 +02:00
Martin Hořeňovský
55b14e1b34 Update last seen line info in the assertionEnded fast path
This costs us about 1% perf in Debug build and 3% in Release build,
but it is worth it for more precise information during unexpected
exceptions or fatal errors.

Given a simple test case like this
```cpp
TEST_CASE("Hard fail") {
    REQUIRE( 1 == 1 );
    REQUIRE( 2 == 2 );
    throw 1;
    REQUIRE( 3 == 3 );
}
```

Catch2 before this change would report the line info from the
`TEST_CASE` macro as the last seen expression before error. With
this change, it will correctly report the line info from the
`REQUIRE(2 == 2)` assertion as the last seen expression before error.
2025-07-20 18:34:21 +02:00
Martin Hořeňovský
ce128f584c Remove assertionPassed fast path from the capture interface
As it is an internal helper for RunContext to speed-up handling of
succesfull assertions, I have no idea why it was added to the
interface back when it was first implemented.
2025-07-20 18:32:25 +02:00
Martin Hořeňovský
c22096846c Let reporters opt into fast path in RunContext::assertionStarting
The fast path allows `RunContext` to skip disabling output redirect,
and notifying the reporters, turning `RunContext::notifyAssertionStarted`
into a no-op. This improves the overall performance of assertion handling
significantly, and also prepares ground for future changes around
assertion handling and thread safety.

For simple 10M assertion run, this improves the running time by ~30%
in Debug build and ~40% in Release build.

For backwards-compatibility reasons, the fast path is disabled
by default. However, none of the first party reporters use the
`assertionStarting` event, so all first party reporters are opted-in.
2025-07-17 13:47:44 +02:00
Martin Hořeňovský
ae33e5b99a Remove empty implementations of assertionStarting from reporters 2025-07-17 12:47:46 +02:00
Martin Hořeňovský
0e8112a762 Only track the last line info in RunContext
There were only two places where we used the full `AssertionInfo`
instance in `m_lastAssertionInfo`:
1) when reporting unexpected exception from running a test case
2) when reporting fatal error

because in those two places we do not have access to a real
instance of `AssertionInfo`, but we still need to send one to the
reporters. As a bonus, in both of these places we were already
constructing a fake-ish assertion info, by using the last encountered
source location, but dummying out the other information.

Instead, we only keep track of the last encountered source location,
and construct the dummy `AssertionInfo` on-demand.

This finishes the set of refactoring around `m_lastAssertionInfo`
in `RunContext` and improves the performance of running assertions
by ~5% in both Debug and Release mode.

--------------

Note that this change also causes small difference in output. It
could be avoided by having an invalidation flag and tracking where
the information would be invalidated before, but the difference
includes more precise line location for unexpected errors (both
exceptions and fatals), so I prefer the new output.
2025-07-17 11:27:03 +02:00
Martin Hořeňovský
9be81c0e05 Move resetAssertionInfo into RunContext::reportExpr
Because `RunContext::populateReaction` no longer implicitly depends
on the value of `RunContext::m_lastAssertionInfo`, we don't have to
delay the reset until `RunContext::reportExpr` is finished.
2025-07-16 21:58:42 +02:00
Martin Hořeňovský
efcb76874e Store only lineinfo if m_lastAssertionInfo is going to be reset
`RunContext::resetAssertionInfo` overwrites everything in
`m_lastAssertionInfo` except for the lineInfo, so if the reset
is called at the end of a copying function, we can save work
by copying only the lineInfo.
2025-07-16 21:52:05 +02:00
Martin Hořeňovský
895b8af6bd Avoid needless copy in handling fatal errors 2025-07-16 21:50:20 +02:00
Martin Hořeňovský
066f00acf5 Pass result disposition into RunContext::populateReaction directly
This avoids implicit dataflow through RunContext::m_lastAssertionInfo,
which will be useful in later refactoring.
2025-07-16 21:43:00 +02:00
Martin Hořeňovský
1b72e45354 Cache config::abortAfter and config::shouldDebugBreak in RunContext 2025-07-16 21:29:53 +02:00
Martin Hořeňovský
f7968e9697 Constify various autoregistry globals to avoid clang-tidy complaints
Closes #2582
2025-07-15 15:23:32 +02:00
Martin Hořeňovský
a483b6d7d3 Default to randomized test case order 2025-07-15 12:10:40 +02:00
Martin Hořeňovský
b9c018b38a Use template variables for our trait-likes in Clara 2025-07-15 11:49:39 +02:00
Martin Hořeňovský
050e14dce0 Disable Wmissing-noreturn for test files
Adding unreachable to `FAIL` and `SKIP` made Clang become very good
at figuring out that tests of `FAIL` and `SKIP` will never return
and thus could be marked as [[noreturn]].

To avoid introducing lot of warning suppression noise into the
test files, we just disable it instead.
2025-07-14 12:15:25 +02:00
Martin Hořeňovský
6097bd6ee9 Mark code past FAIL and SKIP as unreachable
This helps the compiler see that the execution does not continue,
which in turn means that it is not an error if the user does not
return value from the function after FAIL/SKIP.

Closes #2941
2025-07-14 10:39:48 +02:00
Martin Hořeňovský
b62413aee3 Add stream flushes after reporters print out the test run metadata.
This ensures that the test run metadata are printed even if the
process dies right after.

Closes #2964
2025-07-10 23:51:31 +02:00
Martin Hořeňovský
39c32b9662 Avoid needless string temporary in template product test registration 2025-07-09 12:29:36 +02:00
Clement Courbet
2de12cb05f Allow using only types in TEMPLATE_TEST_CASE_SIG. (#2995)
Right now `TEMPLATE_TEST_CASE_SIG` fails to compile when the signature contains only types:

```
TEMPLATE_TEST_CASE_SIG(
  "TemplateTestSig: compiles with two type parameters",
  "[template][onlytypes]",
  ((typename U, typename V), U, V), (int,int)) {}
```

The trick is to resolve the ambiguity between the two overloads of
`get_wrapper` (`TypeList` and `Nttp`) by making one match more strongly.
We also need to allow `reg_test` to register more than one type.

Add unit tests.

Fixes #2680

---------

Co-authored-by: Martin Hořeňovský <martin.horenovsky@gmail.com>
2025-07-08 14:26:25 +02:00
Omar Boukli-Hacene
8dc9f1a124 Move header from internal
Move `catch_test_run_info.hpp` from directory
`src/catch2/internal/` to be directly under `src/catch2/`.

Header `catch_test_run_info.hpp` defines `Catch::TestRunInfo` which is part of the public API, since it is required when overriding `Catch::EventListenerBase`.

Fixes issue caught by Clang Tidy misc-include-cleaner check.
2025-07-07 17:27:48 +02:00
Tyson Jones
bd2d918f32 patched >2-digit tag-count padding in base reporter
Passing the CLI argument '--list-tags' outputs a list like:

```
All available tags:
  11  [mytag]
   4  [myothertag]
2 tags
```

However, the width of the tag-count column was previously hardcoded to 2, such that the formatting broke (with the tag names awkwardly misaligned/displaced) for 3+ digit tag counts. This occurred whenever a tag contains one hundred or more tests, and would result in formatting like:

```
All available tags:
  11  [mytag]
  100  [myjumbotag]
   4  [myothertag]
3 tags
```

This patch pre-computes the maximum number of digits among the tag counts, expanding the padding when there are more than 100 tests in a tag (and handling when tags are empty). It now outputs e.g.

```
All available tags:
   11  [mytag]
  100  [myjumbotag]
    4  [myothertag]
3 tags
```
2025-07-02 14:13:07 +02:00
Jean-Michaël Celerier
d134b0cae3 clang: do not issue bogus warnings about integer manipulation in hash functions with fsanitize=undefined/integer (#2965)
With -fsanitize=integer every over/under-flowing integer manipulation triggers a warning.
This is extremely useful as it allows to find some non-obvious bugs such as

    for(size_t i = 0; i < N - 1; i++) { ... }

But it comes with a lot of false positives, for instance with every hash function
doing shifting on unsigned integer. Random number generators are also often detected
with this sanitizer.

This marks a few of these functions as safe in this case.

Co-authored-by: Martin Hořeňovský <martin.horenovsky@gmail.com>
2025-07-02 13:30:36 +02:00
Eisuke Kawashima
416b075211 fix: make deleted member function public 2025-06-15 13:26:38 -06:00
Eisuke Kawashima
1de7d0ed7b Remove redundant override specifier from functions that are declared final 2025-06-14 18:42:43 -06:00
Chris Thrasher
5abfc0aa9c Add return code constants to API 2025-04-29 09:26:28 -06:00
Martin Hořeňovský
1e7b879fae Small include cleanup 2025-04-29 13:15:34 +02:00
ZXShady
4ff57aba42 Use variable templates 2025-04-27 21:14:04 -06:00
Chris Thrasher
5a6d70eebb Remove unused headers 2025-04-27 11:08:41 -06:00
Chris Thrasher
4c93a595a1 Fix typos 2025-04-26 22:46:42 -06:00
Chris Thrasher
8cfca70ae8 Fix formatting of CMake files
2 spaces seems to be the more common indentation level so that's what
I unified around.
2025-04-26 10:38:36 -06:00
Chris Thrasher
2b60af89e2 v3.8.1 2025-04-08 12:40:18 -06:00
abhishekbelgaonkar23
f51dc98dfc Fix: Clang 19 -Wc++20-extensions warning (#2910) 2025-04-07 15:45:34 -06:00
Martin Hořeňovský
914aeecfe2 v3.8.0 2025-01-06 00:41:45 +01:00
Michal Bukovský
6e9c34aa20 add meson option to not install library 2025-01-05 20:10:45 +01:00
Martin Hořeňovský
b0d0aa43e6 Fix crash when stringifying pre 1970 dates on Windows
`gmtime*` on Windows fails on dates pre 1970, and because we didn't
check the return code, we would then pass invalid `tm` struct to
`strftime` causing it to assert.

Closes #2944
2025-01-05 16:04:38 +01:00
Michal Bukovský
7bbd4b9075 Fix using from_range with std::vector<>::const_iterator 2024-11-09 18:46:07 +01:00
Martin Hořeňovský
119a7bbe53 Cleanup clang-tidy warning about enum sizes 2024-10-29 21:06:54 +01:00
Martin Hořeňovský
e260288807 Allow disabling use of __builtin_constant_p in internal macros
Turns out that even in GCC, the expression in `__builtin_cosntant_p`
can end up evaluated and side-effects executed. To allow users to
work around this bug, I added a configuration option to disable its
use in internal macros.

Related to #2925
2024-10-27 20:27:03 +01:00
Pino Toscano
a6ee7e20cd Use isatty() when using GNU libc
While isatty() is a POSIX interface and theoretically could be used
more broadly than on Linux and macOS, use a conservative approach and
use it on any platform that uses GNU libc.
2024-10-19 20:36:19 +02:00
Sven Fischer
0b2af56271 Explicitly cast values of different types
In case the warning -Werror=conversion is active with GCC, the warnings
about "conversion from A to B may change value" lead to a compilation
error. This explicitly convert the values to address these warnings.
2024-10-14 21:37:35 +02:00
Stefan Haller
69d62abc9a Provide overloads for {Unordered}RangeEquals taking a std::initializer_list
This allows writing something like

  const auto v = calculateSomeVectorOfInts();
  CHECK_THAT(v, RangeEquals({1, 2, 3}));

Fixes #2915.
2024-10-14 21:02:03 +02:00
Stefan Haller
1e0ccb1b21 Use default parameter for comparison instead of overloads in {Unordered}RangeEquals
Saves some code duplication.
2024-10-14 21:02:03 +02:00
Stefan Haller
5ad66ada7b Fix typos in comments 2024-10-14 21:02:03 +02:00
Martin Hořeňovský
fa43b77429 v3.7.1 2024-09-17 10:45:43 +02:00
Martin Hořeňovský
e200443b84 Fix compilation error from missing include in xmlwriter.hpp
Fixes #2907
2024-09-15 22:17:39 +02:00
Martin Hořeňovský
ce22c0fe8a Standardize exit codes for various failures
The main reason for this is to be able to distinguish between
different errors (or "errors") based on the return code. Before
this change, it was impossible to use the exit code to figure out
whether a test binary failed because all tests were skipped or
because exactly 4 assertions have failed.

This meant that using `catch_discover_tests` and telling it to
check for exit code == 4 to determine skipped tests could lead to
false negatives.
2024-09-13 21:33:45 +02:00
Martin Hořeňovský
18df97df00 Sprinkle some constexpr around to make Jason happy
Most of these will not matter in practice due to C++14 imposing
significant limitations on what else we can make constexpr, and we cannot
have references outliving the constexpr context either way.
2024-09-13 16:40:11 +02:00