Skip to content

Commit

Permalink
Enable -Wconversion (facebook#39291)
Browse files Browse the repository at this point in the history
Summary:

X-link: facebook/yoga#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
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Sep 5, 2023
1 parent d468d9d commit 12e107a
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_compile_options(
-Wall
-Wextra
-Werror
-Wconversion
# Disable RTTI
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
# Use -O2 (prioritize speed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,8 @@ static void zeroOutLayoutRecursively(
yoga::Node* const node,
void* layoutContext) {
node->getLayout() = {};
node->setLayoutDimension(0, 0);
node->setLayoutDimension(0, 1);
node->setLayoutDimension(0, YGDimensionWidth);
node->setLayoutDimension(0, YGDimensionHeight);
node->setHasNewLayout(true);

node->iterChildrenAfterCloningIfNeeded(
Expand Down Expand Up @@ -1488,19 +1488,19 @@ static void YGJustifyMainAxis(
betweenMainDim +=
yoga::maxOrDefined(
collectedFlexItemsValues.remainingFreeSpace, 0) /
(collectedFlexItemsValues.itemsOnLine - 1);
static_cast<float>(collectedFlexItemsValues.itemsOnLine - 1);
}
break;
case YGJustifySpaceEvenly:
// Space is distributed evenly across all elements
leadingMainDim = collectedFlexItemsValues.remainingFreeSpace /
(collectedFlexItemsValues.itemsOnLine + 1);
static_cast<float>(collectedFlexItemsValues.itemsOnLine + 1);
betweenMainDim += leadingMainDim;
break;
case YGJustifySpaceAround:
// Space on the edges is half of the space between elements
leadingMainDim = 0.5f * collectedFlexItemsValues.remainingFreeSpace /
collectedFlexItemsValues.itemsOnLine;
static_cast<float>(collectedFlexItemsValues.itemsOnLine);
betweenMainDim += leadingMainDim * 2;
break;
case YGJustifyFlexStart:
Expand Down Expand Up @@ -1550,7 +1550,7 @@ static void YGJustifyMainAxis(
if (child->marginLeadingValue(mainAxis).unit == YGUnitAuto) {
collectedFlexItemsValues.mainDim +=
collectedFlexItemsValues.remainingFreeSpace /
numberOfAutoMarginsOnCurrentLine;
static_cast<float>(numberOfAutoMarginsOnCurrentLine);
}

if (performLayout) {
Expand All @@ -1563,7 +1563,7 @@ static void YGJustifyMainAxis(
if (child->marginTrailingValue(mainAxis).unit == YGUnitAuto) {
collectedFlexItemsValues.mainDim +=
collectedFlexItemsValues.remainingFreeSpace /
numberOfAutoMarginsOnCurrentLine;
static_cast<float>(numberOfAutoMarginsOnCurrentLine);
}
bool canSkipFlex =
!performLayout && measureModeCrossDim == YGMeasureModeExactly;
Expand Down Expand Up @@ -1890,7 +1890,7 @@ static void calculateLayoutImpl(
if (childCount > 1) {
totalMainDim +=
node->getGapForAxis(mainAxis, availableInnerCrossDim).unwrap() *
(childCount - 1);
static_cast<float>(childCount - 1);
}

const bool mainAxisOverflows =
Expand Down Expand Up @@ -2261,22 +2261,26 @@ static void calculateLayoutImpl(
break;
case YGAlignStretch:
if (availableInnerCrossDim > totalLineCrossDim) {
crossDimLead = remainingAlignContentDim / lineCount;
crossDimLead =
remainingAlignContentDim / static_cast<float>(lineCount);
}
break;
case YGAlignSpaceAround:
if (availableInnerCrossDim > totalLineCrossDim) {
currentLead += remainingAlignContentDim / (2 * lineCount);
currentLead +=
remainingAlignContentDim / (2 * static_cast<float>(lineCount));
if (lineCount > 1) {
crossDimLead = remainingAlignContentDim / lineCount;
crossDimLead =
remainingAlignContentDim / static_cast<float>(lineCount);
}
} else {
currentLead += remainingAlignContentDim / 2;
}
break;
case YGAlignSpaceBetween:
if (availableInnerCrossDim > totalLineCrossDim && lineCount > 1) {
crossDimLead = remainingAlignContentDim / (lineCount - 1);
crossDimLead =
remainingAlignContentDim / static_cast<float>(lineCount - 1);
}
break;
case YGAlignAuto:
Expand Down Expand Up @@ -2843,11 +2847,11 @@ bool calculateLayoutInternal(
layout->lastOwnerDirection = ownerDirection;

if (cachedResults == nullptr) {
if (layout->nextCachedMeasurementsIndex + 1 >
(uint32_t) layoutMarkerData.maxMeasureCache) {
layoutMarkerData.maxMeasureCache =
layout->nextCachedMeasurementsIndex + 1;
}

layoutMarkerData.maxMeasureCache = std::max(
layoutMarkerData.maxMeasureCache,
layout->nextCachedMeasurementsIndex + 1u);

if (layout->nextCachedMeasurementsIndex ==
LayoutResults::MaxCachedMeasurements) {
if (gPrintChanges) {
Expand Down
27 changes: 10 additions & 17 deletions packages/react-native/ReactCommon/yoga/yoga/bits/NumericBitfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

namespace facebook::yoga::details {

constexpr size_t log2ceilFn(size_t n) {
constexpr uint8_t log2ceilFn(uint8_t n) {
return n < 1 ? 0 : (1 + log2ceilFn(n / 2));
}

constexpr int mask(size_t bitWidth, size_t index) {
return ((1 << bitWidth) - 1) << index;
constexpr uint32_t mask(uint8_t bitWidth, uint8_t index) {
return ((1u << bitWidth) - 1u) << index;
}

} // namespace facebook::yoga::details
Expand All @@ -29,38 +29,31 @@ namespace facebook::yoga {

// The number of bits necessary to represent enums defined with YG_ENUM_SEQ_DECL
template <typename Enum>
constexpr size_t minimumBitCount() {
constexpr uint8_t minimumBitCount() {
static_assert(
enums::count<Enum>() > 0, "Enums must have at least one entries");
return details::log2ceilFn(enums::count<Enum>() - 1);
}

template <typename Enum>
constexpr Enum getEnumData(int flags, size_t index) {
constexpr Enum getEnumData(uint32_t flags, uint8_t index) {
return static_cast<Enum>(
(flags & details::mask(minimumBitCount<Enum>(), index)) >> index);
}

template <typename Enum>
void setEnumData(uint32_t& flags, size_t index, int newValue) {
flags = (flags & ~details::mask(minimumBitCount<Enum>(), index)) |
((newValue << index) & (details::mask(minimumBitCount<Enum>(), index)));
}

template <typename Enum>
void setEnumData(uint8_t& flags, size_t index, int newValue) {
void setEnumData(uint32_t& flags, uint8_t index, uint32_t newValue) {
flags =
(flags &
~static_cast<uint8_t>(details::mask(minimumBitCount<Enum>(), index))) |
((newValue << index) &
(static_cast<uint8_t>(details::mask(minimumBitCount<Enum>(), index))));
~static_cast<uint32_t>(details::mask(minimumBitCount<Enum>(), index))) |
((newValue << index) & (details::mask(minimumBitCount<Enum>(), index)));
}

constexpr bool getBooleanData(int flags, size_t index) {
constexpr bool getBooleanData(uint32_t flags, uint8_t index) {
return (flags >> index) & 1;
}

inline void setBooleanData(uint8_t& flags, size_t index, bool value) {
inline void setBooleanData(uint32_t& flags, uint8_t index, bool value) {
if (value) {
flags |= 1 << index;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void appendFormattedString(std::string& str, const char* fmt, ...) {
va_start(args, fmt);
va_list argsCopy;
va_copy(argsCopy, args);
std::vector<char> buf(1 + vsnprintf(NULL, 0, fmt, args));
std::vector<char> buf(1 + static_cast<size_t>(vsnprintf(NULL, 0, fmt, args)));
va_end(args);
vsnprintf(buf.data(), buf.size(), fmt, argsCopy);
va_end(argsCopy);
Expand Down Expand Up @@ -96,7 +96,7 @@ static void appendEdges(
} else {
for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) {
std::string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge));
appendNumberIfNotZero(base, str, edges[edge]);
appendNumberIfNotZero(base, str, edges[static_cast<size_t>(edge)]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/ReactCommon/yoga/yoga/event/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum struct LayoutPassReason : int {
struct LayoutData {
int layouts;
int measures;
int maxMeasureCache;
uint32_t maxMeasureCache;
int cachedLayouts;
int cachedMeasures;
int measureCallbacks;
Expand Down
23 changes: 13 additions & 10 deletions packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -48,17 +53,15 @@ struct LayoutResults {
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) & 0x3;
}

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); }
Expand Down
46 changes: 29 additions & 17 deletions packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <algorithm>
#include <cstddef>
#include <iostream>

#include <yoga/algorithm/FlexDirection.h>
Expand Down Expand Up @@ -264,16 +265,16 @@ 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;
}

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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -339,25 +346,30 @@ 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(
uint32_t computedFlexBasisGeneration) {
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
Expand Down
Loading

0 comments on commit 12e107a

Please sign in to comment.