aboutsummaryrefslogtreecommitdiff
path: root/src/helper/compiler.h
blob: 33a075d64feeb64d5b83012811dbaf6c5cea6cd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* SPDX-License-Identifier: GPL-2.0-or-later */

/*
 * This file contains compiler specific workarounds to handle different
 * compilers and different compiler versions.
 * Inspired by Linux's include/linux/compiler_attributes.h
 * and file sys/cdefs.h in libc and newlib.
 */

#ifndef OPENOCD_HELPER_COMPILER_H
#define OPENOCD_HELPER_COMPILER_H

/*
 * __has_attribute is supported on gcc >= 5, clang >= 2.9 and icc >= 17.
 */
#ifndef __has_attribute
# define __has_attribute(x) 0
#endif

/*
 * The __returns_nonnull function attribute marks the return type of the function
 * as always being non-null.
 */
#ifndef __returns_nonnull
# if __has_attribute(__returns_nonnull__)
#  define __returns_nonnull __attribute__((__returns_nonnull__))
# else
#  define __returns_nonnull
# endif
#endif

/*
 * The __nonnull function attribute marks pointer parameters that
 * must not be NULL.
 *
 * clang for Apple defines
 * #define __nonnull _Nonnull
 * that is a per argument attribute, incompatible with the gcc per function attribute __nonnull__.
 * Undefine it to keep compatibility among compilers.
 */
#if defined(__clang__) && defined(__APPLE__)
# undef __nonnull
#endif
#ifndef __nonnull
# if __has_attribute(__nonnull__)
#  define __nonnull(params) __attribute__ ((__nonnull__ params))
# else
#  define __nonnull(params)
# endif
#endif

#endif /* OPENOCD_HELPER_COMPILER_H */