Commit Graph

1870 Commits

Author SHA1 Message Date
Gabor Horvath
410341671e [SE-0458] Unify escapibility inference for AST and Interop
Interop is injecting escapability annotations for the STL and doing a
limited inference for aggregates. Let's reuse the same facilities in the
AST when we calculate the safety of the foreign types.
2025-02-17 13:41:40 +00:00
Doug Gregor
c0fb9f990a [Strict memory safety] Infer safe/unsafe for imported C types 2025-02-16 00:55:43 -08:00
Gabor Horvath
068815c2a3 [cxx-interop] Remove some duplicated lookups
Repeatedly lookup up a key from a dictionary can be justified whenever
the content of the dictionary might change between the lookups (so any
references into the dictionary might get invalidated). We had a couple
of instances where as far as I can tell no such modifications should
happen between two lookups with identical keys. This PR simplifies the
code to remove the extra lookups. It also removes a dictionary that was
completely unused.
2025-02-14 11:04:23 +00:00
Saleem Abdulrasool
9c85fbc8da AST,DependencyScan,IRGen,Serialization,Tooling: track library style (#78777)
Track if the dependency is static or dynamic. This is in preparation for
helping rename the static library to differentiate it from import
libraries.
2025-02-06 13:22:56 -08:00
Steven Wu
c3550e81ce [Dependency Scan] Do not pass clang's -dwarf-debug-flags to swift
When turning on directCC1 scanning, swift will ask clang driver to
expand all cc1 flags and pass that explicitly on command-line. In such
case, if `RC_DEBUG_OPTIONS` env is set on darwin platform, it will cause
clang driver to encode all the options it gets and pass those clang
driver options as a cc1 options `-dwarf-debug-flags`.

After the change, swift scanner will no longer generate a long
`-Xcc -dwarf-debug-flag -Xcc <long list of flags>` for later
compilation. Losing such information in DWARF doesn't affect swift
debugging. It will instead, make command-line a lot shorter, save spaces
in swift binary module, and make swift caching build more reliable.

rdar://144267483
2025-02-05 16:37:00 -08:00
Steven Wu
9d59044bb1 [BrdigingHeader] Auto bridging header chaining
Add ability to automatically chaining the bridging headers discovered from all
dependencies module when doing swift caching build. This will eliminate all
implicit bridging header imports from the build and make the bridging header
importing behavior much more reliable, while keep the compatibility at maximum.

For example, if the current module A depends on module B and C, and both B and
C are binary modules that uses bridging header, when building module A,
dependency scanner will construct a new header that chains three bridging
headers together with the option to build a PCH from it. This will make all
importing errors more obvious while improving the performance.
2025-02-05 09:41:04 -08:00
Ian Anderson
517ced4f4b [ClangImporter][Tests] clang doesn't support the empty string as a path argument
SIL/verify_all_overlays.py is passing `-F ""` on non-Apple platforms. Swift handles that kind of, but clang doesn't support it and will get argument parsing errors. It's a pathological case, so fix SIL/verify_all_overlays.py to not do that, but also add a failsafe in ClangImporter to not pass on empty paths.

rdar://142441042
2025-02-03 13:06:32 -08:00
Anthony Latsis
34f9b80cbc Merge pull request #78750 from AnthonyLatsis/oryza-sativa
[Gardening] Fix some set but not used variables
2025-01-31 04:29:05 +00:00
John Hui
be73254cdc [cxx-interop] Import private members (#78942)
This commit removes the guardrails in ImportDecl.cpp:SwiftDeclConverter
that prevent it from importing non-public C++ members. It also
accordingly adjusts all code that assumes generated Swift decls should
be public. This commit does not import non-public inherited members;
that needs its own follow-up patch.

Note that Swift enforces stricter invariants about access levels than C++.
For instance, public typealiases cannot be assigned private underlying types,
and public functions cannot take or return private types. Meanwhile,
both of these patterns are supported in C++, where exposing private types
from a class's public interface is considered feature. As far as I am aware,
Swift was already importing such private-containing public decls from C++
already, but I added a test suite, access inversion, that checks and
documents this scenario, to ensure that it doesn't trip any assertions.
2025-01-30 14:50:15 -08:00
Anthony Latsis
a84dfc8387 [Gardening] Fix some set but not used variables 2025-01-30 21:34:38 +00:00
QuietMisdreavus
ab26b8b9d7 add support to getTopLevelDecls for clang submodules (#76401)
rdar://126031510
2025-01-30 09:39:58 -07:00
John Hui
1341516dba [cxx-interop] Fix spurious ambiguous member lookup for eagerly imported members (#78673)
Follow-up from #78132, which did not fix issues related to eagerly imported members like subscripts.

This patch restructures recursive ClangRecordMemberLookup requests to importBaseMemberDecl() in the recursive calls, rather than propagating base member decls up to the initial lookup request and doing the import. Doing so seems to fix lingering resolution issues (which I've added to the regression tests).

rdar://141069984
2025-01-21 22:06:09 -08:00
Egor Zhdan
752cb36239 Merge pull request #78014 from swiftlang/egorzhdan/libcxx-overlay-linux
[cxx-interop] Support CxxStdlib overlay for libc++ on Linux
2025-01-20 15:09:26 +00:00
Allan Shortlidge
5d72e69b2e AST: Resolve an unused field warning in ClangImporter.cpp. 2025-01-14 11:35:40 -08:00
Gábor Horváth
4d4ba2d40d Merge pull request #78515 from swiftlang/gaborh/string-safe
[cxx-interop] Consider std::string safe
2025-01-09 16:13:02 +00:00
Gabor Horvath
838a7547ab [cxx-interop] Consider std::string safe
The std::basic_string class is escapable only if its template argument
is escapable. This change helps us consider the regular std::string type
with the non-escapable char template argument as self-contained and a
safe type to use. This prevents spurious warnings in strict memory
safety mode.
2025-01-09 12:17:52 +00:00
Gabor Horvath
4846c56795 [cxx-interop] Generate safe overloads for non-escapable spans
A previous PR already added support to the SwiftifyImport macro to
generate safe wrappers. This PR makes ClangImporter emit the macro to do
the transformation.
2025-01-08 11:19:35 +00:00
Ian Anderson
cdb42c3535 [ClangImporter] clang's -iframework comes before builtin usr/local/include, but Swift's -Fsystem comes after
When Swift passes search paths to clang, it does so directly into the HeaderSearch. That means that those paths get ordered inconsistently compared to the equivalent clang flag, and causes inconsistencies when building clang modules with clang and with Swift. Instead of touching the HeaderSearch directly, pass Swift search paths as driver flags, just do them after the -Xcc ones.

Swift doesn't have a way to pass a search path to clang as -isystem, only as -I which usually isn't the right flag. Add an -Isystem Swift flag so that those paths can be passed to clang as -isystem.

rdar://93951328
2024-12-23 22:15:52 -08:00
Gábor Horváth
4ee8e0df26 Merge pull request #78115 from swiftlang/gaborh/cpp-span-unsafe
[cxx-interop] Make sure C++ span is imported as @unsafe
2024-12-18 13:20:41 +00:00
John Hui
796075ab88 [cxx-interop] Fix spurious ambiguous member lookup (#78132)
Nested calls to importBaseMemberDecl() subvert its cache and compromise its idempotence, causing the semantic checker to spuriously report ambiguous member lookups when multiple ClangRecordMemberLookup requests are made (e.g., because of an unrelated missing member lookup).

One such scenario is documented as a test case: test/Interop/Cxx/class/inheritance/inherited-lookup-typechecker.swift fails without this patch because of the expected error from the missing member. Meanwhile, test/Interop/Cxx/class/inheritance/inherited-lookup-executable.swift works because it does not attempt to access a missing member.

This patch fixes the issue by only calling importBaseMemberDecl() in the most derived class (where the ClangRecordMemberLookup originated, i.e., not in recursive requests).

As a consequence of my patch, synthesized member accessors in the derived class directly invoke the member from the base class where the member is inherited from, rather than incurring an indirection at each level of inheritance. As such, the synthesized symbol names are different (and shorter). I've taken this opportunity to update the relevant tests to // CHECK for more of the mangled symbol, rather than only the synthesized symbol prefix, for more precise testing and slightly better readability.

rdar://141069984
2024-12-17 11:40:18 -08:00
Gabor Horvath
985c3a44f8 [cxx-interop] Make sure C++ span is imported as @unsafe
The C++ span should be a non-escapable type but is imported as escapable
for backward compatibility reason. This is inherently unsafe, so make
sure std::span is imported as such. In the future, we plan to generate
safe overloads using Swift's Span and that will be the preferred way of
using the API.
2024-12-16 17:11:22 +00:00
Steven Wu
90176b3f5a Merge pull request #78138 from cachemeifyoucan/eng/PR-141284501
[ClangImporter] Share CASOptions when cloning CompilerInstance
2024-12-13 09:23:31 -08:00
Steven Wu
3cc9524bba [ClangImporter] Share CASOptions when cloning CompilerInstance
When cloning the clang instance in ClangImporter to perform tasks like
PCM/PCH compilation, make two compiler instance to share the same
CASOptions so they can share the CAS ObjectStore and life time.

This fixes a case where clang emits a diagnostics in a source buffer that
gets mapped in via CAS, and mapped into swift source manager. While CAS has
the ownship from CompilerInvocation -> CASOptions -> CAS, if the
CompilerInvocation is deleted when the cloned instance is deleted, it
left an invalid buffer in the swift source manager.

rdar://141284501
2024-12-12 13:31:34 -08:00
Kuba Mracek
a02586f1f2 [embedded] Include and use cxxshims in the embedded/ resource dir 2024-12-12 12:12:11 -08:00
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