Commit Graph

530 Commits

Author SHA1 Message Date
Egor Zhdan
2db0e8aea8 Merge pull request #85439 from egorzhdan/egorzhdan/endif-objc
[cxx-interop] Emit `#endif // defined(__OBJC__)` with the comment consistently
2025-11-13 03:59:17 +00:00
Egor Zhdan
8cafef09c3 [cxx-interop] Emit #endif // defined(__OBJC__) with the comment consistently
This makes sure that whenever we emit `#if defined(__OBJC__)`, the matching `#endif` has a comment `// defined(__OBJC__)`.

This makes both testing and reading the header file easier.
2025-11-12 11:51:10 +00:00
Alexis Laferrière
94113f4a83 SE-496: Remove references to features CDecl and CImplementation 2025-10-29 17:31:20 -07:00
Alexis Laferrière
000c2604b0 SE-495: Make @c an official feature 2025-10-29 11:55:41 -07:00
Alexis Laferrière
659692e834 Tests: Update all uses of @c to use the identifier format 2025-10-24 11:55:34 -07:00
Gabor Horvath
adca01b1e5 [cxx-interop] Add flag to set minimum access level for reverse interop
rdar://159211965
2025-10-20 10:27:50 -07:00
Ramon Asuncion
b9b0937551 [Test][PrintAsObjC] Remove process substitution for lit compatibility 2025-10-15 14:36:28 -07:00
Alexis Laferrière
9ec824c20b Parser: Rename the experimental attribute @cdecl to @c
There's no users of `@cdecl` yet so we can do a direct rename. The
legacy `@_cdecl` remains unaffected.
2025-09-19 11:55:07 -07:00
Alexis Laferrière
839a334e05 PrintAsClang: Fix unicode-scalar-reference.swift on watchOS
rdar://158076988
2025-08-12 08:57:10 -07:00
Alexis Laferrière
d46655e538 PrintAsClang: Disable Float16 check on platforms where it's unsupported 2025-08-06 14:32:23 -07:00
Alexis Laferrière
20c7334db4 PrintAsClang: Print Unicode.Scalar as char32_t and Float16 as _Float16 2025-08-05 14:31:30 -07:00
Alexis Laferrière
ed61bcf121 PrintAsClang: Use TypeDecl to associate Swift types to C printed type names
There was a hard to see misalignment between types accepted as
representable in C languages defined in `BuiltinMappedTypes.def` and
those with an associated C type name defined in `PrimitiveTypeMapping`.
While `BuiltinMappedTypes` looked through typealiases,
`PrimitiveTypeMapping` didn't and instead referred only to types by
their identifier. So even when both files defined the same types by
name, their underlying type were only partially handled. An affected
type was `Unicode.Scalar` which is the underlying type of `CWideChar`
and `CChar32`. `Unicode.Scalar` was accepted as a C representable type
but was not printed using a corresponding C type name, breaking the
generated compatibility header.

Another issue with the use of identifiers in the PrimitiveTypeMapping
logic was a limited support for nested types. It would track
`Unicode.Scalar` only as `Scalar`.

This change updates `PrimitiveTypeMapping` to keep track of TypeDecls
instead of identifiers and look through typealiases. This fixes the
issue with `Unicode.Scalar`, allowing it to be printed using the type
defined by an alias and avoid matching an unrelated Scalar. It should
also prevent future types behind typealiases to trigger this same kind
of misalignment.

rdar://157332446
2025-08-05 13:36:37 -07:00
Alexis Laferrière
54362d846c PrintAsClang: Protect null dereference and document Scalar behavior
Fix crash when printing references to Unicode.Scalar in the
compatibility header.

Unicode.Scalar should not be printed in the first place, it's a Swift
struct. It should either be considered non-representable or printed as a
C / Objective-C type if that's the intent.

rdar://157120538
2025-07-31 17:22:11 -07:00
Allan Shortlidge
58bf087a0e PrintAsClang: Add support for availability attrs with custom domains.
Resolves rdar://154510571.
2025-07-23 21:53:05 -07:00
Gábor Horváth
badaf82002 Merge pull request #82907 from Xazax-hun/rebranch-c-header-errors 2025-07-10 07:24:25 +01:00
Gabor Horvath
982049fe18 [cxx-interop] Work around errors in some tests
For some reason, the new version of clang started to emit -Wc++-keyword
warnings in C mode when -Weverything is enabled. This PR suppresses this
warning.

rdar://155324207
2025-07-09 14:52:19 +01:00
Alexis Laferrière
5359656929 PrintAsObjC: Add visibilty test for @cdecl with app vs library behavior 2025-07-02 11:25:49 -07:00
Alexis Laferrière
beb980b1f3 AST: Support @cdecl enums without an explicit C name
Accept `@cdecl` enums without an explicit C name. The C name defaults to
the Swift one. It is printed using the `SWIFT_ENUM` macro instead of
`SWIFT_ENUM_NAMED`.
2025-07-01 14:10:53 -07:00
Alexis Laferrière
529cc3ccb6 Merge pull request #82039 from xymus/cdecl-enum
PrintAsClang: Introduce `@cdecl` enums
2025-06-30 09:56:28 -07:00
Alexis Laferrière
81a0f98783 Merge pull request #82194 from xymus/cdecl-parser
Parser: Accept `@cdecl` with an optional identifier for a custom C name
2025-06-27 15:06:11 -07:00
Alexis Laferrière
89f7ebfb18 Sema: @cdecl functions C name defaults to the Swift name 2025-06-11 12:42:38 -07:00
Alexis Laferrière
2601ff44d4 Parser: Accept @cdecl with an indentifier for the C name
Begin accepting the attribute in the form of `@cdecl(cName)`, using an
identifier instead of a string.

For ease of landing this change we still accept the string form. We
should stop accepting it before making this feature available in
production.
2025-06-11 12:42:38 -07:00
Allan Shortlidge
3f305a48c8 AST: Re-map macOS 16 aligned availability versions to 26.
- watchOS 12 -> 26
- visionOS 3 -> 26
- macos 16 -> 26
- iOS 19 -> 26
- tvOS 19 -> 26

The version numbers for `if #available(...)` queries are intentionally not
re-mapped.
2025-06-10 22:11:05 -07:00
Alexis Laferrière
bd110a073d PrintAsClang: Forward reference enums when used transitively
There are two main scenarios when printing a compatibility header that
references a @cdecl enum defined in Swift code. (1) When defined in the
same module as it's used we can print the definition normally and then
reference it. (2) When used in a different mode we need to print a
forward declaration before we can reference it.

This change adds printing the forward declaration and fix an issue where
the compiler would instead print an @include of the Swift module. The
import of the Swift module would work only in a local scenario where a
compatibility header and module would be generated under the same name.
However for a distributed frameworks we do not distribute the
compatibility header so this strategy doesn't work. Relying on a forward
declaration should be more reliable in all cases but clients may need to
import the other compatibility header explicitly.
2025-06-09 11:54:13 -07:00
Alexis Laferrière
138e2daa3e PrintAsClang: Print @cdecl enums in the compatibility header
Print @cdecl enums in the C section of the compatibility header. Use and
extend the macros to support C compiler clients.

The macro is adapted to the features supported by the client compiler.
It uses an Objective-C style macro with raw type when available and
fallbacks to a simple typedef for C compatibility.
2025-06-09 11:54:13 -07:00
Alexis Laferrière
4f71913f39 PrintAsObjC Tests: Update users of -emit-clang-header-nonmodular-include 2025-05-14 16:04:37 -07:00
Alexis Laferrière
c0889d57c3 PrintAsClang: Print #include for headers used from @cdecl functions
Start printing `#include` for headers referenced from `@cdecl` function
signatures. This adds on top of the existing tiered imports. We already
print each module referenced from decls printed in the compatibility
header. Previously we printed mostly `@import` with an option to
fallback on a `#import`. This change adds a third fallback to `#include`
when the module is referenced from a `@cdecl` function signature. The
bridging header can also be imported in a similar way.
2025-05-14 16:04:37 -07:00
Alexis Laferrière
f6dd432450 PrintAsObjC Test: Test pointer types with @cdecl 2025-05-08 11:31:51 -07:00
Alexis Laferrière
0f98021708 PrintAsObjC Test: Don't use modules for C compatibility header tests 2025-05-06 16:18:49 -07:00
Alexis Laferrière
5e7887de64 PrintAsObjC Test: Don't use blocks in the @cdecl test 2025-05-06 16:18:49 -07:00
Alexis Laferrière
1b0a3d28dc PrintAsObjC Test: Ignore poison directories warnings for C 2025-05-05 11:40:16 -07:00
Alexis Laferrière
a55b190be2 PrintAsObjC Test: Extract what needs Objective-C out of cdecl-official 2025-04-18 15:23:23 -07:00
Alexis Laferrière
bcbdfeeb43 PrintAsObjC Test: Don't use -fobjc-arc when targeting the C language 2025-04-18 15:23:23 -07:00
Alexis Laferrière
02933b5b28 PrintAsClang: Print @cdecl in their own block in compatibility headers
Add a block for C clients in the compatibility header. This block
contains only the `@cdecl` functions that are printed using only C
types.

This C block is printed above the Objective-C and C++ blocks as if we
add support for `@cdecl` types other languages should be able to
reference them in function signatures. Other languages block don't
duplicate printing the `@cdecl` functions either as they are already
accessible to them.
2025-04-18 15:23:23 -07:00
Alexis Laferrière
d5b31328cd PrintAsObjc Test: Test compatibility headers for C 2025-04-18 11:43:27 -07:00
Becca Royal-Gordon
f26a6fb985 [PrintAsClang] Tweak fallback member sort rules
Compare the names of all extension members first, before attempting weirder and more expensive comparisons like stringified type and mangled name. This gives us a sort order that’s a little more comprehensible to humans.
2025-02-18 17:46:20 -08:00
Becca Royal-Gordon
da07ff577c [PrintAsClang] Warn about unstable decl order
PrintAsClang is supposed to emit declarations in the same order regardless of the compiler’s internal state, but we have repeatedly found that our current criteria are inadequate, resulting in non-functionality-affecting changes to generated header content. Add a diagnostic that’s emitted when this happens soliciting a bug report.

Since there *should* be no cases where the compiler fails to order declarations, this diagnostic is never actually emitted. Instead, we test this change by enabling `-verify` on nearly all PrintAsClang tests to make sure they are unaffected.

This did demonstrate a missing criterion that only mattered in C++ mode: extensions that varied only in their generic signature were not sorted stably. Add a sort criterion for this.
2025-02-14 21:41:36 -08:00
Becca Royal-Gordon
d2100b7a67 [PrintAsClang] Beef up extension sorting rule
A couple of the rules that `ModuleContentsWriter::write()` uses to sort declarations didn’t actually work because of an incorrect predicate. In addition, there were a number of situations that could come up in C++ interop (where overloading is permitted) where extensions could not be sorted. Rework extension sorting to look for more kinds of differences between extension members.
2025-02-14 21:40:48 -08:00
Becca Royal-Gordon
60f6afb76c Correct newline emission in generated headers
Eliminates extraneous newlines between top-level Objective-C declarations in `-emit-objc-header` headers. Specifically, there should now always be exactly one—no more, no less—empty line between `@end` and whatever follows it.

Besides being more aesthetically pleasing, this eliminates ordering-dependent behavior where PrintAsClang would print an extra newline when visiting an empty extension, which meant that the order in which empty and non-empty extensions were visited during printing could result in whitespace differences in the compiler output. Printing the blank line is now conditional on whether `tell()` indicates that characters were actually written to the output.

Fixes rdar://143533893.
2025-01-24 16:26:33 -08:00
Daniel Rodríguez Troitiño
48415c982c Merge pull request #76740 from drodriguez/test-swift-features
[test] Improve testing of Swift features
2024-11-02 18:21:39 -07:00
Daniel Rodríguez Troitiño
3eabc0bd25 [test] Ignore unused arguments in PrintAsObjC(++) tests (#77334)
The `%check-in-clang-cxx` seems to pass `-fmodule-cache-path` that is
unused in some setups, but not in `%check-in-clang`. Turn the error back
into a warning to avoid the test failing.
2024-11-02 18:20:35 -07:00
Daniel Rodríguez Troitiño
ba68faaed5 [test] Mark tests that use experimental/upcoming features as such
Find all the usages of `--enable-experimental-feature` or
`--enable-upcoming-feature` in the tests and replace some of the
`REQUIRES: asserts` to use `REQUIRES: swift-feature-Foo` instead, which
should correctly apply to depending on the asserts/noasserts mode of the
toolchain for each feature.

Remove some comments that talked about enabling asserts since they don't
apply anymore (but I might had miss some).

All this was done with an automated script, so some formatting weirdness
might happen, but I hope I fixed most of those.

There might be some tests that were `REQUIRES: asserts` that might run
in `noasserts` toolchains now. This will normally be because their
feature went from experimental to upcoming/base and the tests were not
updated.
2024-11-02 11:46:46 -07:00
swift-ci
6ea9995a81 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-19 18:57:20 -07:00
Allan Shortlidge
cb578172ea Tests: Remove -disable-availability-checking in more tests that use concurrency.
Use the `%target-swift-5.1-abi-triple` substitution to compile the tests for
deployment to the minimum OS versions required for use of _Concurrency APIs,
instead of disabling availability checking.
2024-10-19 12:35:20 -07:00
swift-ci
61d5b48a52 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-02 08:15:55 -07:00
Egor Zhdan
fcb590690e [cxx-interop] Support char8_t C++20 type
https://en.cppreference.com/w/cpp/keyword/char8_t

This is based on a patch from Varun Gandhi: https://github.com/swiftlang/swift/pull/26153

rdar://39988329 / resolves https://github.com/swiftlang/swift/issues/68726
2024-09-27 13:56:03 +01:00
Ben Barham
a7b50f357f Merge remote-tracking branch 'origin/main' into manual-main-merge
Conflicts:
  - `lib/Driver/ToolChains.cpp` conflicting with the `addAllArgs` rename
    for multiple options
2024-09-16 13:53:18 -07:00
Becca Royal-Gordon
5d6ca1c10a Partially revert “Support @objc(CustomName) on extensions”
Revert portions of 07b9fe9ce6 to temporarily avoid applying `@objc(CustomName) extension`s to generated header category names.

Fixes rdar://135924149.
2024-09-12 23:15:26 -07:00
swift-ci
3a84dea9bb Merge remote-tracking branch 'origin/main' into rebranch 2024-08-22 08:54:27 -07:00
Ben Langmuir
fd1875dcfb [test] Move availability tests to later fake OS versions
10.50 was once greater than any real macOS version, but now it compares
less than real released versions, which makes these tests depend on the
deployment target unnecessarily. Update these tests to use even larger
numbers to hopefully keep them independent a little longer.
2024-08-21 11:38:54 -07:00