aboutsummaryrefslogtreecommitdiff
path: root/src/lib/cc/ctassert.h
blob: d9d3aa40b07fd6d4d6cbf826a04d1a3f29ed1985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Copyright (c) 2018 The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file ctassert.h
 *
 * \brief Compile-time assertions: CTASSERT(expression).
 */

#ifndef TOR_CTASSERT_H
#define TOR_CTASSERT_H

#include "lib/cc/compat_compiler.h"

/**
 * CTASSERT(expression)
 *
 *       Trigger a compiler error if expression is false.
 */
#if __STDC_VERSION__ >= 201112L

/* If C11 is available, just use _Static_assert.  */
#define CTASSERT(x) _Static_assert((x), #x)

#else /* !(__STDC_VERSION__ >= 201112L) */

/*
 * If C11 is not available, expand __COUNTER__, or __INCLUDE_LEVEL__
 * and __LINE__, or just __LINE__, with an intermediate preprocessor
 * macro CTASSERT_EXPN, and then use CTASSERT_DECL to paste the
 * expansions together into a unique name.
 *
 * We use this name as a typedef of an array type with a positive
 * length if the assertion is true, and a negative length of the
 * assertion is false, which is invalid and hence triggers a compiler
 * error.
 */
#if defined(__COUNTER__)
#define CTASSERT(x) CTASSERT_EXPN((x), c, __COUNTER__)
#elif defined(__INCLUDE_LEVEL__)
#define CTASSERT(x) CTASSERT_EXPN((x), __INCLUDE_LEVEL__, __LINE__)
#else
/* hope it's unique enough */
#define CTASSERT(x) CTASSERT_EXPN((x), l, __LINE__)
#endif /* defined(__COUNTER__) || ... */

#define CTASSERT_EXPN(x, a, b) CTASSERT_DECL(x, a, b)
#define CTASSERT_DECL(x, a, b) \
  typedef char tor_ctassert_##a##_##b[(x) ? 1 : -1] ATTR_UNUSED; EAT_SEMICOLON

#endif /* __STDC_VERSION__ >= 201112L */

#endif /* !defined(TOR_CTASSERT_H) */