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 
50 #ifndef PURE
51 #ifdef __GNUC__
52 #define PURE __attribute__((pure))
53 #else
54 #define PURE
55 #endif
56 #endif
57 
63 #ifndef MAYBE_UNUSED
64 #ifdef __GNUC__
65 #define MAYBE_UNUSED __attribute__((unused))
66 #else
67 #define MAYBE_UNUSED
68 #endif
69 #endif
70 
79 #if defined(__llvm__) || defined(__clang__)
80 #define NO_SANITIZE_ARRAY __attribute__((no_sanitize("address")))
81 #else
82 #define NO_SANITIZE_ARRAY
83 #endif
84 
92 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) || defined(__clang__)
93 #define UNREACHABLE() __builtin_unreachable()
94 #else
95 #define UNREACHABLE() do { /* nothing */ } while (1)
96 #endif
97 
109 #define WITHOUT_PEDANTIC(...) \
110  _Pragma("GCC diagnostic push") \
111  _Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
112  __VA_ARGS__ \
113  _Pragma("GCC diagnostic pop")
114 
125 #define DECLARE_CONSTANT(identifier, const_expr) \
126  WITHOUT_PEDANTIC(enum { identifier = const_expr };)
127 
128 #if DOXYGEN
141 #define IS_CT_CONSTANT(expr) <IMPLEMENTATION>
142 #elif defined(__GNUC__)
143 /* both clang and gcc (which both define __GNUC__) support this */
144 #define IS_CT_CONSTANT(expr) __builtin_constant_p(expr)
145 #else
146 #define IS_CT_CONSTANT(expr) 0
147 #endif
148 
156 #define likely(x) __builtin_expect((uintptr_t)(x), 1)
157 
165 #define unlikely(x) __builtin_expect((uintptr_t)(x), 0)
166 
177 #ifdef NDEBUG
178 #define assume(cond) ((cond) ? (void)0 : UNREACHABLE())
179 #else
180 #define assume(cond) assert(cond)
181 #endif
182 
191 static inline unsigned may_be_zero(unsigned n)
192 {
193  return n;
194 }
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
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...