Loading...
Searching...
No Matches
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
19
20#include <assert.h>
21#include <stdint.h>
22
23#ifdef __cplusplus
24extern "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
126#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
127
139#define WITHOUT_PEDANTIC(...) \
140 _Pragma("GCC diagnostic push") \
141 _Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
142 __VA_ARGS__ \
143 _Pragma("GCC diagnostic pop")
144
155#define DECLARE_CONSTANT(identifier, const_expr) \
156 WITHOUT_PEDANTIC(enum { identifier = const_expr };)
157
158#if DOXYGEN
171# define IS_CT_CONSTANT(expr) <IMPLEMENTATION>
172#elif defined(__GNUC__)
173/* both clang and gcc (which both define __GNUC__) support this */
174# define IS_CT_CONSTANT(expr) __builtin_constant_p(expr)
175#else
176# define IS_CT_CONSTANT(expr) 0
177#endif
178
186#define likely(x) __builtin_expect((uintptr_t)(x), 1)
187
195#define unlikely(x) __builtin_expect((uintptr_t)(x), 0)
196
209#define assume(cond) ((cond) ? (void)0 : (assert(0), UNREACHABLE()))
210
219static inline unsigned may_be_zero(unsigned n)
220{
221 return n;
222}
223
224#ifdef __cplusplus
225}
226#endif
227
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" ty...