From 9ce360be7ea44585907843c8372c203d2fd0e35d Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Mon, 4 Mar 2024 02:28:02 -0800 Subject: [PATCH] Enable Clang Tidy (#43299) Summary: X-link: https://github.com/facebook/litho/pull/976 X-link: https://github.com/facebook/yoga/pull/1586 Pull Request resolved: https://github.com/facebook/react-native/pull/43299 Add the React Clang Tidy config to Yoga, run the auto fixes, and make some manual mechanical tweaks. Notably, the automatic changes to the infra for generating a Yoga tree from JSON capture make it 70% faster. Before: {F1463947076} After: {F1463946802} This also cleans up all the no-op shallow const parameters in headers. {F1463943386} Not all checks are available in all environments, but that is okay, as Clang Tidy will gracefully skip them. Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D54461054 fbshipit-source-id: dbd2d9ce51afd3174d1f2c6d439fa7d08baff46f --- .../first-party/yogajni/jni/ScopedGlobalRef.h | 6 +- .../first-party/yogajni/jni/ScopedLocalRef.h | 5 +- .../main/jni/first-party/yogajni/jni/YGJNI.h | 2 +- .../first-party/yogajni/jni/YGJNIVanilla.cpp | 28 ++- .../yogajni/jni/YogaJniException.cpp | 2 +- .../yogajni/jni/YogaJniException.h | 4 +- .../jni/first-party/yogajni/jni/common.cpp | 16 +- .../first-party/yogajni/jni/corefunctions.cpp | 10 +- .../jni/first-party/yogajni/jni/yogajni.cpp | 4 +- .../ReactCommon/yoga/yoga/YGNode.cpp | 4 +- .../yoga/yoga/algorithm/AbsoluteLayout.h | 18 +- .../yoga/yoga/algorithm/CalculateLayout.cpp | 215 +++++++++--------- .../yoga/yoga/algorithm/CalculateLayout.h | 32 +-- .../yoga/yoga/algorithm/FlexLine.cpp | 2 +- .../yoga/yoga/algorithm/FlexLine.h | 2 +- .../yoga/yoga/algorithm/PixelGrid.h | 14 +- .../ReactCommon/yoga/yoga/config/Config.cpp | 4 - .../ReactCommon/yoga/yoga/config/Config.h | 6 +- .../ReactCommon/yoga/yoga/debug/Log.h | 2 +- .../ReactCommon/yoga/yoga/event/event.cpp | 4 +- .../ReactCommon/yoga/yoga/event/event.h | 11 +- .../ReactCommon/yoga/yoga/node/Node.cpp | 47 ++-- .../ReactCommon/yoga/yoga/node/Node.h | 30 +-- .../yoga/yoga/style/SmallValueBuffer.h | 6 +- .../yoga/yoga/style/StyleValuePool.h | 4 +- 25 files changed, 240 insertions(+), 238 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedGlobalRef.h b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedGlobalRef.h index b563d0525f9..c0ca023e001 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedGlobalRef.h +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedGlobalRef.h @@ -62,7 +62,7 @@ class ScopedGlobalRef { * * @param globalRef the global reference to wrap. Can be NULL. */ - ScopedGlobalRef(T globalRef) : mGlobalRef(globalRef) {} + explicit ScopedGlobalRef(T globalRef) : mGlobalRef(globalRef) {} /** * Equivalent to ScopedGlobalRef(NULL) @@ -72,12 +72,12 @@ class ScopedGlobalRef { /** * Move construction is allowed. */ - ScopedGlobalRef(ScopedGlobalRef&& s) : mGlobalRef(s.release()) {} + ScopedGlobalRef(ScopedGlobalRef&& s) noexcept : mGlobalRef(s.release()) {} /** * Move assignment is allowed. */ - ScopedGlobalRef& operator=(ScopedGlobalRef&& s) { + ScopedGlobalRef& operator=(ScopedGlobalRef&& s) noexcept { reset(s.release()); return *this; } diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedLocalRef.h b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedLocalRef.h index 6765078bbc7..fd49e832033 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedLocalRef.h +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedLocalRef.h @@ -70,12 +70,13 @@ class ScopedLocalRef { /** * Move construction is allowed. */ - ScopedLocalRef(ScopedLocalRef&& s) : mEnv(s.mEnv), mLocalRef(s.release()) {} + ScopedLocalRef(ScopedLocalRef&& s) noexcept + : mEnv(s.mEnv), mLocalRef(s.release()) {} /** * Move assignment is allowed. */ - ScopedLocalRef& operator=(ScopedLocalRef&& s) { + ScopedLocalRef& operator=(ScopedLocalRef&& s) noexcept { reset(s.release()); mEnv = s.mEnv; return *this; diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.h b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.h index 489df201eb5..9b2609775a9 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.h +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.h @@ -36,7 +36,7 @@ class YGNodeEdges { BORDER = 4, }; - YGNodeEdges(YGNodeRef node) { + explicit YGNodeEdges(YGNodeRef node) { auto context = YGNodeContext{}; context.asVoidPtr = YGNodeGetContext(node); edges_ = context.edgesSet; diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp index 97fb17899c5..82e0dc17b13 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp @@ -54,7 +54,9 @@ static void jni_YGConfigSetExperimentalFeatureEnabledJNI( jboolean enabled) { const YGConfigRef config = _jlong2YGConfigRef(nativePointer); YGConfigSetExperimentalFeatureEnabled( - config, static_cast(feature), enabled); + config, + static_cast(feature), + static_cast(enabled)); } static void jni_YGConfigSetUseWebDefaultsJNI( @@ -63,7 +65,7 @@ static void jni_YGConfigSetUseWebDefaultsJNI( jlong nativePointer, jboolean useWebDefaults) { const YGConfigRef config = _jlong2YGConfigRef(nativePointer); - YGConfigSetUseWebDefaults(config, useWebDefaults); + YGConfigSetUseWebDefaults(config, static_cast(useWebDefaults)); } static void jni_YGConfigSetPointScaleFactorJNI( @@ -161,7 +163,7 @@ static void jni_YGConfigSetLoggerJNI( auto context = reinterpret_cast*>(YGConfigGetContext(config)); - if (logger) { + if (logger != nullptr) { if (context == nullptr) { context = new ScopedGlobalRef(); YGConfigSetContext(config, context); @@ -225,14 +227,15 @@ static void jni_YGNodeSetIsReferenceBaselineJNI( jlong nativePointer, jboolean isReferenceBaseline) { YGNodeSetIsReferenceBaseline( - _jlong2YGNodeRef(nativePointer), isReferenceBaseline); + _jlong2YGNodeRef(nativePointer), static_cast(isReferenceBaseline)); } static jboolean jni_YGNodeIsReferenceBaselineJNI( JNIEnv* /*env*/, jobject /*obj*/, jlong nativePointer) { - return YGNodeIsReferenceBaseline(_jlong2YGNodeRef(nativePointer)); + return static_cast( + YGNodeIsReferenceBaseline(_jlong2YGNodeRef(nativePointer))); } static void jni_YGNodeRemoveAllChildrenJNI( @@ -340,7 +343,7 @@ static void jni_YGNodeCalculateLayoutJNI( try { PtrJNodeMapVanilla* layoutContext = nullptr; auto map = PtrJNodeMapVanilla{}; - if (nativePointers) { + if (nativePointers != nullptr) { map = PtrJNodeMapVanilla{nativePointers, javaNodes}; layoutContext = ↦ } @@ -356,7 +359,7 @@ static void jni_YGNodeCalculateLayoutJNI( YGTransferLayoutOutputsRecursive(env, obj, root); } catch (const YogaJniException& jniException) { ScopedLocalRef throwable = jniException.getThrowable(); - if (throwable.get()) { + if (throwable.get() != nullptr) { env->Throw(throwable.get()); } } catch (const std::logic_error& ex) { @@ -626,8 +629,8 @@ static YGSize YGJNIMeasureFunc( uint32_t wBits = 0xFFFFFFFF & (measureResult >> 32); uint32_t hBits = 0xFFFFFFFF & measureResult; - float measuredWidth = std::bit_cast(wBits); - float measuredHeight = std::bit_cast(hBits); + auto measuredWidth = std::bit_cast(wBits); + auto measuredHeight = std::bit_cast(hBits); return YGSize{measuredWidth, measuredHeight}; } else { @@ -645,7 +648,7 @@ static void jni_YGNodeSetHasMeasureFuncJNI( jboolean hasMeasureFunc) { YGNodeSetMeasureFunc( _jlong2YGNodeRef(nativePointer), - hasMeasureFunc ? YGJNIMeasureFunc : nullptr); + static_cast(hasMeasureFunc) ? YGJNIMeasureFunc : nullptr); } static float YGJNIBaselineFunc(YGNodeConstRef node, float width, float height) { @@ -669,7 +672,7 @@ static void jni_YGNodeSetHasBaselineFuncJNI( jboolean hasBaselineFunc) { YGNodeSetBaselineFunc( _jlong2YGNodeRef(nativePointer), - hasBaselineFunc ? YGJNIBaselineFunc : nullptr); + static_cast(hasBaselineFunc) ? YGJNIBaselineFunc : nullptr); } static void jni_YGNodeSetAlwaysFormsContainingBlockJNI( @@ -678,7 +681,8 @@ static void jni_YGNodeSetAlwaysFormsContainingBlockJNI( jlong nativePointer, jboolean alwaysFormsContainingBlock) { YGNodeSetAlwaysFormsContainingBlock( - _jlong2YGNodeRef(nativePointer), alwaysFormsContainingBlock); + _jlong2YGNodeRef(nativePointer), + static_cast(alwaysFormsContainingBlock)); } static jlong diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.cpp b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.cpp index a30b8a6a8ab..e8c6ca923f3 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.cpp @@ -25,7 +25,7 @@ YogaJniException::YogaJniException(jthrowable throwable) { throwable_ = newGlobalRef(getCurrentEnv(), throwable); } -YogaJniException::YogaJniException(YogaJniException&& rhs) +YogaJniException::YogaJniException(YogaJniException&& rhs) noexcept : throwable_(std::move(rhs.throwable_)) {} YogaJniException::YogaJniException(const YogaJniException& rhs) { diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.h b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.h index 1d2cc956113..8c53d4125af 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.h +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.h @@ -22,9 +22,9 @@ class YogaJniException : public std::exception { explicit YogaJniException(jthrowable throwable); - YogaJniException(YogaJniException&& rhs); + YogaJniException(YogaJniException&& rhs) noexcept; - YogaJniException(const YogaJniException& other); + YogaJniException(const YogaJniException& rhs); ScopedLocalRef getThrowable() const noexcept; diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/common.cpp b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/common.cpp index e2dc0df22c3..d634b0af164 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/common.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/common.cpp @@ -16,7 +16,7 @@ void registerNatives( size_t numMethods) { jclass clazz = env->FindClass(className); - assertNoPendingJniExceptionIf(env, !clazz); + assertNoPendingJniExceptionIf(env, clazz == nullptr); auto result = env->RegisterNatives(clazz, methods, static_cast(numMethods)); @@ -31,7 +31,7 @@ jmethodID getStaticMethodId( const char* methodDescriptor) { jmethodID methodId = env->GetStaticMethodID(clazz, methodName, methodDescriptor); - assertNoPendingJniExceptionIf(env, !methodId); + assertNoPendingJniExceptionIf(env, methodId == nullptr); return methodId; } @@ -41,7 +41,7 @@ jmethodID getMethodId( const char* methodName, const char* methodDescriptor) { jmethodID methodId = env->GetMethodID(clazz, methodName, methodDescriptor); - assertNoPendingJniExceptionIf(env, !methodId); + assertNoPendingJniExceptionIf(env, methodId == nullptr); return methodId; } @@ -51,7 +51,7 @@ jfieldID getFieldId( const char* fieldName, const char* fieldSignature) { jfieldID fieldId = env->GetFieldID(clazz, fieldName, fieldSignature); - assertNoPendingJniExceptionIf(env, !fieldId); + assertNoPendingJniExceptionIf(env, fieldId == nullptr); return fieldId; } @@ -82,14 +82,14 @@ callStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID methodId, ...) { va_start(args, methodId); jobject result = env->CallStaticObjectMethodV(clazz, methodId, args); va_end(args); - assertNoPendingJniExceptionIf(env, !result); + assertNoPendingJniExceptionIf(env, result == nullptr); return make_local_ref(env, result); } ScopedGlobalRef newGlobalRef(JNIEnv* env, jobject obj) { jobject result = env->NewGlobalRef(obj); - if (!result) { + if (result == nullptr) { logErrorMessageAndDie("Could not obtain global reference from object"); } @@ -97,9 +97,9 @@ ScopedGlobalRef newGlobalRef(JNIEnv* env, jobject obj) { } ScopedGlobalRef newGlobalRef(JNIEnv* env, jthrowable obj) { - jthrowable result = static_cast(env->NewGlobalRef(obj)); + auto result = static_cast(env->NewGlobalRef(obj)); - if (!result) { + if (result == nullptr) { logErrorMessageAndDie("Could not obtain global reference from object"); } diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp index f094357cf2b..6fcbf78064d 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp @@ -12,9 +12,9 @@ namespace facebook::yoga::vanillajni { namespace { -JavaVM* globalVm = NULL; +JavaVM* globalVm = nullptr; struct JavaVMInitializer { - JavaVMInitializer(JavaVM* vm) { + explicit JavaVMInitializer(JavaVM* vm) { if (!vm) { logErrorMessageAndDie( "You cannot pass a NULL JavaVM to ensureInitialized"); @@ -27,7 +27,7 @@ struct JavaVMInitializer { jint ensureInitialized(JNIEnv** env, JavaVM* vm) { static JavaVMInitializer init(vm); - if (!env) { + if (env == nullptr) { logErrorMessageAndDie( "Need to pass a valid JNIEnv pointer to vanillajni initialization " "routine"); @@ -43,7 +43,7 @@ jint ensureInitialized(JNIEnv** env, JavaVM* vm) { // TODO why we need JNIEXPORT for getCurrentEnv ? JNIEXPORT JNIEnv* getCurrentEnv() { - JNIEnv* env; + JNIEnv* env = nullptr; jint ret = globalVm->GetEnv((void**)&env, JNI_VERSION_1_6); if (ret != JNI_OK) { logErrorMessageAndDie( @@ -68,7 +68,7 @@ void assertNoPendingJniException(JNIEnv* env) { } auto throwable = env->ExceptionOccurred(); - if (!throwable) { + if (throwable == nullptr) { logErrorMessageAndDie("Unable to get pending JNI exception."); } env->ExceptionClear(); diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/yogajni.cpp b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/yogajni.cpp index cab45a13b50..5bc99431371 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/yogajni.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/yogajni.cpp @@ -10,8 +10,8 @@ using namespace facebook::yoga; -jint JNI_OnLoad(JavaVM* vm, void*) { - JNIEnv* env; +jint JNI_OnLoad(JavaVM* vm, void* /*unused*/) { + JNIEnv* env = nullptr; jint ret = vanillajni::ensureInitialized(&env, vm); YGJNIVanilla::registerNatives(env); return ret; diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp index 57f0473b05e..a34685eee62 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp @@ -216,12 +216,12 @@ void YGNodeSetChildren( auto owner = resolveRef(ownerRef); auto children = reinterpret_cast(childrenRefs); - if (!owner) { + if (owner == nullptr) { return; } const std::vector childrenVector = {children, children + count}; - if (childrenVector.size() == 0) { + if (childrenVector.empty()) { if (owner->getChildCount() > 0) { for (auto* child : owner->getChildren()) { child->setLayout({}); diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.h b/packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.h index 3beabf9a4a0..ae84e6da9f2 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.h +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.h @@ -13,16 +13,16 @@ namespace facebook::yoga { void layoutAbsoluteChild( - const yoga::Node* const containingNode, - const yoga::Node* const node, - yoga::Node* const child, - const float containingBlockWidth, - const float containingBlockHeight, - const SizingMode widthMode, - const Direction direction, + const yoga::Node* containingNode, + const yoga::Node* node, + yoga::Node* child, + float containingBlockWidth, + float containingBlockHeight, + SizingMode widthMode, + Direction direction, LayoutData& layoutMarkerData, - const uint32_t depth, - const uint32_t generationCount); + uint32_t depth, + uint32_t generationCount); void layoutAbsoluteDescendants( yoga::Node* containingNode, diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 9b214cf587f..c6fb81e7034 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -80,8 +80,8 @@ static void computeFlexBasisForChild( const float mainAxisSize = isMainAxisRow ? width : height; const float mainAxisownerSize = isMainAxisRow ? ownerWidth : ownerHeight; - float childWidth; - float childHeight; + float childWidth = YGUndefined; + float childHeight = YGUndefined; SizingMode childWidthSizingMode; SizingMode childHeightSizingMode; @@ -120,8 +120,6 @@ static void computeFlexBasisForChild( } else { // Compute the flex basis and hypothetical main size (i.e. the clamped flex // basis). - childWidth = YGUndefined; - childHeight = YGUndefined; childWidthSizingMode = SizingMode::MaxContent; childHeightSizingMode = SizingMode::MaxContent; @@ -606,7 +604,7 @@ static float distributeFreeSpaceSecondPass( -currentLineChild->resolveFlexShrink() * childFlexBasis; // Is this child able to shrink? if (flexShrinkScaledFactor != 0) { - float childSize; + float childSize = YGUndefined; if (yoga::isDefined(flexLine.layout.totalFlexShrinkScaledFactors) && flexLine.layout.totalFlexShrinkScaledFactors == 0) { @@ -650,7 +648,7 @@ static float distributeFreeSpaceSecondPass( const float marginCross = currentLineChild->style().computeMarginForAxis( crossAxis, availableInnerWidth); - float childCrossSize; + float childCrossSize = YGUndefined; float childMainSize = updatedMainSize + marginMain; SizingMode childCrossSizingMode; SizingMode childMainSizingMode = SizingMode::StretchFit; @@ -1794,13 +1792,13 @@ static void calculateLayoutImpl( size_t endIndex = 0; for (size_t i = 0; i < lineCount; i++) { const size_t startIndex = endIndex; - size_t ii; + size_t ii = startIndex; // compute the line's height and find the endIndex float lineHeight = 0; float maxAscentForCurrentLine = 0; float maxDescentForCurrentLine = 0; - for (ii = startIndex; ii < childCount; ii++) { + for (; ii < childCount; ii++) { const auto child = node->getChild(ii); if (child->style().display() == Display::None) { continue; @@ -1837,113 +1835,110 @@ static void calculateLayoutImpl( endIndex = ii; currentLead += i != 0 ? crossAxisGap : 0; - if (performLayout) { - for (ii = startIndex; ii < endIndex; ii++) { - const auto child = node->getChild(ii); - if (child->style().display() == Display::None) { - continue; - } - if (child->style().positionType() != PositionType::Absolute) { - switch (resolveChildAlignment(node, child)) { - case Align::FlexStart: { - child->setLayoutPosition( - currentLead + - child->style().computeFlexStartPosition( - crossAxis, direction, availableInnerWidth), - flexStartEdge(crossAxis)); - break; - } - case Align::FlexEnd: { - child->setLayoutPosition( - currentLead + lineHeight - - child->style().computeFlexEndMargin( - crossAxis, direction, availableInnerWidth) - - child->getLayout().measuredDimension( - dimension(crossAxis)), - flexStartEdge(crossAxis)); - break; - } - case Align::Center: { - float childHeight = - child->getLayout().measuredDimension(dimension(crossAxis)); - - child->setLayoutPosition( - currentLead + (lineHeight - childHeight) / 2, - flexStartEdge(crossAxis)); - break; - } - case Align::Stretch: { - child->setLayoutPosition( - currentLead + - child->style().computeFlexStartMargin( - crossAxis, direction, availableInnerWidth), - flexStartEdge(crossAxis)); - - // Remeasure child with the line height as it as been only - // measured with the owners height yet. - if (!child->hasDefiniteLength( - dimension(crossAxis), availableInnerCrossDim)) { - const float childWidth = isMainAxisRow - ? (child->getLayout().measuredDimension( - Dimension::Width) + - child->style().computeMarginForAxis( - mainAxis, availableInnerWidth)) - : leadPerLine + lineHeight; - - const float childHeight = !isMainAxisRow - ? (child->getLayout().measuredDimension( - Dimension::Height) + - child->style().computeMarginForAxis( - crossAxis, availableInnerWidth)) - : leadPerLine + lineHeight; - - if (!(yoga::inexactEquals( - childWidth, - child->getLayout().measuredDimension( - Dimension::Width)) && - yoga::inexactEquals( - childHeight, - child->getLayout().measuredDimension( - Dimension::Height)))) { - calculateLayoutInternal( - child, - childWidth, - childHeight, - direction, - SizingMode::StretchFit, - SizingMode::StretchFit, - availableInnerWidth, - availableInnerHeight, - true, - LayoutPassReason::kMultilineStretch, - layoutMarkerData, - depth, - generationCount); - } + for (ii = startIndex; ii < endIndex; ii++) { + const auto child = node->getChild(ii); + if (child->style().display() == Display::None) { + continue; + } + if (child->style().positionType() != PositionType::Absolute) { + switch (resolveChildAlignment(node, child)) { + case Align::FlexStart: { + child->setLayoutPosition( + currentLead + + child->style().computeFlexStartPosition( + crossAxis, direction, availableInnerWidth), + flexStartEdge(crossAxis)); + break; + } + case Align::FlexEnd: { + child->setLayoutPosition( + currentLead + lineHeight - + child->style().computeFlexEndMargin( + crossAxis, direction, availableInnerWidth) - + child->getLayout().measuredDimension( + dimension(crossAxis)), + flexStartEdge(crossAxis)); + break; + } + case Align::Center: { + float childHeight = + child->getLayout().measuredDimension(dimension(crossAxis)); + + child->setLayoutPosition( + currentLead + (lineHeight - childHeight) / 2, + flexStartEdge(crossAxis)); + break; + } + case Align::Stretch: { + child->setLayoutPosition( + currentLead + + child->style().computeFlexStartMargin( + crossAxis, direction, availableInnerWidth), + flexStartEdge(crossAxis)); + + // Remeasure child with the line height as it as been only + // measured with the owners height yet. + if (!child->hasDefiniteLength( + dimension(crossAxis), availableInnerCrossDim)) { + const float childWidth = isMainAxisRow + ? (child->getLayout().measuredDimension(Dimension::Width) + + child->style().computeMarginForAxis( + mainAxis, availableInnerWidth)) + : leadPerLine + lineHeight; + + const float childHeight = !isMainAxisRow + ? (child->getLayout().measuredDimension(Dimension::Height) + + child->style().computeMarginForAxis( + crossAxis, availableInnerWidth)) + : leadPerLine + lineHeight; + + if (!(yoga::inexactEquals( + childWidth, + child->getLayout().measuredDimension( + Dimension::Width)) && + yoga::inexactEquals( + childHeight, + child->getLayout().measuredDimension( + Dimension::Height)))) { + calculateLayoutInternal( + child, + childWidth, + childHeight, + direction, + SizingMode::StretchFit, + SizingMode::StretchFit, + availableInnerWidth, + availableInnerHeight, + true, + LayoutPassReason::kMultilineStretch, + layoutMarkerData, + depth, + generationCount); } - break; - } - case Align::Baseline: { - child->setLayoutPosition( - currentLead + maxAscentForCurrentLine - - calculateBaseline(child) + - child->style().computeFlexStartPosition( - FlexDirection::Column, - direction, - availableInnerCrossDim), - PhysicalEdge::Top); - - break; } - case Align::Auto: - case Align::SpaceBetween: - case Align::SpaceAround: - case Align::SpaceEvenly: - break; + break; } + case Align::Baseline: { + child->setLayoutPosition( + currentLead + maxAscentForCurrentLine - + calculateBaseline(child) + + child->style().computeFlexStartPosition( + FlexDirection::Column, + direction, + availableInnerCrossDim), + PhysicalEdge::Top); + + break; + } + case Align::Auto: + case Align::SpaceBetween: + case Align::SpaceAround: + case Align::SpaceEvenly: + break; } } } + currentLead = currentLead + leadPerLine + lineHeight; } } @@ -2244,7 +2239,7 @@ bool calculateLayoutInternal( layout->nextCachedMeasurementsIndex = 0; } - CachedMeasurement* newCacheEntry; + CachedMeasurement* newCacheEntry = nullptr; if (performLayout) { // Use the single layout cache entry. newCacheEntry = &layout->cachedLayout; diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.h b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.h index 1d5a3fd7394..5e6884ec1a4 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.h +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.h @@ -15,24 +15,24 @@ namespace facebook::yoga { void calculateLayout( - yoga::Node* const node, - const float ownerWidth, - const float ownerHeight, - const Direction ownerDirection); + yoga::Node* node, + float ownerWidth, + float ownerHeight, + Direction ownerDirection); bool calculateLayoutInternal( - yoga::Node* const node, - const float availableWidth, - const float availableHeight, - const Direction ownerDirection, - const SizingMode widthSizingMode, - const SizingMode heightSizingMode, - const float ownerWidth, - const float ownerHeight, - const bool performLayout, - const LayoutPassReason reason, + yoga::Node* node, + float availableWidth, + float availableHeight, + Direction ownerDirection, + SizingMode widthSizingMode, + SizingMode heightSizingMode, + float ownerWidth, + float ownerHeight, + bool performLayout, + LayoutPassReason reason, LayoutData& layoutMarkerData, - const uint32_t depth, - const uint32_t generationCount); + uint32_t depth, + uint32_t generationCount); } // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp index 69ae351efab..1fe3e486926 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp @@ -69,7 +69,7 @@ FlexLine calculateFlexLine( if (sizeConsumedIncludingMinConstraint + flexBasisWithMinAndMaxConstraints + childMarginMainAxis + childLeadingGapMainAxis > availableInnerMainDim && - isNodeFlexWrap && itemsInFlow.size() > 0) { + isNodeFlexWrap && !itemsInFlow.empty()) { break; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.h b/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.h index 161a1031c36..0f888be5073 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.h +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.h @@ -63,7 +63,7 @@ struct FlexLine { // computedFlexBasis properly computed(To do this use // computeFlexBasisForChildren function). FlexLine calculateFlexLine( - yoga::Node* const node, + yoga::Node* node, Direction ownerDirection, float mainAxisownerSize, float availableInnerWidth, diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.h b/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.h index 6bedd3ac0ec..f3bf79f98a6 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.h +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.h @@ -15,15 +15,15 @@ namespace facebook::yoga { // Round a point value to the nearest physical pixel based on DPI // (pointScaleFactor) float roundValueToPixelGrid( - const double value, - const double pointScaleFactor, - const bool forceCeil, - const bool forceFloor); + double value, + double pointScaleFactor, + bool forceCeil, + bool forceFloor); // Round the layout results of a node and its subtree to the pixel grid. void roundLayoutResultsToPixelGrid( - yoga::Node* const node, - const double absoluteLeft, - const double absoluteTop); + yoga::Node* node, + double absoluteLeft, + double absoluteTop); } // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp index 607e5774706..881a377360e 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp @@ -20,10 +20,6 @@ bool configUpdateInvalidatesLayout( oldConfig.useWebDefaults() != newConfig.useWebDefaults(); } -Config::Config(YGLogger logger) : cloneNodeCallback_{nullptr} { - setLogger(logger); -} - void Config::setUseWebDefaults(bool useWebDefaults) { useWebDefaults_ = useWebDefaults; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h index e902bd0d1b9..4db25bd0f4d 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h @@ -32,7 +32,7 @@ bool configUpdateInvalidatesLayout( class YG_EXPORT Config : public ::YGConfig { public: - Config(YGLogger logger); + explicit Config(YGLogger logger) : logger_{logger} {} void setUseWebDefaults(bool useWebDefaults); bool useWebDefaults() const; @@ -67,8 +67,8 @@ class YG_EXPORT Config : public ::YGConfig { static const Config& getDefault(); private: - YGCloneNodeFunc cloneNodeCallback_; - YGLogger logger_; + YGCloneNodeFunc cloneNodeCallback_{nullptr}; + YGLogger logger_{}; bool useWebDefaults_ : 1 = false; diff --git a/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h b/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h index 94dc0e0cedf..0a932d80eb0 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h +++ b/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h @@ -20,7 +20,7 @@ void log(LogLevel level, const char* format, ...) noexcept; void log( const yoga::Node* node, LogLevel level, - const char* message, + const char* format, ...) noexcept; void log( diff --git a/packages/react-native/ReactCommon/yoga/yoga/event/event.cpp b/packages/react-native/ReactCommon/yoga/yoga/event/event.cpp index b47e1fab7fd..e286ded0555 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/event/event.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/event/event.cpp @@ -40,14 +40,14 @@ struct Node { std::function subscriber = nullptr; Node* next = nullptr; - Node(std::function&& subscriber) + explicit Node(std::function&& subscriber) : subscriber{std::move(subscriber)} {} }; std::atomic subscribers{nullptr}; Node* push(Node* newHead) { - Node* oldHead; + Node* oldHead = nullptr; do { oldHead = subscribers.load(std::memory_order_relaxed); if (newHead != nullptr) { diff --git a/packages/react-native/ReactCommon/yoga/yoga/event/event.h b/packages/react-native/ReactCommon/yoga/yoga/event/event.h index 4e96e049eab..b4be8ab298f 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/event/event.h +++ b/packages/react-native/ReactCommon/yoga/yoga/event/event.h @@ -9,8 +9,8 @@ #include -#include #include +#include #include #include @@ -46,7 +46,7 @@ struct LayoutData { measureCallbackReasonsCount; }; -const char* LayoutPassReasonToString(const LayoutPassReason value); +const char* LayoutPassReasonToString(LayoutPassReason value); struct YG_EXPORT Event { enum Type { @@ -72,7 +72,7 @@ struct YG_EXPORT Event { public: template - Data(const TypedData& data) : data_{&data} {} + explicit Data(const TypedData& data) : data_{&data} {} template const TypedData& get() const { @@ -90,7 +90,10 @@ struct YG_EXPORT Event { } private: - static void publish(YGNodeConstRef, Type, const Data&); + static void publish( + YGNodeConstRef /*node*/, + Type /*eventType*/, + const Data& /*eventData*/); }; template <> diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp index c431940e164..adb63663bed 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp @@ -26,23 +26,23 @@ Node::Node(const yoga::Config* config) : config_{config} { } } -Node::Node(Node&& node) { - hasNewLayout_ = node.hasNewLayout_; - isReferenceBaseline_ = node.isReferenceBaseline_; - isDirty_ = node.isDirty_; - alwaysFormsContainingBlock_ = node.alwaysFormsContainingBlock_; - nodeType_ = node.nodeType_; - context_ = node.context_; - measureFunc_ = node.measureFunc_; - baselineFunc_ = node.baselineFunc_; - dirtiedFunc_ = node.dirtiedFunc_; - style_ = node.style_; - layout_ = node.layout_; - lineIndex_ = node.lineIndex_; - owner_ = node.owner_; - children_ = std::move(node.children_); - config_ = node.config_; - resolvedDimensions_ = node.resolvedDimensions_; +Node::Node(Node&& node) noexcept + : hasNewLayout_(node.hasNewLayout_), + isReferenceBaseline_(node.isReferenceBaseline_), + isDirty_(node.isDirty_), + alwaysFormsContainingBlock_(node.alwaysFormsContainingBlock_), + nodeType_(node.nodeType_), + context_(node.context_), + measureFunc_(node.measureFunc_), + baselineFunc_(node.baselineFunc_), + dirtiedFunc_(node.dirtiedFunc_), + style_(std::move(node.style_)), + layout_(node.layout_), + lineIndex_(node.lineIndex_), + owner_(node.owner_), + children_(std::move(node.children_)), + config_(node.config_), + resolvedDimensions_(node.resolvedDimensions_) { for (auto c : children_) { c->setOwner(this); } @@ -83,7 +83,7 @@ void Node::setMeasureFunc(YGMeasureFunc measureFunc) { } else { yoga::assertFatalWithNode( this, - children_.size() == 0, + children_.empty(), "Cannot set measure function: Nodes with measure functions cannot have " "children."); // TODO: t18095186 Move nodeType to opt-in function and mark appropriate @@ -122,18 +122,17 @@ void Node::setConfig(yoga::Config* config) { } void Node::setDirty(bool isDirty) { - if (isDirty == isDirty_) { + if (static_cast(isDirty) == isDirty_) { return; } isDirty_ = isDirty; - if (isDirty && dirtiedFunc_) { + if (isDirty && (dirtiedFunc_ != nullptr)) { dirtiedFunc_(this); } } bool Node::removeChild(Node* child) { - std::vector::iterator p = - std::find(children_.begin(), children_.end(), child); + auto p = std::find(children_.begin(), children_.end(), child); if (p != children_.end()) { children_.erase(p); return true; @@ -307,7 +306,7 @@ void Node::markDirtyAndPropagate() { if (!isDirty_) { setDirty(true); setLayoutComputedFlexBasis(FloatOptional()); - if (owner_) { + if (owner_ != nullptr) { owner_->markDirtyAndPropagate(); } } @@ -351,7 +350,7 @@ bool Node::isNodeFlexible() { void Node::reset() { yoga::assertFatalWithNode( this, - children_.size() == 0, + children_.empty(), "Cannot reset a node which still has children attached"); yoga::assertFatalWithNode( this, owner_ == nullptr, "Cannot reset a node still attached to a owner"); diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h index 1eea46f8c1c..ec28f2db91a 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h @@ -7,8 +7,8 @@ #pragma once -#include #include +#include #include #include @@ -34,7 +34,7 @@ class YG_EXPORT Node : public ::YGNode { Node(); explicit Node(const Config* config); - Node(Node&&); + Node(Node&& node) noexcept; // Does not expose true value semantics, as children are not cloned eagerly. // Should we remove this? @@ -65,7 +65,11 @@ class YG_EXPORT Node : public ::YGNode { return measureFunc_ != nullptr; } - YGSize measure(float, MeasureMode, float, MeasureMode); + YGSize measure( + float width, + MeasureMode widthMode, + float height, + MeasureMode heightMode); bool hasBaselineFunc() const noexcept { return baselineFunc_ != nullptr; @@ -73,9 +77,9 @@ class YG_EXPORT Node : public ::YGNode { float baseline(float width, float height) const; - float dimensionWithMargin(const FlexDirection axis, const float widthSize); + float dimensionWithMargin(FlexDirection axis, float widthSize); - bool isLayoutDimensionDefined(const FlexDirection axis); + bool isLayoutDimensionDefined(FlexDirection axis); /** * Whether the node has a "definite length" along the given axis. @@ -214,7 +218,7 @@ class YG_EXPORT Node : public ::YGNode { void setDirty(bool isDirty); void setLayoutLastOwnerDirection(Direction direction); - void setLayoutComputedFlexBasis(const FloatOptional computedFlexBasis); + void setLayoutComputedFlexBasis(FloatOptional computedFlexBasis); void setLayoutComputedFlexBasisGeneration( uint32_t computedFlexBasisGeneration); void setLayoutMeasuredDimension(float measuredDimension, Dimension dimension); @@ -226,15 +230,15 @@ class YG_EXPORT Node : public ::YGNode { void setLayoutPadding(float padding, PhysicalEdge edge); void setLayoutPosition(float position, PhysicalEdge edge); void setPosition( - const Direction direction, - const float mainSize, - const float crossSize, - const float ownerWidth); + Direction direction, + float mainSize, + float crossSize, + float ownerWidth); // Other methods Style::Length resolveFlexBasisPtr() const; void resolveDimension(); - Direction resolveDirection(const Direction ownerDirection); + Direction resolveDirection(Direction ownerDirection); void clearChildren(); /// Replaces the occurrences of oldChild with newChild void replaceChild(Node* oldChild, Node* newChild); @@ -253,12 +257,12 @@ class YG_EXPORT Node : public ::YGNode { private: // Used to allow resetting the node - Node& operator=(Node&&) = default; + Node& operator=(Node&&) noexcept = default; float relativePosition( FlexDirection axis, Direction direction, - const float axisSize) const; + float axisSize) const; void useWebDefaults() { style_.setFlexDirection(FlexDirection::Row); diff --git a/packages/react-native/ReactCommon/yoga/yoga/style/SmallValueBuffer.h b/packages/react-native/ReactCommon/yoga/yoga/style/SmallValueBuffer.h index 2860a420f9c..8d78a54dc97 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/style/SmallValueBuffer.h +++ b/packages/react-native/ReactCommon/yoga/yoga/style/SmallValueBuffer.h @@ -26,7 +26,7 @@ class SmallValueBuffer { SmallValueBuffer(const SmallValueBuffer& other) { *this = other; } - SmallValueBuffer(SmallValueBuffer&& other) = default; + SmallValueBuffer(SmallValueBuffer&& other) noexcept = default; // Add a new element to the buffer, returning the index of the element uint16_t push(uint32_t value) { @@ -116,7 +116,7 @@ class SmallValueBuffer { return *this; } - SmallValueBuffer& operator=(SmallValueBuffer&& other) = default; + SmallValueBuffer& operator=(SmallValueBuffer&& other) noexcept = default; private: struct Overflow { @@ -125,7 +125,7 @@ class SmallValueBuffer { }; uint16_t count_{0}; - std::array buffer_; + std::array buffer_{}; std::bitset wideElements_; std::unique_ptr overflow_; }; diff --git a/packages/react-native/ReactCommon/yoga/yoga/style/StyleValuePool.h b/packages/react-native/ReactCommon/yoga/yoga/style/StyleValuePool.h index f8ee93e47b3..3b177254447 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/style/StyleValuePool.h +++ b/packages/react-native/ReactCommon/yoga/yoga/style/StyleValuePool.h @@ -101,7 +101,7 @@ class StyleValuePool { static constexpr bool isIntegerPackable(float f) { constexpr uint16_t kMaxInlineAbsValue = (1 << 11) - 1; - int32_t i = static_cast(f); + auto i = static_cast(f); return static_cast(i) == f && i >= -kMaxInlineAbsValue && i <= +kMaxInlineAbsValue; } @@ -110,7 +110,7 @@ class StyleValuePool { uint16_t isNegative = value < 0 ? 1 : 0; return static_cast( (isNegative << 11) | - (static_cast(value) * (isNegative ? -1 : 1))); + (static_cast(value) * (isNegative != 0u ? -1 : 1))); } static constexpr float unpackInlineInteger(uint16_t value) {