COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
CompileControl.h
Go to the documentation of this file.
1//
2// Copyright 2015 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
12
13#pragma once
14
15#ifdef __GNUC__
16
17#define GCC_DIAG_STR(s) #s
18#define GCC_DIAG_JOINSTR(x,y) GCC_DIAG_STR(x ## y)
19#define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
20#define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
21#define CC_DISABLE_WARNINGS() \
22 GCC_DIAG_PRAGMA(push) \
23 GCC_DIAG_PRAGMA(ignored "-Wpedantic") \
24 GCC_DIAG_PRAGMA(ignored "-Wctor-dtor-privacy")\
25 GCC_DIAG_PRAGMA(ignored "-Wold-style-cast") \
26 GCC_DIAG_PRAGMA(ignored "-Wconversion")
27
28#define CC_ENABLE_WARNINGS() GCC_DIAG_PRAGMA(pop)
29
30#define COMMS_GNU_WARNING_PUSH GCC_DIAG_PRAGMA(push)
31#define COMMS_GNU_WARNING_DISABLE(x_) GCC_DIAG_PRAGMA(ignored x_)
32#define COMMS_GNU_WARNING_POP GCC_DIAG_PRAGMA(pop)
33
34#else
35
36#define CC_DISABLE_WARNINGS()
37#define CC_ENABLE_WARNINGS()
38#define COMMS_GNU_WARNING_PUSH
39#define COMMS_GNU_WARNING_DISABLE(x_)
40#define COMMS_GNU_WARNING_POP
41
42#endif
43
44#define COMMS_IS_MSVC false
45#define COMMS_IS_GCC false
46#define COMMS_IS_CLANG false
47#define COMMS_IS_USING_GNUC false
48
49#if defined(__GNUC__)
50#undef COMMS_IS_USING_GNUC
51#define COMMS_IS_USING_GNUC true
52#endif // #if defined(__GNUC__) && !defined(__clang__)
53
54#if defined(_MSC_VER) && !defined(__clang__)
55#undef COMMS_IS_MSVC
56#define COMMS_IS_MSVC true
57#endif // #if defined(_MSC_VER) && !defined(__clang__)
58
59#if defined(__GNUC__) && !defined(__clang__)
60#undef COMMS_IS_GCC
61#define COMMS_IS_GCC true
62#endif // #if defined(__GNUC__) && !defined(__clang__)
63
64#if defined(__clang__)
65
66#undef COMMS_IS_CLANG
67#define COMMS_IS_CLANG true
68#endif // #if defined(__clang__)
69
70#define COMMS_IS_GCC_47_OR_BELOW (COMMS_IS_GCC && (__GNUC__ == 4) && (__GNUC_MINOR__ < 8))
71#define COMMS_IS_GCC_9 (COMMS_IS_GCC && (__GNUC__ == 9))
72#define COMMS_IS_GCC_9_OR_BELOW (COMMS_IS_GCC_9 && (__GNUC__ <= 9))
73#define COMMS_IS_GCC_9_OR_ABOVE (COMMS_IS_GCC_9 && (__GNUC__ >= 9))
74#define COMMS_IS_GCC_10 (COMMS_IS_GCC && (__GNUC__ == 10))
75#define COMMS_IS_GCC_10_OR_ABOVE (COMMS_IS_GCC && (__GNUC__ >= 10))
76#define COMMS_IS_GCC_10_OR_BELOW (COMMS_IS_GCC && (__GNUC__ <= 10))
77#define COMMS_IS_GCC_11 (COMMS_IS_GCC && (__GNUC__ == 11))
78#define COMMS_IS_GCC_11_OR_ABOVE (COMMS_IS_GCC && (__GNUC__ >= 11))
79#define COMMS_IS_GCC_12 (COMMS_IS_GCC && (__GNUC__ == 12))
80#define COMMS_IS_GCC_12_OR_ABOVE (COMMS_IS_GCC && (__GNUC__ >= 12))
81#define COMMS_IS_GCC_13 (COMMS_IS_GCC && (__GNUC__ == 13))
82#define COMMS_IS_GCC_14 (COMMS_IS_GCC && (__GNUC__ == 14))
83#define COMMS_IS_GCC_14_OR_BELOW (COMMS_IS_GCC && (__GNUC__ <= 14))
84#define COMMS_IS_CLANG_7_OR_ABOVE (COMMS_IS_CLANG && (__clang_major__ >= 7))
85#define COMMS_IS_CLANG_8 (COMMS_IS_CLANG && (__clang_major__ == 8))
86#define COMMS_IS_CLANG_8_OR_BELOW (COMMS_IS_CLANG && (__clang_major__ <= 8))
87#define COMMS_IS_CLANG_9_OR_BELOW (COMMS_IS_CLANG && (__clang_major__ <= 9))
88#define COMMS_IS_CLANG_9_OR_ABOVE (COMMS_IS_CLANG && (__clang_major__ >= 9))
89#define COMMS_IS_CLANG_16_OR_ABOVE (COMMS_IS_CLANG && (__clang_major__ >= 16))
90#define COMMS_IS_CLANG_18_OR_BELOW (COMMS_IS_CLANG && (__clang_major__ <= 18))
91#define COMMS_IS_CLANG_19_OR_BELOW (COMMS_IS_CLANG && (__clang_major__ <= 19))
92#define COMMS_IS_MSVC_2026 (COMMS_IS_MSVC && (_MSC_VER >= 1945) && (_MSC_VER < 1949))
93#define COMMS_IS_MSVC_2026_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1949))
94#define COMMS_IS_MSVC_2025 (COMMS_IS_MSVC && (_MSC_VER >= 1940) && (_MSC_VER < 1945))
95#define COMMS_IS_MSVC_2025_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1945))
96#define COMMS_IS_MSVC_2022 (COMMS_IS_MSVC && (_MSC_VER >= 1930) && (_MSC_VER < 1940))
97#define COMMS_IS_MSVC_2019 (COMMS_IS_MSVC && (_MSC_VER >= 1920) && (_MSC_VER < 1930))
98#define COMMS_IS_MSVC_2019_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1930))
99#define COMMS_IS_MSVC_2017_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1920))
100#define COMMS_IS_MSVC_2015_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1910))
101
102#if !defined(COMMS_COMPILER_GCC47) && COMMS_IS_GCC_47_OR_BELOW
103#define COMMS_COMPILER_GCC47
104#endif
105
106#define COMMS_IS_CPP14 (__cplusplus >= 201402L)
107#define COMMS_IS_CPP17 (__cplusplus >= 201703L)
108#define COMMS_IS_CPP20 (__cplusplus >= 202002L)
109#define COMMS_IS_CPP23 (__cplusplus >= 202302L)
110
111#if COMMS_IS_MSVC_2025_OR_BELOW
112#undef COMMS_IS_CPP23
113#define COMMS_IS_CPP23 (__cplusplus >= 202003L)
114#endif
115
116#if COMMS_IS_MSVC_2019_OR_BELOW
117#undef COMMS_IS_CPP20
118#define COMMS_IS_CPP20 (__cplusplus >= 201704L)
119#endif
120
121#if COMMS_IS_GCC_10_OR_BELOW
122#undef COMMS_IS_CPP20
123#define COMMS_IS_CPP20 (__cplusplus >= 201709L)
124#endif
125
126#if COMMS_IS_CLANG_9_OR_BELOW
127#undef COMMS_IS_CPP20
128#define COMMS_IS_CPP20 (__cplusplus >= 201709L)
129#endif
130
131#if COMMS_IS_CLANG_8_OR_BELOW
132#undef COMMS_IS_CPP20
133#define COMMS_IS_CPP20 (__cplusplus >= 201707L)
134#endif
135
136#define COMMS_CLANG_HAS_STRING_VIEW false
137#define COMMS_CLANG_HAS_VERSION_HEADER false
138
139#if COMMS_IS_CLANG
140
141// The defines below are seperate because VS2015 doesn't
142// behave well with angle brackets inside macro arguments.
143
144#undef COMMS_CLANG_HAS_STRING_VIEW
145#define COMMS_CLANG_HAS_STRING_VIEW (__has_include(<string_view>))
146
147#undef COMMS_CLANG_HAS_VERSION_HEADER
148#define COMMS_CLANG_HAS_VERSION_HEADER (__has_include(<version>))
149
150#endif // #if COMMS_IS_CLANG
151
152#define COMMS_HAS_CPP20_VERSION_HEADER \
153 COMMS_IS_CPP20 && \
154 (\
155 (COMMS_IS_USING_GNUC && (__GNUC__ >= 9)) || \
156 (COMMS_IS_CLANG && COMMS_CLANG_HAS_VERSION_HEADER) || \
157 (COMMS_IS_MSVC && (_MSC_VER >= 1922)) \
158 )
159
160#if COMMS_HAS_CPP20_VERSION_HEADER
161#include <version>
162#endif
163
164#define COMMS_HAS_CPP17_STRING_VIEW false
165
166#ifndef COMMS_NO_CPP17_STRING_VIEW
167#undef COMMS_HAS_CPP17_STRING_VIEW
168#define COMMS_HAS_CPP17_STRING_VIEW \
169 COMMS_IS_CPP17 && \
170 (\
171 (COMMS_IS_USING_GNUC && (__GNUC__ >= 7)) || \
172 (COMMS_IS_CLANG && COMMS_CLANG_HAS_STRING_VIEW) || \
173 (COMMS_IS_MSVC && (_MSC_VER >= 1910)) \
174 )
175
176#endif // #ifndef COMMS_NO_CPP17_STRING_VIEW
177
178#define COMMS_HAS_CPP20_SPAN false
179#if !defined(COMMS_NO_CPP20_SPAN) && COMMS_IS_CPP20 && defined(__cpp_lib_span)
180#undef COMMS_HAS_CPP20_SPAN
181#define COMMS_HAS_CPP20_SPAN true
182#endif // #if COMMS_IS_CPP20 && defined(__cpp_lib_span)
183
184#if COMMS_IS_MSVC
185
186#define COMMS_MSVC_WARNING_PRAGMA(s_) __pragma(s_)
187#define COMMS_MSVC_WARNING_PUSH __pragma(warning(push))
188#define COMMS_MSVC_WARNING_POP __pragma(warning(pop))
189#define COMMS_MSVC_WARNING_DISABLE(w_) __pragma(warning(disable:w_))
190#define COMMS_MSVC_WARNING_SUPPRESS(w_) __pragma(warning(suppress:w_))
191
192#else // #if COMMS_IS_MSVC
193
194#define COMMS_MSVC_WARNING_PRAGMA(s_)
195#define COMMS_MSVC_WARNING_PUSH
196#define COMMS_MSVC_WARNING_POP
197#define COMMS_MSVC_WARNING_DISABLE(w_)
198#define COMMS_MSVC_WARNING_SUPPRESS(w_)
199
200#endif // #if COMMS_IS_MSVC
201