From 8381fda6241faa911d08eada093d80f75282dfd4 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 16 May 2016 12:49:46 -0500 Subject: [PATCH] Merged compiler-polyfill into toolchain.h from https://github.com/armmbed/compiler-polyfill notes - Adopted existing mbed naming convention of all caps. This avoids conflicts with compiler declarations https://github.com/ARMmbed/compiler-polyfill/issues/7 - Dropped align attribute due to lack of support on IAR. - Currently toolchain.h lives in /hal/api, although with the addition of /util there may be a better home. --- hal/api/toolchain.h | 109 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 7 deletions(-) diff --git a/hal/api/toolchain.h b/hal/api/toolchain.h index 7a79226941..b448fca102 100644 --- a/hal/api/toolchain.h +++ b/hal/api/toolchain.h @@ -16,6 +16,107 @@ #ifndef MBED_TOOLCHAIN_H #define MBED_TOOLCHAIN_H + +// Warning for unsupported compilers +#if !defined(__GNUC__) /* GCC */ \ + && !defined(__CC_ARM) /* ARMCC */ \ + && !defined(__clang__) /* LLVM/Clang */ \ + && !defined(__ICCARM__) /* IAR */ +#warning "This compiler is not yet supported." +#endif + + +// Attributes + +/** PACK + * Pack a structure, preventing any padding from being added between fields. + * + * @code + * #include "toolchain.h" + * + * struct foo { + * char x; + * int y; + * } PACKED; + * @endcode + */ +#ifndef PACKED +#if defined(__ICCARM__) +#define PACKED __packed +#else +#define PACKED __attribute__((packed)) +#endif +#endif + +/** UNUSED + * Declare a function argument to be unused, suppressing compiler warnings + * + * @code + * #include "toolchain.h" + * + * void foo(UNUSED int arg){ + * + * } + * @endcode + */ +#ifndef UNUSED +#if defined(__GNUC__) || defined(__CC_ARM) || defined(__clang__) +#define UNUSED __attribute__((__unused__)) +#else +#define UNUSED +#endif +#endif + +/** WEAK + * Mark a function as being weak. + * + * @note + * weak functions are not friendly to making code re-usable, as they can only + * be overridden once (and if they are multiply overridden the linker will emit + * no warning). You should not normally use weak symbols as part of the API to + * re-usable modules. + * + * @code + * #include "toolchain.h" + * + * WEAK void foo() { + * // a weak implementation of foo that can be overriden by a definition + * // without __weak + * } + * @endcode + */ +#ifndef WEAK +#if defined(__ICCARM__) +#define WEAK __weak +#else +#define WEAK __attribute__((weak)) +#endif +#endif + +/** 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 + * all compilers are able to display the message. + * + * @code + * #include "toolchain.h" + * + * DEPRECATED("don't foo any more, bar instead") + * void foo(int arg); + * @endcode + */ +#ifndef DEPRECATED +#if defined(__GNUC__) || defined(__clang__) +#define DEPRECATED(M) __attribute__((deprecated(M))) +#elif defined(__CC_ARM) +#define DEPRECATED(M) __attribute__((deprecated)) +#else +#define DEPRECATED(M) +#endif +#endif + + +// FILEHANDLE declaration #if defined(TOOLCHAIN_ARM) #include #endif @@ -24,16 +125,10 @@ typedef int FILEHANDLE; #endif +// Backwards compatibility #ifndef EXTERN #define EXTERN extern #endif -#if defined (__ICCARM__) -# define WEAK __weak -# define PACKED __packed -#else -# define WEAK __attribute__((weak)) -# define PACKED __attribute__((packed)) -#endif #endif