Skip to content

Commit 5d9fa36

Browse files
committed
Replace raw #ifdef _MSC_VER with BOOST_CAPY_WORKAROUND macro (#144)
Add a standalone BOOST_CAPY_WORKAROUND(symbol, test) macro to detail/config.hpp, modeled on Boost.Config's BOOST_WORKAROUND. The guard mechanism evaluates to 0 when the compiler symbol is undefined, making workaround sites safe and self-documenting across compilers. Also add BOOST_CAPY_MSVC_WARNING_PUSH/DISABLE/POP helpers that use __pragma() on MSVC and expand to nothing elsewhere, replacing the repetitive 4-line #ifdef/#pragma/#endif sandwich pattern. Feature-detection sites (_MSC_VER for TLS keyword, __forceinline, RTTI, __FUNCSIG__) are intentionally left as raw checks since they are not bug workarounds.
1 parent 19821af commit 5d9fa36

13 files changed

Lines changed: 102 additions & 90 deletions

include/boost/capy/buffers/make_buffer.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
#include <type_traits>
2323
#include <vector>
2424

25-
#ifdef _MSC_VER
26-
#pragma warning(push)
27-
#pragma warning(disable: 4459)
28-
#endif
25+
BOOST_CAPY_MSVC_WARNING_PUSH
26+
BOOST_CAPY_MSVC_WARNING_DISABLE(4459)
2927

3028
namespace boost {
3129
namespace capy {
@@ -531,8 +529,6 @@ make_buffer(
531529
} // capy
532530
} // boost
533531

534-
#ifdef _MSC_VER
535-
#pragma warning(pop)
536-
#endif
532+
BOOST_CAPY_MSVC_WARNING_POP
537533

538534
#endif

include/boost/capy/delay.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ class delay_awaitable
8888
// Aligned storage for the stop callback.
8989
// Declared last: its destructor may block while
9090
// the callback accesses the members above.
91-
#ifdef _MSC_VER
92-
# pragma warning(push)
93-
# pragma warning(disable: 4324)
94-
#endif
91+
BOOST_CAPY_MSVC_WARNING_PUSH
92+
BOOST_CAPY_MSVC_WARNING_DISABLE(4324)
9593
alignas(stop_cb_t)
9694
unsigned char stop_cb_buf_[sizeof(stop_cb_t)];
97-
#ifdef _MSC_VER
98-
# pragma warning(pop)
99-
#endif
95+
BOOST_CAPY_MSVC_WARNING_POP
10096

10197
stop_cb_t& stop_cb_() noexcept
10298
{

include/boost/capy/detail/await_suspend_helper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace detail {
5151
5252
@param h The coroutine handle to transfer to.
5353
*/
54-
#ifdef _MSC_VER
54+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
5555
inline void symmetric_transfer(std::coroutine_handle<> h) noexcept
5656
{
5757
h.resume();

include/boost/capy/detail/config.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,58 @@
1515
# define BOOST_CAPY_ASSERT(expr) assert(expr)
1616
#endif
1717

18+
//------------------------------------------------
19+
//
20+
// Compiler bug workarounds
21+
//
22+
//------------------------------------------------
23+
24+
/* Standalone workaround macro modeled on Boost.Config's BOOST_WORKAROUND.
25+
26+
Guard mechanism: when a compiler symbol is not defined, the
27+
corresponding _WORKAROUND_GUARD macro is 1, which makes
28+
BOOST_CAPY_WORKAROUND evaluate to 0 on that compiler.
29+
30+
Usage:
31+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1) // any MSVC
32+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, <= 1900) // MSVC 14.0 and earlier
33+
#if BOOST_CAPY_WORKAROUND(__GNUC__, < 12) // GCC before 12
34+
*/
35+
36+
#ifndef _MSC_VER
37+
# define _MSC_VER_WORKAROUND_GUARD 1
38+
#else
39+
# define _MSC_VER_WORKAROUND_GUARD 0
40+
#endif
41+
42+
#ifndef __GNUC__
43+
# define __GNUC___WORKAROUND_GUARD 1
44+
#else
45+
# define __GNUC___WORKAROUND_GUARD 0
46+
#endif
47+
48+
#ifndef __clang_major__
49+
# define __clang_major___WORKAROUND_GUARD 1
50+
#else
51+
# define __clang_major___WORKAROUND_GUARD 0
52+
#endif
53+
54+
#define BOOST_CAPY_WORKAROUND(symbol, test) \
55+
((symbol ## _WORKAROUND_GUARD + 0 == 0) && \
56+
(symbol != 0) && (1 % (( (symbol test) ) + 1)))
57+
58+
// MSVC warning suppression helpers.
59+
// On MSVC these expand to __pragma(); elsewhere they are empty.
60+
#ifdef _MSC_VER
61+
# define BOOST_CAPY_MSVC_WARNING_PUSH __pragma(warning(push))
62+
# define BOOST_CAPY_MSVC_WARNING_DISABLE(x) __pragma(warning(disable: x))
63+
# define BOOST_CAPY_MSVC_WARNING_POP __pragma(warning(pop))
64+
#else
65+
# define BOOST_CAPY_MSVC_WARNING_PUSH
66+
# define BOOST_CAPY_MSVC_WARNING_DISABLE(x)
67+
# define BOOST_CAPY_MSVC_WARNING_POP
68+
#endif
69+
1870
// Efficient thread-local storage keyword for POD types
1971
#if !defined(BOOST_CAPY_TLS_KEYWORD)
2072
# if defined(_MSC_VER)

include/boost/capy/ex/async_event.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,11 @@ class async_event
145145
// Aligned storage for stop_cb_t. Declared last:
146146
// its destructor may block while the callback
147147
// accesses the members above.
148-
#ifdef _MSC_VER
149-
# pragma warning(push)
150-
# pragma warning(disable: 4324) // padded due to alignas
151-
#endif
148+
BOOST_CAPY_MSVC_WARNING_PUSH
149+
BOOST_CAPY_MSVC_WARNING_DISABLE(4324) // padded due to alignas
152150
alignas(stop_cb_t)
153151
unsigned char stop_cb_buf_[sizeof(stop_cb_t)];
154-
#ifdef _MSC_VER
155-
# pragma warning(pop)
156-
#endif
152+
BOOST_CAPY_MSVC_WARNING_POP
157153

158154
stop_cb_t& stop_cb_() noexcept
159155
{

include/boost/capy/ex/async_mutex.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,11 @@ class async_mutex
194194
// Aligned storage for stop_cb_t. Declared last:
195195
// its destructor may block while the callback
196196
// accesses the members above.
197-
#ifdef _MSC_VER
198-
# pragma warning(push)
199-
# pragma warning(disable: 4324) // padded due to alignas
200-
#endif
197+
BOOST_CAPY_MSVC_WARNING_PUSH
198+
BOOST_CAPY_MSVC_WARNING_DISABLE(4324) // padded due to alignas
201199
alignas(stop_cb_t)
202200
unsigned char stop_cb_buf_[sizeof(stop_cb_t)];
203-
#ifdef _MSC_VER
204-
# pragma warning(pop)
205-
#endif
201+
BOOST_CAPY_MSVC_WARNING_POP
206202

207203
stop_cb_t& stop_cb_() noexcept
208204
{

include/boost/capy/ex/detail/timer_service.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ class BOOST_CAPY_DECL
104104
void run();
105105

106106
// warning C4251: std types need to have dll-interface
107-
#ifdef _MSC_VER
108-
# pragma warning(push)
109-
# pragma warning(disable: 4251)
110-
#endif
107+
BOOST_CAPY_MSVC_WARNING_PUSH
108+
BOOST_CAPY_MSVC_WARNING_DISABLE(4251)
111109
std::mutex mutex_;
112110
std::condition_variable cv_;
113111
std::condition_variable cancel_cv_;
@@ -120,9 +118,7 @@ class BOOST_CAPY_DECL
120118
timer_id executing_id_ = 0;
121119
bool stopped_ = false;
122120
std::thread thread_;
123-
#ifdef _MSC_VER
124-
# pragma warning(pop)
125-
#endif
121+
BOOST_CAPY_MSVC_WARNING_POP
126122
};
127123

128124
} // detail

include/boost/capy/ex/execution_context.hpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,11 @@ class BOOST_CAPY_DECL
163163
service* next_ = nullptr;
164164

165165
// warning C4251: 'std::type_index' needs to have dll-interface
166-
#ifdef _MSC_VER
167-
# pragma warning(push)
168-
# pragma warning(disable: 4251)
169-
#endif
166+
BOOST_CAPY_MSVC_WARNING_PUSH
167+
BOOST_CAPY_MSVC_WARNING_DISABLE(4251)
170168
detail::type_index t0_{detail::type_id<void>()};
171169
detail::type_index t1_{detail::type_id<void>()};
172-
#ifdef _MSC_VER
173-
# pragma warning(pop)
174-
#endif
170+
BOOST_CAPY_MSVC_WARNING_POP
175171
};
176172

177173
//------------------------------------------------
@@ -503,16 +499,12 @@ class BOOST_CAPY_DECL
503499
struct BOOST_CAPY_DECL
504500
factory
505501
{
506-
#ifdef _MSC_VER
507-
# pragma warning(push)
508-
# pragma warning(disable: 4251)
509-
#endif
510502
// warning C4251: 'std::type_index' needs to have dll-interface
503+
BOOST_CAPY_MSVC_WARNING_PUSH
504+
BOOST_CAPY_MSVC_WARNING_DISABLE(4251)
511505
detail::type_index t0;
512506
detail::type_index t1;
513-
#ifdef _MSC_VER
514-
# pragma warning(pop)
515-
#endif
507+
BOOST_CAPY_MSVC_WARNING_POP
516508

517509
factory(
518510
detail::type_info const& t0_,
@@ -531,16 +523,12 @@ class BOOST_CAPY_DECL
531523
service& use_service_impl(factory& f);
532524
service& make_service_impl(factory& f);
533525

534-
#ifdef _MSC_VER
535-
# pragma warning(push)
536-
# pragma warning(disable: 4251)
537-
#endif
538-
// warning C4251: 'std::type_index' needs to have dll-interface
526+
// warning C4251: std::mutex, std::shared_ptr need dll-interface
527+
BOOST_CAPY_MSVC_WARNING_PUSH
528+
BOOST_CAPY_MSVC_WARNING_DISABLE(4251)
539529
mutable std::mutex mutex_;
540530
std::shared_ptr<void> owned_;
541-
#ifdef _MSC_VER
542-
# pragma warning(pop)
543-
#endif
531+
BOOST_CAPY_MSVC_WARNING_POP
544532
std::pmr::memory_resource* frame_alloc_ = nullptr;
545533
service* head_ = nullptr;
546534
bool shutdown_ = false;

include/boost/capy/ex/recycling_memory_resource.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ namespace capy {
4646
@see get_recycling_memory_resource
4747
@see run_async
4848
*/
49-
#ifdef _MSC_VER
50-
# pragma warning(push)
51-
# pragma warning(disable: 4275) // non dll-interface base class
52-
#endif
49+
BOOST_CAPY_MSVC_WARNING_PUSH
50+
BOOST_CAPY_MSVC_WARNING_DISABLE(4275) // non dll-interface base class
5351
class BOOST_CAPY_DECL recycling_memory_resource : public std::pmr::memory_resource
5452
{
5553
static constexpr std::size_t num_classes = 6;
@@ -184,9 +182,7 @@ class BOOST_CAPY_DECL recycling_memory_resource : public std::pmr::memory_resour
184182
return this == &other;
185183
}
186184
};
187-
#ifdef _MSC_VER
188-
# pragma warning(pop)
189-
#endif
185+
BOOST_CAPY_MSVC_WARNING_POP
190186

191187
/** Returns pointer to the default recycling memory resource.
192188

include/boost/capy/io_result.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct [[nodiscard]] io_result<>
6363
/** The error code from the operation. */
6464
std::error_code ec;
6565

66-
#ifdef _MSC_VER
66+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
6767
// Tuple protocol (unconditional - io_result<> is not an aggregate)
6868
template<std::size_t I>
6969
auto& get() & noexcept
@@ -108,7 +108,7 @@ struct [[nodiscard]] io_result<T1>
108108
/// The first payload value. Unspecified when `ec` is set.
109109
T1 t1{};
110110

111-
#ifdef _MSC_VER
111+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
112112
template<std::size_t I>
113113
auto& get() & noexcept
114114
{
@@ -164,7 +164,7 @@ struct [[nodiscard]] io_result<T1, T2>
164164
/// The second payload value. Unspecified when `ec` is set.
165165
T2 t2{};
166166

167-
#ifdef _MSC_VER
167+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
168168
template<std::size_t I>
169169
auto& get() & noexcept
170170
{
@@ -227,7 +227,7 @@ struct [[nodiscard]] io_result<T1, T2, T3>
227227
/// The third payload value. Unspecified when `ec` is set.
228228
T3 t3{};
229229

230-
#ifdef _MSC_VER
230+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
231231
template<std::size_t I>
232232
auto& get() & noexcept
233233
{
@@ -260,7 +260,7 @@ struct [[nodiscard]] io_result<T1, T2, T3>
260260
#endif
261261
};
262262

263-
#ifdef _MSC_VER
263+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
264264

265265
// Free-standing get() overloads for ADL (MSVC aggregate workaround).
266266
/// @cond
@@ -339,12 +339,12 @@ auto&& get(io_result<T1, T2, T3>&& r) noexcept
339339

340340
/// @endcond
341341

342-
#endif // _MSC_VER
342+
#endif // BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
343343

344344
} // namespace capy
345345
} // namespace boost
346346

347-
#ifdef _MSC_VER
347+
#if BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
348348

349349
// Tuple protocol for structured bindings (MSVC workaround)
350350
// MSVC has a bug with aggregate decomposition in coroutines, so we use
@@ -448,6 +448,6 @@ struct tuple_element<3, boost::capy::io_result<T1, T2, T3>>
448448

449449
} // namespace std
450450

451-
#endif // _MSC_VER
451+
#endif // BOOST_CAPY_WORKAROUND(_MSC_VER, >= 1)
452452

453453
#endif // BOOST_CAPY_IO_RESULT_HPP

0 commit comments

Comments
 (0)