Skip to content

Commit d97460f

Browse files
committed
Add MBED_FALLTHROUGH attribute
C++17 standardised `[[fallthrough]]` for `switch` statements to suppress compiler warnings. Provide access to it, or compiler-specific alternatives.
1 parent 888dfff commit d97460f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

TESTS/mbedmicro-mbed/attributes/attributes.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ int testNoReturn()
145145
}
146146

147147

148+
int testFallthrough1(int i)
149+
{
150+
switch (i) {
151+
case 1:
152+
case 2:
153+
i *= 2;
154+
MBED_FALLTHROUGH;
155+
default:
156+
return i;
157+
}
158+
}
159+
160+
int testFallthrough()
161+
{
162+
return testFallthrough1(0);
163+
}
164+
165+
148166
int testUnreachable1(int i)
149167
{
150168
switch (i) {

TESTS/mbedmicro-mbed/attributes/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern "C" {
3333
int testPure();
3434
int testForceInline();
3535
int testNoReturn();
36+
int testFallthrough();
3637
int testUnreachable();
3738
int testDeprecated();
3839
}
@@ -59,6 +60,7 @@ Case cases[] = {
5960
Case("Testing PURE attribute", test_wrapper<testPure>),
6061
Case("Testing FORCEINLINE attribute", test_wrapper<testForceInline>),
6162
Case("Testing NORETURN attribute", test_wrapper<testNoReturn>),
63+
Case("Testing FALLTHROUGH attribute", test_wrapper<testFallthrough>),
6264
Case("Testing UNREACHABLE attribute", test_wrapper<testUnreachable>),
6365
Case("Testing DEPRECATED attribute", test_wrapper<testDeprecated>),
6466
};

platform/mbed_toolchain.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,44 @@
340340
#endif
341341
#endif
342342

343+
/** MBED_FALLTHROUGH
344+
* Marks a point in a switch statement where fallthrough can occur.
345+
* Should be placed as the last statement before a label.
346+
*
347+
* @code
348+
* #include "mbed_toolchain.h"
349+
*
350+
* int foo(int arg) {
351+
* switch (arg) {
352+
* case 1:
353+
* case 2:
354+
* case 3:
355+
* arg *= 2;
356+
* MBED_FALLTHROUGH;
357+
* default:
358+
* return arg;
359+
* }
360+
* }
361+
* @endcode
362+
*/
363+
#ifndef MBED_FALLTHROUGH
364+
#if __cplusplus >= 201703
365+
#define MBED_FALLTHROUGH [[fallthrough]]
366+
#elif defined(__clang__)
367+
#if __cplusplus >= 201103
368+
#define MBED_FALLTHROUGH [[clang::fallthrough]]
369+
#elif __has_attribute(fallthrough)
370+
#define MBED_FALLTHROUGH __attribute__((fallthrough))
371+
#else
372+
#define MBED_FALLTHROUGH
373+
#endif
374+
#elif defined (__GNUC__) && !defined(__CC_ARM)
375+
#define MBED_FALLTHROUGH __attribute__((fallthrough))
376+
#else
377+
#define MBED_FALLTHROUGH
378+
#endif
379+
#endif
380+
343381
/** MBED_DEPRECATED("message string")
344382
* Mark a function declaration as deprecated, if it used then a warning will be
345383
* issued by the compiler possibly including the provided message. Note that not

0 commit comments

Comments
 (0)