Commit Graph

13 Commits

Author SHA1 Message Date
Gábor Horváth 43cae24ed4 Merge pull request #88228 from Xazax-hun/dedup-attr-logic
[cxx-interop] Deduplicate some logic to search for attributes
2026-05-06 12:22:50 +01:00
Mads Odgaard 66b5fdc03d Add support for SwiftImportAs: opaque_pointer (#88714)
Adds support for making pointers to C structs always import as OpaquePointer through API notes. This is needed to bring down the Android SDK to API level 23, since FILE is imported differently on API 23 vs 24.
2026-05-01 22:45:47 +05:30
Gabor Horvath 970253b14a [cxx-interop] Deduplicate some logic to search for attributes
We wrote the same code multiple times, this PR replaces some of that
code with function calls. It is not entirely NFC as the called function
has some special case for parameters. That being said, having uniform
logic for looking up attributes is probably beneficial. If it does not
work out, we can always introduce a compile-time parameter to opt in/out
of the special case.
2026-04-27 16:52:25 +01:00
John Hui df1f0b18f4 [cxx-interop] Suppress ASSERT failure in FRT diagnostics
This assertion is a sanity check but apparently fails in some scenarios
that I have yet to investigate. Suppressing this assertion is harmless;
nothing depends on the result of this call.

rdar://171995960
2026-04-13 02:05:05 -07:00
John Hui e761efdbb9 [cxx-interop] [NFC] Inline and delete matchSwiftAttrConsideringInheritance
It was an unwieldy helper to begin with.

Also, add a comment about how the "considering inheritance" bit is not
actually the right thing to do in the first place. That behavior will
be deprecated Soon™️
2026-03-19 23:07:49 -07:00
John Hui 50942afd15 [cxx-interop] [NFC] Simplify extraction of return ownership convention
Rename getCxxRefConventionWithAttrs() to getOwnershipOfReturnedFRT()
(since this isn't specific to C++)

Move it to ClangAnalysis.cpp and reuse some helper functions in there
rather than duplicating code.
2026-03-19 23:07:49 -07:00
John Hui 5a632f17bd [cxx-interop] [NFC] Adjust foreign call diagnostic checking logic
Move around the checking logic a little so that it flows more
intuitively---check whether the warning should apply to the function
decl in the first place, *then* look at its return type and determine
whether the called function is missing annotations.

Also, we should never call deduction guides and destructors, so ASSERT
that we don't. We do occasionally call some decls this check doesn't
handle like FunctionTemplateDecls, BlockDecls, and VarDecls (of function
or block pointers), so make that explicit.
2026-03-19 23:07:48 -07:00
John Hui 380e57d685 [cxx-interop] [NFC] Refactor logic of missing RETURNS_RETAINED diagnostics 2026-03-19 23:07:48 -07:00
John Hui 91f3dde9dd [cxx-interop] [NFC] Move Clang call diagnostics to ClangImporter
Moving this here gives us access to ClangImporter's internal APIs for
future improvements. It also keeps changes to interop-specific
diagnostics localized to the interop-specific part of the compiler.
2026-03-19 23:07:46 -07:00
John Hui 5e49e109b5 [cxx-interop] Obselete "import_as_ref" attribute
This is not used anywhere other than our test suite.
2026-03-05 14:22:57 -08:00
John Hui ffd780410b [cxx-interop] ASSERT FRT base is complete and not dependent
The previous logic was inlined from CXXRecordDecl::forAllBases and was
more defensive than we need.
2026-03-05 14:22:57 -08:00
John Hui 6aaa274ab8 [cxx-interop] Remove idempotence from diagnoseForeignReferenceType()
This is unnecessary now that we only have a single call site from
SwiftDeclConverter::VisitCXXRecordDecl. The SwiftDeclConverter already
has its own cacheing to ensure that each Clang decl is only imported
once; that same cache ensures any diagnostics emitted during import
are also idempotent.

rdar://170858418
2026-03-05 14:22:57 -08: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