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

@@ -15,6 +15,14 @@
namespace facebook::yoga {
#pragma pack(push)
#pragma pack(1)
struct LayoutResultFlags {
uint32_t direction : 2;
bool hadOverflow : 1;
};
#pragma pack(pop)
struct LayoutResults {
// This value was chosen based on empirical data:
// 98% of analyzed layouts require less than 8 entries.
@@ -27,10 +35,7 @@ struct LayoutResults {
std::array<float, 4> padding = {};
private:
static constexpr size_t directionOffset = 0;
static constexpr size_t hadOverflowOffset =
directionOffset + minimumBitCount<YGDirection>();
uint8_t flags = 0;
LayoutResultFlags flags_{};
public:
uint32_t computedFlexBasisGeneration = 0;
@@ -48,17 +53,15 @@ public:
CachedMeasurement cachedLayout{};
YGDirection direction() const {
return getEnumData<YGDirection>(flags, directionOffset);
return static_cast<YGDirection>(flags_.direction);
}
void setDirection(YGDirection direction) {
setEnumData<YGDirection>(flags, directionOffset, direction);
flags_.direction = static_cast<uint32_t>(direction) & 0x03;
}
bool hadOverflow() const { return getBooleanData(flags, hadOverflowOffset); }
void setHadOverflow(bool hadOverflow) {
setBooleanData(flags, hadOverflowOffset, hadOverflow);
}
bool hadOverflow() const { return flags_.hadOverflow; }
void setHadOverflow(bool hadOverflow) { flags_.hadOverflow = hadOverflow; }
bool operator==(LayoutResults layout) const;
bool operator!=(LayoutResults layout) const { return !(*this == layout); }

View File

@@ -6,6 +6,7 @@
*/
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <yoga/algorithm/FlexDirection.h>
@@ -264,7 +265,7 @@ YOGA_EXPORT void Node::setMeasureFunc(MeasureWithContextFn measureFunc) {
setMeasureFunc(m);
}
void Node::replaceChild(Node* child, uint32_t index) {
void Node::replaceChild(Node* child, size_t index) {
children_[index] = child;
}
@@ -272,8 +273,8 @@ void Node::replaceChild(Node* oldChild, Node* newChild) {
std::replace(children_.begin(), children_.end(), oldChild, newChild);
}
void Node::insertChild(Node* child, uint32_t index) {
children_.insert(children_.begin() + index, child);
void Node::insertChild(Node* child, size_t index) {
children_.insert(children_.begin() + static_cast<ptrdiff_t>(index), child);
}
void Node::setConfig(yoga::Config* config) {
@@ -311,24 +312,30 @@ bool Node::removeChild(Node* child) {
return false;
}
void Node::removeChild(uint32_t index) {
children_.erase(children_.begin() + index);
void Node::removeChild(size_t index) {
children_.erase(children_.begin() + static_cast<ptrdiff_t>(index));
}
void Node::setLayoutDirection(YGDirection direction) {
layout_.setDirection(direction);
}
void Node::setLayoutMargin(float margin, int index) {
layout_.margin[index] = margin;
void Node::setLayoutMargin(float margin, YGEdge edge) {
assertFatal(
edge < layout_.margin.size(), "Edge must be top/left/bottom/right");
layout_.margin[edge] = margin;
}
void Node::setLayoutBorder(float border, int index) {
layout_.border[index] = border;
void Node::setLayoutBorder(float border, YGEdge edge) {
assertFatal(
edge < layout_.border.size(), "Edge must be top/left/bottom/right");
layout_.border[edge] = border;
}
void Node::setLayoutPadding(float padding, int index) {
layout_.padding[index] = padding;
void Node::setLayoutPadding(float padding, YGEdge edge) {
assertFatal(
edge < layout_.padding.size(), "Edge must be top/left/bottom/right");
layout_.padding[edge] = padding;
}
void Node::setLayoutLastOwnerDirection(YGDirection direction) {
@@ -339,8 +346,10 @@ void Node::setLayoutComputedFlexBasis(const FloatOptional computedFlexBasis) {
layout_.computedFlexBasis = computedFlexBasis;
}
void Node::setLayoutPosition(float position, int index) {
layout_.position[index] = position;
void Node::setLayoutPosition(float position, YGEdge edge) {
assertFatal(
edge < layout_.position.size(), "Edge must be top/left/bottom/right");
layout_.position[edge] = position;
}
void Node::setLayoutComputedFlexBasisGeneration(
@@ -348,16 +357,19 @@ void Node::setLayoutComputedFlexBasisGeneration(
layout_.computedFlexBasisGeneration = computedFlexBasisGeneration;
}
void Node::setLayoutMeasuredDimension(float measuredDimension, int index) {
layout_.measuredDimensions[index] = measuredDimension;
void Node::setLayoutMeasuredDimension(
float measuredDimension,
YGDimension dimension) {
layout_.measuredDimensions[static_cast<size_t>(dimension)] =
measuredDimension;
}
void Node::setLayoutHadOverflow(bool hadOverflow) {
layout_.setHadOverflow(hadOverflow);
}
void Node::setLayoutDimension(float dimension, int index) {
layout_.dimensions[index] = dimension;
void Node::setLayoutDimension(float dimensionValue, YGDimension dimension) {
layout_.dimensions[static_cast<size_t>(dimension)] = dimensionValue;
}
// If both left and right are defined, then use left. Otherwise return +left or

View File

@@ -27,7 +27,7 @@ struct NodeFlags {
bool hasNewLayout : 1;
bool isReferenceBaseline : 1;
bool isDirty : 1;
uint8_t nodeType : 1;
uint32_t nodeType : 1;
bool measureUsesContext : 1;
bool baselineUsesContext : 1;
bool printUsesContext : 1;
@@ -181,8 +181,8 @@ public:
return resolvedDimensions_;
}
YGValue getResolvedDimension(int index) const {
return resolvedDimensions_[index];
YGValue getResolvedDimension(YGDimension dimension) const {
return resolvedDimensions_[static_cast<size_t>(dimension)];
}
static CompactValue computeEdgeValueForColumn(
@@ -257,7 +257,7 @@ public:
}
void setNodeType(YGNodeType nodeType) {
flags_.nodeType = static_cast<uint8_t>(nodeType);
flags_.nodeType = static_cast<uint32_t>(nodeType) & 0x01;
}
void setMeasureFunc(YGMeasureFunc measureFunc);
@@ -303,14 +303,16 @@ public:
void setLayoutComputedFlexBasis(const FloatOptional computedFlexBasis);
void setLayoutComputedFlexBasisGeneration(
uint32_t computedFlexBasisGeneration);
void setLayoutMeasuredDimension(float measuredDimension, int index);
void setLayoutMeasuredDimension(
float measuredDimension,
YGDimension dimension);
void setLayoutHadOverflow(bool hadOverflow);
void setLayoutDimension(float dimension, int index);
void setLayoutDimension(float dimensionValue, YGDimension dimension);
void setLayoutDirection(YGDirection direction);
void setLayoutMargin(float margin, int index);
void setLayoutBorder(float border, int index);
void setLayoutPadding(float padding, int index);
void setLayoutPosition(float position, int index);
void setLayoutMargin(float margin, YGEdge edge);
void setLayoutBorder(float border, YGEdge edge);
void setLayoutPadding(float padding, YGEdge edge);
void setLayoutPosition(float position, YGEdge edge);
void setPosition(
const YGDirection direction,
const float mainSize,
@@ -327,11 +329,11 @@ public:
void clearChildren();
/// Replaces the occurrences of oldChild with newChild
void replaceChild(Node* oldChild, Node* newChild);
void replaceChild(Node* child, uint32_t index);
void insertChild(Node* child, uint32_t index);
void replaceChild(Node* child, size_t index);
void insertChild(Node* child, size_t index);
/// Removes the first occurrence of child
bool removeChild(Node* child);
void removeChild(uint32_t index);
void removeChild(size_t index);
void cloneChildrenIfNeeded(void*);
void markDirtyAndPropagate();