e7d72e0da3fd21843b8caa0879200a4c43c38f21
[lttng-ust.git] / include / lttng / ust-compiler.h
1 #ifndef _LTTNG_UST_COMPILER_H
2 #define _LTTNG_UST_COMPILER_H
3
4 /*
5 * Copyright 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Paul Woegerer <paul_woegerer@mentor.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <assert.h>
28
29 #define lttng_ust_notrace __attribute__((no_instrument_function))
30 #define LTTNG_PACKED __attribute__((__packed__))
31
32 /*
33 * Clang supports the no_sanitize variable attribute on global variables.
34 * GCC only supports the no_sanitize_address function attribute, which is
35 * not what we need.
36 */
37 #if defined(__clang__)
38 # if __has_feature(address_sanitizer)
39 # define __lttng_ust_variable_attribute_no_sanitize_address \
40 __attribute__((no_sanitize("address")))
41 # else
42 # define __lttng_ust_variable_attribute_no_sanitize_address
43 # endif
44 #else
45 # define __lttng_ust_variable_attribute_no_sanitize_address
46 #endif
47
48 /*
49 * g++ 4.8 and prior do not support C99 compound literals. Therefore,
50 * force allocating those on the heap with these C++ compilers.
51 */
52 #if defined (__cplusplus) && defined (__GNUC__) && \
53 (__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 8))
54 # ifndef LTTNG_ALLOCATE_COMPOUND_LITERAL_ON_HEAP
55 # define LTTNG_ALLOCATE_COMPOUND_LITERAL_ON_HEAP
56 # endif
57 #endif
58
59 /*
60 * Compound literals with static storage are needed by LTTng.
61 * Compound literals are part of the C99 and C11 standards, but not
62 * part of the C++ standards. However, those are supported by both g++ and
63 * clang. In order to be strictly C++11 compliant, defining
64 * LTTNG_ALLOCATE_COMPOUND_LITERAL_ON_HEAP before including this header
65 * allocates those on the heap in C++.
66 *
67 * Example use:
68 * static struct mystruct *var = __LTTNG_COMPOUND_LITERAL(struct mystruct, { 1, 2, 3 });
69 */
70 #if defined (__cplusplus) && defined (LTTNG_ALLOCATE_COMPOUND_LITERAL_ON_HEAP)
71 #define __LTTNG_COMPOUND_LITERAL(type, ...) new (type) __VA_ARGS__
72 #else
73 #define __LTTNG_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ }
74 #endif
75
76 /*
77 * Compile time assertion.
78 * - predicate: boolean expression to evaluate,
79 * - msg: string to print to the user on failure when `static_assert()` is
80 * supported,
81 * - c_identifier_msg: message to be included in the typedef to emulate a
82 * static assertion. This parameter must be a valid C identifier as it will
83 * be used as a typedef name.
84 */
85 #if defined (__cplusplus) || __STDC_VERSION__ >= 201112L
86 #define lttng_static_assert(predicate, msg, c_identifier_msg) \
87 static_assert(predicate, msg)
88 #else
89 /*
90 * Evaluates the predicate and emit a compilation error on failure.
91 *
92 * If the predicate evaluates to true, this macro emits a typedef of an array
93 * of size 0.
94 *
95 * If the predicate evaluates to false, this macro emits a typedef of an array
96 * of negative size which is invalid in C and forces a compiler error. The msg
97 * parameter is used in the tentative typedef so it is printed to the user.
98 */
99 #define lttng_static_assert(predicate, msg, c_identifier_msg) \
100 typedef char lttng_static_assert_##c_identifier_msg[2*!!(predicate)-1];
101 #endif
102
103 #endif /* _LTTNG_UST_COMPILER_H */
This page took 0.030338 seconds and 3 git commands to generate.