Enable -Wconversion (#1359)

Summary:
X-link: https://github.com/facebook/react-native/pull/39291

Pull Request resolved: https://github.com/facebook/yoga/pull/1359

This enables clang warnings around potentially unsafe conversions, such as those with mismatched signedness, or ones which may lead to truncation.

This should catch issues in local development which create errors for MSVC (e.g. Dash), who's default `/W3` includes warnings akin to `-Wshorten-64-to-32`.

This full set of warnings here is a tad spammy, but probably more useful than not.

Changelog: [Internal]

Reviewed By: yungsters

Differential Revision: D48954777

fbshipit-source-id: 1ccc07b99d09d1c2d428158149698ffd04025605
This commit is contained in:
Nick Gerleman
2023-09-06 08:16:42 -07:00
committed by Facebook GitHub Bot
parent 95a7b4497e
commit aee43a53bc
17 changed files with 149 additions and 125 deletions

View File

@@ -22,12 +22,12 @@ namespace {
const int HAS_NEW_LAYOUT = 16;
union YGNodeContext {
uintptr_t edgesSet = 0;
int32_t edgesSet = 0;
void* asVoidPtr;
};
class YGNodeEdges {
uintptr_t edges_;
int32_t edges_;
public:
enum Edge {

View File

@@ -146,7 +146,7 @@ static int YGJNILogFunc(
va_list argsCopy;
va_copy(argsCopy, args);
int result = vsnprintf(nullptr, 0, format, argsCopy);
std::vector<char> buffer(1 + result);
std::vector<char> buffer(1 + static_cast<size_t>(result));
vsnprintf(buffer.data(), buffer.size(), format, args);
auto jloggerPtr =
@@ -236,7 +236,9 @@ static void jni_YGNodeInsertChildJNI(
jlong childPointer,
jint index) {
YGNodeInsertChild(
_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer), index);
_jlong2YGNodeRef(nativePointer),
_jlong2YGNodeRef(childPointer),
static_cast<uint32_t>(index));
}
static void jni_YGNodeSwapChildJNI(
@@ -246,7 +248,9 @@ static void jni_YGNodeSwapChildJNI(
jlong childPointer,
jint index) {
YGNodeSwapChild(
_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer), index);
_jlong2YGNodeRef(nativePointer),
_jlong2YGNodeRef(childPointer),
static_cast<uint32_t>(index));
}
static void jni_YGNodeSetIsReferenceBaselineJNI(
@@ -307,13 +311,13 @@ static void YGTransferLayoutOutputsRecursive(
const int arrSize = 6 + (marginFieldSet ? 4 : 0) + (paddingFieldSet ? 4 : 0) +
(borderFieldSet ? 4 : 0);
float arr[18];
arr[LAYOUT_EDGE_SET_FLAG_INDEX] = fieldFlags;
arr[LAYOUT_EDGE_SET_FLAG_INDEX] = static_cast<float>(fieldFlags);
arr[LAYOUT_WIDTH_INDEX] = YGNodeLayoutGetWidth(root);
arr[LAYOUT_HEIGHT_INDEX] = YGNodeLayoutGetHeight(root);
arr[LAYOUT_LEFT_INDEX] = YGNodeLayoutGetLeft(root);
arr[LAYOUT_TOP_INDEX] = YGNodeLayoutGetTop(root);
arr[LAYOUT_DIRECTION_INDEX] =
static_cast<jint>(YGNodeLayoutGetDirection(root));
static_cast<float>(YGNodeLayoutGetDirection(root));
if (marginFieldSet) {
arr[LAYOUT_MARGIN_START_INDEX] = YGNodeLayoutGetMargin(root, YGEdgeLeft);
arr[LAYOUT_MARGIN_START_INDEX + 1] = YGNodeLayoutGetMargin(root, YGEdgeTop);
@@ -670,9 +674,10 @@ static YGSize YGJNIMeasureFunc(
sizeof(measureResult) == 8,
"Expected measureResult to be 8 bytes, or two 32 bit ints");
int32_t wBits = 0xFFFFFFFF & (measureResult >> 32);
int32_t hBits = 0xFFFFFFFF & measureResult;
uint32_t wBits = 0xFFFFFFFF & (measureResult >> 32);
uint32_t hBits = 0xFFFFFFFF & measureResult;
// TODO: this is unsafe under strict aliasing and should use bit_cast
const float* measuredWidth = reinterpret_cast<float*>(&wBits);
const float* measuredHeight = reinterpret_cast<float*>(&hBits);

View File

@@ -14,7 +14,7 @@
#include "jni.h"
class PtrJNodeMapVanilla {
std::map<YGNodeConstRef, size_t> ptrsToIdxs_{};
std::map<YGNodeConstRef, jsize> ptrsToIdxs_{};
jobjectArray javaNodes_{};
public:
@@ -25,13 +25,13 @@ public:
using namespace facebook::yoga::vanillajni;
JNIEnv* env = getCurrentEnv();
size_t nativePointersSize = env->GetArrayLength(javaNativePointers);
std::vector<jlong> nativePointers(nativePointersSize);
jsize nativePointersSize = env->GetArrayLength(javaNativePointers);
std::vector<jlong> nativePointers(static_cast<size_t>(nativePointersSize));
env->GetLongArrayRegion(
javaNativePointers, 0, nativePointersSize, nativePointers.data());
for (size_t i = 0; i < nativePointersSize; ++i) {
ptrsToIdxs_[(YGNodeConstRef) nativePointers[i]] = i;
for (jsize i = 0; i < nativePointersSize; ++i) {
ptrsToIdxs_[(YGNodeConstRef) nativePointers[static_cast<size_t>(i)]] = i;
}
}

View File

@@ -18,7 +18,8 @@ void registerNatives(
assertNoPendingJniExceptionIf(env, !clazz);
auto result = env->RegisterNatives(clazz, methods, numMethods);
auto result =
env->RegisterNatives(clazz, methods, static_cast<int32_t>(numMethods));
assertNoPendingJniExceptionIf(env, result != JNI_OK);
}