Commit Graph

27 Commits

Author SHA1 Message Date
fahadnayyar
49aea31e41 [cxx-interop] Disabling should be annotated with SWIFT_RETURNS_(UN)RETAINED warning (#81411)
In Swift 6.1, we introduced warnings for C++ APIs returning
SWIFT_SHARED_REFERENCE types that lack the SWIFT_RETURNS_(UN)RETAINED
annotations. These warnings serve as a reminder to annotate APIs, as the
absence of these annotations can lead to arbitrary assumptions about the
ownership of returned SWIFT_SHARED_REFERENCE values. This could result
in both use-after-free (memory safety) bugs and memory leaks.

We have received feedback from a few adopters indicating potential false
positive cases where these warnings are triggered. Consequently, we have
decided to disable these warnings in Swift 6.2 and re-qualify the
warnings on larger projects to ensure their effectiveness and eliminate
false positives. Our intention is to re-enable the warnings in later
beta releases of Swift 6.2 once we have stronger evidence of their
effectiveness on large codebases and proof that there are no false
positives.

rdar://150937617
rdar://150800115
2025-05-09 17:36:00 -07:00
Fahad Nayyar
69332f706f [cxx-interop] Diagnose ObjC APIs returning SWIFT_SHARED_REFERENCE types
rdar://142500663
2025-01-08 11:36:49 -08:00
Fahad Nayyar
426d471d1e [cxx-interop] Add support for SWIFT_RETURNS_(UN)RETAINED for ObjC APIs returning C++ FRT
rdar://135360972
2024-12-17 17:31:47 -08:00
Egor Zhdan
80cd6b0795 [cxx-interop] Remove redundant [extern_c] in tests
`[extern_c]` module attribute has no effect in these tests.
2024-10-04 16:50:21 +01:00
Egor Zhdan
62c0a3325f [cxx-interop] Add test for an already-fixed IRGen crash
resolves https://github.com/apple/swift/issues/57308
2024-04-24 18:02:44 +01:00
Egor Zhdan
be24236bea Revert "Revert "[cxx-interop] Look up NSNotificationName in C++ language mode properly""
This reverts commit 80448d2f1c.
2023-11-10 13:26:00 +00:00
Alex Lorenz
21fbd7a48f Revert "Revert "Revert "[cxx-interop] Import custom NS_OPTIONS correctly"""
This reverts commit f7c2257f51.
2023-09-28 18:47:19 -07:00
Egor Zhdan
80448d2f1c Revert "[cxx-interop] Look up NSNotificationName in C++ language mode properly"
This reverts commit e956e8ac1d.
2023-09-25 20:14:00 +01:00
Egor Zhdan
f7c2257f51 Revert "Revert "[cxx-interop] Import custom NS_OPTIONS correctly""
This reverts commit e1d70f06e7.
2023-09-15 13:57:59 +01:00
zoecarver
e1d70f06e7 Revert "[cxx-interop] Import custom NS_OPTIONS correctly"
This reverts commit 46f86f0870.
2023-09-11 11:46:43 -07:00
Egor Zhdan
e956e8ac1d [cxx-interop] Look up NSNotificationName in C++ language mode properly
This change makes sure that `NSNotification.Name.NEVPNStatusDidChange` is imported correctly when C++ interop is enabled.

`importer::findSwiftNewtype` is called while writing Clang modules, at a point when the translation-unit scope does not exist (`clangSema.TUScope` is `nullptr`).

That prevents the Clang lookup from discovering `NSNotificationName` declaration.

Instead of trying to pass a translation-unit scope to Clang, let's use qualified name lookup which does not require a scope.

rdar://112199372
2023-08-30 19:37:44 +01:00
Egor Zhdan
38820c19e8 [cxx-interop] NFC: Fix typo in test module name 2023-08-30 19:33:45 +01:00
Egor Zhdan
69727bb3de [cxx-interop] Add test for INADDR_ANY usage
At some point several months ago the `INADDR_ANY` macro from `Darwin.POSIX` module was not imported into Swift correctly. The issue seems to be resolved now. This change adds a test to make sure we don't regress again.

rdar://107429566
2023-08-22 16:51:02 +01:00
Egor Zhdan
46f86f0870 [cxx-interop] Import custom NS_OPTIONS correctly
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
2023-08-18 15:41:50 +01:00
Egor Zhdan
6edde292f1 [cxx-interop] Import NSStreamDelegate.stream correctly
This Objective-C method
```
- (void)stream:(NSStream *)aStream
   handleEvent:(NSStreamEvent)eventCode;
```
should be imported as
```
optional func stream(
    _ aStream: Stream,
    handle eventCode: Stream.Event)
```
and not
```
optional func stream(
    _ aStream: Stream,
    handleEvent eventCode: Stream.Event)
```
in order to stay compatible with Objective-C interop mode.

rdar://113208675
2023-08-02 17:54:58 +01:00
Egor Zhdan
9edb752ac5 Merge pull request #67301 from apple/egorzhdan/foundation-linker-errors
[cxx-interop] Import CF_OPTIONS types such as `Foundation.NSTextCheckingType` correctly
2023-07-14 20:32:16 +01:00
Egor Zhdan
e6b830b640 [cxx-interop] Import CF_OPTIONS types such as Foundation.NSTextCheckingType correctly
This fixes linker errors which occurred when using Foundation extensions to `Swift.String`, such as `func replacingOccurrences`.

After the last rebranch, Clang started wrapping certain types in `clang::ElaboratedType`. This caused CF_OPTIONS types such as `Foundation.NSTextCheckingType` to be imported incorrectly in C++ language mode (`NSTextCheckingType` was imported as `uint64_t` without getting the special treatment of a CF type), which was causing deserialization errors when Swift tried reading `Foundation.swiftmodule`. Those deserialization errors were silenced by default. The IR for those functions was not emitted, which caused linker errors later.

rdar://109830032
2023-07-14 17:51:49 +01:00
Alex Lorenz
54c49db854 [cxx-interop] NFC, test, drop extern_c from a test module 2023-07-12 09:44:27 -07:00
Egor Zhdan
5a6203251c [cxx-interop] Import OS_OBJECT_DECL consistently with and without C++ interop enabled
Previously ClangImporter was not able to lookup `NSObject` when C++ interop is enabled, which caused types such as `xpc_object_t` from system module XPC to be imported differently: `any OS_xpc_object` without C++ interop vs `any NSObject & OS_xpc_object` with C++ interop enabled.

rdar://110000787
2023-07-04 21:32:17 +01:00
zoecarver
2114833e2e [nfc] Fix notification test 2023-06-23 12:05:11 -07:00
zoecarver
7964918cb2 [cxx-interop] Fix sema lookup scope so we find NSNotification interface even in C++ mode. 2023-06-22 12:21:10 -07:00
Alex Lorenz
4590aa1ede Merge pull request #65813 from hyp/eng/passInRegs
[cxx-interop] Itanium ABI C++ records should have address-only layout when they can't be passed in registers
2023-06-16 07:47:48 -07:00
Puyan Lotfi
fe6ccd7a29 [cxx-interop] Add fix for corner case where NS_OPTIONS typedef has to be desugared
This patch is an add-on to https://github.com/apple/swift/pull/64043.
Essentially when encountering NS_OPTIONS enums, in C++-Interop mode
if they are not specially handled then they can mangle differently than
they do without C++-Interop. This patch adds logic to handle when a
typedef and enum have additional clang::ElaboratedType sugar, but
otherwise it does the same as the existing 64043 patch.

The test case provided was encountered in a real app build. The problem
came from when two modules are each compiled one with and one without
C++-Interop. For the test case code provided the mangling of the
protocol conformance is not consistent and the code in
SILGenLazyConformance.cpp crashes on an invalid conformance with reason
"Invalid conformance in type-checked AST".
2023-06-15 11:17:25 -07:00
Alex Lorenz
49d7e041de [cxx-interop] C++ records should have address-only layout when they can't be passed in registers
This ensures that a C++ record with only ObjC ARC pointers with trivial other members is passed by value in SIL

Fixes https://github.com/apple/swift/issues/61929
2023-05-09 16:23:35 -07:00
Puyan Lotfi
4f2f348356 [C++-Interop] Allow for calling automatically synthesized ctors of arc types
When instantiating C++ types who contain ARC types in the past either
their default constructors are skipped or they are synthesized with
Unmanged<> wrappers. This has made it more cumbersome to instantiate
these from Swift and often requires a static C++ build method to
manually shim the synthesized constructor. The following change drops
the Unmanaged<> so that for the time being these types can be
instantiated directly even if types like Swift -> NSString are not
bridged completely.

(cherry picked from commit accdace5f0438860a0d6760d9598402a9998a324)
(cherry picked from commit 567763c9ec76e14b0d6af5707e50b1674b2c2cbf)
2023-02-15 18:01:04 -05:00
Daniel Rodríguez Troitiño
691dcc9bb3 Test case for const T& in Obj-C++ methods
Add a test case that checks the behaviour introduced during the refactor
of `importFunctionParameterList` and `importMethodParamsAndReturnType`.

The test checks that both Obj-C++ methods and C++ functions treat `const
T&` parameters in the same way.
2022-09-02 11:51:15 -07:00
Alex Langford
9babd8fa8b [cxx-interop][ClangImporter] Fix protocol renaming issue with objc++ interop enabled
When constructing SwiftLookupTable at clang module creation time, calls
to `clang::Sema::LookupName` will fail in C++ mode (including ObjC++).
The specific reason is that `clangSema.TUScope` (which we are passing in
as a Scope) is `nullptr` as we have no active Parser. The C++ path in
`clang::Sema::LookupName` is set to fail early and hard when the Scope
passed in is nullptr. In most, if not all, calls to `LookupName`, we
care about ObjC symbols and not C++ names. For example, the motivation
behind this issue is that ObjC protocols are not being renamed when
there is an ObjC class with the same name, leading to compilation
failures.
2022-04-20 11:52:08 -07:00