Loading...
Searching...
No Matches
compiler_hints.h File Reference

Common macros and compiler attributes/pragmas configuration. More...

Detailed Description

Common macros and compiler attributes/pragmas configuration.

Author
René Kijewski rene..nosp@m.kije.nosp@m.wski@.nosp@m.fu-b.nosp@m.erlin.nosp@m..de
Michel Rottleuthner miche.nosp@m.l.ro.nosp@m.ttleu.nosp@m.thne.nosp@m.r@haw.nosp@m.-ham.nosp@m.burg..nosp@m.de

Definition in file compiler_hints.h.

#include <assert.h>
#include <stdint.h>
+ Include dependency graph for compiler_hints.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

#define NORETURN
 The NORETURN keyword tells the compiler to assume that the function cannot return.
 
#define PURE
 The function has no effects except the return value and its return value depends only on the parameters and/or global variables.
 
#define MAYBE_UNUSED
 tell the compiler something may be unused static functions, function arguments, local variables
 
#define NO_SANITIZE_ARRAY
 Tell the compiler that this array should be ignored during sanitizing.
 
#define UNREACHABLE()   do { /* nothing */ } while (1)
 Tell the compiler that this line of code cannot be reached.
 
#define WITHOUT_PEDANTIC(...)
 Disable -Wpedantic for the argument, but restore diagnostic settings afterwards.
 
#define DECLARE_CONSTANT(identifier, const_expr)    WITHOUT_PEDANTIC(enum { identifier = const_expr };)
 Declare a constant named identifier as anonymous enum that has the value const_expr.
 
#define IS_CT_CONSTANT(expr)   <IMPLEMENTATION>
 Check if given variable / expression is detected as compile time constant.
 
#define likely(x)   __builtin_expect((uintptr_t)(x), 1)
 Hint to the compiler that the condition x is likely taken.
 
#define unlikely(x)   __builtin_expect((uintptr_t)(x), 0)
 Hint to the compiler that the condition x is likely not taken.
 
#define assume(cond)   assert(cond)
 Behaves like an assert(), but tells the compiler that cond can never be false.
 
static unsigned may_be_zero (unsigned n)
 Wrapper function to silence "comparison is always false due to limited range of data type" type of warning when the warning is caused by a preprocessor configuration value that may be zero.
 

Macro Definition Documentation

◆ assume

#define assume (   cond)    assert(cond)

Behaves like an assert(), but tells the compiler that cond can never be false.

This allows the compiler to optimize the code accordingly even when NDEBUG is set / with DEVELHELP=0.

cond being false will result in undefined behavior.

Parameters
[in]condCondition that is guaranteed to be true

Definition at line 181 of file compiler_hints.h.

◆ DECLARE_CONSTANT

#define DECLARE_CONSTANT (   identifier,
  const_expr 
)     WITHOUT_PEDANTIC(enum { identifier = const_expr };)

Declare a constant named identifier as anonymous enum that has the value const_expr.

Warning
This is intended for internal use only

This turns any expression that is constant and known at compile time into a formal compile time constant. This allows e.g. using non-formally but still constant expressions in static_assert().

Definition at line 126 of file compiler_hints.h.

◆ IS_CT_CONSTANT

#define IS_CT_CONSTANT (   expr)    <IMPLEMENTATION>

Check if given variable / expression is detected as compile time constant.

Note
This might return 0 on compile time constant expressions if the compiler is not able to prove the constness at the given level of optimization.

This will return 0 if the used compiler does not support this

Warning
This is intended for internal use only

This allows providing two different implementations in C, with one being more efficient if constant folding is used.

Definition at line 142 of file compiler_hints.h.

◆ likely

#define likely (   x)    __builtin_expect((uintptr_t)(x), 1)

Hint to the compiler that the condition x is likely taken.

Parameters
[in]xCondition that is likely taken
Returns
result of x

Definition at line 157 of file compiler_hints.h.

◆ MAYBE_UNUSED

#define MAYBE_UNUSED

tell the compiler something may be unused static functions, function arguments, local variables

Definition at line 68 of file compiler_hints.h.

◆ NO_SANITIZE_ARRAY

#define NO_SANITIZE_ARRAY

Tell the compiler that this array should be ignored during sanitizing.

In special cases, e.g. XFA, the address sanitizer might interfere in a way that breaks the application. Use this macro to disable address sanitizing for a given variable. Currently only utilised by llvm.

Definition at line 83 of file compiler_hints.h.

◆ NORETURN

#define NORETURN

The NORETURN keyword tells the compiler to assume that the function cannot return.

Definition at line 40 of file compiler_hints.h.

◆ PURE

#define PURE

The function has no effects except the return value and its return value depends only on the parameters and/or global variables.

Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be.

Definition at line 55 of file compiler_hints.h.

◆ unlikely

#define unlikely (   x)    __builtin_expect((uintptr_t)(x), 0)

Hint to the compiler that the condition x is likely not taken.

Parameters
[in]xCondition that is unlikely
Returns
result of x

Definition at line 166 of file compiler_hints.h.

◆ UNREACHABLE

#define UNREACHABLE ( )    do { /* nothing */ } while (1)

Tell the compiler that this line of code cannot be reached.

Most useful in junction with NORETURN. Use this if the compiler cannot tell that e.g. an assembler instruction causes a longjmp, or a write causes a reboot.

Definition at line 96 of file compiler_hints.h.

◆ WITHOUT_PEDANTIC

#define WITHOUT_PEDANTIC (   ...)
Value:
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
__VA_ARGS__ \
_Pragma("GCC diagnostic pop")

Disable -Wpedantic for the argument, but restore diagnostic settings afterwards.

Parameters
...The expression that -Wpendantic should not apply to
Warning
This is intended for internal use only

This is particularly useful when declaring non-strictly conforming preprocessor macros, as the diagnostics need to be disabled where the macro is evaluated, not where the macro is declared.

Definition at line 110 of file compiler_hints.h.

Function Documentation

◆ may_be_zero()

static unsigned may_be_zero ( unsigned  n)
inlinestatic

Wrapper function to silence "comparison is always false due to limited range of data type" type of warning when the warning is caused by a preprocessor configuration value that may be zero.

Parameters
[in]nVariable that may be zero
Returns
The same variable n

Definition at line 192 of file compiler_hints.h.