2018-11-07 17:02:26 +01:00
|
|
|
/* Copyright (c) 2018 The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
2018-10-24 01:53:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \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)
|
|
|
|
*
|
2018-11-06 21:41:32 +01:00
|
|
|
* Trigger a compiler error if expression is false.
|
2018-10-24 01:53:39 +02:00
|
|
|
*/
|
|
|
|
#if __STDC_VERSION__ >= 201112L
|
|
|
|
|
|
|
|
/* If C11 is available, just use _Static_assert. */
|
2018-11-06 21:41:14 +01:00
|
|
|
#define CTASSERT(x) _Static_assert((x), #x)
|
2018-10-24 01:53:39 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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__)
|
2018-11-06 21:41:14 +01:00
|
|
|
#define CTASSERT(x) CTASSERT_EXPN((x), c, __COUNTER__)
|
2018-10-24 01:53:39 +02:00
|
|
|
#elif defined(__INCLUDE_LEVEL__)
|
2018-11-06 21:41:14 +01:00
|
|
|
#define CTASSERT(x) CTASSERT_EXPN((x), __INCLUDE_LEVEL__, __LINE__)
|
2018-10-24 01:53:39 +02:00
|
|
|
#else
|
2018-11-06 21:41:14 +01:00
|
|
|
/* hope it's unique enough */
|
|
|
|
#define CTASSERT(x) CTASSERT_EXPN((x), l, __LINE__)
|
2018-10-24 01:53:39 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* !defined(TOR_CTASSERT_H) */
|