Commit Graph

95 Commits

Author SHA1 Message Date
Becca Royal-Gordon
60f6afb76c Correct newline emission in generated headers
Eliminates extraneous newlines between top-level Objective-C declarations in `-emit-objc-header` headers. Specifically, there should now always be exactly one—no more, no less—empty line between `@end` and whatever follows it.

Besides being more aesthetically pleasing, this eliminates ordering-dependent behavior where PrintAsClang would print an extra newline when visiting an empty extension, which meant that the order in which empty and non-empty extensions were visited during printing could result in whitespace differences in the compiler output. Printing the blank line is now conditional on whether `tell()` indicates that characters were actually written to the output.

Fixes rdar://143533893.
2025-01-24 16:26:33 -08:00
Becca Royal-Gordon
eff456ffc3 Merge pull request #72203 from beccadax/does-this-sign-conform-to-the-protocol
[PrintAsObjC] Use NSUInteger if protocol requires
2024-03-11 19:14:22 -07:00
Becca Royal-Gordon
0ef9b7952a [PrintAsObjC] Use NSUInteger if protocol requires
When printing an ObjC member into a header with an `Int` parameter or result, PrintAsClang would look up any imported Objective-C member it overrode to see if ClangImporter imported `NSUInteger` as `Int`, and if so, would print `NSUInteger` instead of `NSInteger`. However, it did not do the same for protocol requirements the member witnessed. Correct this oversight.

Fixes rdar://124300674.
2024-03-08 16:26:10 -08:00
Becca Royal-Gordon
bcdbeb4640 [PrintAsClang] Don’t print any objcImpl overrides
Previously we only excluded implicitly-created overrides, but it turns out explicit overrides are just as problematic and just as unnecessary.

Fixes rdar://123633538.
2024-03-05 11:32:18 -08:00
Becca Royal-Gordon
8651af4325 Make @objcImpl work with @_cdecl
No real diagnostics yet, but we’re emitting mostly correct code.
2023-12-13 11:09:49 -08:00
Michael Spencer
b2640e15e4 [test] Rename all module.map files to module.modulemap
`module.map` as a module map name has been discouraged since 2014, and
Clang will soon warn on its usage. This patch renames all instances of
`module.map` in the Swift tests to `module.modulemap` in preparation
for this change to Clang.

rdar://106123303
2023-08-21 15:58:59 -07:00
Steven Wu
16e4cfae76 [ObjcHeader] Fix objc header generation when pch is explicited passed
When swift-frontend is explicitly passed the pch file as bridging header
on command-line through `-import-objc-header`, it needs to print the
original source file name if needed to the generated objc header.

rdar://109411245
2023-05-17 14:15:55 -07:00
Alex Lorenz
f4f6f7a408 [interop][SwiftToCxx] avoid importing C++ stdlib in C++ section of generated header inside of 'pragma clang attribute' block 2023-05-04 17:01:39 -07:00
QuietMisdreavus
c63d899b0f add test to ensure consistent compatibility macro definitions (#64086)
rdar://106087804
2023-04-26 10:26:02 -06:00
Becca Royal-Gordon
1629521b12 Exclude @objcImpl member impls from PrintAsObjC
PrintAsClang was not aware of @objcImplementation. Teach it to skip over both member implementations (which are declared in handwritten headers, so printing them would be a redeclaration) and overrides (which may not be valid in a category, if e.g. they are declaring a designated initializer).

Fixes rdar://106035578.
2023-02-28 21:34:25 -08:00
Nuri Amari
cd4b9a6416 Add support for textual imports to -emit-objc-header
Currently headers produced with `-emit-objc-header` /
`-emit-objc-header-path` produce headers that include modular imports.
If the consumer wishes to operate without modules enabled, these headers
cannot be used. This patch introduces a new flag
(`-emit-clang-header-nonmodular-includes`) that when enabled
attempts to argument each modular import included in such a header with
a set of equivalent textual imports.
2022-12-03 13:07:39 -08:00
Evan Wilde
7c0c89546f Revert "Add support for textual imports to -emit-objc-header" 2022-12-02 15:02:19 -08:00
Nuri Amari
89a01dddfb Add support for textual imports to -emit-objc-header
Currently headers produced with `-emit-objc-header` /
`-emit-objc-header-path` produce headers that include modular imports.
If the consumer wishes to operate without modules enabled, these headers
cannot be used. This patch introduces a new flag
(`-emit-clang-header-nonmodular-includes`) that when enabled
attempts to argument each modular import included in such a header with
a set of equivalent textual imports.
2022-11-19 12:05:09 -08:00
swift-ci
4917d26524 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-22 14:13:43 -07:00
Victoria Mitchell
76fe621c1f markup: add support for swift-cmark inline attributes
rdar://96830173
2022-07-21 08:13:07 -06:00
Ben Barham
6094e9f22f [next] Add missing void for function taking no arguments 2022-05-11 17:06:29 -07:00
Alex Lorenz
2e3aa87737 Revert "Revert "Merge pull request #41831 from hyp/unify-header""
This reverts commit 4c9582c295.
2022-03-19 13:36:28 -07:00
Alex Lorenz
4c9582c295 Revert "Merge pull request #41831 from hyp/unify-header"
This reverts commit cd93d23bac, reversing
changes made to f9f5476e9a.
2022-03-18 10:03:07 -07:00
Alex Lorenz
9d52099d5b [cxx-interop] start to emitting a unified header file for a Swift module
This change removes the -emit-cxx-header option, and adds a new -emit-clang-header-path option instead. It's aliased to -emit-objc-header-path for now, but in the future, -emit-objc-header-path will alias to it. After this change Swift can start emitting a single header file that can be expose declarations to C, Objective-C, or C++. For now C++ interface is generated (for all public decls) only when -enable-cxx-interop flag is passed, but that behavior will change once  attribute is supported.
2022-03-17 10:34:47 -07:00
Brent Royal-Gordon
9d4281b46b Support generic @compatibility_alias in PrintAsObjC
When @compatibility_alias is used with an ObjC generic class, this ends up importing as a generic typealias. PrintAsObjC previously didn’t handle declarations involving these types correctly; it would fail an assertion in asserts compilers, and potentially print an incorrect compatibility header in non-asserts compilers.

This PR makes it so that PrintAsObjC can now correctly use generic typealiases imported from Objective-C modules. It is, of course, still not possible to declare a generic typealias in Swift that will be printed into the Objective-C header.

Fixes rdar://67256866.
2020-08-20 16:21:50 -07:00
Slava Pestov
14cc7dd9bd PrintAsObjC: Fix crash when printing typedef that was imported inside an Objective-C generic class
The assertion here is too strict. The TypeAliasDecl will inherit a
generic signature from the outer context even if it does not have
generic parameters of its own. Instead, let's just assert that the
TypeAliasDecl does not have its own generic parameters.

Fixes <rdar://problem/63188938>.
2020-05-21 17:35:49 -04:00
Alexis Laferrière
e078a86f0b [PrintAsObjC] Test that we don't leak extended implementationOnly types 2020-04-20 16:37:13 -07:00
Doug Gregor
091f987e28 Revert "[PrintAsObjC] Test that we don't leak extended implementationOnly types"
This reverts commit 4e8867e021.
2020-04-13 13:58:07 -07:00
Xi Ge
df0493c288 PrintAsObjC: allow users to specify bridging header relative path for printing
Compatibility header may #import bridging header if specified with -import-underlying-module.
How these two headers are relative to each other is subject to project setting. To accommodate
this, we should allow users to specify bridging header directory for header generating purposes.

rdar://59110975
2020-03-11 17:09:53 -07:00
Alexis Laferrière
4e8867e021 [PrintAsObjC] Test that we don't leak extended implementationOnly types 2020-03-09 14:11:06 -07:00
Xi Ge
fa659ede76 [PrintAsObjc] add macro guard against potential recursive definitions 2020-02-05 12:37:01 -08:00
Varun Gandhi
d4bafdf9fa Add minimal test case for rdar://problem/55519276.
We are dropping the getter and setter names we get from Objective-C.
This leads to a runtime crash. Oops.
2019-10-06 06:57:50 -07:00
Jordan Rose
406a9d9cf1 Add the notion of @_implementationOnly overrides
When an @_implementationOnly import includes Objective-C categories
for existing types, it's useful to be able to override the members
provided in those categories without exposing them to clients of the
framework being built. Allow this as long as the overriding
declaration is marked as @_implementationOnly itself, with an
additional check that the type of the declaration does not change.
(Normally overrides are allowed to change in covariant ways.)

Part of rdar://50827914
2019-06-17 17:48:40 -07:00
Slava Pestov
2ad15fec3d PrintAsObjC: Add support for classes with resilient ancestry
Without -enable-resilient-objc-class-stubs, don't print classes with
resilient ancestry at all, since they're not actually visible to
Clang and referencing one will not actually work.

When the flag is specified, such classes do appear in the generated
header with the new objc_class_stub Clang attribute.
2019-03-26 18:58:13 -04:00
Jordan Rose
480e02b0cf [test] Update PrintAsObjC test (just one) for Swift 3 removal
Use Swift 4 as the "old" version instead of Swift 3.
2018-07-12 15:44:10 -07:00
Slava Pestov
5964c00091 Migrate PrintAsObjC tests to Swift 4
I didn't migrate test/PrintAsObjC/versioned.swift, because it
explicitly checks the Swift 3 projection of some APIs.
2018-06-26 16:57:04 -07:00
Jordan Rose
d734ce800c [PrintAsObjC] Sort imports alphabetically. (#15899)
This ensures that 'Foo' always gets imported before 'Foo_Private'.
This shouldn't strictly be necessary but does end up with more
reliable results in practice.

rdar://problem/36159006
2018-04-12 20:57:42 -07:00
Jordan Rose
520d6b9b91 Make NS_TYPED_ENUMS ObjectiveCBridgeable when they wrap an object (#15270)
This allows them to be used in generic arguments for NSArray et al.
We already do this for the ones that wrap bridged values (like
NSString/String), but failed to do it for objects that /weren't/
bridged to Swift values (class instances and protocol compositions),
or for Error-which-is-special.

In addition to this being a sensible thing to do, /not/ doing this led
to IRGen getting very confused (i.e. crashing) when we imported a
Objective-C protocol that actually used an NS_TYPED_ENUM in this way.

(We actually shouldn't be using Swift's IRGen logic to emit protocol
descriptors for imported protocols at all, because it's possible we
weren't able to import all the requirements. But that's a separate
issue.)

https://bugs.swift.org/browse/SR-6844
2018-03-15 16:17:38 -07:00
Jordan Rose
9a9ea6915f [PrintAsObjC] Downgrade +new unavailability to deprecation in Swift 4
The general policy has been that even if something crashes at run
time, we don't make it a hard error in Swift 4 mode (or Swift 3 mode!)
if it wasn't a hard error in Swift 4.0 (3.0). In this case, we thought
we could get away with it, and then it turns out it actually caused
some problems. (And as 2bc010681 shows, we can still make mistakes.)

This change isn't perfect because the diagnostic appears in /clients/
rather than in the module that's being compiled as Swift 4 (instead of
Swift 5). But it still means that someone who hasn't changed
/anything/ from a valid Swift 4.0 project will be able to compile
without any changes, even if they were relying on being able to call
+new when -init was unavailable for some reason.

More rdar://problem/35942058
2017-12-13 17:59:55 -08:00
Jordan Rose
01add3aec1 [PrintAsObjC] Defend against macros named 'any' (#12396)
SourceKit uses a clever pragma to automatically attach an attribute to
every declaration in the file. However, the form of this pragma (clang
attribute push) uses a parenthesized list marked with 'any', which turns
out to be a name that people sometimes use for macros. Guard against this
using the push_macro pragma (originally from MSVC), under the assumption
that anyone who supports the highly-use-specific 'external_source_symbol'
attribute probably implements this more common extension. This does make
the generated header a little uglier, but it's not like it was really
pretty to begin with.

rdar://problem/34168022
2017-10-12 13:39:07 -07:00
Jordan Rose
bd0cb3de8a [PrintAsObjC] Silence -Wnullability for the generated declarations. (#10247)
Since Swift 3 and Swift 4 might have different views of an Objective-C
API's nullability, we can end up with incompatible overrides,
including with inherited initializers. This is unfortunate but also
realistic; the Swift 3 code is /not/ set up to handle the new nullability
used by Swift 4 and Objective-C. Just silence the warning.

(It would be nice to not print inherited initializers at all, but that
would mean making sure there are no convenience initializers we have
to print as well. Otherwise the class would get mistaken for one
without explicit designated initializers.)

rdar://problem/32571301
2017-06-14 13:50:06 -07:00
Jordan Rose
eb9f047ad8 [PrintAsObjC] Handle the importer's compatibility typealiases. (#10042)
These are TypeAliasDecls whose Clang nodes are not TypedefNameDecls.
This worked all right for classes, but dropped the tag keyword
(e.g. 'struct') for tag decls with names of their own, and didn't
print any name at all for C types that used the
typedef-for-anonymous-tag pattern.

rdar://problem/32514335
2017-06-01 15:28:44 -07:00
Doug Gregor
b610a509fc [PrintAsObjC] Handle typealiases to non-type Clang declarations.
For historic reasons, Clang's representation of an Objective-C class
declaration ObjCInterfaceDecl) and compatibility alias
(ObjCCompatibleAliasDecl) are not actually Clang TypeDecl nodes. Cope
with this in Objective-C printing, fixing rdar://problem/32308192.
2017-05-19 21:57:41 -07:00
Argyrios Kyrtzidis
28b15852d8 Fix tests due to clang namespacing USR changes 2017-04-24 17:42:36 -07:00
Rintaro Ishizaki
dd8531e37b Merge pull request #5542 from danielmartin/fix-SR-2860
[SR-2860] Change the way comments are exported to Doxygen
2017-01-16 23:11:11 +09:00
Daniel Martín
d510793566 Fix SR-2860 2017-01-16 09:39:36 +01:00
Jordan Rose
33ed767933 [ClangImporter] Preserve the names of imported ObjC properties. (#6182)
This is necessary for proper working of #keyPath, as well as improving
the experience of PrintAsObjC.

rdar://problem/28543037
2017-01-10 13:37:12 -08:00
Daniel Martín
4ce3070133 Add test case that reproduces bug SR-2860 2017-01-07 18:09:21 +01:00
Christopher Rogers
a33aed0180 [PrintAsObjC] Inline code segments in documentation should remain inline. (#6625)
Fixes <https://bugs.swift.org/browse/SR-3163>.
2017-01-06 17:21:59 -08:00
Tim Bodeit
96c964ee0d [test/PrintAsObjc] Adjust existing test cases for SWIFT_WARN_UNUSED_RESULT 2016-12-10 01:48:49 +01:00
Jordan Rose
7add05fa38 [PrintAsObjC] Use of imported generics require the full definition. (#5518)
I'm not sure why this didn't occur to me in 8282160d: of course if you
see a generic type with arguments, you need to see the @interface for
that type in order to supply the arguments. Maybe I was thinking the
generated interface would automatically import anything the module
itself imports, but that hasn't ever been true.

rdar://problem/28738008
2016-10-31 10:34:25 -07:00
Jacob Bandes-Storch
89d0d62bd6 [PrintAsObjC] print __attribute__((noescape)) in Obj-C headers (#4438)
https://bugs.swift.org/browse/SR-2406
2016-08-31 09:27:59 -07:00
Jordan Rose
ea4d110146 [PrintAsObjC] Handle typealiases in ObjC generics.
More specifically, don't try to emit a definition for them. Just fall
through to what we do for forward-declarations...which also needed some
fixing, to make sure we don't use a Swift typealias as its underlying
type but never import the underlying type.

https://bugs.swift.org/browse/SR-2352
2016-08-16 11:09:49 -07:00
Jordan Rose
8282160de8 [PrintAsObjC] Handle circularities introduced by ObjC generics.
Like Swift generics, Objective-C generics may have constraints; unlike
Swift generics, Objective-C doesn't do separate parsing and
type-checking passes. This means that any generic arguments for
constrained generic parameters must be fully-defined, in order to
check that they satisfy the constraints.

This commit addresses this problem with three different mechanisms,
one for each kind of declaration that might run into this issue:

- For classes, if a member references a type with constrained generic
  parameter, and the corresponding argument type hasn't been printed
  yet, that member is "delayed", which means it is put into a category
  at the end of the file.

- Protocols cannot have categories, so for protocols we instead see if
  we can print the definition of the other type first. To break
  circular dependencies, the printer will not attempt this if both the
  type and the protocol are already being depended on. This isn't
  perfect (see below).

- Rather than delaying members of extensions, we just delay them
  wholesale. This keeps related members together, but also has
  problems (see below).

These approaches solve the most common cases while still not crashing
in the uncommon ones. However, there are still a number of problems:

- The protocol heuristic is overly negative, which means we may generate
  an invalid header even when there's a reasonable ordering. For example,
  a single class might inherit from a class A and conform to protocol P,
  and protocol P depends on class A as a generic argument. In this case,
  defining class A first is the right thing to do, but it's possible for
  the printer to decide that there's circularity here and just forward-
  declare A instead.

- Protocols really can be circular. This can be fixed by printing a
  forward-declared protocol alongside the generic constraints, i.e.
  'id <MoreThanNSCopying, NSCopying>' instead of just
  'id <MoreThanNSCopying>'.

- Extensions can introduce protocols as well. This is not modeled at
  all; if a member depends on a protocol conformance, it's assumed
  that simply printing the class would be sufficient. This could be
  fixed by checking how a generic argument satisfies its constraints,
  possibly delaying individual members from extensions in order to
  print them sooner.

- More cases I haven't thought about.

Test cases for some of these problems are in the new
circularity-errors.swift file, mostly to make sure the ObjC printer
doesn't crash when it encounters them.

rdar://problem/27109377
2016-08-09 10:41:09 -07:00
Jordan Rose
02d2517866 [PrintAsObjC] Hack: Assume all option sets have typedefs. (#3851)
...because otherwise option sets that get imported as members using
NS_SWIFT_NAME are printed with an 'enum' tag, and the definition of
NS_OPTIONS only declares the typedef under C++.

We should come back and figure out something more principled for this
later, but for now this solves an issue with generated headers
imported into C++ translation units.

rdar://problem/27130343
2016-08-03 13:03:50 -07:00