Skip to content

[cxx-interop] Import custom NS_OPTIONS correctly #67865

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

Merged
merged 2 commits into from
Aug 21, 2023
Merged

Conversation

egorzhdan
Copy link
Contributor

This changes the heuristic that we use to detect CF_OPTIONS/NS_OPTIONS instantiations in C++ language mode.

Since libraries sometimes declare custom option types that break the assumptions about the names of such types, this lifts the requirements on the type name.

rdar://113524090
rdar://113279214

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Aug 10, 2023
@egorzhdan egorzhdan requested review from NuriAmari and plotfi August 10, 2023 18:37
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@NuriAmari
Copy link
Contributor

I will take a closer look soon -- I've forgotten the AST structure that we to identify NS_OPTION, so I need to refresh my memory.

That said, in #64043 I made some attempt to incapsulate the heuristic used to identify NS_OPTION into a helper method. Looks like its in the mangler, which probably isn't convenient to get to from the ClangImporter.

That said, I think we should centralize this NS_OPTION identification heuristic logic somewhere, it appears in 4 or 5 places.

@egorzhdan egorzhdan force-pushed the egorzhdan/cfoptions branch from 9fbf8bd to 2250b6d Compare August 17, 2023 15:23
}
if (camel_case::sameWordIgnoreFirstCase(word, "units"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess I don't entirely understand the change here. This seems like a tightening of the rules, before we only checked for an unavailable typedef (which seems insufficient), and now we check for the anonymous enum next to it having the right attributes and matching value names. From the description I thought the idea was to relax the requirements here?

Aside from that, don't we still need the other cases 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.

We're relaxing the requirements imposed on the name of the NS_OPTIONS type. Previously we were only detecting type names ending with certain words here, now we're ignoring the type name (since custom frameworks can declare NS_OPTIONS types with arbitrary names) and relying on the AST structure of an NS_OPTIONS expansion.
So the type name detection cases were intentionally removed.

@egorzhdan
Copy link
Contributor Author

@NuriAmari thanks for pointing me towards that review, I'll unify the NS_OPTION type detection logic.

@NuriAmari
Copy link
Contributor

NuriAmari commented Aug 17, 2023

@NuriAmari thanks for pointing me towards that review, I'll unify the NS_OPTION type detection logic.

There's more adhoc versions of this: https://github.com/apple/swift/blob/be3492152e93da17ca4def14c02bfb7229eb93f0/lib/ClangImporter/ImportType.cpp#L2135 . I think every call to findAnonymousEnumForTypedef should be wrapped in such detection logic.

@egorzhdan egorzhdan force-pushed the egorzhdan/cfoptions branch 2 times, most recently from 18fcadc to 1755b13 Compare August 17, 2023 19:26
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

This changes the heuristic that we use to detect `CF_OPTIONS`/`NS_OPTIONS` instantiations in C++ language mode.

Since libraries sometimes declare custom option types that break the assumptions about the names of such types, this lifts the requirements on the type name.

rdar://113524090
rdar://113279214
@egorzhdan egorzhdan force-pushed the egorzhdan/cfoptions branch from 1755b13 to f20617b Compare August 18, 2023 15:24
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

@swift-ci please test Windows

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

Great. Thank you, Egor!

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.

3 participants