Commit Graph

38 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
John Hui
fa5c4f298f [cxx-interop] Remove NDEBUG check when importing OptionSetTypes 2025-05-21 10:18:05 -07:00
John Hui
80db05455f [cxx-interop] Fix assertion failure from unavailable NS_ENUM typedef
The NS_OPTIONS macro sometimes uses a pattern where it loosely
associates a typedef with an anonymous enum via a shared underlying
integer type (emphasis on "loosely"). The typedef is marked as
unavailable in Swift so as to not cause name ambiguity when we associate
the anonymous enum with said typedef. We use unavailability as
a heuristic during the import process, but that conflates NS_OPTIONS
with NS_ENUMs that can be marked as unavailable for entirely unrelated
reasons.

That in and of itself is fine, because the import logic is general
enough to handle both cases, but we have an assertion that seems to be
unaware of this scenario, and trips on unavailable NS_ENUMs. (In those
cases, the typedef points to the enum rather than the underlying integer
type.) This patch fixes the assertion to be resilient against such cases
by looking through the enum a typedef refers to.

rdar://150399978
2025-05-21 10:16:11 -07:00
John Hui
2c7d01fa91 [NFC] Use explicit ImportedType() constructor 2025-05-19 22:17:08 -07:00
John Hui
5aa5bcfea2 [NFC] [cxx-interop] Flatten findOptionSetEnum() and move it to ImportEnumInfo.cpp
It's at home there alongside other ObjC enum-specific logic, rather than
in the middle of ImportDecl.cpp (since it isn't directly or exclusively
related to importing decls).
2025-05-19 14:21:49 -07:00
John Hui
c7070e73fe [NFC] [cxx-interop] Consolidate uses of findOptionSetType() helper function
Also renames it to findOptionSetEnum() to make it a bit clearer at face
value that the returned ImportedType will contain a Swift enum.

Also refactors some nearby instances of

  if (auto e = dyn_cast<ElaboratedType>(t))
      t = e->desugar();

into a helper function, desugarIfElaborated().
2025-05-19 14:10:49 -07:00
Xi Ge
736ccef626 Merge remote-tracking branch 'apple/main' into rebranch 2024-06-20 15:16:55 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Ben Barham
a4405405a1 [ClangImporter] Rename SwiftVersionedAttr to SwiftVersionedAdditionAttr
Renamed in our LLVM fork 628ee3b842b1fcc93afdebc646220e8ae6302ed6.
2024-04-08 08:58:59 -07:00
Ben Barham
1fdda023b3 Rename StringRef::endswith references to StringRef::ends_with
Missed this when doing the `startswith` renaming. `endswith` has also
been deprecated upstream (and presumably soon to be removed).
2024-04-01 10:59:16 -07: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
Josh Soref
3d488f685e Spelling clangimporter (#42464)
* spelling: enumerators

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: handler

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: heuristic

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: implicitly

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: included

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: instantiate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: integer

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: nested

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: otherthing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overridden

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: simultaneously

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: special

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: typecheck

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unfortunately

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unknown

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: version

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-21 09:31:12 -07:00
zoecarver
965c7ca443 [cxx-interop] Allow anonymous enums to use the name of their base.
If an anonymous enum inherits from a typedef, it will have the typedef's name.
2022-04-18 11:05:58 -07:00
zoecarver
3c5f2c9d52 [cxx-interop] Support scoped enums (enum classes).
Simply treat scoped enums as (pre-existing) "non frozen enums". C++
scoped enums are actually imported as Swift enums (unlike other enums)
and no global variables need be created (given their "scoped" nature).
2020-12-16 13:47:55 -08:00
Bob Wilson
b0dd41b8ab [master-next] Use the new AST getBeginLoc API instead of getLocStart
The getLocStart API is deprecated and slated for removal from LLVM
very soon. Switch to use getBeginLoc instead. I did not see any uses
of the corresponding getLocEnd API.
2019-02-07 16:53:03 -08:00
Ted Kremenek
46510a5eba Bump compiler version to Swift 5.
Main pieces:

- Bump swift-version to 5
- Remove -swift-version 3 support
2018-11-06 14:38:55 -08:00
Bob Wilson
c3e02955bb [master-next] Adjust for VersionTuple moving from clang to llvm.
LLVM r334399 (and related Clang changes) moved clang::VersionTuple to
llvm::VersionTuple. Update Swift to match.

Patch by Jason Molenda.
rdar://problem/41025046
2018-06-12 16:44:11 -07:00
Jordan Rose
6ea47bd603 [ClangImporter] Prefer later enum_extensibility over earlier ones
Since NS_ENUM has an enum_extensibility(open) in it already, we want
to allow people to undo that by sticking enum_extensibility(closed) on
the end of their enum.
2018-03-20 14:49:11 -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
Jordan Rose
138304a29e [ClangImporter] Handle ns_error_domain on nameless enums with typedefs
This doesn't come up when using the NS_ERROR_ENUM macro, but it
shouldn't crash the compiler, and it was easy enough to fix.
2018-01-04 15:06:25 -08:00
Jordan Rose
8a9495a3d1 [ClangImporter] Honor Swift 4 API notes in Swift 3 mode
*** Depends on Clang change "[APINotes] Record what version caused ***
*** an annotation to get replaced." Update your Clang checkout!    ***

More generally, change the meaning of the SwiftVersions section in API
notes to be "this version or earlier" rather than "exactly this
version". We mostly get this behavior for free from the Clang-side
changes, but for SwiftName and the enum annotations we look at inactive
attributes as well. The latter is simple, but the former means being
careful about finding the annotation we /would/ have picked, i.e. the
one closest to the version we requested.
2017-09-15 14:30:24 -07:00
Jordan Rose
43c0289dd4 [ClangImporter] Avoid trying to import enums without definitions. (#11394)
This would fail later down the line anyway, meaning this does not
change the ultimate behavior of the importer, but since I added an
assertion that we're expecting a definition here in 2c68f8d49d, we
need to avoid even asking the question.

Also fix up a few other places where we aren't sure we'll have a
definition when calling these functions.

rdar://problem/33784466
2017-08-09 13:51:03 -07:00
Jordan Rose
2c68f8d49d [ClangImporter] Assert when we're expecting a definition (#11170)
No functionality change.
2017-07-27 09:06:43 -07:00
Jordan Rose
6f7925cb5d [ClangImporter] If enum_extensibility was removed, it's not an enum. (#8992)
(and similar for flag_enum)

This commit prepares the importer for a world in which NS_ENUM and
NS_OPTIONS have adopted the new Clang attributes 'enum_extensibility'
and 'flag_enum', but API notes are used to reverse the effect. Without
this there would be no transition path for adopting the standard Cocoa
macros, which have applied unconditionally up to now.

rdar://problem/18744821
2017-04-25 09:04:16 -07:00
Jordan Rose
a03c40cb2c [ClangImporter] Classify enums using flag_enum and enum_extensibility (#8910)
These new Clang attributes identify whether an enum is intended to
represent an option set or not, and whether the set of cases listed in
the enum declaration is exhaustive. (Swift doesn't currently have a
closed/open distinction for enums, so treat any C enum with
enum_extensibility as a proper closed Swift enum, like we do with
NS_ENUM.)

Enums with neither attribute will continue to be imported as unique
types.

rdar://problem/28476618
2017-04-21 08:57:27 -07:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Hugh Bellamy
071ec47224 Fix errors and warnings build swift/clangImporter using MSVC on Windows (#5950) 2016-11-30 13:27:36 -08: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
practicalswift
17503b0d85 [gardening] Use American English. 2016-11-06 10:12:04 +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
15af7f224d [ClangImporter] Refactor Clang reasoning into new files.
Introduces new files ClangAdapter.h/cpp, which will serve as a
convenient place to put code reasoning about Clang details. Refactors
out most Clang-related is*, has*, and get* methods from the
ImporterImpl. In the future, an adapter class could help serve to
seperate the concerns of the importer from the details of how to
correctly use Clang APIs.
2016-09-12 21:02:09 -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
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