Skip to content

Commit 7c29455

Browse files
committed
Merge pull request #1784 from geky/attributes
Add useful attributes supported by supported compilers
2 parents bddce7c + 5cdb151 commit 7c29455

File tree

1 file changed

+215
-7
lines changed

1 file changed

+215
-7
lines changed

hal/api/toolchain.h

Lines changed: 215 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,214 @@
1616
#ifndef MBED_TOOLCHAIN_H
1717
#define MBED_TOOLCHAIN_H
1818

19+
20+
// Warning for unsupported compilers
21+
#if !defined(__GNUC__) /* GCC */ \
22+
&& !defined(__CC_ARM) /* ARMCC */ \
23+
&& !defined(__clang__) /* LLVM/Clang */ \
24+
&& !defined(__ICCARM__) /* IAR */
25+
#warning "This compiler is not yet supported."
26+
#endif
27+
28+
29+
// Attributes
30+
31+
/** MBED_PACKED
32+
* Pack a structure, preventing any padding from being added between fields.
33+
*
34+
* @code
35+
* #include "toolchain.h"
36+
*
37+
* MBED_PACKED(struct) foo {
38+
* char x;
39+
* int y;
40+
* };
41+
* @endcode
42+
*/
43+
#ifndef MBED_PACKED
44+
#if defined(__ICCARM__)
45+
#define MBED_PACKED(struct) __packed struct
46+
#else
47+
#define MBED_PACKED(struct) struct __attribute__((packed))
48+
#endif
49+
#endif
50+
51+
/** MBED_ALIGN(N)
52+
* Declare a variable to be aligned on an N-byte boundary.
53+
*
54+
* @code
55+
* #include "toolchain.h"
56+
*
57+
* MBED_ALIGN(16) char a;
58+
* @endcode
59+
*/
60+
#ifndef MBED_ALIGN
61+
#if defined(__ICCARM__)
62+
#define _MBED_ALIGN(N) _Pragma(#N)
63+
#define MBED_ALIGN(N) _MBED_ALIGN(data_alignment=N)
64+
#else
65+
#define MBED_ALIGN(N) __attribute__((aligned(N)))
66+
#endif
67+
#endif
68+
69+
/** MBED_UNUSED
70+
* Declare a function argument to be unused, suppressing compiler warnings
71+
*
72+
* @code
73+
* #include "toolchain.h"
74+
*
75+
* void foo(MBED_UNUSED int arg) {
76+
*
77+
* }
78+
* @endcode
79+
*/
80+
#ifndef MBED_UNUSED
81+
#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
82+
#define MBED_UNUSED __attribute__((__unused__))
83+
#else
84+
#define MBED_UNUSED
85+
#endif
86+
#endif
87+
88+
/** MBED_WEAK
89+
* Mark a function as being weak.
90+
*
91+
* @note
92+
* weak functions are not friendly to making code re-usable, as they can only
93+
* be overridden once (and if they are multiply overridden the linker will emit
94+
* no warning). You should not normally use weak symbols as part of the API to
95+
* re-usable modules.
96+
*
97+
* @code
98+
* #include "toolchain.h"
99+
*
100+
* MBED_WEAK void foo() {
101+
* // a weak implementation of foo that can be overriden by a definition
102+
* // without __weak
103+
* }
104+
* @endcode
105+
*/
106+
#ifndef MBED_WEAK
107+
#if defined(__ICCARM__)
108+
#define MBED_WEAK __weak
109+
#else
110+
#define MBED_WEAK __attribute__((weak))
111+
#endif
112+
#endif
113+
114+
/** MBED_PURE
115+
* Hint to the compiler that a function depends only on parameters
116+
*
117+
* @code
118+
* #include "toolchain.h"
119+
*
120+
* MBED_PURE int foo(int arg){
121+
* // no access to global variables
122+
* }
123+
* @endcode
124+
*/
125+
#ifndef MBED_PURE
126+
#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
127+
#define MBED_PURE __attribute__((const))
128+
#else
129+
#define MBED_PURE
130+
#endif
131+
#endif
132+
133+
/** MBED_FORCEINLINE
134+
* Declare a function that must always be inlined. Failure to inline
135+
* such a function will result in an error.
136+
*
137+
* @code
138+
* #include "toolchain.h"
139+
*
140+
* MBED_FORCEINLINE void foo() {
141+
*
142+
* }
143+
* @endcode
144+
*/
145+
#ifndef MBED_FORCEINLINE
146+
#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
147+
#define MBED_FORCEINLINE static inline __attribute__((always_inline))
148+
#elif defined(__ICCARM__)
149+
#define MBED_FORCEINLINE _Pragma("inline=forced") static
150+
#else
151+
#define MBED_FORCEINLINE static inline
152+
#endif
153+
#endif
154+
155+
/** MBED_NORETURN
156+
* Declare a function that will never return.
157+
*
158+
* @code
159+
* #include "toolchain.h"
160+
*
161+
* MBED_NORETURN void foo() {
162+
* // must never return
163+
* while (1) {}
164+
* }
165+
* @endcode
166+
*/
167+
#ifndef MBED_NORETURN
168+
#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
169+
#define MBED_NORETURN __attribute__((noreturn))
170+
#elif defined(__ICCARM__)
171+
#define MBED_NORETURN __noreturn
172+
#else
173+
#define MBED_NORETURN
174+
#endif
175+
#endif
176+
177+
/** MBED_UNREACHABLE
178+
* An unreachable statement. If the statement is reached,
179+
* behaviour is undefined. Useful in situations where the compiler
180+
* cannot deduce the unreachability of code.
181+
*
182+
* @code
183+
* #include "toolchain.h"
184+
*
185+
* void foo(int arg) {
186+
* switch (arg) {
187+
* case 1: return 1;
188+
* case 2: return 2;
189+
* ...
190+
* }
191+
* MBED_UNREACHABLE;
192+
* }
193+
* @endcode
194+
*/
195+
#ifndef MBED_UNREACHABLE
196+
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
197+
#define MBED_UNREACHABLE __builtin_unreachable()
198+
#else
199+
#define MBED_UNREACHABLE while (1)
200+
#endif
201+
#endif
202+
203+
/** MBED_DEPRECATED("message string")
204+
* Mark a function declaration as deprecated, if it used then a warning will be
205+
* issued by the compiler possibly including the provided message. Note that not
206+
* all compilers are able to display the message.
207+
*
208+
* @code
209+
* #include "toolchain.h"
210+
*
211+
* MBED_DEPRECATED("don't foo any more, bar instead")
212+
* void foo(int arg);
213+
* @endcode
214+
*/
215+
#ifndef MBED_DEPRECATED
216+
#if defined(__GNUC__) || defined(__clang__)
217+
#define MBED_DEPRECATED(M) __attribute__((deprecated(M)))
218+
#elif defined(__CC_ARM)
219+
#define MBED_DEPRECATED(M) __attribute__((deprecated))
220+
#else
221+
#define MBED_DEPRECATED(M)
222+
#endif
223+
#endif
224+
225+
226+
// FILEHANDLE declaration
19227
#if defined(TOOLCHAIN_ARM)
20228
#include <rt_sys.h>
21229
#endif
@@ -24,15 +232,15 @@
24232
typedef int FILEHANDLE;
25233
#endif
26234

27-
#if defined (__ICCARM__)
28-
# define WEAK __weak
29-
# define PACKED __packed
30-
#else
31-
# define WEAK __attribute__((weak))
32-
# define PACKED __attribute__((packed))
235+
// Backwards compatibility
236+
#ifndef WEAK
237+
#define WEAK MBED_WEAK
238+
#endif
239+
240+
#ifndef PACKED
241+
#define PACKED MBED_PACKED()
33242
#endif
34243

35-
// Backwards compatibility
36244
#ifndef EXTERN
37245
#define EXTERN extern
38246
#endif

0 commit comments

Comments
 (0)