Commit Graph

2064 Commits

Author SHA1 Message Date
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
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
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
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
susmonteiro
2c9bb7a6eb Revert "[cxx-interop] Implicitly defined copy constructors"
This reverts commit 1c5bbe0290.
2025-11-05 10:42:05 +00:00
John Hui
4389cc9bea Revert "[cxx-interop] Mark class templates with unsafe type arguments as unsafe"
This reverts commit afaa499e3a.
2025-11-03 18:03:16 -08:00
John Hui
ad8b5a5204 Revert "[cxx-interop] Use clang::CanQualType in ClangTypeExplicitSafetyDescriptor"
This reverts commit 5328572fbe.
2025-11-03 17:58:34 -08:00
John Hui
6607ae9c56 Revert "[cxx-interop] Check template argument safety in ClangDeclExplicitSafety"
This reverts commit d2632d21af.
2025-11-03 14:37:05 -08:00
John Hui
a13c95ded6 Revert "[cxx-interop] Avoid cycles in ClangDeclExplicitSafety requests"
This reverts commit 929d0d8c26.
2025-11-03 14:37:03 -08:00
John Hui
80fa8c18ef Revert "[cxx-interop] Avoid cycles in ClangTypeExplicitSafety requests"
This reverts commit d0bad4cebd.
2025-11-03 14:36:47 -08:00
Henrik G. Olsson
c541666b21 Merge pull request #85023 from hnrklssn/swiftify-bridging-header
[ClangImporter] add implicit import of `Swift` for `__ObjC` module
2025-10-31 18:08:47 -07:00
Henrik G. Olsson
87af5538d3 Merge pull request #84871 from hnrklssn/inherit-literal-std
[ImportResolution] Only disallow explicit `std` imports (allow implicit)
2025-10-31 14:22:16 -07:00
susmonteiro
1c5bbe0290 [cxx-interop] Implicitly defined copy constructors 2025-10-30 14:17:19 +00:00
Becca Royal-Gordon
393965090e Merge pull request #34556 from beccadax/mod-squad-2
[SE-0491] Implement lookup and diagnostics for module selectors (MyMod::someName)
2025-10-28 16:00:26 -07:00
Allan Shortlidge
2887334ffb Merge pull request #85167 from tshortli/new-platform-kinds
AST: Introduce the `Swift`, `anyAppleOS`, and `DriverKit` platform kinds
2025-10-28 11:21:38 -07:00
John Hui
d0bad4cebd [cxx-interop] Avoid cycles in ClangTypeExplicitSafety requests
rdar://163511959
2025-10-28 07:50:16 -07:00
Allan Shortlidge
92227ad1cf AST: Introduce the Swift, anyAppleOS, and DriverKit platform kinds.
This change just stages in a few new platform kinds, without fully adding
support for them yet.

- The `Swift` platform represents availability of the Swift runtime across all
  platforms that support an ABI stable Swift runtime (see the pitch at
  https://forums.swift.org/t/pitch-swift-runtime-availability/82742).
- The `anyAppleOS` platform is an experimental platform that represents all of
  Apple's operating systems. This is intended to simplify writing availability
  for Apple's platforms by taking advantage of the new unified OS versioning
  system announced at WWDC 2025.
- The `DriverKit` platform corresponds to Apple DriverKit which is already
  supported by LLVM.
2025-10-27 19:15:04 -07:00
Qiongsi Wu
1cf8d2ede1 [ClangImporter] Fix Build Break Due to clang API changes (#84925)
<!--
If this pull request is targeting a release branch, please fill out the
following form:

https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1

Otherwise, replace this comment with a description of your changes and
rationale. Provide links to external references/discussions if
appropriate.
If this pull request resolves any GitHub issues, link them like so:

  Resolves <link to issue>, resolves <link to another issue>.

For more information about linking a pull request to an issue, see:

https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->

<!--
Before merging this pull request, you must run the Swift continuous
integration tests.
For information about triggering CI builds via @swift-ci, see:

https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci

Thank you for your contribution to Swift!
-->

https://github.com/swiftlang/llvm-project/pull/11631 is cherry-picking
`clang` commits that change `CompilerInstance`'s APIs. The API changes
are causing Swift build breaks. This PR fixes the API breaks.

---------

Co-authored-by: Jan Svoboda <jan@svoboda.ai>
2025-10-27 16:50:56 -07:00
John Hui
90c0509b16 Merge pull request #85095 from j-hui/template-arg-safety-in-req 2025-10-27 11:40:09 -07:00
Becca Royal-Gordon
ffaa35810c Handle lookup into Builtin with module selector
Lookups like Builtin::Int64 were failing because BuiltinUnit rejected all unqualified lookups. Make it allow unqualified lookups with a module selector.
2025-10-24 16:23:48 -07:00
John Hui
929d0d8c26 [cxx-interop] Avoid cycles in ClangDeclExplicitSafety requests 2025-10-24 14:30:29 -07:00
Jan Svoboda
7fef14853b [ClangImporter] Adjust for LLVM changes (IO sandboxing) (#85093) 2025-10-24 08:11:01 -07:00
John Hui
d2632d21af [cxx-interop] Check template argument safety in ClangDeclExplicitSafety
Checking this upon import was overlooking annotations that explicitly
mark a type as safe (or unsafe). Instead, we consolidate this logic in
the ClangDeclExplicitSafety request.

rdar://163196609
2025-10-23 13:05:57 -07:00
Jan Svoboda
be3b16b8c7 Revert "[ClangImporter] Adjust for LLVM changes (IO sandboxing) (#84995)"
This reverts commit 3c3f6e752e.
2025-10-21 15:48:13 -07:00
Henrik G. Olsson
507846cb38 don't try to load stdlib when it's known to fail
This prevents it from being loaded correctly later.
2025-10-21 13:48:31 -07:00
Henrik G. Olsson
c9a62660f3 [ClangImporter] add implicit import of Swift for __ObjC module
Any safe wrapper expansion originating in a bridging header would fail
typechecking because the `__ObjC` module doesn't import the standard
implicit imports. This is because the main module is not available to
inherit implicit imports from when the `__ObjC` module is created. We
don't need all of the implicit modules for safe wrappers, so import
`Swift` specifically.

rdar://163078116
2025-10-20 18:02:28 -07:00
Jan Svoboda
3c3f6e752e [ClangImporter] Adjust for LLVM changes (IO sandboxing) (#84995) 2025-10-20 14:03:24 -07:00
Steven Wu
5d480ef063 Merge pull request #84861 from cachemeifyoucan/eng/PR-swift-cas-configuration
[Caching][NFC] Using llvm::cas::CASConfiguration
2025-10-20 09:27:19 -07:00
Mads Odgaard
c92e5b47f3 Merge pull request #84574 from madsodgaard/android-availability 2025-10-20 10:40:37 +09:00
John Hui
ae0243cb68 Merge pull request #84987 from j-hui/value-semantics-implicit-dtors 2025-10-17 19:59:30 -07:00
Steven Wu
4f059033bb [Caching][NFC] Using llvm::cas::CASConfiguration
Prefer llvm::cas::CASConfiguration where it used to clang::CASOption.
2025-10-17 16:42:35 -07:00
John Hui
071552245f [cxx-interop] Check for simple destructors when checking value semantics
hasDestroyTypeOperations() does not account for the scenario where
decl has an implicit destructor that has not yet been materialized
(i.e., using clang::Sema::{Declare,Define}ImplicitDestructor()).
This can sometimes happen to a type Foo if we check the value semantics
of, say, std::vector<Foo> (which recursively checks the value semantics
of Foo without first importing Foo).

rdar://162539654
2025-10-17 10:34:46 -07:00
Gabor Horvath
69f0a7d756 [cxx-interop] Do not treat SWIFT_RETURNS_INDEPENDENT_VALUE unsafe
The explicit safety analysis inadvertently considered every instance
method with this annotation unsafe. We trust these annotations, so let's
not consider the annotated methods unsafe.

rdar://162602614
2025-10-17 13:14:06 +01:00
Hamish Knight
73710e3eef [AST] Introduce Decl::addAttribute
Introduce a convenience entrypoint that also calls `attachToDecl` on
the attribute, and migrate all existing uses of `getAttrs().add` onto
it.
2025-10-16 11:21:54 +01:00
Henrik G. Olsson
c66de2965d Merge pull request #84896 from hnrklssn/inherit-skip-unfulfilled-require
[MacrosOnImports] Don't inherit imports with unfulfilled `required` clauses
2025-10-15 19:39:02 -07:00
John Hui
dc4ff24d5d Merge pull request #84866 from j-hui/do-not-import-template-type-arguments
[cxx-interop] Do not import template type arguments
2025-10-15 18:57:58 -07:00
Henrik G. Olsson
8542903fa1 Only inherit C++ modules when C++ interop enabled
Imports of modules marked `requires: cplusplus` should only be inherited
when C++ interop is enabled. Similarly, imports of modules marked
`requires: !cplusplus` should only be inherited when C++ interop is
disabled.
2025-10-15 14:25:08 -07:00
Henrik G. Olsson
634084bc37 [MacrosOnImports] Don't import !swift modules
Inheriting a clang import marked `requires: !swift` will always result
in an error. This skips such imports, which *may* result in name lookup
errors instead, but also may not, depending on the module.

rdar://161795145
2025-10-15 14:25:06 -07:00
John Hui
61f9f62dae [NFC][cxx-interop] Delete stray comment that refers to non-existent code
importAsUnsafe was removed in c859557f8b
2025-10-15 09:21:48 -07:00
John Hui
5328572fbe [cxx-interop] Use clang::CanQualType in ClangTypeExplicitSafetyDescriptor
This will hopefully improve the cache hit rate.
2025-10-15 09:19:37 -07:00
John Hui
afaa499e3a [cxx-interop] Mark class templates with unsafe type arguments as unsafe
Doing so preserves ClangImporter's behavior first introduced in
f12b48aa86, but do so without relying on
importing the type argument (since that can lead to template
over-instantiation).

This patch also promotes the logic of hasUnsafeType() to a standalone
SimpleRequest, to provide a QualType-typed entry point for evaluating
the safety of a Clang entity.
2025-10-14 15:13:24 -07:00
Egor Zhdan
dc6ec456d4 [cxx-interop] Assign correct owning module to class template specializations
When importing C++ class template specializations into Swift, we were assigning the owning module to the imported Swift structs inconsistently. For specializations that had a typedef (or a using-decl), we assumed the module that declares the typedef to be the owning module for the specialization. For specializations that do not have a typedef, we assumed the module that declares the class template itself to be the owning module. This changes the behavior to always assume the latter.

rdar://158589803
2025-10-14 18:21:23 +01:00
Henrik G. Olsson
8d4e81c2fa [ImportResolution] Only disallow explicit std imports (allow implicit)
When expanding a Swift macro in a clang module where the original clang
module imported a submodule in a C++ standard library module other than
`std`, e.g. a submodule to `std_core`, this would result in an error.
This is because `std_core.math.abs` would be imported as
`CxxStdlib.math.abs`, which would later be translated back as
`std.math.abs` which doesn’t exist.

This changes the mapping to only map `std` to `CxxStdlib`. To prevent
errors when importing modules starting with `std_`, this error is moved
from the late-stage module import to the earlier processing of
`ImportDecl`s. This results in these module names still being forbidden
in explicit imports (i.e. naming them in source code), while still being
allowed in implicit imports inherited from clang modules.

This also fixes a fix-it bug where only the first 3 characters would be
selected for replacing with `CxxStdlib` when importing `std_core`.

This also fixes a diagnostic bug where aliased modules would refer to
the module name in the source code rather than the real module name, and
adds a note clarifying the situation.

rdar://161795429
rdar://161795673
rdar://161795793

fix non-fatal import error
2025-10-13 18:51:03 -07:00
Hamish Knight
2b8a1cccfd [AST] Store owning Decl/DeclContext on CustomAttr
Introduce CustomAttrOwner that can store either a Decl for an
attached attribute, or a DeclContext for e.g a type or closure
attribute. Store this on CustomAttr such that we can query it from
the name lookup requests.
2025-10-13 13:37:29 +01:00
Gábor Horváth
7159b12936 Merge pull request #83752 from Xazax-hun/fix-inline-namespaces
[cxx-interop] Support calling functions in inline namespaces
2025-10-08 18:41:38 +01:00
Gabor Horvath
0e56809834 [cxx-interop] Support calling functions in inline namespaces
The overload resolution generated a constraint that tried to bind
outer.inline_inner to outer. This constraint failed. This PR attempts to
recognize this scenario and make the constraint succeed.

rdar://158401346
2025-10-07 11:39:07 +01:00
John Hui
4fe7180ca9 [NFC] [cxx-interop] Slightly improve function instantiation logic
We were not handling nullptrs consistently. This shouldn't actually
change the behavior for any known case but makes the error handling
logic a little more explicit and robust.
2025-10-06 23:54:04 -07:00