Commit Graph

5028 Commits

Author SHA1 Message Date
Alex Lorenz
b25dec58ba [cxx-interop] add a SWIFT_MUTATING customization macro
Fixes https://github.com/apple/swift/issues/66322
2023-06-06 10:54:40 -07:00
Doug Gregor
b374c099ac [Clang importer] Eliminate redundant imports of C++ fields as properties
A recent refactoring uncovered two places where we could end up
importing a C++ field declaration as a property more than once:

1. Importing the declaration context of a field in C++ mode can then
  go import all of the fields. In such a case, check that the field
  we're importing didn't happen already, and bail out early if it did.
  This is common practice in the Clang importer but wasn't happening here.
2. One caller to the function that imported a field from a C++ base
  class into its inheriting class (as a computed property) wasn't
  checking the cache, and therefore created a redundant version.

Fix both issues.
2023-06-03 10:03:10 -07:00
Egor Zhdan
f2771584df [cxx-interop] Do not treat std::pair<UnsafeType, T> as safe
rdar://109529750
2023-06-01 14:00:03 +01:00
Egor Zhdan
25ef5d7016 Merge pull request #66220 from apple/egorzhdan/c-default-init
[cxx-interop] Synthesize a deprecated zero initializer for C++ structs
2023-06-01 11:33:46 +01:00
Egor Zhdan
fec48f9e63 [cxx-interop] Synthesize a deprecated zero initializer for C++ structs
When importing a C header in the C++ language mode, Clang/Swift treat C structs as C++ structs.

Currently Swift synthesizes a default initializer that zero-initializes the backing memory of the struct for C structs, but not for C++ structs.

This is causing issues in existing projects that use C libraries and rely on having the default initializer available in Swift. This change enables the synthesis of a default initializer for C++ structs. Since many C++ structs are not designed to be initialized this way, the initializer is marked as deprecated in Swift.

rdar://109727620
2023-05-31 13:41:25 +01:00
Nuri Amari
f676b75af0 [Swift-ObjC-Interop] Speculative cyclic lookup fix
The lookup to resolve an ObjC forward declaration
to its potential native Swift definition within
a mixed module is becoming cyclic somehow.

The old uncached lookup still works, so it seems
this is an issue with cache invalidation somehow.
Until the route of the issue, use the old uncached
lookup method.
2023-05-28 11:45:54 -07:00
Slava Pestov
b2bc2c72ec AST: Introduce PackElementType 2023-05-25 11:17:30 -04:00
Steven Wu
c2c24145bf Merge pull request #65925 from cachemeifyoucan/eng/PR-swift-frontend-tweaks-for-caching
Cleanup some redundant dependencies and unnecessary works that prevent compiler caching.
2023-05-23 15:32:05 -07:00
Steven Wu
add598762c Merge pull request #65985 from cachemeifyoucan/eng/PR-109411245
[ObjcHeader] Fix objc header generation when pch is explicited passed
2023-05-18 10:30:44 -07:00
Steven Wu
16e4cfae76 [ObjcHeader] Fix objc header generation when pch is explicited passed
When swift-frontend is explicitly passed the pch file as bridging header
on command-line through `-import-objc-header`, it needs to print the
original source file name if needed to the generated objc header.

rdar://109411245
2023-05-17 14:15:55 -07:00
QuietMisdreavus
34faa58b52 [ClangImporter] don't add swift_attr files to a module's auxiliary files (#65867)
rdar://107624995
2023-05-17 09:10:13 -06:00
Steven Wu
f4569a6e98 [Module] Simplify explicit clang module loading
When using explicit module build, there is no need to check top level
module map to see if the module exists or not, since dependency scanning
already pulled in all needed modules. Furthermore, when using clang
include tree, the module maps are not available through FS for this
search. Just directly try to load modules when using explicit module
build.
2023-05-16 16:38:55 -07:00
Steven Wu
cc01780a9f [ClangImporter] Don't add clang path when creating from cc1 commands
When creating ClangImporter directly from cc1 args, there is no need for
clang path, and it might even be mistaken as an input path. Don't use
clang executable path in the arguments when creating from cc1 commands.
2023-05-16 16:38:54 -07:00
Steven Wu
68db1b4de5 [ClangImporter] Don't add cxx shim module map when not needed
Don't add cxx shim when c++ interop is not used. This is to cut down the
unnecessary dependencies that are not seen by the dependency scanner.
2023-05-16 16:38:54 -07:00
Steven Wu
359c37eba7 [EmitPCH] Do not cache PCH in memory during emit-pch action
When pch are explicitly created, there is no need to cached produced pch
in memory since pch is only going to be consumed by a later process.
2023-05-16 16:38:54 -07:00
Artem Chikin
99f545a8eb Merge pull request #65930 from artemcm/SeparateClangTargetHandlingForIRGen
[Explicit Module Builds] Ensure IRGen uses Swift compiler's target triple when '-clang-target' is set.
2023-05-16 15:29:02 -07:00
Kavon Farvardin
878066bcd2 Merge pull request #65937 from kavon/defensive-objc-async-property-import
account for errors importing ObjC method as async prop
2023-05-16 10:58:24 -07:00
Artem Chikin
f2cf55a472 [Explicit Module Builds] Ensure IRGen uses Swift compiler's target triple when '-clang-target' is set.
The Clang importer's Clang instance may be configured with a different (higher)
OS version than the compilation target itself in order to be able to load
pre-compiled Clang modules that are aligned with the broader SDK, and match the
SDK deployment target against which Swift modules are also built. In this case,
we must use the Swift compiler's OS version triple in order to generate the
binary as-requested.

This change makes 'ClangImporter' 'Implementation' keep track of a distinct
'TargetInfo' and 'CodeGenOpts' containers that are meant to be used by clients
in IRGen. When '-clang-target' is not set, they are defined to be copies of the
'ClangImporter's built-in module-loading Clang instance. When '-clang-target' is
set, they are configured with the Swift compilation's target triple and OS
version (but otherwise identical) instead. To distinguish IRGen clients from
module loading clients, 'getModuleAvailabilityTarget' is added for module
loading clients of 'ClangImporter'.

The notion of using a different triple for loading Clang modules arises for the
following reason:
- Swift is able to load Swift modules built against a different target triple
  than the source module that is being compiled. Swift relies on availability
  annotations on the API within the loaded modules to ensure that compilation
  for the current target only uses appropriately-available API from its
  dependencies.
- Clang, in contrast, requires that compilation only ever load modules (.pcm)
  that are precisely aligned to the current source compilation. Because the
  target triple (OS version in particular) between Swift source compilation and
  Swift dependency module compilation may differ, this would otherwise result in
  builtin multiple copies of the same Clang module, against different OS
  versions, once for each different triple in the build graph.
Instead, with Explicitly-Built Modules, Swift sets a '-clang-target' argument
that ensures that all Clang modules participating in the build are built against
the SDK deployment target, matching the Swift modules in the SDK, which allows
them to expose a maximally-available API surface as required by
potentially-depending Swift modules' target OS version.
--------------------------------------------
For example:
Suppose we are building a source module 'Foo', targeting 'macosx10.0', using an
SDK with a deployment target of 'macosx12.0'. Swift modules in said SDK will be
built for 'macosx12.0' (as hard-coded in their textual interfaces), meaning they
may reference symbols expected to be present in dependency Clang modules at that
target OS version.

Suppose the source module 'Foo' depends on Swift module 'Bar', which then
depends on Clang module `Baz`. 'Bar' must be built targeting 'macosx12.0'
(SDK-matching deployment target is hard-coded into its textual interface). Which
means that 'Bar' expects 'Baz' to expose symbols that may only be available when
targeting at least 'macosx12.0'. e.g. 'Baz' may have symbols guarded with
'__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_12_0'. For this reason, we use
'-clang-target' to ensure 'Baz' is built targeting 'macosx12.0', and can be
loaded by both 'Foo' and 'Bar'.

As a result, we cannot direclty use the Clang instance's target triple here and
must check if we need to instead use the triple of the Swift compiler instance.

Resolves rdar://109228963
2023-05-16 09:42:30 -07:00
Kavon Farvardin
f3c4996442 account for errors importing ObjC method as async prop
forgot to check for errors when importing an
ObjC method as an async computed property.

rdar://109377788
2023-05-15 21:15:51 -07:00
Artem Chikin
61ea49c840 Merge pull request #65869 from artemcm/ClangImporterExplicitBuildOptions
[Explicit Module Builds] Add 'ClangImporter' setting for explicitly-built modules
2023-05-15 15:46:29 -07:00
Artem Chikin
e2ea6cb824 [Explicit Module Builds] Add 'ClangImporter' setting for explicitly-built modules
This will mean that '-disable-implicit-swift-modules' also automatically implies two things:
1. Clang modules must also be explicit, and the importer's clang instance will get '-fno-implicit-modules' and '-fno-implicit-module-maps'
2. The importer's clang instance will no longer get a '-fmodules-cache-path=', since it is not needed in explicit builds
2023-05-15 09:45:42 -07:00
Egor Zhdan
3865b5de6f Merge pull request #65715 from apple/egorzhdan/cxx-disable-local-visibility
[cxx-interop] Explicitly disable `-fmodules-local-submodule-visibility`
2023-05-14 18:08:33 +01:00
Hamish Knight
1ffa8eeea4 [ClangImporter] Query Preprocessor::macros instead of getPreprocessingRecord for header printing
This better matches what the clang importer does
normally, avoids a Clang issue where
`getPreprocessedEntitiesInRange` returns incorrect
results, and avoids the need to enable the
preprocessor record. This then lets us re-enable
`print_clang_headers.swift`.

rdar://102151774
2023-05-11 21:10:28 +01:00
Alex Lorenz
5f7f5daa52 [cxx-interop] add a build setting to control whether the <swift/bridging> header is shipped with compiler
rdar://109050483
2023-05-08 15:59:38 -07:00
Egor Zhdan
6e878df4e8 Merge pull request #65740 from apple/egorzhdan/cxx-computed-property
[cxx-interop] Do not mix up computed properties from different records
2023-05-06 23:21:08 +01:00
Egor Zhdan
42b3973de8 [cxx-interop] Do not mix up computed properties from different records
If two different C++ structs have methods with the same name, both annotated with `SWIFT_COMPUTED_PROPERTY`, ClangImporter previously confused them when one of the structs referenced the other struct.

rdar://108990490 / resolves https://github.com/apple/swift/issues/65675
2023-05-06 17:04:08 +01:00
Alex Lorenz
175b64e813 Merge pull request #65665 from hyp/eng/function-template-rvalue-ref
[interop] do not import function template with templated rvalue ref
2023-05-05 14:22:42 -07:00
Egor Zhdan
aed5614ec2 [cxx-interop] Explicitly disable -fmodules-local-submodule-visibility
Clang implicitly enables local submodule visibility when compiling in C++20 mode. ClangImporter does not support it, so let's disable it explicitly.

rdar://108959307 / https://github.com/apple/swift/issues/65710
2023-05-05 21:02:16 +01:00
Allan Shortlidge
0366c42293 Merge pull request #65557 from tshortli/unavailable-decl-optimization-stub
Introduce `stub` mode for `-unavailable-decl-optimization`
2023-05-05 09:11:41 -07:00
eeckstein
3d7df83624 Merge pull request #65647 from eeckstein/fix-enum-stack-protection
clang importer: don't trigger stack protection when accessing an imported enum
2023-05-05 06:35:20 +02:00
Alex Lorenz
196c717a3d [interop] do not import function template with templated rvalue / perfect forwarding ref
they can cause compiler crashes
2023-05-04 14:41:28 -07:00
Zoe Carver
f358cbea8e Merge pull request #65578 from zoecarver/disable-rvalue-ref
[cxx-interop] Disable rvalue references. We don't support them correc…
2023-05-04 10:15:56 -07:00
Erik Eckstein
782b777658 clang importer: don't trigger stack protection when accessing an imported enum
rdar://108867547
2023-05-04 09:49:02 +02:00
Alex Lorenz
50edc0b79b [interop] add more specific diagnostic when rvalue reference parameter isn't imported 2023-05-03 18:29:26 -07:00
Alex Lorenz
62fd801828 [interop] C++ parameter of reference type should be imported as its pointee type in Swift even if parameter's type is type alias
This finally allows std::vector.push_back to be called with the pushed value directly passed into it
2023-05-03 18:23:03 -07:00
Allan Shortlidge
54fe8a17df NFC: Fix warnings. 2023-05-03 15:19:31 -07:00
Alex Lorenz
3e89c4d4d2 Merge pull request #65589 from hyp/eng/bridging-header-fixups
[interop] update <swift/bridging> to have language comment and __has_attribute guard
2023-05-03 10:52:24 -07:00
Alex Lorenz
2fcad59338 [interop] <swift/bridging> guard macros using has_attribute to make this header usable with GCC and MSVC 2023-05-02 10:47:52 -07:00
Alex Lorenz
b55bc19560 [interop] <swift/bridging> header should have a language comment line for IDE recognition 2023-05-02 10:32:00 -07:00
zoecarver
e05daed3b4 [cxx-interop] Disable rvalue references. We don't support them correctly (leading to lifetime issues). We are missing a consuming. 2023-05-01 23:13:33 -07:00
Alex Lorenz
60fd510744 [interop] lookup FRT retain/release functions in top level module
Lookups in submodules do not work and are explicitly ignored in the clang importer
2023-05-01 18:58:39 -07:00
Alex Lorenz
bde105cbf0 Merge pull request #65543 from hyp/eng/revert-c81325461afdc602b0901fa06f9d6f988381622b
Revert "[cxx-interop] Treat un-instantiated templated types as unsafe"
2023-05-01 16:44:01 -07:00
Alex Lorenz
89b1c6391f Revert "[cxx-interop] Treat un-instantiated templated types as unsafe"
This reverts commit c81325461a.

This caused https://github.com/apple/swift/issues/65446
2023-05-01 10:25:47 -07:00
Alex Hoppen
053d215d55 Merge pull request #65427 from ahoppen/ahoppen/macro-attribute-completions
[CodeCompletion] Support completion for macro roles and the 'names:' argument label
2023-04-28 17:20:24 -07:00
Alex Lorenz
3282fb7bc1 Merge pull request #65437 from hyp/eng/nomathinthiscompiler
[interop] avoid importing math functions from the C++ standard library
2023-04-28 07:21:13 -07:00
Alex Hoppen
186b854721 [Parser] Pass code completion status from attribute parsing to decl parsing
This is needed to be able to perform code completion inside `@freestanding` and `@attached`.
2023-04-27 18:13:16 -07:00
Alex Lorenz
8e7766bf18 [interop] avoid importing math functions from the C++ standard library
They cause ambiguities with math functions from platform's libc
2023-04-27 10:25:51 -07:00
Alex Lorenz
4ccacd72dd Merge pull request #65447 from hyp/eng/template-param-type-logic-upgrade
[interop] Ensure an FRT or a pointer to struct/class gets a Swift typ…
2023-04-27 10:19:28 -07:00
Alex Lorenz
0fff76915b [interop] Ensure an FRT or a pointer to struct/class gets a Swift type name for a C++ template parameter
In the follow-up, I should also expand this to cover pointers to builtin types too, but for now lets go with a miminal fix here
2023-04-26 14:48:21 -07:00
Artem Chikin
9d4e831ad3 [Dependency Scanner] Ensure the Clang dependency scanner working directory matches the invocation when querying bridging header dependencies
The Swift compiler does not have a concept of a working directory. It is instead handled by the Swift driver by resolving relative paths according to the driver's working directory argument. On the other hand, Clang does have a concept working directory which may be specified on this Clang invocation with '-working-directory'. If so, it is crucial that we use this directory as an argument to the Clang scanner API. Otherwiswe, we risk having a mismatch between the working directory specified on the scanner's Clang invocation and the one use from the scanner API entry-points, which leads to downstream inconsistencies and errors.

This was originally fixed for the main by-name module dependencies query in https://github.com/apple/swift/pull/61025 (03136e06aa), but the Bridging Header dependencies code has continued to incorrectly expect the Swift ASTContext to both have the working directory set *and* be consistent with that of the Clang scanner instance.

Resolves rdar://108464467
2023-04-26 10:55:01 -07:00