compiler_hints.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freie Universität Berlin
3  * 2017 HAW-Hamburg
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
21 #ifndef COMPILER_HINTS_H
22 #define COMPILER_HINTS_H
23 
24 #include <assert.h>
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
36 #ifndef NORETURN
37 #ifdef __GNUC__
38 #define NORETURN __attribute__((noreturn))
39 #else
40 #define NORETURN
41 #endif
42 #endif
43 
51 #ifndef PURE
52 #ifdef __GNUC__
53 #define PURE __attribute__((pure))
54 #else
55 #define PURE
56 #endif
57 #endif
58 
64 #ifndef MAYBE_UNUSED
65 #ifdef __GNUC__
66 #define MAYBE_UNUSED __attribute__((unused))
67 #else
68 #define MAYBE_UNUSED
69 #endif
70 #endif
71 
80 #if defined(__llvm__) || defined(__clang__)
81 #define NO_SANITIZE_ARRAY __attribute__((no_sanitize("address")))
82 #else
83 #define NO_SANITIZE_ARRAY
84 #endif
85 
93 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) || defined(__clang__)
94 #define UNREACHABLE() __builtin_unreachable()
95 #else
96 #define UNREACHABLE() do { /* nothing */ } while (1)
97 #endif
98 
110 #define WITHOUT_PEDANTIC(...) \
111  _Pragma("GCC diagnostic push") \
112  _Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
113  __VA_ARGS__ \
114  _Pragma("GCC diagnostic pop")
115 
126 #define DECLARE_CONSTANT(identifier, const_expr) \
127  WITHOUT_PEDANTIC(enum { identifier = const_expr };)
128 
129 #if DOXYGEN
142 #define IS_CT_CONSTANT(expr) <IMPLEMENTATION>
143 #elif defined(__GNUC__)
144 /* both clang and gcc (which both define __GNUC__) support this */
145 #define IS_CT_CONSTANT(expr) __builtin_constant_p(expr)
146 #else
147 #define IS_CT_CONSTANT(expr) 0
148 #endif
149 
157 #define likely(x) __builtin_expect((uintptr_t)(x), 1)
158 
166 #define unlikely(x) __builtin_expect((uintptr_t)(x), 0)
167 
178 #ifdef NDEBUG
179 #define assume(cond) ((cond) ? (void)0 : UNREACHABLE())
180 #else
181 #define assume(cond) assert(cond)
182 #endif
183 
192 static inline unsigned may_be_zero(unsigned n)
193 {
194  return n;
195 }
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif /* COMPILER_HINTS_H */
POSIX.1-2008 compliant version of the assert macro.
static unsigned may_be_zero(unsigned n)
Wrapper function to silence "comparison is always false due to limited range of data type" t...