Commit Graph

1871 Commits

Author SHA1 Message Date
Egor Zhdan
07aec92198 [cxx-interop] Pass sysroot to the Clang driver when detecting libstdc++ path
The Swift build scripts pass `-sdk /` to the Swift compiler when building the Swift stdlib and the overlays, and this affects the default include paths of Clang. We need to make sure that libstdc++ path detected by ClangImporter is the same path that will be used by Clang during compilation. To do that, let's pass `--sysroot` to the Clang driver when detecting the libstdc++ path.
2022-05-31 16:22:25 +01:00
swift-ci
fc001b2fcc Merge remote-tracking branch 'origin/main' into rebranch 2022-05-31 03:33:46 -07:00
Egor Zhdan
127e29f365 [cxx-interop] Do not warn about missing libstdc++ on Android
Android uses libc++.

This fixes a few tests on Android.
2022-05-30 23:22:03 +01:00
Ehud Adler
16c1e1defa Format 2022-05-26 21:29:43 -04:00
Ben Barham
92b6dd24c2 Merge branch 'main' into manually-merge-main 2022-05-25 15:55:35 -07:00
swift-ci
645d73f27e Merge pull request #58480 from beccadax/so-i-put-versions-in-your-version-numbers
Update SWIFT_COMPILER_VERSION language features
2022-05-24 17:47:43 -07:00
Sam Kortekaas
e73bb6fd9d [cxx-interop] Import attributes on inherited C++ methods 2022-05-23 13:21:06 +02:00
Ehud Adler
3196ea9b39 Refactor and format 2022-05-21 21:44:07 -04:00
Ehud Adler
e7fe6f0fe7 Fix tests and re-enable support for CXX operators 2022-05-21 21:28:03 -04:00
swift-ci
b83cd2f0a9 Merge remote-tracking branch 'origin/main' into rebranch 2022-05-19 03:33:42 -07:00
Egor Zhdan
abf56bcb33 [cxx-interop] Extract libstdc++ installation path from Clang
To inject a modulemap into the libstdc++ directory, Swift needs to know the path of the libstdc++ installation. Previously it was assumed to be `/usr/include/c++/{version}`, however, this might not be the case. For example, on CentOS the stdlib is located at `/opt/rh/devtoolset-{version}/root/usr/include/c++/{version}`.

This change teaches Swift to extract the libstdc++ include paths from the Clang driver, and fixes C++ stdlib lookup on CentOS.

rdar://93341210
2022-05-18 19:27:29 +01:00
Ehud Adler
11730e8f07 Remap class operator function names (-/+/*....) to imported operator names __operator(Minus, Plus,...) and fix test cases 2022-05-18 10:31:04 -04:00
Ehud Adler
4267958d51 Mapping decl base name to imported name 2022-05-18 10:31:04 -04:00
Ehud Adler
e14e2fbc76 Add changes 2022-05-18 10:31:03 -04:00
swift-ci
ea3d49ce7f Merge remote-tracking branch 'origin/main' into rebranch 2022-05-14 10:13:56 -07:00
Arnold Schwaighofer
9cb25736e0 ClangImporter.cpp: Fix compilation on some linux systems 2022-05-14 08:07:20 -07:00
swift-ci
658f98c851 Merge remote-tracking branch 'origin/main' into rebranch 2022-05-13 16:54:31 -07:00
Egor Zhdan
1ef02efed1 Merge pull request #58843 from apple/egorzhdan/libstdcxx-vfs
[cxx-interop] Use VFS to inject modulemap into libstdc++ installation
2022-05-14 00:40:16 +01:00
Egor Zhdan
f3790def63 [cxx-interop] Use VFS to inject modulemap into libstdc++ installation
Previously the modulemap for the C++ stdlib on Linux was provided via `-fmodule-map-file=` Clang argument pointing to the modulemap file within the Swift toolchain. The modulemap file could not reference the stdlib headers directly, since the exact stdlib include directory varies across Linux versions (it generally looks like `/usr/include/c++/{gcc_version}`). So the modulemap file instead referenced a local header, which `#include <>`-ed the stdlib headers, relying on Clang include resolution.

Unfortunately this did not work properly in the presence of another C++ module which included the stdlib headers: sometimes decls from the stdlib were hijacked by the other module, and were not treated as a part of the stdlib by Clang. This caused compile errors in Swift.

This change uses LLVM VFS to inject the modulemap file into the libstdc++ directory. The modulemap file is now able to reference the stdlib headers directly, which fixes the issue.

Credits to Rintaro Ishizaki for proposing a similar idea for SwiftGlibc back in 2016.
2022-05-13 22:21:40 +01:00
Ben Barham
87ce3bd38f Merge pull request #58774 from bnbarham/more-rebranch-fixes
[next] Disable opaque pointers by default
2022-05-09 18:21:01 -07:00
Ben Barham
edcd53b09f [next] Disable opaque pointers by default
LLVM has enabled opaque pointers by default. Disable while we migrate.
Note that individual tests can have opaque pointers enabled by using
`-Xcc -Xclang -Xcc -opaque-pointers`.
2022-05-09 16:17:55 -07:00
swift-ci
6a45007a08 Merge remote-tracking branch 'origin/main' into rebranch 2022-05-07 11:13:10 -07:00
Robert Widmann
77bf3aff93 Teach the ClangImporter About Mac Catalyst Availability
Add some missing definitions for the mac catalyst platform. This enables the importing of macCatalyst platform availability attributes.
2022-05-06 14:37:24 -07:00
Ben Barham
4e1eb1abe8 Merge pull request #58620 from bnbarham/rebranch-fixes
[rebranch] Various fixes to get macos to a compiling state
2022-05-06 09:59:38 -07:00
Ben Barham
0dff9ac150 [next] Update PPCallbacks::InclusionDirective overrides
`InclusionDirective` was changed in llvm/llvm-project
d79ad2f1dbc2db63121620f55d6cfa915f2733ac to pass an
`Optional<FileEntryRef>` rather than a `FileEntry *`. Update overrides
in Swift to use the new API.
2022-05-05 16:25:10 -07:00
Ben Barham
1531ed7a25 [next] Update ParseTopLevelDecl callers
`ParseTopLevelDecl` was changed in llvm/llvm-project
ab28488efe6de6f8fa856a1dfd8c0320d41d7608 and now takes a
Sema::ModuleImportState output parameter rather than the previous
(defaulted) boolean.
2022-05-05 16:25:10 -07:00
Egor Zhdan
dabaa69024 Merge pull request #58659 from apple/egorzhdan/cxx-namespace-lookup-fix
[cxx-interop] Fix namespace lookup
2022-05-04 18:51:08 +01:00
Egor Zhdan
7042fb5a07 Merge pull request #58637 from apple/egorzhdan/cxx-fix-use-string
[cxx-interop] Fix `test/Interop/Cxx/stdlib/use-std-string.swift`
2022-05-04 14:20:04 +01:00
Egor Zhdan
b3feb9e692 [cxx-interop] Fix namespace lookup
If a namespace has redecls in different modules, Swift sometimes picked a redecl which is not visible from the requested module, and wrongly removed the namespace from lookup results.

This resulted in errors like this:
```
*** DESERIALIZATION FAILURE ***
Could not deserialize type for 'init(cxxString:)'
Caused by: top-level value not found (std)
Cross-reference to module '__ObjC'
... std
```
2022-05-04 13:29:38 +01:00
Egor Zhdan
48208380e3 Merge pull request #58543 from apple/egorzhdan/cxx-template-inst-lookup
[cxx-interop] Fix lookup of class template instantiations
2022-05-03 19:46:25 +01:00
Egor Zhdan
7db3666e43 [cxx-interop] Fix test/Interop/Cxx/stdlib/use-std-string.swift
Temporarily disable the checks for mutable fields in a C++ struct. A better solution would be to use some heuristic to detect if a method mutates a mutable field, and allow the user to adjust mutability with the existing `nonmutating` annotation.

rdar://92621793
2022-05-03 16:36:40 +01:00
Egor Zhdan
c952fc175b [cxx-interop] Fix lookup of class template instantiations
To fix a crash during deserialization, we need to store the mangled names of C++ class template instantiations in the lookup table.

For example, `std::string` will be serialized as `__CxxTemplateInstNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE`, and when another instance of the Swift compiler will later attempt to deserialize a decl referencing this instantiation, it will query the `SwiftLookupTable` for a decl with this name. If this instantiation is not referenced from the module that is currently being compiled, Swift will not instantiate `std::string` during typechecking, and the mangled name will be missing from `SwiftLookupTable`, leading to a deserialization failure. This happens when compiling with cross-module optimization enabled, and the current module does not reference `std::string`, but one of its dependencies does.
2022-04-29 21:55:59 +01:00
Sam Kortekaas
60fe6aae95 [cxx-interop] Add a "nonmutating" swift_attr attribute
Previously, the clang importer marked all const methods as mutating whenever a C++ record had mutable fields. This change allows overriding this behavior by using the "nonmutating" swift_attr attribute.

Fixes SR-15907.
2022-04-28 15:42:56 +02:00
Becca Royal-Gordon
3843c7cd5e Update SWIFT_COMPILER_VERSION language features
The `SWIFT_COMPILER_VERSION` define is used to stamp a vendor’s version number into a Swift compiler binary. It can be queried from Swift code using `#if _compiler_version` and from Clang by using a preprocessor definition called `__SWIFT_COMPILER_VERSION`. These are unsupported compiler-internal features used primarily by Apple Swift.

In Swift 1.0 through 5.5, Apple Swift used a scheme for `SWIFT_COMPILER_VERSION` where the major version matched the embedded clang (e.g. 1300 for Apple Clang 13.0.0) and the minor version was ignored. Starting in Swift 5.6, Apple Swift started using major and minor version numbers that matched the Swift.org version number. This makes them easier to understand, but it means that version 1300.0.x was followed by version 5.6.x. Not only did version numbers go backwards, but also the old logic to ignore minor versions was now a liability, because it meant you would not be able to target a change to 5.7.x compilers but not 5.6.x compilers.

This commit addresses the problem by:

* Modifying the existing `#if _compiler_version(string-literal)` feature so it transforms the major version into a major and minor that will compare correctly to new version numbers. For instance, “1300.*” is transformed into “1.300”, which will compare correctly to a “5.6” or “5.7” version even if it doesn’t really capture the fact that “1300” was a Swift 5.5 compiler. As a bonus, this allows you to use the feature to backwards-compatibly test new compilers using the existing feature: “5007.*” will be seen by compilers before 5.7 as an unknown future version, but will be seen by 5.7 compilers as targeting them.

* Modifying the `__SWIFT_COMPILER_VERSION` clang define similarly so that, to preprocessor conditions written for the old scheme, a 5.7 compiler will appear to have major version 5007.

* Adding a new variant of `#if _compiler_version` with the same syntax as `#if swift` and `#if compiler`—that is, taking a comparison operator and a bare set of dotted version numbers, rather than a string literal. Going forward, this will be how version checks are written once compatibility with compilers before this change is no longer a concern.

These changes are only lightly tested because tests have to work without any compiler version defined (the default in most configurations), but I’ve tested what I can.

Fixes rdar://89841295.
2022-04-27 18:27:52 -07:00
Egor Zhdan
2285c518f7 Merge pull request #58428 from apple/egorzhdan/cxx-sil-deserialization
[cxx-interop] Fix SIL deserialization error
2022-04-27 20:31:20 +02:00
Egor Zhdan
8c58a7776b [cxx-interop] Fix AST serialization error
Do not attempt to serialize C++ builtin decls. This caused an assertion failure in `clang::ASTWriter::getDeclID` when building SwiftCompilerSources:

```
Assertion failed: (DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"), function getDeclID, file ASTWriter.cpp, line 5272.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	<eof> parser at end of file
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x00000001123e21c7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x00000001123e10e8 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x00000001123e2810 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007fff204a0d7d _sigtramp + 29
4  libsystem_platform.dylib 0x0000000000000008 _sigtramp + 18446603339974439592
5  libsystem_c.dylib        0x00007fff203b0406 abort + 125
6  libsystem_c.dylib        0x00007fff203af7d8 err + 0
7  swift-frontend           0x000000011345b983 clang::ASTWriter::getDeclID(clang::Decl const*) (.cold.1) + 35
8  swift-frontend           0x0000000110a584b2 clang::ASTWriter::getDeclID(clang::Decl const*) + 322
9  swift-frontend           0x000000010e36649c llvm::OnDiskChainedHashTableGenerator<(anonymous namespace)::BaseNameToEntitiesTableWriterInfo>::Emit(llvm::raw_ostream&, (anonymous namespace)::BaseNameToEntitiesTableWriterInfo&) + 1244
10 swift-frontend           0x000000010e364927 swift::SwiftLookupTableWriter::writeExtensionContents(clang::Sema&, llvm::BitstreamWriter&) + 1351
11 swift-frontend           0x0000000110a59b62 clang::ASTWriter::WriteModuleFileExtension(clang::Sema&, clang::ModuleFileExtensionWriter&) + 946

```
2022-04-27 17:00:26 +01:00
Zoe Carver
b07f059f2c Merge pull request #42506 from zoecarver/ufo-with-base
[cxx-interop] Add support for calling members of base classes on UFOs.
2022-04-22 09:18:32 -07:00
zoecarver
336cd29333 [cxx-interop] Add support for calling members of base classes on UFOs. 2022-04-21 13:40:59 -07:00
Josh Soref
3d488f685e Spelling clangimporter (#42464)
* spelling: enumerators

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: handler

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: heuristic

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: implicitly

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: included

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: instantiate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: integer

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: nested

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: otherthing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overridden

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: simultaneously

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: special

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: typecheck

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unfortunately

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unknown

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: version

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-21 09:31:12 -07:00
Egor Zhdan
fca494e66d [cxx-interop] Ignore mutable fields when detecting method mutability
rdar://91961524
2022-04-19 18:23:32 +01:00
Egor Zhdan
a3e914364a [cxx-interop] Import libstdc++ as a Clang module
This change adds a module map for libstdc++, which allows it to be properly imported into Swift as a module. The module map is installed into `usr/lib/swift/linux/{arch}` similarly to `glibc.modulemap`, and is passed to Clang as `-fmodule-map-file`.

That means it is now possible to import std directly from Swift on Linux, when C++ interop is enabled.

The module map currently declares a single `std` module without splitting the headers into submodules. This is going to change in the near future.

rdar://87654514
2022-04-08 14:10:04 +01:00
zoecarver
13e64f3a3f [nfc][cxx-interop] Add some pretty stack traces for various things that crash a lot. 2022-03-17 16:54:54 -07:00
Puyan Lotfi
09cdd36c0a [c++-interop] For failed imports in ClangImporter, cache them regardless. (#41173)
As per SR-14137 this caches entries in ImportedDecls even when the
import failed.

Also have to mention I did this based on Thomas's PR #36747.

This should help us better handle complex templates and dependant types.
2022-03-10 10:33:04 -08:00
Allan Shortlidge
1be8913c9b NFC: Fix a number of warnings emitted when building swift-frontend. 2022-03-04 16:15:33 -08:00
Ben Barham
f93bc6431d Merge pull request #41636 from apple/revert-41546-use-overlay-fs-instead
Revert "[Importer] Wrap the VFS passed to Clang with an overlay FS instead"
2022-03-03 14:39:54 -08:00
Zoe Carver
d3c40ef9f1 Merge pull request #41624 from zoecarver/cache-thunks-for-spec
[cxx-interop] Cache specialized function templates.
2022-03-03 14:15:44 -08:00
Ben Barham
103f04e8df Revert "[Importer] Wrap the VFS passed to Clang with an overlay FS instead" 2022-03-02 15:12:58 -08:00
zoecarver
e6a99cb969 [cxx-interop] Cache specialized function templates.
This not only prevents us from creating a bunch of FuncDecls but actually is required to prevent duplicate symbol errors.
2022-03-02 11:14:09 -08:00
zoecarver
680d5d2cc8 [nfc] [cxx-interop] Move function template specialization logic into clang-module-loader. 2022-03-02 10:58:14 -08:00
Zoe Carver
10b5031e93 Merge pull request #41610 from zoecarver/import-subscript-in-base
[cxx-interop] Add support for subscripts in base classes.
2022-03-02 08:40:33 -08:00