Implement a generalized mechanism for pushing/popping warnings
authorOrvid King <blah38621@gmail.com>
Wed, 27 Jan 2016 21:57:12 +0000 (13:57 -0800)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Wed, 27 Jan 2016 22:20:27 +0000 (14:20 -0800)
Summary: Folly synchronized requires disabling the shadow warning in a macro, but that doesn't work under MSVC, so abstract a mechansim out that allows them to be handled gracefully.

Reviewed By: yfeldblum

Differential Revision: D2870357

Pulled By: Orvid

fb-gh-sync-id: a4b0e425736ddd5293f020b360244554571d397f

folly/Portability.h
folly/Synchronized.h

index 8b95c31e64d55b3dea6565a38c19debd7f84f3ab..03717262bb74b0234b6904fe8a6828993b8f0da1 100644 (file)
 # define FOLLY_PACK_POP /**/
 #endif
 
+// Generalize warning push/pop.
+#if defined(_MSC_VER)
+# define FOLLY_PUSH_WARNING __pragma(warning(push))
+# define FOLLY_POP_WARNING __pragma(warning(pop))
+// Disable the GCC warnings.
+# define FOLLY_GCC_DISABLE_WARNING(warningName)
+# define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
+#elif defined(__clang__) || defined(__GNUC__)
+# define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
+# define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
+#define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
+#define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
+  FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
+#define FOLLY_GCC_DISABLE_WARNING(warningName)                       \
+  _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
+          FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
+// Disable the MSVC warnings.
+# define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
+#else
+# define FOLLY_PUSH_WARNING
+# define FOLLY_POP_WARNING
+# define FOLLY_GCC_DISABLE_WARNING(warningName)
+# define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
+#endif
+
 // portable version check
 #ifndef __GNUC_PREREQ
 # if defined __GNUC__ && defined __GNUC_MINOR__
index 668f67409c6e513f70269878986a8cd57ecf15af..7c7e029c5d0a829dfe4cc9434fd473200443ebb9 100644 (file)
@@ -682,8 +682,8 @@ void swap(Synchronized<T, M>& lhs, Synchronized<T, M>& rhs) {
  * examples.
  */
 #define SYNCHRONIZED(...)                                       \
-  _Pragma("GCC diagnostic push")                                \
-  _Pragma("GCC diagnostic ignored \"-Wshadow\"")                \
+  FOLLY_PUSH_WARNING                                            \
+  FOLLY_GCC_DISABLE_WARNING(shadow)                             \
   if (bool SYNCHRONIZED_state = false) {} else                  \
     for (auto SYNCHRONIZED_lockedPtr =                          \
            (FB_ARG_2_OR_1(__VA_ARGS__)).operator->();           \
@@ -691,7 +691,7 @@ void swap(Synchronized<T, M>& lhs, Synchronized<T, M>& rhs) {
       for (auto& FB_ARG_1(__VA_ARGS__) =                        \
              *SYNCHRONIZED_lockedPtr.operator->();              \
            !SYNCHRONIZED_state; SYNCHRONIZED_state = true)      \
-  _Pragma("GCC diagnostic pop")
+  FOLLY_POP_WARNING
 
 #define TIMED_SYNCHRONIZED(timeout, ...)                           \
   if (bool SYNCHRONIZED_state = false) {} else                     \