Merge pull request #12032 from kjbracey-arm/fallthrough

Add MBED_FALLTHROUGH attribute
pull/12132/head
Anna Bridge 2019-12-17 16:20:35 +00:00 committed by GitHub
commit 6b4262f7a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 0 deletions

View File

@ -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) int testUnreachable1(int i)
{ {
switch (i) { switch (i) {

View File

@ -33,6 +33,7 @@ extern "C" {
int testPure(); int testPure();
int testForceInline(); int testForceInline();
int testNoReturn(); int testNoReturn();
int testFallthrough();
int testUnreachable(); int testUnreachable();
int testDeprecated(); int testDeprecated();
} }
@ -59,6 +60,7 @@ Case cases[] = {
Case("Testing PURE attribute", test_wrapper<testPure>), Case("Testing PURE attribute", test_wrapper<testPure>),
Case("Testing FORCEINLINE attribute", test_wrapper<testForceInline>), Case("Testing FORCEINLINE attribute", test_wrapper<testForceInline>),
Case("Testing NORETURN attribute", test_wrapper<testNoReturn>), Case("Testing NORETURN attribute", test_wrapper<testNoReturn>),
Case("Testing FALLTHROUGH attribute", test_wrapper<testFallthrough>),
Case("Testing UNREACHABLE attribute", test_wrapper<testUnreachable>), Case("Testing UNREACHABLE attribute", test_wrapper<testUnreachable>),
Case("Testing DEPRECATED attribute", test_wrapper<testDeprecated>), Case("Testing DEPRECATED attribute", test_wrapper<testDeprecated>),
}; };

View File

@ -340,6 +340,44 @@
#endif #endif
#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") /** MBED_DEPRECATED("message string")
* Mark a function declaration as deprecated, if it used then a warning will be * 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 * issued by the compiler possibly including the provided message. Note that not