Commit Graph

1896 Commits

Author SHA1 Message Date
Henrik G. Olsson
ec8ea94fb9 Infer @PointerBounds macro from clang __counted_by parameters (#77387)
This results in an automatic wrapper function with safe pointer types
when the imported function has bounds attributes. This exercises similar
pathways as the recently added functionality for specifying macros from
swift_attr. The new functionality is guarded by the experimental
language feature SafeInteropWrappers.

rdar://97942270
2024-12-11 13:36:56 -08:00
Egor Zhdan
282f3b127b [cxx-interop] Support CxxStdlib overlay for libc++ on Linux
This teaches Swift to rebuild the CxxStdlib overlay module from its interface when using a C++ standard library that is not the platform default, specifically libc++ on Linux.

rdar://138838506
2024-12-10 14:19:59 +00:00
Gábor Horváth
745d9b16be Merge pull request #77829 from swiftlang/gaborh/escapability-annotate-STL
[cxx-interop] Inject escapability for the C++ standard library
2024-12-06 15:05:17 +01:00
Gabor Horvath
9027edd430 [cxx-interop] Inject escapability for the C++ standard library
We only add conditional annotations because those do not break backward
compatibility (we might import span and similar view types as
non-escapable in the future). We inject these annotations in the
importer to make sure we have consistent behavior acress the different
standard library implementations. Once we can ship APINotes for the STL
and we have conditional escapability support in APINotes we can migrate
to that solution. But it is not possible as of today and Clang already
has precedent of injecting information for the STL with lifetimebound.

rdar://139065558
2024-12-05 16:27:34 +01:00
Egor Zhdan
8202dfdecd [cxx-interop] Do not emit spurious lifetime diagnostics for C++ types
In rare scenarios, Swift was emitting diagnostics that looked like this:
```
warning: 'import_owned' swift attribute ignored on type 'basic_string': type is not copyable or destructible
```

This change makes sure the compiler does not emit these (incorrect) warnings. See the inline comment for more details.
2024-12-04 16:10:08 +00:00
Gabor Horvath
751bcfd952 [cxx-interop] Support parameter packs in escapability annotations
rdar://140271527
2024-11-22 12:32:51 +00:00
Hamish Knight
23e3f5f5de Merge pull request #77666 from hamishknight/lets-try-this-again
[AST] Remove `ModuleDecl::addFile`
2024-11-21 20:15:58 +00:00
Gábor Horváth
d3064c5fc6 Merge pull request #77678 from swiftlang/gaborh/conditional-escapability
[cxx-interop] Support nested templates for conditional escapability
2024-11-20 13:59:00 +00:00
Gabor Horvath
ad4bfef762 [cxx-interop] Support nested templates for conditional escapability
rdar://140115862
2024-11-18 14:16:40 +00:00
Egor Zhdan
004ef749be Merge pull request #77646 from swiftlang/egorzhdan/reland-copy-ctor
Re-land "[cxx-interop] Check the presence of copy constructor correctly"
2024-11-18 12:50:27 +00:00
Hamish Knight
4946c799af [AST] Remove ModuleDecl::addFile
Rather than exposing an `addFile` member on
ModuleDecl, have the `create` members take a
lambda that populates the files for the module.
Once module construction has finished, the files
are immutable.
2024-11-17 14:17:20 +00:00
Gábor Horváth
5b2f3b7441 Merge pull request #77588 from swiftlang/gaborh/conditional-escapability
[cxx-interop] Support conditional escapability
2024-11-15 18:26:17 +00:00
Egor Zhdan
aa584bf876 [cxx-interop] Check the presence of copy constructor correctly
This reverts commit 3066bd6919.

This re-lands a change after it got reverted because of a regression in the build of SwiftCompilerSources.

rdar://136838485
2024-11-15 15:21:55 +00:00
Gabor Horvath
558380f6f8 [cxx-interop] Support conditional escapability
This PR adds a variadic macro that builds a SwiftAttr string containing
the names of the template type parameters that need to be escapable for
the type to be considered escapable. It also adds logic to interpret
this annotation.

rdar://139065437
2024-11-15 14:27:54 +00:00
Egor Zhdan
3066bd6919 Revert "[cxx-interop] Check the presence of copy constructor correctly"
This reverts commit fbbec48c

The change was causing regressions in certain build configs that need to be investigated.

rdar://139723218
2024-11-14 16:31:27 +00:00
Doug Gregor
11ed132614 [Clang importer + macros] Handle name lookup and type checking for expanded macros
Introduce a number of fixes to allow us to fully use declarations that
are produced by applying a peer macro to an imported declarations.
These changes include:
* Ensuring that we have the right set of imports in the source file
containing the macro expansion, because it depends only on the module
it comes from
* Ensuring that name lookup looks in that file even when the
DeclContext hierarchy doesn't contain the source file (because it's
based on the Clang module structure)

Expand testing to be sure that we're getting the right calls,
diagnostics, and generated IR symbols.
2024-11-13 21:21:56 -08:00
Jan Svoboda
ec086e1a3c [ClangImporter] Fix build after LLVM #9573 (#77557) 2024-11-12 14:50:08 -08:00
Egor Zhdan
6943986c71 [cxx-interop] Import private fields of C++ structs
While private and protected fields coming from C++ cannot be accessed from Swift, they can affect Swift typechecking.

For instance, the Swift typechecker mechanism that adds implicit `Sendable` conformances works by iterating over all of the struct's fields and checking whether all of them are `Sendable`. This logic was broken for C++ types with private fields, since they were never accounted for. This resulted in erroneous implicit `Sendable` confromances being added.

Same applies for `BitwiseCopyable`.

In addition to this, ClangImporter used to mistakenly mark all C++ structs that have private fields as types with unreferenceable storage, which hampered optimizations.

As a side effect of this change, we now also provide a better diagnostic when someone tries to access a private C++ field from Swift.

rdar://134430857
2024-11-12 12:47:26 +00:00
Steven Wu
828fff5eb6 Merge pull request #77485 from cachemeifyoucan/eng/PR-138664252-swift
[ClangImporter] Allow direct explicit load when implicit module is enabled
2024-11-09 09:25:06 -08:00
Steven Wu
ed8032cbc3 [ClangImporter] Allow direct explicit load when implicit module is enabled
Improve clang importor so it can directly load explicit module even
implicit module is enabled. This is a special configuration used by lldb
because lldb sometimes need to load additional modules when binding
external types. This provide the path in clang importer that can load
explicit module without locating the clang module map, while fallback to
module map lookup when implicit module is needed.
2024-11-08 15:34:55 -08:00
Egor Zhdan
dcdcd13036 [cxx-interop] Workaround build error in SwiftCompilerSources
Certain build configurations of SwiftCompilerSources now incorrectly treat `BridgedSwiftObject` as a non-copyable type, causing compiler errors. This is a short-term workaround for these errors.

rdar://138924133
2024-11-08 21:27:37 +00:00
Rintaro Ishizaki
d4db99ce9d [Parse] Remove unnecessary dependencies to Parser.h
C++ swift::Parser is going to be replaced with SwiftParser+ASTGen.
Direct dependencies to it should be removed. Before that, remove
unnecessary '#include "swift/Parse/Parser.h"' to clarify what actually
depends on 'swift::Parser'.

Split 'swift::parseDeclName()' et al. into the dedicated files.
2024-11-02 01:23:59 -07:00
fahadnayyar
f430088616 Merge pull request #76798 from fahadnayyar/frt-return-unannotated-warning
[cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REF…
2024-10-31 17:40:12 -07:00
Gabor Horvath
86e708a39b [cxx-interop] Import more C++ source locations into Swift
Occasionally, when the Swift compiler emits a diagnostic for a construct
that was imported from C++ we get a diagnostic with unknown location.
This is a bad user experience. It is particularly bad with the
borrow-checker related diagnostics. This patch extends the source
location importing to declarations in ClangImporter. There are some
invariants enforced by the Swift compile, e.g., a source range is
comprised of two valid source locations or two invalid ones. As a
result, this patch adds approximate source locations to some separators
like braces or parens that are not maintained by Clang. Having slightly
incorrect ranges in this case is better than emitting unknown source
locations.
2024-10-30 10:58:26 +00:00
fahadnayyar
e640ed637d [cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types 2024-10-29 17:31:15 -07:00
Egor Zhdan
ffb22d7287 Merge pull request #73297 from swiftlang/egorzhdan/correct-check-for-copy-ctor
[cxx-interop] Check the presence of copy constructor correctly
2024-10-29 17:22:49 +00:00
Egor Zhdan
fbbec48c76 [cxx-interop] Check the presence of copy constructor correctly
This removes a longstanding workaround in the import logic for C++ structs:
Swift assumed that if a C++ struct has no copy constructor that is explicitly deleted, then the struct is copyable. This is not actually correct. This replaces the workaround with a proper check for the presence of a C++ copy constructor.

rdar://136838485
2024-10-28 20:05:57 +00:00
swift-ci
72676c2c4a Merge remote-tracking branch 'origin/main' into rebranch 2024-10-08 15:43:57 -07:00
Doug Gregor
f53670be08 Move _SWIFT_ATTR_SUPPORTS_MACROS to the right place 2024-10-08 11:25:46 -07:00
Doug Gregor
688dd234b4 Define __SWIFT_ATTR_SUPPORTS_MACROS when the MacrosOnImports features is available 2024-10-08 11:12:55 -07:00
Doug Gregor
cdcabd05bd Allow attached macros to be applied to imported C declarations
The Clang importer maps arbitrary attributes spelled with `swift_attr("...")`
over to Swift attributes, using the Swift parser to process those attributes.
Extend this mechanism to allow `swift_attr` to refer to an attached macro,
expanding that macro as needed.

When a macro is applied to an imported declaration, that declaration is
pretty-printed (from the C++ AST) to provide to the macro implementation.
There are a few games we need to place to resolve the macro, and a few more
to lazily perform pretty-printing and adjust source locations to get the
right information to the macro, but this demonstrates that we could
take this path.

As an example, we use this mechanism to add an `async` version of a C
function that delivers its result via completion handler, using the
`@AddAsync` example macro implementation from the swift-syntax
repository.
2024-10-08 11:12:50 -07:00
Steven Wu
ab219d246e [ClangImporter] Update the error condition when delayed inputs are used
Return error only when no inputs are available, instead of relying on
checking no errors occured in diagnostics.
2024-10-03 11:23:16 -07:00
swift-ci
fa8b276b48 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-03 07:54:56 -07:00
Egor Zhdan
c874e6f84c Merge pull request #76119 from finagolfin/droid
[test] Check that the C/C++ module maps are looked for in `-resource-dir` before `-sdk`
2024-10-03 15:36:35 +01:00
Steven Wu
8d2d92a96f [ClangImporter][CAS] Add delay init CAS inputs in clang importer
Handle the special clang instance usage in clang importer to make sure
the inputs from CAS are initialized before perform frontend actions.
2024-10-01 11:33:43 -07:00
swift-ci
c4877a5680 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-01 10:54:18 -07:00
Alastair Houghton
ac750a6aec Merge pull request #76545 from al45tair/eng/PR-115918181
[ClangImporter][ModuleWrap] Turn off libc warnings.
2024-10-01 18:46:06 +01:00
Alastair Houghton
bace89ffbd [ClangImporter] Just turn off the warning, not the actual overlays.
We should still try adding the overlays, even if we're asked not to
generate a diagnostic while doing so.  That's slightly safer because
it means that we're less likely to find ourselves in a situation
where `swift-modulewrap` wants to use types from the C/C++ library
and can't.

rdar://115918181
2024-09-30 17:02:41 +01:00
swift-ci
59a7eb4710 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-27 06:15:16 -07:00
Gabor Horvath
cbd8cdfe95 [cxx-interop] Add rules to recognize escapability of aggregates
For now, this logic is used for importing fewer unannotated types as
unsafe. In the future, this logic will be used by escapability inference
for other (non-aggregate) types.
2024-09-24 16:41:00 +01:00
swift-ci
df338c19a8 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-19 09:34:45 -07:00
Gabor Horvath
929c0ca7d8 [cxx-interop] Introduce a safe C++ interop mode
In this mode all C++ types are imported as unsafe by default. Users
explicitly marking types are escapable or not escapable can make them
imported as safe. In the future, we also want to import unannotated
functions as unsafe and add more logic to infer types that are actually
safe, like agregates of escapable types.
2024-09-19 12:34:07 +01:00
Alastair Houghton
6e48b1d781 [ClangImporter][ModuleWrap] Turn off libc warnings.
`swift-modulewrap` uses the `ClangImporter` to obtain a module loader,
but it doesn't take an SDK argument (nor does anything bother to pass
one), which means that when cross-compiling you get warnings about not
being able to find the C library.

Suppress the warning by telling the `ClangImporter` that we don't care
about the C library here.

rdar://115918181
2024-09-18 13:56:53 +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
fahadnayyar
7753ad0885 Merge pull request #76131 from fahadnayyar/cxx-interop-frt-retain-release-diagnostics
Fixed diagnostics for incorrect parameters of retain/release function…
2024-09-11 10:24:20 -07:00
fahadnayyar
f3f4e1913b Fixed diagnostics for incorrect signature of retain/release functions of SWIFT_SHARED_REFERENCE C++ types. 2024-09-10 16:15:07 -07:00
Alexis Laferrière
839f99ec40 Sema: Register clang's export_as in ModuleDecl::getExportAsModule
This service on ModuleDecl wasn't actually used before this PR. The main
client in ASTPrinter calls direclty the underlying logic in FileUnit.
Let's update it for our needs.
2024-09-09 16:57:46 -07:00
swift-ci
189d9fd1e3 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-05 14:54:27 -07:00
Saleem Abdulrasool
a8f47b3ef5 ClangImporter: support SDK for APINotes
Honour the SDK for APINotes to augment the system libraries. This allows
us to distribute APINotes with the Swift SDK and impact the system
without having to map the APINotes into the filesystem.
2024-09-04 15:59:57 -07:00
swift-ci
f1877410ef Merge remote-tracking branch 'origin/main' into rebranch 2024-09-03 02:53:13 -07:00