Commit Graph

183 Commits

Author SHA1 Message Date
Sam Pyankov 8d385590d1 ClangImporter: Fix null dereference in finalizeLookupTable for decls without an owning module
getOwningModule() can return nullptr for declarations encountered
during bridging PCH generation. Guard the IsSystem check to avoid
crashing.

rdar://173729736
2026-04-02 13:26:54 -07:00
Gabor Horvath 0da3b379db [cxx-interop] Fix some clang tidy warnings
* Make some methods const or static
* Other minor cleanups
2026-03-26 14:48:24 +00:00
John Hui 346bfe476b [cxx-interop] [NFCi] Clean up reference type analysis logic
Previously, the getRefParentOrDiag() function was used both to determine
the reference typedness of an imported record, and to diagnose cases
where that determination was invalid (e.g., due to complications that
arise from inheritance). However, it exposed a rather brutish interface
for controlling whether diagnostics are emitted: when it is given
a non-null ClangImporter::Implementation pointer, it emits diagnostics,
and when given a null pointer it doesn't. A lack of consideration for
where we actually needed these diagnostics led to that pointer being
unnecessarily threaded through a couple of requests and a half dozen
call sites, leading to unnecessarily obscure control flow.

This patch gets rid of getRefParentOrDiag() and replaces it with two
well-defined entry points: a request that does not emit diagnostics, and
a function that does. The nullable ClangImporter::Implementation pointer
is hidden from the interface of those entry points, and their side
effects are well-documented.

The code is migrated from ClangImporter.cpp to ClangAnalysis.cpp, which
I've introduced as the new home for various subroutines that analyze and
extract information from clang decls.

The reference type inference logic of getRefParentOrDiag() (and its
associated helpers) is also rewritten to replace the clang-provided
clang::CXXRecordDecl::forAllBases() with an explicit graph traversal
through the class hierarchy that is easier to debug and adjust (with
imminent behavior changes in mind). It also avoids an unnecessary
class hierarchy traversal whose only purpose was to look for reference
types that participates in diamond inheritance.

No behavior change is intended: we (should) still make perform the same
reference typedness analysis, and emit the exact same diagnostics.

rdar://170858418
2026-03-05 14:22:57 -08:00
Gabor Horvath 75dc9b0283 [cxx-interop] More cleaups, no functional change intended
* Simplify some patterns (isa + cast)
* Add some consts
* Get rid of some temporary objects by constructing more objects in
  place
* Remove some duplicated lookups
* Other minor cleanups
2026-02-28 23:43:22 +00:00
Gabor Horvath 76e1f71674 [cxx-interop] Various code cleanups, no functional change intended
* Simplify conditionals
* Simplify loops
* Use llvm:: algorithms
* Simplify use of isa
* Deduplicate some code
* Other minor simplifications
2026-02-28 10:57:25 +00:00
John Hui 3a213c0085 [cxx-interop] Look for non-unsafe name during Clang lookup (#87471) 2026-02-26 20:21:13 -05:00
Sam Pyankov 5a7d85c471 Merge pull request #87207 from sepy97/system_pcm_suppress_warning
ClangImporter: Suppress warnings from system modules during pcm gener…
2026-02-14 09:35:43 -08:00
Sam Pyankov f8c6174578 ClangImporter: Suppress warnings from system modules during pcm generation
Suppress Swift warnings (unresolvable_clang_decl) when generating
precompiled modules for system frameworks.

rdar://170174282
2026-02-13 10:26:22 -08:00
John Hui f305ffc69f [cxx-interop] Instantiate CXXMethodDecl return type before importing
This needs to happen because, at this type of commit, IsSafeUseOfCxxDecl
(and thus ImportName) depends on the definition of the return type.

By eagerly instantiating the return type, we make the safety checking
(and thus the rename logic) more reliable.
2026-02-11 17:47:29 -08:00
Hiroshi Yamauchi be641d73ef Fix the GUID type not found error on Windows
Predefined declarations (like _GUID) are special forward declarations
inserted by Clang and aren't serialized into the pcm and their
definition pointers aren't retained across serialization and
deserialization, which causes this type not found error. Avoid putting
non-defining predefined declarations into the swift lookup table when
their definitions exist in the same module so that the definitions
will be associated with the base name and avoid this error.
2025-11-19 17:27:50 -08:00
swift-ci 65bba7d138 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-23 11:57:20 -07:00
Egor Zhdan c3a56fdc91 Revert "[cxx-interop] Import decls in extern blocks within namespaces"
This reverts commit 844787fd

Reverting because of a modularization issue in host libc++.

rdar://160536819
rdar://160536878
2025-09-17 19:17:14 +01:00
Egor Zhdan d15e139014 Revert "[cxx-interop] Do not look into extern blocks in libc++'s std_private_random_binomial_distribution"
This reverts commit 8e71509108.
2025-09-17 19:15:57 +01:00
swift-ci c0223fb423 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-15 12:15:24 -07:00
Egor Zhdan 8e71509108 [cxx-interop] Do not look into extern blocks in libc++'s std_private_random_binomial_distribution
rdar://139067788
2025-08-08 18:47:28 +01:00
Egor Zhdan a5b4975204 Revert "Revert "[cxx-interop] Import decls in extern blocks within namespaces""
This reverts commit 8f60840551.
2025-08-07 15:03:08 +01:00
swift-ci e50230f935 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-06 22:35:25 -07:00
Allan Shortlidge 8f60840551 Revert "[cxx-interop] Import decls in extern blocks within namespaces"
This reverts commit 844787fddb.
2025-08-06 21:50:55 -07:00
swift-ci 43b83de02e Merge remote-tracking branch 'origin/main' into rebranch 2025-08-06 04:34:26 -07:00
Egor Zhdan 844787fddb [cxx-interop] Import decls in extern blocks within namespaces
This teaches ClangImporter to import C++ decls that are declared within `extern "C" { ... }`/`extern "C++" { ... }` blocks which are nested in namespaces.

rdar://139067788
2025-08-04 19:42:43 +01:00
swift-ci 3998a187f9 Merge remote-tracking branch 'origin/main' into rebranch 2025-07-30 09:36:41 -07:00
Anthony Latsis 3f841b515c Merge remote-tracking branch 'origin/main' into jepa-rebranch 2025-07-30 14:31:33 +01:00
Anthony Latsis fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Egor Zhdan ba8807802f Merge pull request #83261 from egorzhdan/egorzhdan/allow-qual-swift-name-reland
Reland "[cxx-interop] Allow import-as-member for types in namespaces"
2025-07-25 12:35:59 +01:00
Egor Zhdan e4af21f4c2 [cxx-interop] Fix source compatibility for NSEvent.SpecialKey.upArrow.rawValue
AppKit renames some of its C constants via API Notes, e.g. `NSUpArrowFunctionKey` is renamed into `NSEvent.SpecialKey.upArrow.rawValue`.

In this example, `NSEvent` is an existing type, but `SpecialKey` is not, so the Swift compiler synthesizes an empty type named `SpecialKey`.

The logic that was added to support import-as-member for namespaces did not account for this: it assumed that if a nested type wasn't found, then the name must be invalid.

rdar://154783494
2025-07-24 12:47:17 +01:00
swift-ci c8477b5f42 Merge remote-tracking branch 'origin/main' into rebranch 2025-07-23 13:17:41 -07:00
Doug Gregor 72be0e0851 [Clang importer] Import incomplete SWIFT_SHARED_REFERENCE types
Normally, Swift cannot import an incomplete type. However, when we are
importing a SWIFT_SHARED_REFERENCE type, we're always dealing with
pointers to the type, and there is no need for the underlying type to
be complete. This permits a common pattern in C libraries where the
actual underlying storage is opaque and all APIs traffic in the
pointer, e.g.,

    typedef struct MyTypeImpl *MyType;
    void MyTypeRetain(MyType ptr);
    void MyTypeRelease(MyType ptr);

to use SWIFT_SHARED_REFERENCE to import such types as foreign
references, rather than as OpaquePointer.

Fixes rdar://155970441.
2025-07-22 20:20:23 -07:00
Egor Zhdan ab54e00097 Revert "Revert "[cxx-interop] Allow import-as-member for types in namespaces""
This reverts commit dd809c6452.
2025-07-22 18:36:27 +01:00
swift-ci b02a3ab4c2 Merge remote-tracking branch 'origin/main' into rebranch 2025-07-18 04:17:04 -07:00
Susana Monteiro 0337f7e64a Merge pull request #83067 from susmonteiro/rename-virtual-methods
[cxx-interop] Adding swift_name attributes to virtual methods overrides
2025-07-18 11:57:46 +01:00
susmonteiro 585ca5e2da [cxx-interop] Adding swift_name attributes to virtual methods overrides 2025-07-17 16:23:32 +01:00
swift-ci 1330e7a74c Merge remote-tracking branch 'origin/main' into rebranch 2025-07-10 08:55:10 -07:00
Ben Barham dd809c6452 Revert "[cxx-interop] Allow import-as-member for types in namespaces"
This reverts commit e95f6a3ce9.
2025-07-09 12:50:34 -07:00
swift-ci 4129e2fc45 Merge remote-tracking branch 'origin/main' into rebranch 2025-06-27 05:38:12 -07:00
Egor Zhdan e95f6a3ce9 [cxx-interop] Allow import-as-member for types in namespaces
This adds support for `swift_name` attribute being used with C++ types that are declared within namespaces, e.g.
```
__attribute__((swift_name("MyNamespace.MyType.my_method()")))
```

Previously import-as-member would only accept a top-level unqualified type name.

rdar://138934888
2025-06-26 12:39:50 +01:00
Anthony Latsis a090bfed1b ClangImporter: Refactor call to obsoleted clang::ASTWriter::getMacroID
Per 5d62a79bb79fee20f92f26dc55fd78440b9945ca (llvm-project).
2025-04-24 01:52:47 +01:00
Allan Shortlidge 6527a068eb ClangImporter: Remove custom AvailabilityDomain serialization.
Partially revert https://github.com/swiftlang/swift/pull/80035 now that Clang
has its own APIs for querying serialized modules for the decl representing the
availability domain with a given name.
2025-04-08 08:53:06 -07:00
Allan Shortlidge 72c821e2a7 ClangImporter: Look up availability domains in Clang modules.
In addition to tracking availability domains in SwiftLookupTable, also
serialize and deserialize the mapping from domain name to `clang::VarDecl`.
Ideally this serialization and lookup infrastructure would be entirely handled
by Clang, since it also needs to look up availability domains in serialized
modules, but the implementation for that is not ready yet.

Part of rdar://138441266.
2025-03-15 07:44:37 -07:00
Allan Shortlidge 017dae382e ClangImporter: Look up availability domains defined in bridging headers.
This is very brittle in this first iteration. For now we require the
declaration representing the availability domain be deserialized before it can
be looked up by name since Clang does not have a lookup table for availabilty
domains in its module representation. As a result, it only works for bridging
headers that are not precompiled.

Part of rdar://138441266.
2025-03-15 07:44:37 -07:00
fahadnayyar d8f919778d [cxx-interop] [cxx-interop] Infer SWIFT_SHARED_REFERENCE for types inheriting from a C++ foreign reference type
rdar://97914474
2025-02-20 08:46:57 -08:00
Ben Barham 05d62155f8 Merge remote-tracking branch 'origin/main' into manual-main-20240814
Conflicts:
  - `include/swift/Localization/LocalizationFormat.h`
  - `lib/ClangImporter/SwiftLookupTable.cpp`
  - `lib/ClangImporter/SwiftLookupTable.h`
  - `lib/Serialization/ModuleFormat.h`
  - `lib/Serialization/Serialization.cpp`

All from the hash changes being added to main. Took main except for the
lookup table minor version, which needs to be bumped still because of
other changes.
2024-08-14 19:21:52 -07:00
Ben Barham 273d7ee79d [Serialization] Do not serialize unstable hashes
https://github.com/llvm/llvm-project/pull/96282 changed
`get_execution_seed` to be non-deterministic. Use stable hashes instead.
2024-08-13 19:51:35 -07:00
Ben Barham cfbc007535 [rebranch] Do not serialize unstable hashes
https://github.com/llvm/llvm-project/pull/96282 changed
`get_execution_seed` to be non-deterministic. Use stable hashes instead.
2024-08-01 14:33:44 -07:00
Becca Royal-Gordon 0a6e5874e2 Accommodate 64-bit clang serialization IDs
Several serialization IDs that used to be 32 bits are being widened to 64. Modify SwiftLookupTable and its supporting types to accommodate this.

The new design uses a 64-bit integer for the pointer, decl, macro, or identifier ID, plus a 32-bit integer for the submodule ID (this field is set to all ones to indicate a decl vs. a macro). An additional in-memory bool distinguishes pointer nodes from ID nodes. Advantages:

• The main ID is now 64 bits wide, accommodating recent changes in clang.
• We’re no longer stealing bits from clang (we *do* steal the max value of the submodule ID, though).
• There’s no on-disk bit that, when set, will cause an ID to be interpreted as a pointer.
• Design is robust against `clang::serialization::SubmoduleID` also becoming 64-bit (although this will waste space).

Fixes rdar://131134424.
2024-07-31 08:57:31 -07:00
Ben Barham 70c348129f [ClangImporter] Use new LocalDeclID
There were a number of upstream LLVM changes to `DeclID` in order to
separate "local" and "global" IDs:
https://github.com/llvm/llvm-project/commit/07b1177eed7549d0badf72078388422ce73167a0
https://github.com/llvm/llvm-project/commit/b8e3b2ad66cf78ad2b7832577b1d58dc93c5da21
https://github.com/llvm/llvm-project/commit/b467c6b53660dcaa458c2b5d7fbf5f93ee2af910
https://github.com/llvm/llvm-project/pull/89873
https://github.com/llvm/llvm-project/commit/d86cc73bbfd9a22d9a0d498d72c9b2ee235128e9
https://github.com/llvm/llvm-project/commit/8af86025af2456c70c84aec309cca9a069124671
... and probably more.

This likely needs further cleaning up to not use `DeclID` at all.
2024-07-02 16:13:49 -07:00
Ben Barham b7954411ec Merge remote-tracking branch 'origin/main' into manually-merge-main-to-rebranch
Conflicts:
  - `include/swift/AST/PluginRegistry.h`
2024-06-27 14:56:11 -07:00
Hamish Knight bb4d0126f4 [ClangImporter] Switch lookupGlobalsAsMembers to take a non-optional EffectiveClangContext
The only caller is `loadNamedMembers`, and that
passes in a non-optional EffectiveClangContext,
meaning that we'd miss the case when
`getEffectiveClangContext` returns `nullptr`, crashing
in `translateContext`. No test case unfortunately
as I haven't been able to come up with a reproducer.

rdar://129619711
2024-06-25 12:27:48 +01:00
Xi Ge 736ccef626 Merge remote-tracking branch 'apple/main' into rebranch 2024-06-20 15:16:55 -07:00
Tim Kientzle 1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Ben Barham cacfd3e3ae Rename llvm::support::endianness to llvm::endianness
LLVM is gearing up to move to `std::endianness` and as part of that has
moved `llvm::support::endianness` to `llvm::endianness`
(bbdbcd83e6702f314d147a680247058a899ba261). Rename our uses.
2024-04-08 08:58:58 -07:00