Add MBED_NORETURN attributes

Save some ROM space by putting MBED_NORETURN attributes on error
functions and failed asserts.

mbed_error was documented as returning an error code. It never
actually could return, so documentation updated, but return type
kept.
pull/8328/head
Kevin Bracey 2018-10-05 16:52:52 +03:00
parent 57748bd46e
commit ea16a6ba1d
8 changed files with 17 additions and 20 deletions

View File

@ -97,7 +97,7 @@
void lwip_mbed_tracef_debug(const char *fmt, ...); void lwip_mbed_tracef_debug(const char *fmt, ...);
void lwip_mbed_tracef_error(const char *fmt, ...); void lwip_mbed_tracef_error(const char *fmt, ...);
void lwip_mbed_tracef_warn(const char *fmt, ...); void lwip_mbed_tracef_warn(const char *fmt, ...);
void lwip_mbed_assert_fail(const char *msg, const char *func, const char *file, unsigned int line); MBED_NORETURN void lwip_mbed_assert_fail(const char *msg, const char *func, const char *file, unsigned int line);
#define LWIP_PLATFORM_DIAG(vars) lwip_mbed_tracef_debug vars #define LWIP_PLATFORM_DIAG(vars) lwip_mbed_tracef_debug vars
#define LWIP_PLATFORM_DIAG_SEVERE(vars) lwip_mbed_tracef_error vars #define LWIP_PLATFORM_DIAG_SEVERE(vars) lwip_mbed_tracef_error vars
@ -109,7 +109,7 @@ void lwip_mbed_assert_fail(const char *msg, const char *func, const char *file,
#else // MBED_CONF_LWIP_USE_MBED_TRACE #else // MBED_CONF_LWIP_USE_MBED_TRACE
#include <stdio.h> #include <stdio.h>
void assert_printf(char *msg, int line, char *file); MBED_NORETURN void assert_printf(char *msg, int line, char *file);
/* Plaform specific diagnostic output */ /* Plaform specific diagnostic output */
#define LWIP_PLATFORM_DIAG(vars) printf vars #define LWIP_PLATFORM_DIAG(vars) printf vars

View File

@ -589,7 +589,7 @@ void lwip_mbed_tracef_error(const char *fmt, ...)
va_end(ap); va_end(ap);
} }
void lwip_mbed_assert_fail(const char *msg, const char *func, const char *file, unsigned int line) MBED_NORETURN void lwip_mbed_assert_fail(const char *msg, const char *func, const char *file, unsigned int line)
{ {
mbed_tracef(TRACE_LEVEL_ERROR, "lwIP", "Assertion failed: %s, function %s, file %s, line %u.", msg, func, file, line); mbed_tracef(TRACE_LEVEL_ERROR, "lwIP", "Assertion failed: %s, function %s, file %s, line %u.", msg, func, file, line);
exit(EXIT_FAILURE); // XXX how about abort? mbed_assert uses exit, so follow suit exit(EXIT_FAILURE); // XXX how about abort? mbed_assert uses exit, so follow suit
@ -605,7 +605,7 @@ void lwip_mbed_assert_fail(const char *msg, const char *func, const char *file,
\param[in] line Line number in file with error \param[in] line Line number in file with error
\param[in] file Filename with error \param[in] file Filename with error
*/ */
void assert_printf(char *msg, int line, char *file) { MBED_NORETURN void assert_printf(char *msg, int line, char *file) {
if (msg) if (msg)
error("%s:%d in file %s\n", msg, line, file); error("%s:%d in file %s\n", msg, line, file);
else else

View File

@ -20,7 +20,7 @@
#include "platform/mbed_critical.h" #include "platform/mbed_critical.h"
#include "platform/mbed_error.h" #include "platform/mbed_error.h"
void mbed_assert_internal(const char *expr, const char *file, int line) MBED_NORETURN void mbed_assert_internal(const char *expr, const char *file, int line)
{ {
core_util_critical_section_enter(); core_util_critical_section_enter();
mbed_error(MBED_ERROR_ASSERTION_FAILED, expr, 0, file, line); mbed_error(MBED_ERROR_ASSERTION_FAILED, expr, 0, file, line);

View File

@ -24,6 +24,7 @@
#define MBED_ASSERT_H #define MBED_ASSERT_H
#include "mbed_preprocessor.h" #include "mbed_preprocessor.h"
#include "mbed_toolchain.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -37,7 +38,7 @@ extern "C" {
* @param file File where assertation failed. * @param file File where assertation failed.
* @param line Failing assertation line number. * @param line Failing assertation line number.
*/ */
void mbed_assert_internal(const char *expr, const char *file, int line); MBED_NORETURN void mbed_assert_internal(const char *expr, const char *file, int line);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -21,7 +21,7 @@
#include "platform/mbed_retarget.h" #include "platform/mbed_retarget.h"
#include "platform/mbed_critical.h" #include "platform/mbed_critical.h"
WEAK void mbed_die(void) WEAK MBED_NORETURN void mbed_die(void)
{ {
#if !defined (NRF51_H) && !defined(TARGET_EFM32) #if !defined (NRF51_H) && !defined(TARGET_EFM32)
core_util_critical_section_enter(); core_util_critical_section_enter();

View File

@ -45,7 +45,7 @@ static mbed_error_hook_t error_hook = NULL;
static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsigned int error_value, const char *filename, int line_number, void *caller); static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsigned int error_value, const char *filename, int line_number, void *caller);
//Helper function to halt the system //Helper function to halt the system
static void mbed_halt_system(void) static MBED_NORETURN void mbed_halt_system(void)
{ {
// Prevent recursion if halt is called again during halt attempt - try // Prevent recursion if halt is called again during halt attempt - try
// something simple instead. // something simple instead.
@ -66,7 +66,7 @@ static void mbed_halt_system(void)
exit(1); exit(1);
} }
WEAK void error(const char *format, ...) WEAK MBED_NORETURN void error(const char *format, ...)
{ {
// Prevent recursion if error is called again during store+print attempt // Prevent recursion if error is called again during store+print attempt
if (!core_util_atomic_flag_test_and_set(&error_in_progress)) { if (!core_util_atomic_flag_test_and_set(&error_in_progress)) {
@ -169,14 +169,14 @@ int mbed_get_error_count(void)
return error_count; return error_count;
} }
//Sets a fatal error //Sets a non-fatal error
mbed_error_status_t mbed_warning(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number) mbed_error_status_t mbed_warning(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number)
{ {
return handle_error(error_status, error_value, filename, line_number, MBED_CALLER_ADDR()); return handle_error(error_status, error_value, filename, line_number, MBED_CALLER_ADDR());
} }
//Sets a fatal error, this function is marked WEAK to be able to override this for some tests //Sets a fatal error, this function is marked WEAK to be able to override this for some tests
WEAK mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number) WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number)
{ {
// Prevent recursion if error is called again during store+print attempt // Prevent recursion if error is called again during store+print attempt
if (!core_util_atomic_flag_test_and_set(&error_in_progress)) { if (!core_util_atomic_flag_test_and_set(&error_in_progress)) {
@ -188,8 +188,6 @@ WEAK mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char
} }
mbed_halt_system(); mbed_halt_system();
return MBED_ERROR_FAILED_OPERATION;
} }
//Register an application defined callback with error handling //Register an application defined callback with error handling

View File

@ -169,8 +169,7 @@ typedef int mbed_error_status_t;
* @param error_status mbed_error_status_t status to be set(See mbed_error_status_t enum above for available error status values). * @param error_status mbed_error_status_t status to be set(See mbed_error_status_t enum above for available error status values).
* @param error_msg The error message to be printed out to STDIO/Serial. * @param error_msg The error message to be printed out to STDIO/Serial.
* @param error_value Value associated with the error status. This would depend on error code/error scenario. Only available with MBED_ERROR1 * @param error_value Value associated with the error status. This would depend on error code/error scenario. Only available with MBED_ERROR1
* @return 0 or MBED_SUCCESS. * @return Does not return
* MBED_ERROR_INVALID_ARGUMENT if called with invalid error status/codes
* *
* @code * @code
* *
@ -872,7 +871,7 @@ typedef struct _mbed_error_ctx {
* *
*/ */
void error(const char *format, ...); MBED_NORETURN void error(const char *format, ...);
/** /**
* Call this Macro to generate a mbed_error_status_t value for a System error * Call this Macro to generate a mbed_error_status_t value for a System error
@ -979,8 +978,7 @@ int mbed_get_error_count(void);
* @param error_value Value associated with the error status. This would depend on error code/error scenario. * @param error_value Value associated with the error status. This would depend on error code/error scenario.
* @param filename Name of the source file originating the error( Most callers can pass __FILE__ here ). * @param filename Name of the source file originating the error( Most callers can pass __FILE__ here ).
* @param line_number The line number of the source file originating the error( Most callers can pass __LINE__ here ) . * @param line_number The line number of the source file originating the error( Most callers can pass __LINE__ here ) .
* @return 0 or MBED_SUCCESS. * @return Does not return.
* MBED_ERROR_INVALID_ARGUMENT if called with invalid error status/codes
* *
* @code * @code
* *
@ -990,7 +988,7 @@ int mbed_get_error_count(void);
* *
* @note See MBED_WARNING/MBED_ERROR macros which provides a wrapper on this API * @note See MBED_WARNING/MBED_ERROR macros which provides a wrapper on this API
*/ */
mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number); MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number);
/** /**
* Registers an application defined error callback with the error handling system. * Registers an application defined error callback with the error handling system.

View File

@ -121,7 +121,7 @@ void mbed_mac_address(char *mac);
/** Cause the mbed to flash the BLOD (Blue LEDs Of Death) sequence /** Cause the mbed to flash the BLOD (Blue LEDs Of Death) sequence
*/ */
void mbed_die(void); MBED_NORETURN void mbed_die(void);
/** Print out an error message. This is typically called when /** Print out an error message. This is typically called when
* handling a crash. * handling a crash.