Commit Graph

19 Commits

Author SHA1 Message Date
John Hui
89f8855ad6 [cxx-interop] Check for NS_OPTIONS macro in findOptionSetEnum()
importer::findOptionSetEnum() uses some fragile heuristics to determine
whether a typedef is involved in the construction of a CF_OPTIONS or
NS_OPTIONS type. This patch adds an explicit check that the typedef is
expanded from either of those macros, to prevent, e.g., an unavailable
NS_ENUM, from being mistakenly recognized as an NS_OPTIONS.

Note that doing this is still kind of fragile, and prevents users from
building {NS,CF}_OPTIONS with their own macros. The right thing to do is
probably specifically look for the flag_enum attribute, but that is not
currently what we're doing for reasons whose discovery is left as
an exercise to the future git archaeologist.

This patch also removes (part of) a test case that builds
a CF_OPTIONS-like type with the "SOME_OPTIONS" macro, which is no longer
supported as of this patch.

rdar://150399978
2025-05-21 15:01:09 -07:00
Egor Zhdan
5b7c595b2e [cxx-interop] Remove workarounds for CF_OPTIONS default arguments
ClangImporter has logic that infers default arguments of certain C/C++ types, such as the types declared via `CF_OPTIONS`/`NS_OPTIONS` macros.

There were some workarounds in place which triggered for C++ interop mode specifically. The workarounds were applying a heuristic based on the name of the type, which tried to match the behavior to non-C++ interop mode for certain types from the OS SDK. That was not working well for user-defined types, causing source compatibility breakages when enabling C++ interop.

This change replaces the name-based heuristic with a more robust criteria.

See also 3791ccb6.

rdar://142961112
2025-03-11 12:47:21 +00:00
Egor Zhdan
8d98f50a81 [cxx-interop][rebranch] Handle clang elaborated types properly
Clang recently started wrapping more types in `clang::ElaboratedType` after this change: https://reviews.llvm.org/D112374

This broke some assumptions in ClangImporter, specifically in enum handling logic. This change adjusts those.

Fixes these tests in rebranch:
* `Interop/Cxx/enum/anonymous-with-swift-name-module-interface.swift`
* `Interop/Cxx/enum/anonymous-with-swift-name-objc-module-interface.swift`
* `Interop/Cxx/enum/anonymous-with-swift-name.swift`
* `Interop/Cxx/enum/c-enums-NS_OPTIONS-NS_REFINED_FOR_SWIFT.swift`
* `Interop/Cxx/enum/c-enums-NS_OPTIONS.swift`
* `Interop/Cxx/enum/c-enums-withOptions-omit.swift`
* `Interop/Cxx/class/inheritance/fields-module-interface.swift`
* `Interop/Cxx/class/inheritance/fields.swift`
* `Interop/Cxx/class/inheritance/functions-module-interface.swift`
* `Interop/Cxx/class/inheritance/sub-types-module-interface.swift`
* `Interop/Cxx/class/inheritance/type-aliases-module-interface.swift`
2022-11-22 13:51:23 +00:00
Saleem Abdulrasool
d281b98220 litter the tree with llvm_unreachable
This silences the instances of the warning from Visual Studio about not all
codepaths returning a value.  This makes the output more readable and less
likely to lose useful warnings.  NFC.
2018-09-13 15:26:14 -07:00
Jordan Rose
52af3f3f65 [ClangImporter] Translate Clang's enum_extensibility to @_frozen
Still to do: test and fix up the use of multiple enum_extensibility
annotations, possibly with API notes. This is important because the
definition of NS/CF_ENUM /includes/ enum_extensibility(open) as of
Xcode 9.0; there should be a convenient out people can use to declare
exhaustive enums in C that's backwards-compatible.
2018-03-20 14:49:10 -07:00
Jordan Rose
00b890f9f1 [ClangImporter] Stop copying EnumInfo strings into Swift's ASTContext
They're all kept alive by Clang's ASTContext already. Since the enum
info is only accessible through the Clang importer, that's a
sufficient lifetime. (Note that this only works because all of the
strings are derived from text actually present in the C sources:
either attribute arguments or names of declarations.)

Fixes a small TODO and probably saves a tiny bit of memory usage.
2018-01-04 15:06:26 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Michael Ilseman
1d5ccc0d1b [ClangImporter] EnumInfos are now cached by Clang decl
Now that we have a EnumInfoCache per NameImporter which is per Clang
instance, we can just cache our EnumInfo based off of Clang decl,
rather than using a faux-USR technique. This greatly speeds up and
simplifies enum imports.

EnumInfos are requested about 200k times when building the module
cache for Foundation, and thousands of times for simple tests during
normal import, so efficiency here is important. While building module
caches, the EnumInfoCache is hit 99% of the time, and it is hit 98% of
the time during normal import with pre-built module caches.

NFC.
2016-10-01 17:20:00 -07:00
Michael Ilseman
a2267fdf86 [Clang Importer] Refactor and simplify
Now that we have per-Clang-instance NameImporters, we can drop the
2-phase initialization that was internally present in
NameImporter. This lets NameImporter host the EnumInfoCache directly,
and streamlines the APIs.

NFC
2016-10-01 17:20:00 -07:00
Michael Ilseman
bf77f75aa7 [Clang Importer] Make EnumInfoCache be per-Clang-instance based.
Delay initialization of the EnumInfoCache until a Clang instance is
ready, simplifying its interface and allowing us to finally make this
per-Clang-instance. This will allow us to further de-couple ImportName
from the importer imply, as well as allow us to use a more efficient
and simpler caching mechanism. It is now owned by the NameImporter.

NFC.
2016-10-01 16:37:31 -07:00
Michael Ilseman
7a08549734 [ClangImporter] EnumInfo refactoring
Pull off the caching code from the Impl and onto a new EnumInfoCache
class, that will be useful as we decouple import naming from the Impl
class state.
2016-09-12 21:01:47 -07:00
Doug Gregor
71882c6516 [API Notes] Use API notes rather than overlays to make error enums bridge.
The Clang attribute allows one to state that a particular enumeration
type describes an error, and associates it with a particular domain
constant. However, due to lack of API notes support, this attribute
wasn't actually getting used. Instead, we had a number of explicit
extensions to enum types to make them conform to the _BridgedNSError
protocol explicitly.

Now that we have API notes, use them to make these enums into error
enums with the appropriate domain, so that the Clang importer will
synthesize the _BridgedNSError conformances. Then, remove all of the
explicit conformances---and with them, the overlays for 12 frameworks.

There is a small fix to more eagerly consider these conformances as
"used" if an expression is formed with the error enum as a value
type. This better ensures that the conformances will be available at
runtime when needed.

This cleanup is needed to implement SE-0112 (NSError bridging),
although it is useful by itself.
2016-07-06 09:54:59 -07:00
Michael Ilseman
476f92d2de [Clang Importer] Only strip common prefix for swift_newtype
Rather than do whole-word common-word stripping, we only strip common
prefixes. This is not as powerful as what was originally intended, but
much less magical in the mapping into Swift.

Test case adjusted, and common utility functions exposed.
2016-04-22 16:26:19 -07:00
Doug Gregor
af3769773e [Clang importer] Fix the use-after-free with enum info harder.
This is more of rdar://problem/24869070; StringRefs are leaking out of
importFullName still, in a DenseMap that can get deallocated. When we
cache this data, put them in "permanent" (Swift ASTContext) storage.
2016-03-04 14:37:03 -08:00
Doug Gregor
b2eafa4c85 [Clang importer] Fix a use-after-free in enum info caching.
Also, make the cache kick in between when a module is built and when
it is used. This isn't actually an important case, but it makes it a
whole lot more common to actually see the use-after-free break things.

Fixes rdar://problem/24869070.
2016-03-03 22:08:50 -08:00
practicalswift
50f0d0a7bd [gardening] Fix header consistency in recently introduced files 2016-02-05 17:05:35 +01:00
Michael Ilseman
7c9d53a0a0 [Clang Importer] Import ns_error_domain attribute with _BridgedNSError
ns_error_domain can now be used to communicate with the ClangImporter
when an enum has an associated error domain string. In this case, when
we import it as a Swift enum, we can also synthesize a conformance to
_BridgedNSError.

This allows the creation of something like NS_ERROR_ENUM, in which the
developer can declare an enum for the purposes of error handling. Adds
Sema and executable tests demonstrating this funcionality.

In order for the imported ns_error_domain bridging to work, we have to
at some point forcibly pull in a _BridgedNSError conformance, as one
will not be pulled in normally. This is a problem, and is explicitly
signaled in the provided test case
2016-02-04 17:23:28 -08:00
Michael Ilseman
c8be2873e8 [Clang Importer] Refactor information pertaining to enums out
Introduces new class, EnumInfo, just for the implementation of the
ClangImporter to encapsulate various computed information about the
enum we're importing.

This helps refactor some functionality, aids clarity, and also
prevents us from repeating calculations multiple times, as we were
doing with classifyEnum's macro-expansion tracking. Provides a base
where we can add more heavy lifting in classifyEnum in the future.
2016-02-04 16:57:22 -08:00