Allow unit tests to be compiled with -fno-exception #15214
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR makes it possible to compile unit tests using the
-fno-exception
flag.For some reason, using mbed_assert in unit tests would throw an exception. This is strange as when using mbed on real hardware, exceptions are disabled, thus it's not clear how and why unit test code would use exceptions.
The
FAIL()
mechanism provided by Google Tests should be sufficient.But in order to not break user code relying on mbed_assert throwing an exception, we check if exceptions are enabled or not.
The main advantage of running unit tests with
-fno-exception
is for branch coverage.With exceptions enabled:
With
-fno-exception
:One concrete example is this: we have a
PwmOut
wrapper calledCorePWM
. It's simple enough that branch coverage should 100%, but it's not as gcovr tells us that there is a branch on the PwmOut constructor, because it callsinit()
which in turn will at some point call MBED_ASSERT. This creates a lot of noise for the developers writing unit tests because we don't know if the branch can be controlled or not.With exceptions enabled:
With
-fno-exception
:Another possibility to simplify is to not use throw at all and just rely on the FAIL() in the if/else. I can update the PR if needed.
Good resources on the topic: