compiler_hints.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2014 Freie Universität Berlin
3  * SPDX-FileCopyrightText: 2017 HAW-Hamburg
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
20 #include <assert.h>
21 #include <stdint.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
32 #ifndef NORETURN
33 # ifdef __GNUC__
34 # define NORETURN __attribute__((noreturn))
35 # else
36 # define NORETURN
37 # endif
38 #endif
39 
46 #ifndef NONSTRING
47 # if ((__GNUC__ >= 15) || (__clang_major__ >= 21))
48 # define NONSTRING __attribute__((nonstring))
49 # else
50 # define NONSTRING
51 # endif
52 #endif
53 
61 #ifndef PURE
62 # ifdef __GNUC__
63 # define PURE __attribute__((pure))
64 # else
65 # define PURE
66 # endif
67 #endif
68 
74 #ifndef MAYBE_UNUSED
75 # ifdef __GNUC__
76 # define MAYBE_UNUSED __attribute__((unused))
77 # else
78 # define MAYBE_UNUSED
79 # endif
80 #endif
81 
90 #if defined(__llvm__) || defined(__clang__)
91 # define NO_SANITIZE_ARRAY __attribute__((no_sanitize("address")))
92 #else
93 # define NO_SANITIZE_ARRAY
94 #endif
95 
103 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) || defined(__clang__)
104 # define UNREACHABLE() __builtin_unreachable()
105 #else
106 # define UNREACHABLE() do { /* nothing */ } while (1)
107 #endif
108 
120 #define WITHOUT_PEDANTIC(...) \
121  _Pragma("GCC diagnostic push") \
122  _Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
123  __VA_ARGS__ \
124  _Pragma("GCC diagnostic pop")
125 
136 #define DECLARE_CONSTANT(identifier, const_expr) \
137  WITHOUT_PEDANTIC(enum { identifier = const_expr };)
138 
139 #if DOXYGEN
152 # define IS_CT_CONSTANT(expr) <IMPLEMENTATION>
153 #elif defined(__GNUC__)
154 /* both clang and gcc (which both define __GNUC__) support this */
155 # define IS_CT_CONSTANT(expr) __builtin_constant_p(expr)
156 #else
157 # define IS_CT_CONSTANT(expr) 0
158 #endif
159 
167 #define likely(x) __builtin_expect((uintptr_t)(x), 1)
168 
176 #define unlikely(x) __builtin_expect((uintptr_t)(x), 0)
177 
188 #ifdef NDEBUG
189 # define assume(cond) ((cond) ? (void)0 : UNREACHABLE())
190 #else
191 # define assume(cond) assert(cond)
192 #endif
193 
202 static inline unsigned may_be_zero(unsigned n)
203 {
204  return n;
205 }
206 
207 #ifdef __cplusplus
208 }
209 #endif
210 
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...