Commit Graph

56 Commits

Author SHA1 Message Date
Henrik G. Olsson
314d3d520b add required feature 2025-10-21 13:50:06 -07:00
Henrik G. Olsson
c9a62660f3 [ClangImporter] add implicit import of Swift for __ObjC module
Any safe wrapper expansion originating in a bridging header would fail
typechecking because the `__ObjC` module doesn't import the standard
implicit imports. This is because the main module is not available to
inherit implicit imports from when the `__ObjC` module is created. We
don't need all of the implicit modules for safe wrappers, so import
`Swift` specifically.

rdar://163078116
2025-10-20 18:02:28 -07:00
Henrik G. Olsson
634084bc37 [MacrosOnImports] Don't import !swift modules
Inheriting a clang import marked `requires: !swift` will always result
in an error. This skips such imports, which *may* result in name lookup
errors instead, but also may not, depending on the module.

rdar://161795145
2025-10-15 14:25:06 -07:00
Hamish Knight
99f6f60e8f [test] Fix availability in import-as-instance-method.swift
This test can run with non-macOS targets, update the availability
condition to be platform agnostic.
2025-10-06 11:47:16 +01:00
Henrik G. Olsson
ed4f058c01 Merge pull request #84507 from hnrklssn/swiftify-import-as-method
[Swiftify] Add support for free functions imported as instance methods
2025-10-04 00:40:00 -07:00
Henrik G. Olsson
0c3db50e3d fix -verify-additional-file path on windows 2025-10-03 15:25:06 -07:00
Henrik G. Olsson
bfe96dd84b [Swiftify] Reorder RUN lines (NFC)
Since the compiler invokation performing the macro expansion dump
redirects its stderr output, any errors are not displayed by default.
This is extra problematic for test failures in CI. By performing the
compiler invokation with -verify first, errors are shown.
2025-10-02 16:37:44 -07:00
Henrik G. Olsson
439b9e437b use swift/bridging in test 2025-10-02 16:37:43 -07:00
Henrik G. Olsson
748ef20c8c fix tests in CI 2025-10-02 16:37:42 -07:00
Henrik G. Olsson
87642d499a [Swiftify] Add support for free functions imported as instance methods
This adds support for attaching safe interop wrappers to functions
explicitly imported as instance methods using swift_name.

rdar://156288883
2025-10-02 16:37:40 -07:00
Henrik G. Olsson
472644e335 [Interop] Fix tests relying on transitive module imports (NFC) 2025-09-22 14:58:47 -07:00
swift-ci
ce0f17aeac Merge remote-tracking branch 'origin/main' into rebranch 2025-09-19 21:35:40 -07:00
Henrik G. Olsson
33634becd8 [ImportResolution] Remove redundant "Swift" module import
Import resolution now adds the Swift module to the list of imports, so
we no longer need to add it manually.
2025-09-16 21:10:06 -07:00
Henrik G. Olsson
0ea5677aa1 [ImportResolution] Deduplicate top-level clang modules from import list
Importing clang submodules results in an implicit import of the
top-level module as well. This can result in the same TLM being imported
many different times, if multiple submodules are imported from the same
module. This deduplicates these imports.

Other imports are not expected to be duplicated, so care is taken to
only deduplicate clang TLM imports.
2025-09-16 21:10:05 -07:00
Henrik G. Olsson
369fc18c01 [ClangImporter] Only import explicit submodules to the current module
Non-explicit submodules don't need to be explicitly added to the list of
imports to be visible, since their decls are implicitly exported. Skip
these modules even when present in the list of imports. Explicit
submodules are imported *regardless* of whether another module
imports them however.
2025-09-16 21:10:04 -07:00
Henrik G. Olsson
928cc6c83e [ClangImporter] Deduplicate imports copied from submodule to TLM
The imported top-level module inherits the imports of all its
(transitive) submodules. Since multiple submodules can import the same
modules these need to be deduplicated to avoid redundant work.
2025-09-16 21:10:03 -07:00
Henrik G. Olsson
17c0e564e8 Add hidden --dump-source-file-imports flag
This flag dumps all imports for each SourceFile after it's gone through
import resolution. It is only intended for testing purposes.
There are other ways to print imports, but they don't correspond 1:1 to
the imports actually resolved, which is a bit problematic when testing
implicit clang module imports.
2025-09-16 21:09:59 -07:00
Henrik G. Olsson
ef1af64f40 [SwiftifyImport] Add various submodule import test cases
Updates to these test cases will make it easier to follow the effects of
the next commits.
2025-09-08 17:25:37 -07:00
Henrik G. Olsson
3e1358b208 test cxx mode in clang-includes tests 2025-09-08 17:25:36 -07:00
Henrik G. Olsson
13b1faf471 [ClangImporter] Add submodules as implicit imports of wrapper module
Decls in Swift wrapper module may not originate in the top-level clang
module with the same name, since decls in clang submodules are dumped
into the top-level module. This is because Swift has no concept of
submodules. To make sure that any imported decl has access to the same
symbols as the original clang decl had, all transitive submodules, and
their imports, are added as implicit imports of the wrapper module. This
is necessary in the case where a submodule is marked `explicit`.

The content in explicit submodules isn't normally made visible when
importing the parent module. Decls from explicit submodules still
end up in the top-level wrapper module however, so in Swift they do
still need to be visible from the top-level module. This is relevant
for macro expansions, so that they can refer to the same types as the
original decl.
2025-09-08 17:25:35 -07:00
Henrik G. Olsson
9dd52eedc5 fix tests after rebase 2025-09-08 17:25:33 -07:00
Henrik G. Olsson
a43c8474bc [ClangImporter] Add clang module imports to Swift wrapper module
When macros like _SwiftifyImport are added to a wrapper module for a
clang module, they may need to refer to symbols declared in another
clang module that the wrapped module imports (e.g. because they are used
in the original signature). This adds all the imported clang modules as
implicit imports to the wrapper module.

rdar://151611573
2025-09-08 17:25:26 -07:00
swift-ci
4925d9287e Merge remote-tracking branch 'origin/main' into rebranch 2025-09-08 15:34:33 -07:00
Henrik G. Olsson
5ea84522fa update Interop/C/swiftify-import/counted-by-noescape.swift 2025-09-07 12:10:23 -07:00
swift-ci
a06b4d6144 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-07 23:17:13 -07:00
Gabor Horvath
402ad33463 [StrictMemorySafety] Check the safety of return types of calls
Previously, we skipped checking the return type of a function for safety
as we expected to warn at the use of the returned value:

  let x = returnsUnsafe()
  usesUnsafe(x) // warn here

Unfortunately, this resulted in missing some unsafe constructs that can
introduce memory safety issues when the use of the return value had a
different shape resulting in false negatives for cases like:

  return returnsUnsafe()

or

  usesUnsafe(returnsUnsafe())

This PR changes the analysis to always take return types of function
calls into account.

rdar://157237301
2025-08-05 12:16:44 +01:00
swift-ci
e409379385 Merge remote-tracking branch 'origin/main' into rebranch 2025-07-22 22:57:59 -07:00
Meghana Gupta
b705b8fa13 Update tests 2025-07-22 13:07:03 -07:00
Anthony Latsis
4ad7cb646a [test] Fix test failure due to C attribute position
The `stable/20250601` Clang apparently wants an attributed parameter to
have a name, and that the attributes follow the name:

```
/Users/alatsis/Desktop/rebranch/swift/test/Interop/C/swiftify-import/Inputs/counted-by-lifetimebound.h:7:76: error: 'lifetimebound' attribute only applies to parameters and implicit object parameters
int * __counted_by(len) simple(int len, int len2, int * __counted_by(len2) __lifetimebound p);
                                                                           ^
/Users/alatsis/Desktop/rebranch/swift/test/Interop/C/swiftify-import/Inputs/counted-by-lifetimebound.h:5:40: note: expanded from macro '__lifetimebound'
                                       ^
```

Also humor the compiler here:

```
 3 | #define __counted_by(x) __attribute__((__counted_by__(x)))
   |                                                       `- note: expanded from macro '__counted_by'
 4 | #define __counted_by_or_null(x) __attribute__((__counted_by_or_null__(x)))
 5 | #define __lifetimebound __attribute__((lifetimebound))
   :
22 | int * __counted_by(len) noncountedLifetime(int len, int * p __lifetimebound);
23 |
24 | int * __counted_by(13) _Nullable constant(int * _Nullable p __counted_by(13) __lifetimebound);
   |                                                                          `- error: combining '__counted_by' with non-zero count (which cannot be null) and '_Nullable'; did you mean '__counted_by_or_null' instead?
```
2025-07-09 23:06:15 +01:00
Doug Gregor
5900c8a9e3 Update tests for Span back-deployment 2025-07-07 22:57:44 -07:00
Henrik G. Olsson
374658aae0 [Swiftify] Don't import counted_by with suffixed integer literals (#82469)
Integer literal expressions with types that are not of type `int` are
printed with a suffix to indicate the type (e.g. `123U` or `456L` for
`unsigned` and `long`). This is not valid syntax for integer literals in
Swift, so until we fully translate the count expr syntax to Swift we
need to avoid importing these count expressions.

Also fixes some -Werror related stuff in test cases.

rdar://154141719
2025-06-27 09:27:50 -07:00
Meghana Gupta
44e05fa858 [NFC] Update tests and diagnostics 2025-06-07 12:49:01 -07:00
Henrik G. Olsson
114a1a924c address review comments 2025-06-04 11:26:15 -07:00
Henrik G. Olsson
4a72c1ee5c [Swiftify] Enable warnings-as-errors in interop test cases (NFC)
This enables -strict-memory-safety -warnings-as-errors on the Swift side
to verify that the macro expansions don't cause any warnings and that
they use `unsafe` correctly. On the clang side it enables -Xcc -Werror.
To reduce noise in the test output and pass -Werror cleanly it also
enables -Xcc -Wno-nullability-completeness. This will make it easier to
detect mistakes when writing tests, because warnings stand out whereas
previously they've been drowned out in the noise.
2025-05-29 13:32:57 -07:00
Henrik G. Olsson
0f0a2ca4bb [Swiftify] Support __sized_by on byte-sized pointee types
Previously we would emit a macro that would error on expansion when
trying to add a safe wrapper to a function with __sized_by on a type
that mapped to UnsafePointer<T> instead of UnsafeRawPointer or
OpaquePointer. __sized_by is acceptable when used on byte-sized pointee
types, so this adds machinery in the macro expansion to support that.
Meanwhile on the ClangImporter side, we add a check so that __sized_by
on pointee types with a size is ignored if that size is larger than 1
byte.

When _SwiftifyImport applies .sizedBy to a pointer of type
UnsafePointer<T> it will still map it to a
RawSpan/UnsafeRawBufferPointer in the safe overload. The assumption is
that any API using __sized_by is dealing with raw bytes, so raw pointers
are a better Swift abstraction than UnsafePointer<CChar> etc. It also
lets the user avoid doing a scary pointer cast from some potentially
larger-than-byte-sized pointer to a byte-sized pointer. Casts to
RawPointers are generally safer and more ergonomic.

rdar://150966684
rdar://150966021
2025-05-29 13:32:53 -07:00
Henrik G. Olsson
f5fa481205 [Swiftify] Always remove count parameters when possible (#81585)
Previously we did not remove count parameters if any count parameters
were shared between count expressions, or if any count expression
contained operations. Buffer sizes were also just checked to be larger
than or equal than the given count.

We now extract the count from Spans/BufferPointers whenever possible,
and store that value in a variable at the start of the function. If
multiple parameters share the same count, a bounds check is emitted to
make sure that they have the same size. Subspans can be used if one span
is larger than necessary.

The message in the bounds check is changed so that it includes the
expected and actual value, to aid in debugging.

This patch also fixes some incorrect indentation, and adds the
Whitespace.swift test case to act as a regression test in case the
indentation changes, since the other test cases don't use significant
whitespace.

rdar://151488820
rdar://151511090
rdar://146333006
rdar://147715799
2025-05-24 22:08:51 -07:00
Henrik G. Olsson
0f312adb92 [Swiftify] Always annotate overloads with @_disfavoredOverload (#81579)
Previously we would only add @_disfavoredOverload if the only type
changed was the return type, because in any other case it is unambiguous
which overload to call. However it is still ambiguous when storing the
function as a value rather than calling the function, unless explicit
type annotations are used.

To avoid breaking any existing code, this patch adds
@_disfavoredOverload to every overload generated by @_SwiftifyImport.

rdar://151206394
2025-05-23 21:21:49 -07:00
Henrik G. Olsson
ebe2c60f43 [Swiftify] Escape param decl refs (#81550)
Parameters can be named with keywords without escaping, because it's
unambiguous in the grammar that they are parameters. They still need to
escaped when referred to inside the function body however. This escapes
all references to parameters using backticks.

Parameter names are also checked for clashes with the function name - in
such cases the parameter is renamed in the same way as unnamed
parameters.

rdar://151024645
2025-05-23 12:33:58 -07:00
Henrik G. Olsson
526c68333c [Swiftify] Fix __sized_by and nullable return values (#81693)
Nullable return Spans did not include __swiftifyOverrideLifetime,
resulting in a lifetime error when returning the Span. Meanwhile return
values for __sized_by did not use the correct label for the call to the
RawSpan initializer, using `count` instead of `byteCount`.

rdar://151804085
rdar://151799287
2025-05-22 15:34:58 -07:00
Henrik G. Olsson
6534b9b14f [Swiftify] Copy doc comment from clang node (#81584)
Swift nodes imported from clang don't have doc comments carried over,
but IDEs are clever enough to fetch the comments from the associated
clang node. The swift node in the macro expansion from _SwiftifyImport
doesn't have a clang node directly associated with it however.

This patch adds the same comment from the clang node to the
_SwiftifyImport macro invocation node. Since the macro has access to
this node, it can easily copy over its leading trivia.

For now the comment is not altered at all, meaning @param still remains
even if the parmeter is removed.

rdar://151346977
2025-05-20 08:06:20 -07:00
Henrik G. Olsson
e1eb960e7f [Swiftify] Don't create safe wrapper for autoreleasing pointers (#81568)
_SwiftifyImport doesn't know how to handle
AutoreleasingUnsafeMutablePointer, so we should not attach any
.countedBy information for pointers that are imported as this type.

This also adds defensive checks against adding .countedBy to any pointer
type that _SwiftifyImport doesn't know how to transform.

rdar://151479521
2025-05-17 20:33:00 -07:00
John Hui
027a178ada Merge pull request #81395 from j-hui/dont-swiftify-invalid-expr
[Swiftify] Do not swiftify non-Swift-like counted_by exprs
2025-05-09 11:07:46 -07:00
Henrik G. Olsson
8e27947b2a [Swiftify] Handle anonymous parameters (#81384)
_SwiftifyImport would expand with syntax errors if applied to a function
with anonymous parameters, because it would try to refer to parameters
using the name `_`. Detect these cases and create names for unnamed
parameters.

rdar://150955944
2025-05-08 23:28:14 -07:00
John Hui
e5b1f4a251 [Swiftify] Do not swiftify non-Swift-like counted_by exprs
__counted_by (and __sized_by) expressions can have arbitrary C syntax
in them, such as:

    void foo(int * __counted_by(*len) p, int *len);

When @_SwififyImport tries to generate Swift code for this, the
expression `*len` leads to a syntax error, since it isn't valid Swift.

This patch adds a check to ensure we only attach the Swiftify macro to
__counted_by expressions that are also syntactically valid in Swift.

rdar://150956352
2025-05-08 21:13:20 -07:00
Henrik G. Olsson
59d7d3160f [Swiftify] Emit @availability when expansions contain Span (#81320)
This prevents errors when compiling for older targets using a newer
compiler.

rdar://150740330
2025-05-08 16:13:24 -07:00
Henrik G. Olsson
bd233ea26e [Swiftify] Don't use count from Span inside withUnsafeBufferPointer call (#81267) 2025-05-04 01:52:05 -07:00
Henrik G. Olsson
87f5309144 [Swiftify] enable mutable span (#80387)
* [Swiftify] Emit Mutable[Raw]Span when possible

Previously wrappers would use UnsafeMutable[Raw]Pointer for mutable
pointers, and Span for non-const std::span, to prevent the compiler from
complaining that MutableSpan didn't exist.

Now that MutableSpan has landed we can finally emit MutableSpan without
causing compilation errors. While we had (disabled) support for MutableSpan
syntax already, some unexpected semantic errors required additional
changes:
 - Mutable[Raw]Span parameters need to be inout (for mutation)
 - inout ~Escapable paramters need explicit lifetime annotations
 - MutableSpan cannot be directly bitcast to std::span, because it is
   ~Copyable, so they need unwrapping to UnsafeMutableBufferPointer

rdar://147883022

* [Swiftify] Wrap if-expressions in Immediately Called Closures

When parameters in swiftified wrapper functions are nullable, we use
separate branches for the nil and nonnil cases, because
`withUnsafeBufferPointer` (and similar) cannot be called on nil.
If-expressions have some limitations on where they are allowed in the
grammar, and cannot be passed as arguments to a function. As such, when
the return value is also swiftified, we get an error when trying to
pass the if-expression to the UnsafeBufferPointer/Span constructor.
While it isn't pretty, the best way forward seems to be by wrapping the
if-expressions in Immediately Called Closures.

The closures have the side-effect of acting as a barrier for 'unsafe':
unsafe keywords outside the closure do not "reach" unsafe expressions
inside the closure. We therefore have to emit "unsafe" where unsafe
expressions are used, rather than just when returning.

rdar://148153063
2025-03-29 05:05:01 -07:00
Henrik G. Olsson
d1737b9c20 [Swiftify] Add MutableSpan support for std::span, and disable it (#80315)
__counted_by already had MutableSpan support, so add it for std::span
for parity. But since MutableSpan hasn't landed in the standard library
yet, disable emitting it to prevent compilation errors in expansions.

rdar://147882736
2025-03-27 16:37:31 -07:00
Andrew Trick
64a48d08e1 Update tests for strict @lifetime type checking 2025-03-19 11:59:04 -07:00
Henrik G. Olsson
ad426fbc47 [Swiftify] Emit Span for counted_by return values with lifetime info
__counted_by return values with .lifetimeDependence are now mapped to
Span instead of UnsafeBufferPointer. Also fixes bug where std::span
return values would map to Span even if lifetime dependence info was
missing.
2025-02-21 20:30:58 -08:00