Commit fa67b121 authored by Kirk Shoop's avatar Kirk Shoop Committed by Facebook Github Bot

support if-constexpr macros on MSVC

Summary: change the macros to work on MSVC

Reviewed By: yfeldblum

Differential Revision: D14942488

fbshipit-source-id: 12d6814230bd9b92b54603cd711d03cd8b5f8086
parent 366747df
......@@ -42,47 +42,64 @@
* }
*/
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 5)
#define PUSHMI_CEXP_NOT_ON_WINDOWS 1
#else
#define PUSHMI_CEXP_NOT_ON_WINDOWS 0
#endif
#ifndef __has_attribute
#define PUSHMI_HAS_ATTRIBUTE(x) 0
#define PUSHMI_CEXP_HAS_ATTRIBUTE(x) 0
#else
#define PUSHMI_HAS_ATTRIBUTE(x) __has_attribute(x)
#define PUSHMI_CEXP_HAS_ATTRIBUTE(x) __has_attribute(x)
#endif
#ifndef __has_cpp_attribute
#define PUSHMI_HAS_CPP_ATTRIBUTE(x) 0
#define PUSHMI_CEXP_HAS_CPP_ATTRIBUTE(x) 0
#else
#define PUSHMI_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#define PUSHMI_CEXP_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#endif
#if PUSHMI_HAS_CPP_ATTRIBUTE(maybe_unused)
#define PUSHMI_MAYBE_UNUSED [[maybe_unused]]
#elif PUSHMI_HAS_ATTRIBUTE(__unused__) || __GNUC__
#define PUSHMI_MAYBE_UNUSED __attribute__((__unused__))
#if PUSHMI_CEXP_HAS_CPP_ATTRIBUTE(maybe_unused)
#define PUSHMI_CEXP_MAYBE_UNUSED [[maybe_unused]]
#elif PUSHMI_CEXP_HAS_ATTRIBUTE(__unused__) || __GNUC__
#define PUSHMI_CEXP_MAYBE_UNUSED __attribute__((__unused__))
#else
#define PUSHMI_MAYBE_UNUSED
#define PUSHMI_CEXP_MAYBE_UNUSED
#endif
// disable buggy compatibility warning about "requires" and "concept" being
// C++20 keywords.
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 5)
#define PUSHMI_PP_IGNORE_SHADOW_BEGIN \
#if PUSHMI_CEXP_NOT_ON_WINDOWS
#define PUSHMI_CEXP_IGNORE_SHADOW_BEGIN \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wshadow\"") \
_Pragma("GCC diagnostic ignored \"-Wshadow-local\"") \
/**/
#define PUSHMI_PP_IGNORE_SHADOW_END \
#define PUSHMI_CEXP_IGNORE_SHADOW_END \
_Pragma("GCC diagnostic pop")
#else
#define PUSHMI_PP_IGNORE_SHADOW_BEGIN
#define PUSHMI_PP_IGNORE_SHADOW_END
#define PUSHMI_CEXP_IGNORE_SHADOW_BEGIN
#define PUSHMI_CEXP_IGNORE_SHADOW_END
#endif
#define PUSHMI_COMMA ,
#define PUSHMI_CEXP_COMMA ,
#define PUSHMI_EVAL(F, ...) F(__VA_ARGS__)
#if PUSHMI_CEXP_NOT_ON_WINDOWS
#define PUSHMI_CEXP_EVAL(X, ...) X(__VA_ARGS__)
#define PUSHMI_STRIP(...) __VA_ARGS__
#define PUSHMI_CEXP_STRIP(...) __VA_ARGS__
#else
// https://stackoverflow.com/questions/5134523/msvc-doesnt-expand-va-args-correctly
#define PUSHMI_CEXP_VA_ARGS_EXPANDER_INNER(X) X
#define PUSHMI_CEXP_VA_ARGS_EXPANDER(X) PUSHMI_CEXP_VA_ARGS_EXPANDER_INNER(X)
#define PUSHMI_CEXP_EVAL(X, ...) PUSHMI_CEXP_VA_ARGS_EXPANDER(X(__VA_ARGS__))
#define PUSHMI_CEXP_STRIP(...) PUSHMI_CEXP_VA_ARGS_EXPANDER(__VA_ARGS__)
#endif
namespace folly {
namespace pushmi {
......@@ -105,22 +122,22 @@ struct id_fn {
#define PUSHMI_IF_CONSTEXPR(LIST) \
if constexpr (::folly::pushmi::detail::id_fn id = {}) { \
} else if constexpr \
PUSHMI_EVAL(PUSHMI_IF_CONSTEXPR_ELSE_, PUSHMI_IF_CONSTEXPR_IF_ LIST)
PUSHMI_CEXP_EVAL(PUSHMI_IF_CONSTEXPR_ELSE_, PUSHMI_IF_CONSTEXPR_IF_ LIST)
#define PUSHMI_IF_CONSTEXPR_RETURN(LIST)\
PUSHMI_PP_IGNORE_SHADOW_BEGIN \
PUSHMI_CEXP_IGNORE_SHADOW_BEGIN \
PUSHMI_IF_CONSTEXPR(LIST)\
PUSHMI_PP_IGNORE_SHADOW_END \
PUSHMI_CEXP_IGNORE_SHADOW_END \
/**/
#define PUSHMI_IF_CONSTEXPR_IF_(...) \
(__VA_ARGS__) PUSHMI_COMMA PUSHMI_IF_CONSTEXPR_THEN_
(__VA_ARGS__) PUSHMI_CEXP_COMMA PUSHMI_IF_CONSTEXPR_THEN_
#define PUSHMI_IF_CONSTEXPR_THEN_(...) \
({__VA_ARGS__}) PUSHMI_COMMA
({__VA_ARGS__}) PUSHMI_CEXP_COMMA
#define PUSHMI_IF_CONSTEXPR_ELSE_(A, B, C) \
A PUSHMI_STRIP B PUSHMI_IF_CONSTEXPR_ ## C
A PUSHMI_CEXP_STRIP B PUSHMI_IF_CONSTEXPR_ ## C
#define PUSHMI_IF_CONSTEXPR_else(...) \
else {__VA_ARGS__}
......@@ -130,15 +147,15 @@ struct id_fn {
#include <type_traits>
#define PUSHMI_IF_CONSTEXPR(LIST)\
PUSHMI_PP_IGNORE_SHADOW_BEGIN \
PUSHMI_EVAL(PUSHMI_IF_CONSTEXPR_ELSE_, PUSHMI_IF_CONSTEXPR_IF_ LIST)\
PUSHMI_PP_IGNORE_SHADOW_END \
PUSHMI_CEXP_IGNORE_SHADOW_BEGIN \
PUSHMI_CEXP_EVAL(PUSHMI_IF_CONSTEXPR_ELSE_, PUSHMI_IF_CONSTEXPR_IF_ LIST)\
PUSHMI_CEXP_IGNORE_SHADOW_END \
/**/
#define PUSHMI_IF_CONSTEXPR_RETURN(LIST)\
PUSHMI_PP_IGNORE_SHADOW_BEGIN \
return PUSHMI_EVAL(PUSHMI_IF_CONSTEXPR_ELSE_, PUSHMI_IF_CONSTEXPR_IF_ LIST)\
PUSHMI_PP_IGNORE_SHADOW_END \
PUSHMI_CEXP_IGNORE_SHADOW_BEGIN \
return PUSHMI_CEXP_EVAL(PUSHMI_IF_CONSTEXPR_ELSE_, PUSHMI_IF_CONSTEXPR_IF_ LIST)\
PUSHMI_CEXP_IGNORE_SHADOW_END \
/**/
#define PUSHMI_IF_CONSTEXPR_IF_(...) \
......@@ -146,7 +163,7 @@ struct id_fn {
/**/
#define PUSHMI_IF_CONSTEXPR_THEN_(...) \
([&](PUSHMI_MAYBE_UNUSED auto id)mutable->decltype(auto){__VA_ARGS__})) PUSHMI_COMMA \
([&](PUSHMI_CEXP_MAYBE_UNUSED auto id)mutable->decltype(auto){__VA_ARGS__})) PUSHMI_CEXP_COMMA \
/**/
#define PUSHMI_IF_CONSTEXPR_ELSE_(A, B) \
......@@ -154,7 +171,7 @@ struct id_fn {
/**/
#define PUSHMI_IF_CONSTEXPR_else(...) \
([&](PUSHMI_MAYBE_UNUSED auto id)mutable->decltype(auto){__VA_ARGS__});\
([&](PUSHMI_CEXP_MAYBE_UNUSED auto id)mutable->decltype(auto){__VA_ARGS__});\
/**/
namespace folly {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment