Commit Graph

6685 Commits

Author SHA1 Message Date
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
Gábor Horváth
981ad6647f Merge pull request #85711 from Xazax-hun/virtual-move-only-types
[cxx-interop] Fix crash when virtual methods take move-only types
2025-12-01 21:09:47 +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
Gábor Horváth
bf1dbc6d36 Merge pull request #85694 from Xazax-hun/const-frts-collision
[cxx-interop] Do not drop CV qualifiers during printing template names
2025-12-01 21:08:28 +00:00
Gabor Horvath
c473c55ce3 [cxx-interop] Fix crash when virtual methods take move-only types
We build forwarding methods to call the virtual methods. The forwarding
methods tried to copy move-only types which resulted in a compiler
crash. Now we try to detect this scenario and insert the required cast
to make sure we get a move instead.

rdar://162195228
2025-11-26 15:41:57 +00:00
Gabor Horvath
cd4e3ccba3 [cxx-interop] Do not drop CV qualifiers during printing template names
Dropping qualifiers can result in collisions that can trigger compiler
crashes or suprious errors. This PR attempts to make sure we always
handle the qualifiers when a qualified type is present in the Clang API.
The handling of pointers is somewhat special as foreign reference types
has custom printing logic.

rdar://164917816
2025-11-25 16:10:06 +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
Mishal Shah
a1b41acf8d Merge pull request #85550 from hjyamauchi/guid
Fix the issue that struct _GUID isn't found on Windows
2025-11-21 09:32:28 -08:00
Slava Pestov
27097430cc Serialization: Lazily deserialize OpaqueTypeDecl's underlying substitutions
We need to serialize the underlying type substitution map for an
inlinable function. However, there is no reason to deserialize it
eagerly, since doing so can lead to cycles. It is better for
correctness and performance to only deserialize it when needed.

Technically this fixes a regression from #84299, but the actual
problem was there all along, it was just exposed by my change
on a specific project.

Fixes rdar://163301203.
2025-11-20 18:13:50 -05: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
Egor Zhdan
6b118aca0d Merge pull request #85488 from CrazyFanFan/optimization/cxx_set_contains
Optimize `contains` method implementation in `CxxSet`
2025-11-20 12:03:52 +00:00
Hiroshi Yamauchi
be641d73ef Fix the GUID type not found error on Windows
Predefined declarations (like _GUID) are special forward declarations
inserted by Clang and aren't serialized into the pcm and their
definition pointers aren't retained across serialization and
deserialization, which causes this type not found error. Avoid putting
non-defining predefined declarations into the swift lookup table when
their definitions exist in the same module so that the definitions
will be associated with the base name and avoid this error.
2025-11-19 17:27:50 -08:00
Xi Ge
443237860c Merge pull request #85549 from nkcsgexi/private-predicate-check
CustomAvailability: synthesized dynamic availability checking function should be private
2025-11-18 18:52:13 -08:00
Steven Wu
e519e59acc Merge pull request #85547 from cachemeifyoucan/eng/PR-164903080
Revert "[Caching][NFC] Using llvm::cas::CASConfiguration"
2025-11-18 09:43:41 -08:00
Xi Ge
d144524209 CustomAvailability: synthesized dynamic availability checking function should be private
When compiling a Swift module in incremental mode, each Swift source file is compiled into an object file
and we use linker to link them together. Because the predicate function for checking dynamic feature
availability is eagerly synthesized per compilation unit, the linker will complain about duplicated
symbols for them. Setting their visibility as private ensures that linker doesn't see them, thus addressing
the linker errors.

One workaround for this problem is to enable WMO.

rdar://164971313
2025-11-18 09:19:26 -08:00
John Hui
f830b1c665 [ClangImporter] Do not import enum when already imported via DeclContext (#85424)
If we try to import this in ObjC interop mode:

```objc
typedef CF_OPTIONS(uint32_t, MyFlags) {
  ...
} CF_SWIFT_NAME(MyCtx.Flags);

struct MyStruct {
  MyFlags flags;
  ...
} CF_SWIFT_NAME(MyCtx);
```

ClangImporter tries to import `MyCtx/MyStruct` before it imports
`MyFlags` (via `importDeclContextOf()`), which in turn tries to import
`MyFlags` again due to the `flags` field. The existing cycle-breaking
mechanism prevents us from looping infinitely, but leads us to import
two copies of the Swift `EnumDecl`, which can cause errors later during
CodeGen.

~~This patch adds an assertion to catch such issues earlier, and breaks
the cycle by checking the cache again.~~ This patch no longer does so
because that caused issues beyond the scope of this patch.

rdar://162317760
2025-11-17 13:18:59 -08: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
Crazy凡
f4eecc9fba optimize contains method in CxxSet 2025-11-14 16:14:05 +08:00
Henrik G. Olsson
33b40cbbe6 Merge pull request #85129 from hnrklssn/swiftify-structured-debug-logging
[ClangImporter] improve structure of swiftify debug logs
2025-11-14 00:12:49 -08:00
Henrik G. Olsson
eb0d894986 [ClangImporter] improve structure of swiftify debug logs
This adds automatic scope management and indentation to the logs to make
them more structured. Also adds some additional logging.
Here's an example of what it can look like now:
```
[swiftify:593] Checking 'CountedByProtocol' protocol for methods with bounds and lifetime info
[swiftify:350] | Checking 'simple::' for bounds and lifetime info
[swiftify:411] | | Checking parameter 'len' with type 'int'
[swiftify:411] | | Checking parameter 'p' with type 'int * __counted_by(len)'
[swiftify:435] | | | Found bounds info 'int * __counted_by(len)'
[swiftify:350] | Checking 'shared:::' for bounds and lifetime info
[swiftify:411] | | Checking parameter 'len' with type 'int'
[swiftify:411] | | Checking parameter 'p1' with type 'int * __counted_by(len)'
[swiftify:435] | | | Found bounds info 'int * __counted_by(len)'
[swiftify:411] | | Checking parameter 'p2' with type 'int * __counted_by(len)'
[swiftify:435] | | | Found bounds info 'int * __counted_by(len)'
[swiftify:350] | Checking 'complexExpr:::' for bounds and lifetime info
[swiftify:411] | | Checking parameter 'len' with type 'int'
[swiftify:411] | | Checking parameter 'offset' with type 'int'
[swiftify:411] | | Checking parameter 'p' with type 'int * __counted_by(len - offset)'
[swiftify:435] | | | Found bounds info 'int * __counted_by(len - offset)'
[swiftify:350] | Checking 'nullUnspecified::' for bounds and lifetime info
[swiftify:411] | | Checking parameter 'len' with type 'int'
[swiftify:411] | | Checking parameter 'p' with type 'int * __counted_by(len) _Null_unspecified'
[swiftify:435] | | | Found bounds info 'int * __counted_by(len) _Null_unspecified'
[swiftify:350] | Checking 'nonnull::' for bounds and lifetime info
[swiftify:411] | | Checking parameter 'len' with type 'int'
[swiftify:411] | | Checking parameter 'p' with type 'int * __counted_by(len) _Nonnull'
[swiftify:435] | | | Found bounds info 'int * __counted_by(len) _Nonnull'
[swiftify:350] | Checking 'nullable::' for bounds and lifetime info
[swiftify:411] | | Checking parameter 'len' with type 'int'
[swiftify:411] | | Checking parameter 'p' with type 'int * __counted_by(len) _Nullable'
[swiftify:435] | | | Found bounds info 'int * __counted_by(len) _Nullable'
[swiftify:350] | Checking 'returnPointer:' for bounds and lifetime info
[swiftify:379] | | Found bounds info 'int * __counted_by(len)' on return value
[swiftify:411] | | Checking parameter 'len' with type 'int'
[swiftify:350] | Checking 'staticMethod::' for bounds and lifetime info
[swiftify:411] | | Checking parameter 'len' with type 'int'
[swiftify:411] | | Checking parameter 'p' with type 'int * __counted_by(len)'
[swiftify:435] | | | Found bounds info 'int * __counted_by(len)'
```
2025-11-13 19:06:49 -08:00
John Hui
b4075fc2a4 [cxx-interop] Refactor importFunctionDecl() (#85475)
This patch re-arranges the implementation of importFunctionDecl() to
delay side-effectful logic as late as possible, to avoid importing or
allocating parts of the AST that we do not need.
2025-11-13 17:41:30 -08:00
Henrik G. Olsson
f1d564ddfe Merge pull request #85123 from hnrklssn/swiftify-obj-c2
[ClangImporter] Attach _SwiftifyImportProtocol to imported protocols with bounds attributes
2025-11-13 12:02:17 -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
Slava Pestov
e8004b73d9 Merge pull request #85459 from slavapestov/rename-generic-environment-operations
AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment()
2025-11-13 02:51:17 -05: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
Hiroshi Yamauchi
a96b57de17 Fix direct clang cc1 emit-pcm commands with vfs overlay on Windows (#85325)
Explicit module builds currently fail on Windows because
direct-clang-cc1-module-build emit-pcm commands take overlaid system
module map files as inputs but miss the clang VFS overlay. This change
adds the overlay and fixes explicit module builds on Windows.
2025-11-12 21:03:15 -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
Anthony Latsis
8992ea82a2 Merge pull request #85433 from swiftlang/jepa-main4
AST: Rename `GenericContext::isGeneric` to `hasGenericParamList`
2025-11-11 21:12:34 +00:00
Anthony Latsis
bda6edb85c AST: Rename GenericContext::isGeneric to hasGenericParamList
`isGeneric` is a misleading name because this method checks for the
existence of a `GenericParamList`, which is not implied by genericity.
2025-11-11 15:55:16 +00:00
Hamish Knight
42cfda1b2a Merge pull request #85409 from hamishknight/fishers-landing
[ClangImporter] Check for builtin conformance in `importNumericLiteral`
2025-11-11 14:10:13 +00:00
Xi Ge
bc9df74304 Merge pull request #85405 from nkcsgexi/serialization-dynamic-check
CustomAvailability: avoid exposing the synthesized function for dynamic availability checking as an API entry
2025-11-10 18:30:58 -08:00
Steven Wu
3067602eb7 Merge pull request #85386 from cachemeifyoucan/eng/PR-164205657
[CAS] Don't leak `-iapinotes-modules` clang flag in caching build
2025-11-10 15:51:15 -08:00
Xi Ge
e31c1cc7f8 CustomAvailability: avoid exposing the synthesized function for dynamic availability checking as an API entry
For dynamic features defined from a header, we synthesize a pure Clang function to check whether the feature should
be enabled at runtime. Swift modules don't have capability to deserialize this clang predicate function, leading to
crasher as a result. This change fixes the crasher by hiding the synthesized function to be a module internal one.

rdar://164410957
2025-11-10 09:26:28 -08:00
Hamish Knight
cdffd55d12 [ClangImporter] Check for builtin conformance in importNumericLiteral
Make sure the destination type actually conforms to the corresponding
builtin literal protocol before attempting to import it. We may want
to consider allowing any type that conforms to the non-builtin literal
protocol, but I want to keep this patch low risk and just ensure we at
least don't crash for now.

rdar://156524292
2025-11-10 15:57:33 +00:00
Doug Gregor
020b69d4b6 [SE-0497] Implement @export attribute syntax
Implement the @export(implementation) and @export(interface) attributes
to replace @_alwaysEmitIntoClient and @_neverEmitIntoClient. Provide a
warning + Fix-It to start staging out the very-new
@_neverEmitIntoClient. We'll hold off on pushing folks toward
@_alwaysEmitIntoClient for a little longer.
2025-11-07 22:00:40 -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
Pavel Yaskevich
5f91e49c9c Merge pull request #85105 from xedin/rdar-140928937
[AST/Sema] Allow `Sendable` suppression on Objective-C class declarations
2025-11-07 15:09:35 -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
3255ffbd74 [cxx-interop] Do not import template type arguments
Prior to this patch, we eagerly imported template type arguments.
A consequence of doing so is that we over-instantiate (potentially
unused) type arguments, which causes unnecessary errors.

The only apparent use we have for these type arguments are to check
whether they are unsafe, so that we can mark the instantiated type as
unsafe as well... even if the instantiated type does not use its unsafe
template type argument at all.

Using un-instantiatable types in template type arguments is supported
in C++. The test case included in this patch validates this behavior,
for both missing member and incomplete type errors.

Note, also, that as of this patch, dumping the module interface of
CxxModule actually causes swift-ide-test to emit compiler errors, since
it tries to instantiate the invalid types MissingMember<Empty> and
IncompleteField<Incomplete>. However, these errors are simply swallowed
by swift-ide-test, so they should be harmless, though we should probably
get rid of them entirely in future work.

rdar://145238539
2025-11-07 00:12:44 -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