1 // SPDX-FileCopyrightText: 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2011-2012 Paul Woegerer <paul_woegerer@mentor.com>
4 // SPDX-License-Identifier: MIT
6 #ifndef _LTTNG_UST_COMPILER_H
7 #define _LTTNG_UST_COMPILER_H
12 * By default, LTTng-UST uses the priority 150 for the tracepoint and probe
13 * provider constructors to trace tracepoints located within
14 * constructors/destructors with a higher priority value within the same
15 * module. This priority can be overridden by the application.
17 #ifndef LTTNG_UST_CONSTRUCTOR_PRIO
18 #define LTTNG_UST_CONSTRUCTOR_PRIO 150
21 #define lttng_ust_notrace __attribute__((no_instrument_function))
24 * Clang supports the no_sanitize variable attribute on global variables.
25 * GCC only supports the no_sanitize_address function attribute, which is
28 #if defined(__clang__)
29 # if __has_feature(address_sanitizer)
30 # define __lttng_ust_variable_attribute_no_sanitize_address \
31 __attribute__((no_sanitize("address")))
33 # define __lttng_ust_variable_attribute_no_sanitize_address
36 # define __lttng_ust_variable_attribute_no_sanitize_address
40 * g++ 4.8 and prior do not support C99 compound literals. Therefore,
41 * force allocating those on the heap with these C++ compilers.
43 #if defined (__cplusplus) && !defined (__clang__) && defined (__GNUC__) && \
44 ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 8)))
45 # ifndef LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP
46 # define LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP
51 * Compound literals with static storage are needed by LTTng.
52 * Compound literals are part of the C99 and C11 standards, but not
53 * part of the C++ standards. However, those are supported by both g++ and
54 * clang. In order to be strictly C++11 compliant, defining
55 * LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP before including this header
56 * allocates those on the heap in C++.
59 * static struct mystruct *var = LTTNG_UST_COMPOUND_LITERAL(struct mystruct, { 1, 2, 3 });
61 #if defined (__cplusplus) && defined (LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP)
62 #define LTTNG_UST_COMPOUND_LITERAL(type, ...) new (type) __VA_ARGS__
64 #define LTTNG_UST_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ }
68 * Compile time assertion.
69 * - predicate: boolean expression to evaluate,
70 * - msg: string to print to the user on failure when `static_assert()` is
72 * - c_identifier_msg: message to be included in the typedef to emulate a
73 * static assertion. This parameter must be a valid C identifier as it will
74 * be used as a typedef name.
77 #define lttng_ust_static_assert(predicate, msg, c_identifier_msg) \
78 static_assert(predicate, msg)
79 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
80 #define lttng_ust_static_assert(predicate, msg, c_identifier_msg) \
81 _Static_assert(predicate, msg)
84 * Evaluates the predicate and emit a compilation error on failure.
86 * If the predicate evaluates to true, this macro emits a function
87 * prototype with an argument type which is an array of size 0.
89 * If the predicate evaluates to false, this macro emits a function
90 * prototype with an argument type which is an array of negative size
91 * which is invalid in C and forces a compiler error. The
92 * c_identifier_msg parameter is used as the argument identifier so it
93 * is printed to the user when the error is reported.
95 #define lttng_ust_static_assert(predicate, msg, c_identifier_msg) \
96 void lttng_ust_static_assert_proto(char c_identifier_msg[2*!!(predicate)-1])
99 /* Combine two tokens. */
100 #define LTTNG_UST_COMPILER__COMBINE_TOKENS(_tokena, _tokenb) \
102 #define LTTNG_UST_COMPILER_COMBINE_TOKENS(_tokena, _tokenb) \
103 LTTNG_UST_COMPILER__COMBINE_TOKENS(_tokena, _tokenb)
105 * Wrap constructor and destructor functions to invoke them as functions with
106 * the constructor/destructor GNU C attributes, which ensures that those
107 * constructors/destructors are ordered before/after C++
108 * constructors/destructors.
110 * Wrap constructor and destructor functions as the constructor/destructor of a
111 * variable defined within an anonymous namespace when building as C++ with
112 * LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP defined. With this option,
113 * there are no guarantees that the events in C++ constructors/destructors will
116 #if defined (__cplusplus) && defined (LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP)
117 #define LTTNG_UST_DECLARE_CONSTRUCTOR_DESTRUCTOR(name, constructor_func, \
118 destructor_func, ...) \
121 namespace details { \
122 class LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, \
125 LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)() __VA_ARGS__; \
126 ~LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)() __VA_ARGS__; \
128 LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)::LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)() \
130 constructor_func(); \
132 LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)::~LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_destructor_, name)() \
141 const lttng::ust::details::LTTNG_UST_COMPILER_COMBINE_TOKENS( \
142 lttng_ust_constructor_destructor_, name) \
143 LTTNG_UST_COMPILER_COMBINE_TOKENS(name, registration_instance); \
146 #define LTTNG_UST_DECLARE_CONSTRUCTOR_DESTRUCTOR(name, constructor_func, \
147 destructor_func, ...) \
148 static void LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_, name)(void) \
149 __attribute__((constructor(LTTNG_UST_CONSTRUCTOR_PRIO))) __VA_ARGS__; \
150 static void LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_constructor_, name)(void) \
152 constructor_func(); \
154 static void LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_destructor_, name)(void) \
155 __attribute__((destructor(LTTNG_UST_CONSTRUCTOR_PRIO))) __VA_ARGS__; \
156 static void LTTNG_UST_COMPILER_COMBINE_TOKENS(lttng_ust_destructor_, name)(void) \
162 #endif /* _LTTNG_UST_COMPILER_H */
This page took 0.039989 seconds and 4 git commands to generate.