Skip to content

[cxx-interop] Update CxxMethodBridging with snake_case #41617

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

cabmeurer
Copy link
Contributor

Update CxxMethodBridging to transform snake_case names

related: #40842

@cabmeurer
Copy link
Contributor Author

Updated the CxxMethodBridging, let me know what changes/fixes or if there is anything else I should add to this pr.

@hyp @zoecarver

@cabmeurer cabmeurer changed the title [cxx-interop] Update CxxMethodBridging with SnakeCase [cxx-interop] Update CxxMethodBridging with snake_case Mar 2, 2022
@zoecarver
Copy link
Contributor

Hey @cabmeurer. Really, sorry. I should have elaborated more in the forum post.

The idea behind this whole set of features is that unlike C++, Swift has a formalized style. As we import APIs we can try to be smart about this and transform them to adopt Swift's style (to a certain extent). It is unlikely that we'll ever want to transform something to snake_case in Swift.

Right now we will transform GetFoo -> foo and getUTF8String -> utf8String. However, we don't know how to transform snake_case, so get_foo turns into _foo and get_utf8_string -> _utf8_string which is not ideal. To fix this, you can update importNameAsCamelCaseName to handle the case where if (classifyNameKind() == NameKind::snake).

You can also look at that PR to see how to write the tests. Start by adding a snake_case struct to implicit-computed-properties.h and then seeing how this changes the output that is checked in implicit-computed-properties-module-interface.swift. Let me know if you need help with any of this.

That being said, this PR looks really good! If we wanted to import something as snake case, this patch would have been perfect :)

@cabmeurer
Copy link
Contributor Author

cabmeurer commented Mar 2, 2022

Hey @cabmeurer. Really, sorry. I should have elaborated more in the forum post.

The idea behind this whole set of features is that unlike C++, Swift has a formalized style. As we import APIs we can try to be smart about this and transform them to adopt Swift's style (to a certain extent). It is unlikely that we'll ever want to transform something to snake_case in Swift.

Right now we will transform GetFoo -> foo and getUTF8String -> utf8String. However, we don't know how to transform snake_case, so get_foo turns into _foo and get_utf8_string -> _utf8_string which is not ideal. To fix this, you can update importNameAsCamelCaseName to handle the case where if (classifyNameKind() == NameKind::snake).

You can also look at that PR to see how to write the tests. Start by adding a snake_case struct to implicit-computed-properties.h and then seeing how this changes the output that is checked in implicit-computed-properties-module-interface.swift. Let me know if you need help with any of this.

That being said, this PR looks really good! If we wanted to import something as snake case, this patch would have been perfect :)

Ahh yeah that makes a lot more sense, will change this PR to allow for get_foo -> _foo and get_utf8_string ->_utf8_string

Thank you!

@zoecarver
Copy link
Contributor

Ahh yeah that makes a lot more sense, will change this PR to allow for get_foo -> _foo and get_utf8_string ->_utf8_string

Well, that's what happens today. What we want is get_foo -> foo and get_utf8_string -> utf8String. Make sense?

@cabmeurer
Copy link
Contributor Author

cabmeurer commented Mar 2, 2022

Oh yeah makes sense

edit: Okay, pushed that changes hopefully this is what we are wanting lol

@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch 3 times, most recently from fd92c9c to 1075dbc Compare March 2, 2022 18:41
@zoecarver
Copy link
Contributor

Okay, pushed that changes hopefully this is what we are wanting lol

Yes! This is perfect. Nicely done!

@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch from 1075dbc to 040ac76 Compare March 2, 2022 20:09
@cabmeurer cabmeurer requested a review from zoecarver March 2, 2022 20:09
@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch from 040ac76 to ae4cc13 Compare March 2, 2022 20:14
@zoecarver
Copy link
Contributor

This looks good but there are a couple issues with the tests. Are you able to run the tests locally?

@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch from ae4cc13 to 211fd5e Compare March 3, 2022 00:23
@cabmeurer
Copy link
Contributor Author

cabmeurer commented Mar 3, 2022

I ran this: utils/run-test --build-dir ../build/Xcode-DebugAssert/swift-macosx-x86_64 test/Interop/Cxx/

what is the best way to run tests locally?

so i am running utils/lit/lit.py -sv /swift-macosx-x86_64/test-macosx-x86_64/ now

@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch 2 times, most recently from 275c08f to 3855614 Compare March 3, 2022 16:17
@cabmeurer
Copy link
Contributor Author

This looks good but there are a couple issues with the tests. Are you able to run the tests locally?

@zoecarver so i made some changes to the tests, that i think should fix it

thank you for being so patient

@cabmeurer cabmeurer requested a review from zoecarver March 3, 2022 21:57
@zoecarver
Copy link
Contributor

so i am running utils/lit/lit.py -sv /swift-macosx-x86_64/test-macosx-x86_64/ now

Yeah, that looks correct. And the tests are passing?

BTW if you just want to run the interop tests, you can run utils/lit/lit.py -sv /swift-macosx-x86_64/test-macosx-x86_64/Interop/Cxx.

@cabmeurer
Copy link
Contributor Author

okay, so when i ran utils/lit/lit.py -sv /swift-macosx-x86_64/test-macosx-x86_64/Interop/Cxx
the output i got was:

Testing Time: 56.03s Unsupported: 8 Failed : 222 28 warning(s) in tests

@zoecarver
Copy link
Contributor

Testing Time: 56.03s Unsupported: 8 Failed : 222 28 warning(s) in tests

Awesome! So the good news is the tests are running. The bad news is the tests are failing 😁

Why might they be failing... Can you post an output? Given that they're all failing, I suspect the stdlib isn't built. But not 100% sure.

@stevapple
Copy link
Contributor

What we want is get_foo -> foo and get_utf8_string -> utf8String. Make sense?

Should we maintain a list of known abbreviations that should keep the same case? It’s not so good to see get_processed_utf8_string to be processedUtf8String in Swift, or get_processed_html_string to be processedHtmlString.

@cabmeurer
Copy link
Contributor Author

What we want is get_foo -> foo and get_utf8_string -> utf8String. Make sense?

Should we maintain a list of known abbreviations that should keep the same case? It’s not so good to see get_processed_utf8_string to be processedUtf8String in Swift, or get_processed_html_string to be processedHtmlString.

I am into that idea, I can bring it up in the interop workgroup

@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch from 3855614 to d4de548 Compare March 6, 2022 07:34
@cabmeurer cabmeurer requested a review from zoecarver March 6, 2022 07:36
@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch from d4de548 to 064d457 Compare March 6, 2022 15:06
@cabmeurer
Copy link
Contributor Author

cabmeurer commented Mar 8, 2022

Awesome! So the good news is the tests are running. The bad news is the tests are failing 😁
Why might they be failing... Can you post an output? Given that they're all failing, I suspect the stdlib isn't built. But not 100% sure.

Hey @zoecarver, my tests are now all passing locally 🙌 let me know if there are any other changes I need to make

@zoecarver zoecarver added the c++ interop Feature: Interoperability with C++ label Mar 8, 2022
@zoecarver
Copy link
Contributor

Hey @zoecarver, my tests are now all passing locally 🙌 let me know if there are any other changes I need to make

Awesome! I'm glad you figured it out! Sorry I was sort of quiet over the past couple days.

@cabmeurer
Copy link
Contributor Author

Hey @zoecarver, my tests are now all passing locally 🙌 let me know if there are any other changes I need to make

Awesome! I'm glad you figured it out! Sorry I was sort of quiet over the past couple days.

All good! Gave me some time to learn more about how our testing works and experiment

@bro4all
Copy link
Contributor

bro4all commented Mar 10, 2022

@swift-ci please test

@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch from 064d457 to 9bbb2fd Compare March 16, 2022 06:31
@cabmeurer
Copy link
Contributor Author

Hey @bro4all, thanks for looking over this! I made some changes, let me know what other changes i need to make

@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch from 9bbb2fd to df47aa4 Compare March 16, 2022 14:25
@cabmeurer cabmeurer requested review from zoecarver and bro4all March 17, 2022 01:54
@@ -96,6 +127,24 @@ struct CXXMethodBridging {
if (output.empty())
return output;

if (classifyNameKind() == NameKind::snake) {
for (std::size_t i = 0; i < output.size(); i++) {
size_t next = i + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

If the last character is an underscore, output[next] isn't checked to exist and is erased. We should check the validity of next.

Suggested change
size_t next = i + 1;
size_t next = i + 1;
if (output[next] == output.end())
return output;

@cabmeurer cabmeurer force-pushed the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch from df47aa4 to 436847b Compare March 18, 2022 21:33
@cabmeurer
Copy link
Contributor Author

Closing this pull request and created a new one: Add snake_case to CXXMethodBridging
Finally had some time to fix my tests and make the necessary changes.

@cabmeurer cabmeurer closed this Mar 19, 2022
@cabmeurer cabmeurer deleted the cxx-interop/Add-snake_case-support-to-CXXMethodBridging branch March 19, 2022 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants