Skip to content

Adopt more smart pointers in dom/Text #46876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

jelee53
Copy link
Contributor

@jelee53 jelee53 commented Jun 17, 2025

dcd1706

Adopt more smart pointers in dom/Text
https://bugs.webkit.org/show_bug.cgi?id=294640
rdar://153684504 (Adopt more smart pointers in dom/Text (294640))

Reviewed by Ryosuke Niwa.

Smart pointer adoption as per the static analyzer.

* Source/WebCore/SaferCPPExpectations/UncheckedLocalVarsCheckerExpectations:
* Source/WebCore/SaferCPPExpectations/UncountedLocalVarsCheckerExpectations:
* Source/WebCore/dom/Text.cpp:
(WebCore::Text::wholeText const):
* Source/WebCore/dom/TextNodeTraversal.cpp:
(WebCore::TextNodeTraversal::appendContents):
(WebCore::TextNodeTraversal::childTextContent):
* Source/WebCore/dom/TextNodeTraversal.h:
(WebCore::TextNodeTraversal::firstTextWithinTemplate):
(WebCore::TextNodeTraversal::traverseNextTextTemplate):

Canonical link: https://commits.webkit.org/296685@main

20a3947

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win
✅ 🧪 bindings ✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 ⏳ 🧪 win-tests
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe
✅ 🧪 ios-wk2-wpt ✅ 🧪 mac-wk1 ✅ 🛠 wpe-cairo
✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk
✅ 🛠 vision ✅ 🧪 mac-AS-debug-wk2 ✅ 🧪 gtk-wk2
✅ 🛠 vision-sim ✅ 🧪 mac-wk2-stress ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🧪 mac-intel-wk2 🛠 playstation
✅ 🛠 🧪 unsafe-merge ✅ 🛠 tv ✅ 🛠 mac-safer-cpp
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@jelee53 jelee53 self-assigned this Jun 17, 2025
@jelee53 jelee53 added the New Bugs Unclassified bugs are placed in this component until the correct component can be determined. label Jun 17, 2025
@@ -24290,6 +24290,9 @@
FB39D0701200ED9200088E69 /* Project object */ = {
isa = PBXProject;
attributes = {
KnownAssetTags = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to figure out how to force push a new commit that overwrites this...

Please ignore this for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Updated commit should remove this.

@jelee53 jelee53 force-pushed the eng/Adopt-more-smart-pointers-in-dom-Text branch from af255e6 to 0a81702 Compare June 17, 2025 23:28
@jelee53 jelee53 force-pushed the eng/Adopt-more-smart-pointers-in-dom-Text branch from 0a81702 to d0244c2 Compare June 17, 2025 23:29
@jelee53 jelee53 requested a review from RupinMittal June 17, 2025 23:57
Copy link
Contributor

@RupinMittal RupinMittal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good, a few small comments.

const Text* endText = latestLogicallyAdjacentTextNode(this);
ASSERT(endText);
const Node* onePastEndText = TextNodeTraversal::nextSibling(*endText);
RefPtr startText = const_cast<Text*>(earliestLogicallyAdjacentTextNode(this));
Copy link
Contributor

@RupinMittal RupinMittal Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we don't need the const_cast<Text*> here. You should be able to just do:

RefPtr startText = earliestLogicallyAdjacentTextNode(this);

@jelee53 jelee53 marked this pull request as ready for review June 24, 2025 00:02
@jelee53 jelee53 requested review from rniwa and cdumez as code owners June 24, 2025 00:02
ASSERT(endText);
const Node* onePastEndText = TextNodeTraversal::nextSibling(*endText);
RefPtr startText = const_cast<Text*>(earliestLogicallyAdjacentTextNode(this));
RefPtr endText = const_cast<Text*>(latestLogicallyAdjacentTextNode(this));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Thanks for pointing this out Rupin!

const Node* onePastEndText = TextNodeTraversal::nextSibling(*endText);
RefPtr startText = const_cast<Text*>(earliestLogicallyAdjacentTextNode(this));
RefPtr endText = const_cast<Text*>(latestLogicallyAdjacentTextNode(this));
RefPtr onePastEndText = const_cast<Text*>(TextNodeTraversal::nextSibling(*endText));
Copy link
Contributor

@RupinMittal RupinMittal Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use:

RefPtr<const Node> onePastEndText = TextNodeTraversal::nextSibling(*endText);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe RefPtr would deduce as RefPtr<const Node> without the const_cast. No need to be explicit about it.


StringBuilder result;
for (const Text* text = startText; text != onePastEndText; text = TextNodeTraversal::nextSibling(*text))
for (RefPtr text = startText; text != onePastEndText; text = const_cast<Text*>(TextNodeTraversal::nextSibling(*text)))
Copy link
Contributor

@RupinMittal RupinMittal Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use RefPtr<const Text> and remove the const_cast.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely should avoid const_cast as much as possible.

@cdumez
Copy link
Contributor

cdumez commented Jun 24, 2025

Adopt more smart pointers in dom/Text
https://bugs.webkit.org/show_bug.cgi?id=294640
Include a Radar link (OOPS!).

Please drop the OOPS line or put the radar link if you have one (not mandatory).

@cdumez
Copy link
Contributor

cdumez commented Jun 24, 2025

When starting out, I'd recommend staying out of dom/ as this is performance sensitive code. This PR will have to be A/B tested on Speedometer.

@jelee53 jelee53 force-pushed the eng/Adopt-more-smart-pointers-in-dom-Text branch from d0244c2 to 8c691e4 Compare June 24, 2025 18:28
@jelee53 jelee53 requested a review from pvollan June 24, 2025 20:31
Copy link
Contributor

@RupinMittal RupinMittal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there, but as Chris mentioned, this will have to be A/B tested before landing.

ASSERT(endText);
const Node* onePastEndText = TextNodeTraversal::nextSibling(*endText);
RefPtr<const Text> onePastEndText = TextNodeTraversal::nextSibling(*endText);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep using Node here. So RefPtr<const Node>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I missed that it was using Node originally. Thanks for catching that Rupin!


StringBuilder result;
for (const Text* text = startText; text != onePastEndText; text = TextNodeTraversal::nextSibling(*text))
for (RefPtr<const Text> text = startText; text != onePastEndText; text = TextNodeTraversal::nextSibling(*text))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since startText is RefPtr<const Text> from above, we don't need to specify the <const Text> part again. We can just use RefPtr text here.

Copy link
Contributor Author

@jelee53 jelee53 Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. When we create RefPtr text variable., RefPtr is automatically created with the same type as StartText (aka a constText as the typeParameter). Good point! This has been fixed and thanks Rupin!

@jelee53 jelee53 force-pushed the eng/Adopt-more-smart-pointers-in-dom-Text branch from 8c691e4 to 20a3947 Compare June 24, 2025 22:00
@WebKit WebKit deleted a comment from jelee53 Jun 25, 2025
@jelee53 jelee53 added the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Jun 26, 2025
@webkit-commit-queue
Copy link
Collaborator

@jelee53 does not have committer permissions according to https://raw.githubusercontent.com/WebKit/WebKit/main/metadata/contributors.json.

If you do have committer permissions, please ensure that your GitHub username is added to contributors.json.

Rejecting 20a3947 from merge queue.

@webkit-commit-queue webkit-commit-queue added merging-blocked Applied to prevent a change from being merged and removed unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing labels Jun 26, 2025
@pvollan pvollan added unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing and removed merging-blocked Applied to prevent a change from being merged labels Jun 26, 2025
https://bugs.webkit.org/show_bug.cgi?id=294640
rdar://153684504 (Adopt more smart pointers in dom/Text (294640))

Reviewed by Ryosuke Niwa.

Smart pointer adoption as per the static analyzer.

* Source/WebCore/SaferCPPExpectations/UncheckedLocalVarsCheckerExpectations:
* Source/WebCore/SaferCPPExpectations/UncountedLocalVarsCheckerExpectations:
* Source/WebCore/dom/Text.cpp:
(WebCore::Text::wholeText const):
* Source/WebCore/dom/TextNodeTraversal.cpp:
(WebCore::TextNodeTraversal::appendContents):
(WebCore::TextNodeTraversal::childTextContent):
* Source/WebCore/dom/TextNodeTraversal.h:
(WebCore::TextNodeTraversal::firstTextWithinTemplate):
(WebCore::TextNodeTraversal::traverseNextTextTemplate):

Canonical link: https://commits.webkit.org/296685@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/Adopt-more-smart-pointers-in-dom-Text branch from 20a3947 to dcd1706 Compare June 26, 2025 21:48
@webkit-commit-queue
Copy link
Collaborator

Committed 296685@main (dcd1706): https://commits.webkit.org/296685@main

Reviewed commits have been landed. Closing PR #46876 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit dcd1706 into WebKit:main Jun 26, 2025
@webkit-commit-queue webkit-commit-queue removed the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New Bugs Unclassified bugs are placed in this component until the correct component can be determined.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants