Skip to content

Commit 3e0d3ae

Browse files
NickGerlemanmeta-codesync[bot]
authored andcommitted
Use std::ranges::reverse_view and enable clang-tidy reverse ranges (#55755)
Summary: Pull Request resolved: #55755 Previously, the RN build in fbcode was using an old platform version with poor support for std::ranges, so we had disabled clang-tidy's `modernize-loop-convert.UseCxx20ReverseRanges` check. This is no longer the case, so we can remove the suppression and replace instances of reverse iteration using rbegin/rend iterators with `std::ranges::reverse_view`. Changelog: [Internal] Reviewed By: jorge-cab Differential Revision: D75834622
1 parent 2d81437 commit 3e0d3ae

File tree

8 files changed

+29
-25
lines changed

8 files changed

+29
-25
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,9 +1227,9 @@ - (void)invalidateLayer
12271227
if (!_boxShadowLayers) {
12281228
_boxShadowLayers = [NSMutableArray new];
12291229
}
1230-
for (auto it = _props->boxShadow.rbegin(); it != _props->boxShadow.rend(); ++it) {
1230+
for (const auto &shadow : std::ranges::reverse_view(_props->boxShadow)) {
12311231
CALayer *shadowLayer = RCTGetBoxShadowLayer(
1232-
*it,
1232+
shadow,
12331233
RCTCornerRadiiFromBorderRadii(borderMetrics.borderRadii),
12341234
RCTUIEdgeInsetsFromEdgeInsets(borderMetrics.borderWidths),
12351235
self.layer.bounds.size);

packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "ProfileTreeNode.h"
1111
#include "TraceEventSerializer.h"
1212

13+
#include <ranges>
1314
#include <string_view>
1415
#include <unordered_map>
1516

@@ -192,8 +193,7 @@ void processCallStack(
192193
}
193194

194195
ProfileTreeNode* previousNode = &rootNode;
195-
for (auto it = callStack.rbegin(); it != callStack.rend(); ++it) {
196-
const RuntimeSamplingProfile::SampleCallStackFrame& callFrame = *it;
196+
for (const auto& callFrame : std::ranges::reverse_view(callStack)) {
197197
bool isGarbageCollectorFrame = callFrame.kind ==
198198
RuntimeSamplingProfile::SampleCallStackFrame::Kind::GarbageCollector;
199199

packages/react-native/ReactCommon/react/renderer/core/LayoutableShadowNode.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "LayoutableShadowNode.h"
99

10+
#include <ranges>
11+
1012
#include <react/renderer/core/LayoutConstraints.h>
1113
#include <react/renderer/core/LayoutContext.h>
1214
#include <react/renderer/core/LayoutMetrics.h>
@@ -106,8 +108,8 @@ LayoutMetrics LayoutableShadowNode::computeRelativeLayoutMetrics(
106108
// root because we measure it from an outside tree perspective.
107109
shadowNodeList.push_back(descendantNode);
108110

109-
for (auto it = ancestors.rbegin(); it != ancestors.rend(); it++) {
110-
auto& shadowNode = it->first.get();
111+
for (auto& ancestor : std::ranges::reverse_view(ancestors)) {
112+
auto& shadowNode = ancestor.first.get();
111113

112114
shadowNodeList.push_back(&shadowNode);
113115

@@ -334,8 +336,8 @@ std::shared_ptr<const ShadowNode> LayoutableShadowNode::findNodeAtPoint(
334336
return lhs->getOrderIndex() < rhs->getOrderIndex();
335337
});
336338

337-
for (auto it = sortedChildren.rbegin(); it != sortedChildren.rend(); it++) {
338-
const auto& childShadowNode = *it;
339+
for (const auto& childShadowNode :
340+
std::ranges::reverse_view(sortedChildren)) {
339341
auto hitView = findNodeAtPoint(childShadowNode, newPoint);
340342
if (hitView) {
341343
return hitView;

packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <react/renderer/core/ShadowNodeFragment.h>
1515
#include <react/renderer/debug/DebugStringConvertible.h>
1616
#include <react/renderer/debug/debugStringConvertibleUtils.h>
17+
#include <ranges>
1718

1819
#include <utility>
1920

@@ -391,9 +392,8 @@ std::shared_ptr<ShadowNode> ShadowNode::cloneTree(
391392

392393
auto childNode = newShadowNode;
393394

394-
for (auto it = ancestors.rbegin(); it != ancestors.rend(); ++it) {
395-
auto& parentNode = it->first.get();
396-
auto childIndex = it->second;
395+
for (auto& [ancestorRef, childIndex] : std::ranges::reverse_view(ancestors)) {
396+
auto& parentNode = ancestorRef.get();
397397

398398
auto children = parentNode.getChildren();
399399
react_native_assert(

packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <react/featureflags/ReactNativeFeatureFlags.h>
1313
#include <react/renderer/core/ComponentDescriptor.h>
1414
#include <react/renderer/core/State.h>
15+
#include <ranges>
1516

1617
#include <utility>
1718

@@ -116,8 +117,7 @@ AncestorList ShadowNodeFamily::getAncestors(
116117

117118
auto ancestors = AncestorList{};
118119
auto parentNode = &ancestorShadowNode;
119-
for (auto it = families.rbegin(); it != families.rend(); it++) {
120-
auto childFamily = *it;
120+
for (auto childFamily : std::ranges::reverse_view(families)) {
121121
auto found = false;
122122
auto childIndex = 0;
123123
for (const auto& childNode : *parentNode->children_) {

packages/react-native/ReactCommon/react/renderer/uimanager/PointerEventsProcessor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <glog/logging.h>
1111
#include <react/renderer/bridging/bridging.h>
12+
#include <ranges>
1213

1314
namespace facebook::react {
1415

@@ -79,8 +80,9 @@ static bool isAnyViewInPathToRootListeningToEvents(
7980
auto ancestors = nodeFamily.getAncestors(*owningRootShadowNode);
8081

8182
// Check for listeners from the target's parent to the root
82-
for (auto it = ancestors.rbegin(); it != ancestors.rend(); it++) {
83-
auto& currentNode = it->first.get();
83+
for (auto& [ancestorNode, childIndex] :
84+
std::ranges::reverse_view(ancestors)) {
85+
auto& currentNode = ancestorNode.get();
8486
if (isViewListeningToEvents(currentNode, eventTypes)) {
8587
return true;
8688
}
@@ -488,11 +490,9 @@ void PointerEventsProcessor::handleIncomingPointerEventOnNode(
488490
}
489491

490492
// Actually emit the leave events (in order from target to root)
491-
for (auto it = targetsToEmitLeaveTo.rbegin();
492-
it != targetsToEmitLeaveTo.rend();
493-
it++) {
493+
for (auto& target : std::ranges::reverse_view(targetsToEmitLeaveTo)) {
494494
eventDispatcher(
495-
*it, "topPointerLeave", ReactEventPriority::Discrete, event);
495+
target, "topPointerLeave", ReactEventPriority::Discrete, event);
496496
}
497497

498498
// Over

packages/react-native/ReactCommon/react/renderer/uimanager/PointerHoverTracker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "PointerHoverTracker.h"
99

10+
#include <ranges>
1011
#include <utility>
1112

1213
namespace facebook::react {
@@ -139,8 +140,8 @@ EventPath PointerHoverTracker::getEventPathTargets() const {
139140
auto ancestors = target_->getFamily().getAncestors(*root_);
140141

141142
result.emplace_back(*target_);
142-
for (auto it = ancestors.rbegin(); it != ancestors.rend(); it++) {
143-
result.push_back(it->first);
143+
for (auto& [ancestor, index] : std::ranges::reverse_view(ancestors)) {
144+
result.push_back(ancestor);
144145
}
145146

146147
return result;

packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerUpdateShadowTree.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <deque>
1515
#include <memory>
1616
#include <optional>
17+
#include <ranges>
1718
#include <vector>
1819

1920
namespace facebook::react {
@@ -52,8 +53,8 @@ void addAncestorsToUpdateList(
5253

5354
int ancestorDepth = static_cast<int>(ancestors.size() - 1);
5455
// iterate from current ShadowNode's parent to root ShadowNode
55-
for (auto iter = ancestors.rbegin(); iter != ancestors.rend(); ++iter) {
56-
auto& ancestorShadowNode = iter->first.get();
56+
for (auto& [ancestorRef, childIdx] : std::ranges::reverse_view(ancestors)) {
57+
auto& ancestorShadowNode = ancestorRef.get();
5758
auto ancestorTag = ancestorShadowNode.getTag();
5859
auto ancestorAddedToUpdateList = std::find_if(
5960
shadowNodesToUpdate.begin(),
@@ -67,10 +68,10 @@ void addAncestorsToUpdateList(
6768
.tag = ancestorShadowNode.getTag(),
6869
.depth = ancestorDepth,
6970
.node = ancestorShadowNodesShared[ancestorDepth],
70-
.updatedChildrenIndices = {iter->second},
71+
.updatedChildrenIndices = {childIdx},
7172
});
7273
} else {
73-
ancestorAddedToUpdateList->updatedChildrenIndices.push_back(iter->second);
74+
ancestorAddedToUpdateList->updatedChildrenIndices.push_back(childIdx);
7475
}
7576
ancestorDepth--;
7677
}

0 commit comments

Comments
 (0)