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 
10 #pragma once
11 
23 #include <assert.h>
24 #include <stdint.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
35 #ifndef NORETURN
36 # ifdef __GNUC__
37 # define NORETURN __attribute__((noreturn))
38 # else
39 # define NORETURN
40 # endif
41 #endif
42 
49 #ifndef NONSTRING
50 # if (__GNUC__ >= 15)
51 # define NONSTRING __attribute__((nonstring))
52 # else
53 # define NONSTRING
54 # endif
55 #endif
56 
64 #ifndef PURE
65 # ifdef __GNUC__
66 # define PURE __attribute__((pure))
67 # else
68 # define PURE
69 # endif
70 #endif
71 
77 #ifndef MAYBE_UNUSED
78 # ifdef __GNUC__
79 # define MAYBE_UNUSED __attribute__((unused))
80 # else
81 # define MAYBE_UNUSED
82 # endif
83 #endif
84 
93 #if defined(__llvm__) || defined(__clang__)
94 # define NO_SANITIZE_ARRAY __attribute__((no_sanitize("address")))
95 #else
96 # define NO_SANITIZE_ARRAY
97 #endif
98 
106 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) || defined(__clang__)
107 # define UNREACHABLE() __builtin_unreachable()
108 #else
109 # define UNREACHABLE() do { /* nothing */ } while (1)
110 #endif
111 
123 #define WITHOUT_PEDANTIC(...) \
124  _Pragma("GCC diagnostic push") \
125  _Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
126  __VA_ARGS__ \
127  _Pragma("GCC diagnostic pop")
128 
139 #define DECLARE_CONSTANT(identifier, const_expr) \
140  WITHOUT_PEDANTIC(enum { identifier = const_expr };)
141 
142 #if DOXYGEN
155 # define IS_CT_CONSTANT(expr) <IMPLEMENTATION>
156 #elif defined(__GNUC__)
157 /* both clang and gcc (which both define __GNUC__) support this */
158 # define IS_CT_CONSTANT(expr) __builtin_constant_p(expr)
159 #else
160 # define IS_CT_CONSTANT(expr) 0
161 #endif
162 
170 #define likely(x) __builtin_expect((uintptr_t)(x), 1)
171 
179 #define unlikely(x) __builtin_expect((uintptr_t)(x), 0)
180 
191 #ifdef NDEBUG
192 # define assume(cond) ((cond) ? (void)0 : UNREACHABLE())
193 #else
194 # define assume(cond) assert(cond)
195 #endif
196 
205 static inline unsigned may_be_zero(unsigned n)
206 {
207  return n;
208 }
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
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...