fix: __STDC_VERSION__ can be undefined in C++
[lttng-ust.git] / include / lttng / ust-compiler.h
CommitLineData
a8909ba5 1/*
c0c0989a 2 * SPDX-License-Identifier: MIT
a8909ba5 3 *
c0c0989a
MJ
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2011-2012 Paul Woegerer <paul_woegerer@mentor.com>
a8909ba5
PW
6 */
7
c0c0989a
MJ
8#ifndef _LTTNG_UST_COMPILER_H
9#define _LTTNG_UST_COMPILER_H
10
7f264068
FD
11#include <assert.h>
12
a8909ba5
PW
13#define lttng_ust_notrace __attribute__((no_instrument_function))
14
4f74bc5e
MD
15/*
16 * Clang supports the no_sanitize variable attribute on global variables.
17 * GCC only supports the no_sanitize_address function attribute, which is
18 * not what we need.
19 */
20#if defined(__clang__)
21# if __has_feature(address_sanitizer)
22# define __lttng_ust_variable_attribute_no_sanitize_address \
23 __attribute__((no_sanitize("address")))
24# else
25# define __lttng_ust_variable_attribute_no_sanitize_address
26# endif
27#else
28# define __lttng_ust_variable_attribute_no_sanitize_address
29#endif
30
e1904921
MD
31/*
32 * g++ 4.8 and prior do not support C99 compound literals. Therefore,
33 * force allocating those on the heap with these C++ compilers.
34 */
7850c5cc
MJ
35#if defined (__cplusplus) && !defined (__clang__) && defined (__GNUC__) && \
36 ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 8)))
439f90cf
MD
37# ifndef LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP
38# define LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP
e1904921
MD
39# endif
40#endif
41
7edfc172
MD
42/*
43 * Compound literals with static storage are needed by LTTng.
44 * Compound literals are part of the C99 and C11 standards, but not
45 * part of the C++ standards. However, those are supported by both g++ and
46 * clang. In order to be strictly C++11 compliant, defining
439f90cf 47 * LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP before including this header
7edfc172
MD
48 * allocates those on the heap in C++.
49 *
50 * Example use:
5defa774 51 * static struct mystruct *var = LTTNG_UST_COMPOUND_LITERAL(struct mystruct, { 1, 2, 3 });
7edfc172 52 */
439f90cf 53#if defined (__cplusplus) && defined (LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP)
5defa774 54#define LTTNG_UST_COMPOUND_LITERAL(type, ...) new (type) __VA_ARGS__
7edfc172 55#else
5defa774 56#define LTTNG_UST_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ }
7edfc172
MD
57#endif
58
7f264068
FD
59/*
60 * Compile time assertion.
61 * - predicate: boolean expression to evaluate,
62 * - msg: string to print to the user on failure when `static_assert()` is
63 * supported,
64 * - c_identifier_msg: message to be included in the typedef to emulate a
65 * static assertion. This parameter must be a valid C identifier as it will
66 * be used as a typedef name.
67 */
89350fda 68#ifdef __cplusplus
3a98c813 69#define lttng_ust_static_assert(predicate, msg, c_identifier_msg) \
7f264068 70 static_assert(predicate, msg)
3615ef97 71#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
89350fda
MD
72#define lttng_ust_static_assert(predicate, msg, c_identifier_msg) \
73 _Static_assert(predicate, msg)
7f264068
FD
74#else
75/*
76 * Evaluates the predicate and emit a compilation error on failure.
77 *
78 * If the predicate evaluates to true, this macro emits a typedef of an array
79 * of size 0.
80 *
81 * If the predicate evaluates to false, this macro emits a typedef of an array
82 * of negative size which is invalid in C and forces a compiler error. The msg
83 * parameter is used in the tentative typedef so it is printed to the user.
84 */
3a98c813 85#define lttng_ust_static_assert(predicate, msg, c_identifier_msg) \
b4c54822 86 typedef char lttng_ust_static_assert_##c_identifier_msg[2*!!(predicate)-1]
7f264068
FD
87#endif
88
08f6e282
MD
89/* Combine two tokens. */
90#define LTTNG_UST_COMPILER__COMBINE_TOKENS(_tokena, _tokenb) \
91 _tokena##_tokenb
92#define LTTNG_UST_COMPILER_COMBINE_TOKENS(_tokena, _tokenb) \
93 LTTNG_UST_COMPILER__COMBINE_TOKENS(_tokena, _tokenb)
05bfa3dc
JG
94/*
95 * Wrap constructor and destructor functions to invoke them as functions with
96 * the constructor/destructor GNU C attributes when building as C, or as the
97 * constructor/destructor of a variable defined within an anonymous namespace
98 * when building as C++.
99 */
100#ifdef __cplusplus
08f6e282
MD
101#define LTTNG_UST_DECLARE_CONSTRUCTOR_DESTRUCTOR(name, constructor_func, \
102 destructor_func, ...) \
103namespace lttng { \
104namespace ust { \
105namespace details { \
106class LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, \
107 name) { \
108public: \
801389e7
MD
109 LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)() __VA_ARGS__; \
110 ~LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)() __VA_ARGS__; \
08f6e282 111}; \
801389e7
MD
112LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)::LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)() \
113{ \
114 constructor_func(); \
115} \
116LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)::~LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)() \
117{ \
118 destructor_func(); \
119} \
08f6e282
MD
120} \
121} \
122} \
123 \
124namespace { \
125const lttng::ust::details::LTTNG_UST_COMPILER_COMBINE_TOKENS( \
126 lttng_ust_constructor_destructor_, name) \
127 LTTNG_UST_COMPILER_COMBINE_TOKENS(name, registration_instance); \
05bfa3dc
JG
128}
129#else /* __cplusplus */
08f6e282
MD
130#define LTTNG_UST_DECLARE_CONSTRUCTOR_DESTRUCTOR(name, constructor_func, \
131 destructor_func, ...) \
132 static void LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_, name)(void) \
133 __attribute__((constructor)) __VA_ARGS__; \
134 static void LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_, name)(void) \
135 { \
136 constructor_func(); \
137 } \
138 static void LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_destructor_, name)(void) \
139 __attribute__((destructor)) __VA_ARGS__; \
140 static void LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_destructor_, name)(void) \
141 { \
142 destructor_func(); \
05bfa3dc
JG
143 }
144#endif
145
a8909ba5 146#endif /* _LTTNG_UST_COMPILER_H */
This page took 0.037231 seconds and 4 git commands to generate.