HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SYS_Compiler.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SYS_Compiler.h (SYS Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __SYS_COMPILER_H_INCLUDED__
12 #define __SYS_COMPILER_H_INCLUDED__
13 
14 #if defined(__GNUC__)
15  #define SYS_IS_GCC_GE(MAJOR, MINOR) \
16  (__GNUC__ > MAJOR || (__GNUC__ == MAJOR && __GNUC_MINOR__ >= MINOR))
17  #define SYS_IS_GCC_EQ(MAJOR, MINOR) \
18  (__GNUC__ == MAJOR && __GNUC_MINOR__ == MINOR)
19 #else
20  #define SYS_IS_GCC_GE(MAJOR, MINOR) 0
21  #define SYS_IS_GCC_EQ(MAJOR, MINOR) 0
22 #endif
23 
24 #if defined(__clang__)
25  #define SYS_IS_CLANG_GE(MAJOR, MINOR) \
26  (__clang_major__ > MAJOR || (__clang_major__ == MAJOR && __clang_minor__ >= MINOR))
27 #else
28  #define SYS_IS_CLANG_GE(MAJOR, MINOR) 0
29 #endif
30 
31 #if (__cplusplus >= 201402) || (defined(_MSC_VER) && _MSVC_LANG >= 201402)
32  #define SYS_HAS_CXX14 1
33 #else
34  #define SYS_HAS_CXX14 0
35 #endif
36 
37 // Token concatenation
38 #define SYS_CONCAT(a, b) SYS_CONCAT_(a, b)
39 #define SYS_CONCAT_(a, b) a ## b
40 
41 #define SYS_TO_STRING_(a) # a
42 #define SYS_TO_STRING(a) SYS_TO_STRING_(a)
43 
44 // Handy macro to add warnings (for fix-me's etc.)
45 #if defined(_MSC_VER)
46 #define SYS_MESSAGE(desc) __pragma(message(__FILE__ "(" SYS_TO_STRING(__LINE__) ") : Warning: " #desc))
47 #else
48 #define SYS_MESSAGE(__msg__) _Pragma(SYS_TO_STRING(message __msg__))
49 #endif
50 
51 // MSVC 2012 does not define the 'noexcept' keyword.
52 #if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 180021114
53 #define SYS_NOEXCEPT
54 #else
55 #define SYS_NOEXCEPT noexcept
56 #endif
57 
58 // Define "explicit" for C++11 safe-bool operator
59 #if (__cplusplus >= 201103) || (_MSC_VER >= 1800)
60  #define SYS_SAFE_BOOL explicit
61 #else
62  #define SYS_SAFE_BOOL
63 #endif
64 
65 // Define the [[fallthrough]] and [[maybe_unused]] attributes.
66 #if defined(__has_cpp_attribute)
67  #if __has_cpp_attribute(fallthrough) == 201603
68  #define SYS_FALLTHROUGH [[fallthrough]]
69  #endif
70  #if __has_cpp_attribute(maybe_unused) == 201603
71  #define SYS_MAYBE_UNUSED [[maybe_unused]]
72  #endif
73 #endif
74 #if !defined(SYS_FALLTHROUGH)
75  #define SYS_FALLTHROUGH
76 #endif
77 #if !defined(SYS_MAYBE_UNUSED)
78  #define SYS_MAYBE_UNUSED
79 #endif
80 
81 // gcc 7 & 8 produce warnings about ignored attributes when using maybe_unused
82 // on a member variable.
83 #if SYS_IS_GCC_GE(7, 0)
84  #define SYS_MAYBE_UNUSED_MEMBER
85 #else
86  #define SYS_MAYBE_UNUSED_MEMBER SYS_MAYBE_UNUSED
87 #endif
88 
89 #if defined(__has_cpp_attribute)
90  #if __has_cpp_attribute(nodiscard) == 201603
91  #define SYS_NO_DISCARD_RESULT [[nodiscard]]
92  #endif
93 #elif __cplusplus >= 201703L // We live in the future and using c++17 or later!
94  #define SYS_NO_DISCARD_RESULT [[nodiscard]]
95 #endif
96 #if !defined(SYS_NO_DISCARD_RESULT)
97  #if defined(__GNUC__)
98  #define SYS_NO_DISCARD_RESULT __attribute__((warn_unused_result))
99  #else
100  #define SYS_NO_DISCARD_RESULT
101  #endif
102 #endif
103 
104 // Use to mark declared variables as unused. This is typically for
105 // static function variables which are declared just to run initialization
106 // code, or if the variable is used only in debug builds.
107 #if defined(__GNUC__)
108 # define SYS_UNUSED_VAR_ATTRIB __attribute__((unused))
109 #else
110 # define SYS_UNUSED_VAR_ATTRIB
111 #endif
112 
113 // Apple dropped the ball on implementing C++11 thread_local in clang on OSX.
114 // Cited reason was that they thought they could do better than previous
115 // implementation, but then never did.
116 // See: https://devforums.apple.com/message/1079348#1079348
117 #if !(defined(__APPLE__) && defined(__clang__))
118  #define SYS_THREAD_LOCAL thread_local
119 #else
120  #define SYS_THREAD_LOCAL __thread
121 #endif
122 
123 // Instruct the compiler to assume that the expression given will be true and
124 // use that information to guide optimizations.
125 #if defined(_MSC_VER)
126  #define SYS_ASSUME(EXPR) __assume(EXPR)
127 #elif defined(__clang__)
128  #if __has_builtin(__builtin_assume)
129  #define SYS_ASSUME(EXPR) __builtin_assume(EXPR)
130  #else
131  #define SYS_ASSUME(EXPR) if (!(EXPR)) __builtin_unreachable()
132  #endif
133 #else
134  #define SYS_ASSUME(EXPR) if (!(EXPR)) __builtin_unreachable()
135 #endif
136 
137 // Defines to disable compiler sanitizer checks
138 #if defined(__has_feature)
139  #if __has_feature(thread_sanitizer)
140  #define SYS_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
141  #endif
142  #if __has_feature(memory_sanitizer)
143  #define SYS_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
144  #endif
145  #if __has_feature(address_sanitizer)
146  #define SYS_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
147  #endif
148 #endif
149 #if !defined(SYS_NO_SANITIZE_THREAD)
150  #define SYS_NO_SANITIZE_THREAD
151 #endif
152 #if !defined(SYS_NO_SANITIZE_MEMORY)
153  #define SYS_NO_SANITIZE_MEMORY
154 #endif
155 #if !defined(SYS_NO_SANITIZE_ADDRESS)
156  #define SYS_NO_SANITIZE_ADDRESS
157 #endif
158 
159 #endif // __SYS_COMPILER_H_INCLUDED__