Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unit tests to be compiled with -fno-exception #15214

Merged

Conversation

ladislas
Copy link
Contributor

@ladislas ladislas commented Jan 28, 2022

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:

image

With -fno-exception:

image

One concrete example is this: we have a PwmOut wrapper called CorePWM. 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 calls init() 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:

Screenshot 2022-01-28 at 10 44 20

With -fno-exception:

Screenshot 2022-01-28 at 10 44 11

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:

@ciarmcom ciarmcom requested a review from a team January 28, 2022 10:00
@ciarmcom
Copy link
Member

@ladislas, thank you for your changes.
@ARMmbed/mbed-os-maintainers please review.

Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just styling, preprocessor macros should start at the beginning

#ifdef
     code....
#else
    code...
#endif

@ladislas ladislas force-pushed the ladislas/bugfix/mbed_assert_no_throw branch from b987236 to 568ffbf Compare January 28, 2022 10:11
@mergify mergify bot dismissed 0xc0170’s stale review January 28, 2022 10:12

Pull request has been modified.

@ladislas
Copy link
Contributor Author

Just styling, preprocessor macros should start at the beginning

done ✅

@ciarmcom
Copy link
Member

@ladislas, thank you for your changes.
@ARMmbed/mbed-os-maintainers please review.

@ciarmcom ciarmcom requested a review from a team January 28, 2022 10:30
@0xc0170
Copy link
Contributor

0xc0170 commented Jan 28, 2022

CI started

@mbed-ci
Copy link

mbed-ci commented Jan 28, 2022

Jenkins CI Test : ✔️ SUCCESS

Build Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_unittests ✔️

@0xc0170 0xc0170 merged commit 9e2c949 into ARMmbed:master Jan 31, 2022
@mergify mergify bot removed the ready for merge label Jan 31, 2022
@mergify
Copy link

mergify bot commented Jan 31, 2022

This PR does not contain release version label after merging.

@mergify mergify bot added the release version missing When PR does not contain release version, bot should label it and we fix it afterwards label Jan 31, 2022
@0xc0170 0xc0170 added release-type: patch Indentifies a PR as containing just a patch and removed release version missing When PR does not contain release version, bot should label it and we fix it afterwards labels Jan 31, 2022
@0xc0170
Copy link
Contributor

0xc0170 commented Jan 31, 2022

This PR does not contain release version label after merging.

Fixed

@ladislas ladislas deleted the ladislas/bugfix/mbed_assert_no_throw branch January 31, 2022 09:31
@mbedmain mbedmain added release-version: 6.16.0 Release-pending and removed release-type: patch Indentifies a PR as containing just a patch Release-pending labels Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants