Skip to content

Add MBED_FALLTHROUGH attribute #12032

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

Merged
merged 1 commit into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions TESTS/mbedmicro-mbed/attributes/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ int testNoReturn()
}


int testFallthrough1(int i)
{
switch (i) {
case 1:
case 2:
i *= 2;
MBED_FALLTHROUGH;
default:
return i;
}
}

int testFallthrough()
{
return testFallthrough1(0);
}


int testUnreachable1(int i)
{
switch (i) {
Expand Down
2 changes: 2 additions & 0 deletions TESTS/mbedmicro-mbed/attributes/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern "C" {
int testPure();
int testForceInline();
int testNoReturn();
int testFallthrough();
int testUnreachable();
int testDeprecated();
}
Expand All @@ -59,6 +60,7 @@ Case cases[] = {
Case("Testing PURE attribute", test_wrapper<testPure>),
Case("Testing FORCEINLINE attribute", test_wrapper<testForceInline>),
Case("Testing NORETURN attribute", test_wrapper<testNoReturn>),
Case("Testing FALLTHROUGH attribute", test_wrapper<testFallthrough>),
Case("Testing UNREACHABLE attribute", test_wrapper<testUnreachable>),
Case("Testing DEPRECATED attribute", test_wrapper<testDeprecated>),
};
Expand Down
38 changes: 38 additions & 0 deletions platform/mbed_toolchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,44 @@
#endif
#endif

/** MBED_FALLTHROUGH
* Marks a point in a switch statement where fallthrough can occur.
* Should be placed as the last statement before a label.
*
* @code
* #include "mbed_toolchain.h"
*
* int foo(int arg) {
* switch (arg) {
* case 1:
* case 2:
* case 3:
* arg *= 2;
* MBED_FALLTHROUGH;
* default:
* return arg;
* }
* }
* @endcode
*/
#ifndef MBED_FALLTHROUGH
#if __cplusplus >= 201703
#define MBED_FALLTHROUGH [[fallthrough]]
#elif defined(__clang__)
#if __cplusplus >= 201103
#define MBED_FALLTHROUGH [[clang::fallthrough]]
#elif __has_attribute(fallthrough)
#define MBED_FALLTHROUGH __attribute__((fallthrough))
#else
#define MBED_FALLTHROUGH
#endif
#elif defined (__GNUC__) && !defined(__CC_ARM)
#define MBED_FALLTHROUGH __attribute__((fallthrough))
#else
#define MBED_FALLTHROUGH
#endif
#endif

/** MBED_DEPRECATED("message string")
* Mark a function declaration as deprecated, if it used then a warning will be
* issued by the compiler possibly including the provided message. Note that not
Expand Down