Commit Graph

2084 Commits

Author SHA1 Message Date
Susana Monteiro
91d1d2e06f Merge pull request #85793 from susmonteiro/susmonteiro/diagnose-swift-attrs
[cxx-interop] Diagnose invalid swift attributes
2025-12-11 11:06:41 +00:00
susmonteiro
fb5bc7282f [cxx-interop] Diagnose invalid copyability, escapability and mutability attributes
Fixes: https://github.com/swiftlang/swift/issues/84559
2025-12-10 17:26:14 +00:00
Egor Zhdan
ad56e061af Merge pull request #85066 from egorzhdan/egorzhdan/std-function-context
[cxx-interop] Allow initializing `std::function` from Swift capturing closures
2025-12-10 13:35:34 +00:00
Egor Zhdan
7fc815e383 [cxx-interop] Allow initializing std::function from Swift capturing closures
This introduces support for converting a Swift closure that captures variables from its surrounding context into an instance of `std::function`, which is useful for working with C++ APIs that use callbacks.

Each instantiation of `std::function` gets a synthesized Swift constructor that takes a Swift closure. Unlike the previous implementation, the closure is _not_ marked as `@convention(c)`. The body of the constructor is created lazily.

Under the hood, the closure is bitcast to a pair of a function pointer and a context pointer, which are then wrapped in a C++ object, `__SwiftFunctionWrapper`, that manages the lifetime of the context object via calls to `swift_retain`/`swift_release` from the copy constructor and the destructor. The `__SwiftFunctionWrapper` class is templated, and is instantiated by ClangImporter.

rdar://133777029
2025-12-05 15:41:59 +00:00
Anthony Latsis
38c4867e8c Merge pull request #85798 from swiftlang/jepa-main4
Address `llvm::StringSwitch` deprecations in advance
2025-12-04 13:19:08 +00:00
Anthony Latsis
8572b7e38c Address llvm::StringSwitch deprecations in advance
There's a whole bunch of these, e.g.
- https://github.com/llvm/llvm-project/pull/163405
- https://github.com/llvm/llvm-project/pull/164276
- https://github.com/llvm/llvm-project/pull/165119
- https://github.com/llvm/llvm-project/pull/166016
- https://github.com/llvm/llvm-project/pull/166066
2025-12-02 17:13:17 +00:00
Susana Monteiro
9e216edf5e Merge pull request #85643 from susmonteiro/susmonteiro/remove-annotationonly-flag
[cxx-interop] Remove annotationOnly flag from Escapability request
2025-12-02 14:36:56 +00:00
susmonteiro
b06631e4da [cxx-interop] Remove annotationOnly flag from Escapability request 2025-12-02 09:27:30 +00:00
susmonteiro
fbfcd4d241 [cxx-interop] Implicitly defined copy and move constructors 2025-12-02 09:15:37 +00:00
Susana Monteiro
92cbcb6864 Merge pull request #85485 from susmonteiro/susmonteiro/remove-request-cycles
[cxx-interop] Refactor ClangTypeEscapability and CxxValueSemantics requests
2025-12-01 21:09:03 +00:00
Hamish Knight
7bca421d51 [ClangImporter] Add check for Bool in importNumericLiteral
I somehow missed this in my original patch, make sure we also handle
the bool case here.

rdar://164916048
2025-11-24 12:02:31 +00:00
susmonteiro
791194b0ff Simplify CxxValueSemanticsKind 2025-11-20 18:43:04 +00:00
susmonteiro
de52134e29 Refactor checkConditionalParams 2025-11-20 18:41:01 +00:00
susmonteiro
5dfb76637d Make ClangTypeEscapability request non-recursive 2025-11-20 18:41:01 +00:00
susmonteiro
351644866a Make CxxValueSemantics request non-recursive 2025-11-20 18:41:01 +00:00
Steven Wu
8e68fab034 Revert "[Caching][NFC] Using llvm::cas::CASConfiguration"
This reverts commit 4f059033bb. The change
is actually not NFC since previously, there is a cache in the
CompilerInvocation that prevents the same CAS from the same CASOptions
from being initialized multiple times, which was relied upon when
running inside sub invocation. When switching to a non-caching simple
CASOption types, it causes every single sub instance will create its own
CAS, and it can consume too many file descriptors and causing errors
during dependency scanning.

rdar://164903080
2025-11-17 12:21:53 -08:00
Doug Gregor
b6ba81d721 Merge pull request #85477 from DougGregor/no-mangled-extern-c-decls
[Clang mangling] Don't mangle when Clang says not to
2025-11-13 11:29:33 -08:00
Daniel Rodríguez Troitiño
21ccb27768 [ClangImporter] Undo changes around createClangInvocation (followup to #85445) (#85457)
Feedback in #85445 after it merged pointed out that the changes around
`createClangInvocation` are not necessary because `CompilerInvocation`
do not hold a reference to `clang::DiagnosticOptions`, only the
`clang::driver::Driver` does.

These changes undo the modifications done there and return the code to
the previous state (but keeps the changes around `createClangDriver`
which was causing the use-after-free).
2025-11-13 08:01:37 -08:00
Doug Gregor
c02811d47b [Clang mangling] Don't mangle when Clang says not to
We have been mangling extern "C" symbols when building with C++
interoperability, leading to incorrectly-mangled names such as
_Z6memset that should have been unmangled "memset". Fix this so we get
consistent mangling between C and C++ interoperability modes.

Fixes rdar://164495210.
2025-11-12 22:18:17 -08:00
Slava Pestov
819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Daniel Rodríguez Troitiño
9eca612b86 [ClangImporter] Avoid use-after-free of clang::DiagnosticOptions after rebranch (#85445)
Upstream LLVM in llvm/llvm-project#139584 changed `DiagnosticOptions`
from being a referenced counted object to just be a reference, not owned
by the `clang::DiagnosticEngine`.

In 0981b71090 (part of #82243), the usages
of the Swift repository were adapted to the new memory model, but it
introduced at least one use-after-free and a potential one around the
usage of Clang in the Clang Importer.

This commit tries to fix the use-after-free in both cases, by returning
a `unique_ptr` to the `clang::DiagnosticOptions`, which makes the
lifetime of the `DiagnosticOptions` match the lifetime of the variable
that uses it (normally a `CompilerInvocation`).

Other cases in 0981b71090 should be safe
because the lifetime of the `DiagnosticOptions` do not seem to propagate
beyond the scope of the functions where they live (but I am not fully
sure about the one in `IDETool/CompilerInvocation.cpp` completely).

This was causing compiler crashes during the test
`Interop/Cxx/stdlib/unsupported-stdlib.swift` which eventually uses
`createClangDriver` and tries to emit a diagnostic, which in some cases
was reading the memory from `DiagnosticOptions` when it was already out
of scope.
2025-11-12 08:38:36 -08:00
John Hui
d5b3079567 Merge pull request #85379 from j-hui/dont-import-template-type-arguments-round-2
[cxx-interop] Make ClangDeclExplicitSafety request non-recursive
[cxx-interop] Do not import template type arguments
[cxx-interop] Check template argument safety in ClangDeclExplicitSafety
2025-11-11 13:44:07 -08:00
John Hui
6b3f8c75cb [cxx-interop] Rename CxxDeclExplicitSafetyDescriptor -> ClangDeclExplicitSafetyDescriptor
Keep the naming convention consistent; this isn't specific to Cxx
2025-11-07 15:24:03 -08:00
John Hui
2b9b507923 [cxx-interop] Add a comment about ClangDeclExplicitSafety return Safe by default 2025-11-07 15:22:03 -08:00
John Hui
6c997ae561 [cxx-interop] Make ClangDeclExplicitSafety request non-recursive
Doing so reduces opportunities for caching, but it's not obvious to me
that we benefit from such fine-grained caching. The benefit here is that
we also avoid the pitfalls of incremental caching, as illustrated by the
added test case. Finally, we eliminate the use of hasActiveRequest(),
which is an anti-pattern.
2025-11-07 15:02:19 -08:00
Steven Wu
d6c39cd360 [CAS] Don't leak -iapinotes-modules clang flag in caching build
The search paths for apinotes are leaked when using compilation caching
but they are not needed like other search path because the apinotes are
found and stored inside include-tree.

rdar://164205657
2025-11-07 11:01:33 -08:00
John Hui
b80ab98ac0 [cxx-interop] Check template argument safety in ClangDeclExplicitSafety
Checking this upon import may overlook annotations that explicitly
mark a type as safe (or unsafe). Instead, we consolidate this logic in
the ClangDeclExplicitSafety request.

rdar://163196609
2025-11-07 00:12:35 -08:00
susmonteiro
2c9bb7a6eb Revert "[cxx-interop] Implicitly defined copy constructors"
This reverts commit 1c5bbe0290.
2025-11-05 10:42:05 +00:00
John Hui
4389cc9bea Revert "[cxx-interop] Mark class templates with unsafe type arguments as unsafe"
This reverts commit afaa499e3a.
2025-11-03 18:03:16 -08:00
John Hui
ad8b5a5204 Revert "[cxx-interop] Use clang::CanQualType in ClangTypeExplicitSafetyDescriptor"
This reverts commit 5328572fbe.
2025-11-03 17:58:34 -08:00
John Hui
6607ae9c56 Revert "[cxx-interop] Check template argument safety in ClangDeclExplicitSafety"
This reverts commit d2632d21af.
2025-11-03 14:37:05 -08:00
John Hui
a13c95ded6 Revert "[cxx-interop] Avoid cycles in ClangDeclExplicitSafety requests"
This reverts commit 929d0d8c26.
2025-11-03 14:37:03 -08:00
John Hui
80fa8c18ef Revert "[cxx-interop] Avoid cycles in ClangTypeExplicitSafety requests"
This reverts commit d0bad4cebd.
2025-11-03 14:36:47 -08:00
Henrik G. Olsson
c541666b21 Merge pull request #85023 from hnrklssn/swiftify-bridging-header
[ClangImporter] add implicit import of `Swift` for `__ObjC` module
2025-10-31 18:08:47 -07:00
Henrik G. Olsson
87af5538d3 Merge pull request #84871 from hnrklssn/inherit-literal-std
[ImportResolution] Only disallow explicit `std` imports (allow implicit)
2025-10-31 14:22:16 -07:00
susmonteiro
1c5bbe0290 [cxx-interop] Implicitly defined copy constructors 2025-10-30 14:17:19 +00:00
Becca Royal-Gordon
393965090e Merge pull request #34556 from beccadax/mod-squad-2
[SE-0491] Implement lookup and diagnostics for module selectors (MyMod::someName)
2025-10-28 16:00:26 -07:00
Allan Shortlidge
2887334ffb Merge pull request #85167 from tshortli/new-platform-kinds
AST: Introduce the `Swift`, `anyAppleOS`, and `DriverKit` platform kinds
2025-10-28 11:21:38 -07:00
John Hui
d0bad4cebd [cxx-interop] Avoid cycles in ClangTypeExplicitSafety requests
rdar://163511959
2025-10-28 07:50:16 -07:00
Allan Shortlidge
92227ad1cf AST: Introduce the Swift, anyAppleOS, and DriverKit platform kinds.
This change just stages in a few new platform kinds, without fully adding
support for them yet.

- The `Swift` platform represents availability of the Swift runtime across all
  platforms that support an ABI stable Swift runtime (see the pitch at
  https://forums.swift.org/t/pitch-swift-runtime-availability/82742).
- The `anyAppleOS` platform is an experimental platform that represents all of
  Apple's operating systems. This is intended to simplify writing availability
  for Apple's platforms by taking advantage of the new unified OS versioning
  system announced at WWDC 2025.
- The `DriverKit` platform corresponds to Apple DriverKit which is already
  supported by LLVM.
2025-10-27 19:15:04 -07:00
Qiongsi Wu
1cf8d2ede1 [ClangImporter] Fix Build Break Due to clang API changes (#84925)
<!--
If this pull request is targeting a release branch, please fill out the
following form:

https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1

Otherwise, replace this comment with a description of your changes and
rationale. Provide links to external references/discussions if
appropriate.
If this pull request resolves any GitHub issues, link them like so:

  Resolves <link to issue>, resolves <link to another issue>.

For more information about linking a pull request to an issue, see:

https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->

<!--
Before merging this pull request, you must run the Swift continuous
integration tests.
For information about triggering CI builds via @swift-ci, see:

https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci

Thank you for your contribution to Swift!
-->

https://github.com/swiftlang/llvm-project/pull/11631 is cherry-picking
`clang` commits that change `CompilerInstance`'s APIs. The API changes
are causing Swift build breaks. This PR fixes the API breaks.

---------

Co-authored-by: Jan Svoboda <jan@svoboda.ai>
2025-10-27 16:50:56 -07:00
John Hui
90c0509b16 Merge pull request #85095 from j-hui/template-arg-safety-in-req 2025-10-27 11:40:09 -07:00
Becca Royal-Gordon
ffaa35810c Handle lookup into Builtin with module selector
Lookups like Builtin::Int64 were failing because BuiltinUnit rejected all unqualified lookups. Make it allow unqualified lookups with a module selector.
2025-10-24 16:23:48 -07:00
John Hui
929d0d8c26 [cxx-interop] Avoid cycles in ClangDeclExplicitSafety requests 2025-10-24 14:30:29 -07:00
Jan Svoboda
7fef14853b [ClangImporter] Adjust for LLVM changes (IO sandboxing) (#85093) 2025-10-24 08:11:01 -07:00
John Hui
d2632d21af [cxx-interop] Check template argument safety in ClangDeclExplicitSafety
Checking this upon import was overlooking annotations that explicitly
mark a type as safe (or unsafe). Instead, we consolidate this logic in
the ClangDeclExplicitSafety request.

rdar://163196609
2025-10-23 13:05:57 -07:00
Jan Svoboda
be3b16b8c7 Revert "[ClangImporter] Adjust for LLVM changes (IO sandboxing) (#84995)"
This reverts commit 3c3f6e752e.
2025-10-21 15:48:13 -07:00
Henrik G. Olsson
507846cb38 don't try to load stdlib when it's known to fail
This prevents it from being loaded correctly later.
2025-10-21 13:48:31 -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
Jan Svoboda
3c3f6e752e [ClangImporter] Adjust for LLVM changes (IO sandboxing) (#84995) 2025-10-20 14:03:24 -07:00