summaryrefslogtreecommitdiff
path: root/include/compiler.h
blob: 736fac4850e3aa4c408bc4cf2fcdf15eac45207d (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef __CROS_EC_COMPILER_H
#define __CROS_EC_COMPILER_H

/*
 * See https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
 */
#ifndef CONFIG_ZEPHYR
/* If building with Zephyr, use its GCC version. */
#define GCC_VERSION \
	(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif /* !CONFIG_ZEPHYR */

/*
 * The EC codebase assumes that typeof() is available but it is not in Zephyr.
 * We use an #ifdef since arch/arm/include/aarch32/cortex_m/cmse.h defines this
 * macro.
 */
#ifndef typeof
#define typeof(x) __typeof__(x)
#endif

/**
 * ISO C forbids forward references to enum types, but gcc allows it as long as
 * the "-pedantic" flag is not used.
 *
 * In C++11 and newer, forward references to enums are only allowed if the
 * underlying type is specified.
 *
 * New uses of this macro are strongly discouraged; instead of forward
 * declaring the enum, provide the definition.
 *
 * TODO(http://b/187105190): Remove uses of this macro.
 */
#ifdef __cplusplus
#define FORWARD_DECLARE_ENUM(x) enum x : int
#else
#define FORWARD_DECLARE_ENUM(x) enum x
#endif /* __cplusplus */

#define _DO_PRAGMA(x) _Pragma(#x)

#define _DISABLE_COMPILER_WARNING(compiler, warning) \
	_DO_PRAGMA(compiler diagnostic push)         \
	_DO_PRAGMA(compiler diagnostic ignored warning)

#define _ENABLE_COMPILER_WARNING(compiler, warning) \
	_DO_PRAGMA(compiler diagnostic pop)

/**
 * Disable the specified compiler warning for both clang and gcc.
 */
#define DISABLE_COMPILER_WARNING(warning) \
	_DISABLE_COMPILER_WARNING(GCC, warning)

/**
 * Re-enable the specified compiler warning for both clang and gcc. Can only be
 * used after a call to DISABLE_COMPILER_WARNING.
 */
#define ENABLE_COMPILER_WARNING(warning) _ENABLE_COMPILER_WARNING(GCC, warning)

#ifdef __clang__
/**
 * Disable the specified compiler warning for clang.
 */
#define DISABLE_CLANG_WARNING(warning) _DISABLE_COMPILER_WARNING(clang, warning)
/**
 * Re-enable the specified compiler warning for clang. Can only be used after
 * a call to DISABLE_CLANG_WARNING.
 */
#define ENABLE_CLANG_WARNING(warning) _ENABLE_COMPILER_WARNING(clang, warning)
#else
#define DISABLE_CLANG_WARNING(warning)
#define ENABLE_CLANG_WARNING(warning)
#endif

#if defined(__GNUC__) && !defined(__clang__)
/**
 * Disable the specified compiler warning for gcc.
 */
#define DISABLE_GCC_WARNING(warning) _DISABLE_COMPILER_WARNING(GCC, warning)
/**
 * Re-enable the specified compiler warning for clang. Can only be used after
 * a call to DISABLE_GCC_WARNING.
 */
#define ENABLE_GCC_WARNING(warning) _ENABLE_COMPILER_WARNING(GCC, warning)
#else
#define DISABLE_GCC_WARNING(warning)
#define ENABLE_GCC_WARNING(warning)
#endif

#endif /* __CROS_EC_COMPILER_H */