Skip to content

Commit

Permalink
(Attempt to) Fix compilation of new props parsing for MSVC
Browse files Browse the repository at this point in the history
Summary:
MSVC doesn't like Clang macros. Oops!

Constraints with this bit of code:

1. I'm trying to enforce constexpr in the most obvious, intuitive way possible. Macros are ugly but a "proper" solution would result in 10x as much code that is, totally subjectively, less readable to me.
2. This is evaluating at compile-time a hash of a string which is usually used in the `case` line of a switch statement.

For now I'm just hoping that MSVC will allow us to shadow `hash` which Xcode doesn't like. We might need to add a special pragma(s) for MSVC if it still doesn't like this.

Changelog: [Internal]

Reviewed By: lyahdav

Differential Revision: D37529949

fbshipit-source-id: 9aa605a9786bf5d194819ef8dade778875ae744e
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Jun 29, 2022
1 parent 24560b6 commit d1d11c7
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions ReactCommon/react/renderer/core/PropsMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@

#include <react/renderer/core/RawPropsPrimitives.h>

// We need to use clang pragmas inside of a macro below,
// so we need to pull out the "if" statement here.
#if __clang__
#define CLANG_PRAGMA(s) _Pragma(s)
#else
#define CLANG_PRAGMA(s)
#endif

// Get hash at compile-time. sizeof(str) - 1 == strlen
// Auto-formatting makes this more ugly than it has to be, but it works.
// The pragma is to ignore warnings about variable shadowing of `len` and/or
// `hash`, which are technically shadowing but it doesn't matter since they're
// constexpr'd.
#define CONSTEXPR_RAW_PROPS_KEY_HASH(s) \
({ \
_Pragma("clang diagnostic push") _Pragma( \
"clang diagnostic ignored \"-Wshadow\"") constexpr RawPropsPropNameLength \
len = sizeof(s) - 1; \
constexpr RawPropsPropNameHash hash = folly::hash::fnv32_buf(s, len); \
hash; \
_Pragma("clang diagnostic pop") \
#define CONSTEXPR_RAW_PROPS_KEY_HASH(s) \
({ \
CLANG_PRAGMA("clang diagnostic push") \
CLANG_PRAGMA("clang diagnostic ignored \"-Wshadow\"") \
constexpr RawPropsPropNameHash propNameHash = \
folly::hash::fnv32_buf(s, sizeof(s) - 1); \
propNameHash; \
CLANG_PRAGMA("clang diagnostic pop") \
})

#define RAW_PROPS_KEY_HASH(s) folly::hash::fnv32_buf(s, std::strlen(s))
Expand Down

0 comments on commit d1d11c7

Please sign in to comment.