27 Commits

Author SHA1 Message Date
Doug Gregor
ae90b60fc8
[APINotes] Add SwiftDestroyOp API note to map to the "destroy" function (#153261)
Like retain/release for reference types, "destroy" lets us specify an
operation that is used to deinitialize an instance of a noncopyable
type.
2025-08-15 08:02:14 +01:00
Doug Gregor
2b37471f72
[API notes] Allow SwiftConformsTo on Typedefs
SwiftConformsTo specifies an additional conformance that should be
applied on import. Allow this on typedefs, because those can be imported
as wrapper types.
2025-08-12 19:57:43 +01:00
Kazu Hirata
8f71650baa
[clang] Use a new constructor of ArrayRef (NFC) (#146007)
ArrayRef now has a new constructor that takes a parameter whose type
has data() and size().  This patch migrates:

  ArrayRef<T>(X.data(), X.size()

to:

  ArrayRef<T>(X)
2025-06-26 23:38:05 -07:00
fahadnayyar
62a2f0fdc7
[APINotes] Add support for SWIFT_RETURED_AS_UNRETAINED_BY_DEFAULT (#138699)
This patch adds support in APINotes for annotating C++ user-defined
types with: `swift_attr("returned_as_unretained_by_default")`
This attribute allows to specify a default ownership convention for
return values of `SWIFT_SHARED_REFERENCE` c++ types. Specifically, it
marks all unannotated return values of this type as `unretained` (`+0`)
by default, unless explicitly overridden at the API level using
`swift_attr("returns_retained")` or `swift_attr("returns_unretained")`.

The corresponding Swift compiler support for this annotation enables
developers to suppress warnings about unannotated return ownership in
large codebases while maintaining safe and predictable ownership
semantics. By enabling this in APINotes, library authors can define this
behavior externally without needing to modify C++ source headers
directly.

### Example usage in APINotes:
```
- Name: RefCountedTypeWithDefaultConvention
  SwiftImportAs: reference
  SwiftDefaultOwnership: unretained

```
rdar://150764491
2025-05-07 13:42:39 -07:00
fahadnayyar
48f7f63ac7
[APINotes] Add SWIFT_RETURNS_(UN)RETAINED support (#118938)
Adding support to APINotes to annotate C++ methods and functions with
`swift_attr("returns_retained")` and `swift_attr("returns_unretained")`

rdar://141007510
2024-12-09 15:37:09 -08:00
Gábor Horváth
d2db9bd708
[clang][APINotes] Add support for the SwiftEscapable attribute (#115866)
This is similar to SwiftCopyable. Also fix missing SwiftCopyable dump
for TagInfo.
2024-11-12 21:34:56 +00:00
Gábor Horváth
5f4e3a3ced
[clang] Support 'this' position for lifetimebound attribute (#115021)
This patch makes the position -1 interpreted as the position for 'this'.
Adds some basic infrastructure and support for lifetimebound attribute.
2024-11-07 17:54:04 +00:00
Gábor Horváth
7ac78f1342
[clang] Add preliminary lifetimebound support to APINotes (#114830) 2024-11-04 20:29:56 +00:00
Kazu Hirata
13c14c61cd
[APINotes] Avoid repeated hash lookups (NFC) (#107959) 2024-09-10 07:31:36 -07:00
Kazu Hirata
bdb6f1b9d6
[APINotes] Avoid repeated hash lookups (NFC) (#107758) 2024-09-08 09:50:44 -07:00
Egor Zhdan
b81697718f
[APINotes] Support fields of C/C++ structs
This allows annotating fields of C/C++ structs using API Notes.

Previously API Notes supported Objective-C properties, but not fields.

rdar://131548377
2024-08-16 18:48:39 +01:00
Egor Zhdan
dc8c217db6
[APINotes] Support C++ tag conformances to Swift protocols
This allows adding a Clang attribute
`swift_attr("conforms_to:ModuleName.ProtocolName")` to C++ structs via
API Notes.

The Swift compiler respects this attribute when importing C++ types into
Swift by automatically declaring the C++ type as a conforming type to
the given Swift protocol.

rdar://131388824
2024-08-13 12:47:23 +01:00
Egor Zhdan
8a79dc7e6f
[APINotes] Support annotating C++ methods
This adds support for adding Clang attributes to C++ methods declared
within C++ records by using API Notes.

For instance:
```
Tags:
- Name: IntWrapper
  Methods:
  - Name: getIncremented
    Availability: none
```

This is the first instance of something within a C++ record being
annotated with API Notes, so it adds the necessary infra to make a C++
record an "API Notes context".

Notably this does not add support for nested C++ tags. That will be
added in a follow-up patch.

rdar://131387880
2024-07-19 13:35:13 +01:00
Egor Zhdan
c5f402f95d
[APINotes] Reduce memory footprint for Obj-C/C++ contexts
We were storing extraneous data for certain Objective-C/C++ entities.
Specifically, for declarations that can be nested in another context
(such as functions) we were storing the kind of the parent context in
addition to its ID. The ID is always sufficient.

This removes the logically incorrect usages of `ContextTableKey` that
don't actually describe a context, but rather describe a single
declaration. This introduces `SingleDeclTableKey` to store that kind of
entities in a more compact and reasonable way.
2024-07-18 13:27:24 +01:00
Egor Zhdan
82ee7ae3c9
[APINotes] Refactor: remove references to ObjCContext...
API Notes now support in C++. In preparation for supporting C++ methods
in API Notes, this change renames the remaining usages of
`ObjCContextABC` into `ContextABC` to make it clear that those contexts
might actually be C++, not Objective-C.

This is NFC-ish.
2024-07-10 15:07:09 +01:00
Egor Zhdan
b2098db248
[APINotes] Allow annotating a C++ type as non-copyable in Swift
Certain C++ types, such as `std::chrono::tzdb` in libstdc++, are
non-copyable, but don't explicitly delete their copy constructor.
Instead, they trigger template instantiation errors when trying to call
their implicit copy constructor. The Swift compiler inserts implicit
copies of value types in some cases, which trigger compiler errors for
such types.

This adds a Clang API Notes attribute that allows annotating C++ types
as non-copyable in Swift. This lets the Swift compiler know that it
should not try to instantiate the implicit copy constructor for a C++
struct.

rdar://127049438
2024-04-26 19:30:08 +01:00
Dmitri Gribenko
219511aee2 [APINotes] Make an assert in a std::sort call tolerate self-comparisons
libc++ debug mode verifies that a comparator passed to std::sort defines
a strict weak order by calling it with the same element.

See also:

- RFC that introduced the feature:
  https://discourse.llvm.org/t/rfc-strict-weak-ordering-checks-in-the-debug-libc/70217

- `strict_weak_ordering_check.h` in libc++ sources.
2024-03-28 23:11:58 +01:00
Egor Zhdan
5e4c4365f8
[APINotes] Fix a few accidental refactoring artifacts
This fixes a few breakages introduced during upstreaming.
2024-02-27 00:37:15 +00:00
Egor Zhdan
41021e8ef8
[APINotes] Upstream APINotes YAML compiler
This upstreams more of the Clang API Notes functionality that is
currently implemented in the Apple fork:
https://github.com/apple/llvm-project/tree/next/clang/lib/APINotes
2023-11-13 19:36:36 +00:00
Kazu Hirata
b8885926f8 Use llvm::endianness::{big,little,native} (NFC)
Note that llvm::support::endianness has been renamed to
llvm::endianness while becoming an enum class as opposed to an enum.
This patch replaces llvm::support::{big,little,native} with
llvm::endianness::{big,little,native}.
2023-10-10 22:54:51 -07:00
Egor Zhdan
195cdfd9ec
[APINotes] Support SwiftImportAs for C++ structs
This upstreams a few Clang API Notes attributes that were recently added
downstream in the Apple fork
(https://github.com/apple/llvm-project/pull/7386).
2023-09-07 08:39:35 +01:00
Egor Zhdan
8dc789a226
[APINotes] Upstream APINotesWriter
This upstreams more of the Clang API Notes functionality that is
currently implemented in the Apple fork:
https://github.com/apple/llvm-project/tree/next/clang/lib/APINotes
2023-09-04 19:45:55 +01:00
Egor Zhdan
9939556625
[APINotes] Initial support for C++ namespaces
This upstreams a part of the C++ namespaces support in Clang API Notes.

The complete patch was recently merged downstream in the Apple fork: https://github.com/apple/llvm-project/pull/7230.

This patch only adds the parts of the namespace support that can be cleanly applied on top of the API Notes infrastructure that was upstreamed previously.

Differential Revision: https://reviews.llvm.org/D159092
2023-08-30 12:54:42 +01:00
Simon Pilgrim
09c5041911 Fix MSVC "'|': unsafe operation: no value of type '_Ty' promoted to type 'int' can equal the given constant" warning. NFC. 2023-08-18 09:43:46 +01:00
Michael Liao
4edde41dae [clang][APINotes] Fix build error due to -fpermissive on GCC. NFC 2023-08-17 15:00:39 -04:00
Saleem Abdulrasool
6cdaf7e6ad APINotes: remove accidentally committed downstream changes
Remove the inferAsMember downstream changes which are not meant to be
part of the APINotes support.
2023-08-17 10:05:10 -07:00
Saleem Abdulrasool
f806be5eaa APINotes: add initial stub of APINotesWriter
This adds the skeleton for serializing out the APINotes data from the
APINotes. The writer uses a private implementation pattern to reduce
the exposed surface to just the programmatic representation of the
APINotes and not expose the details of the bitcode encoding. The format
itself is not considered stable and should only be accessed through the
APINotes Reader and Writer types.

Differential Revision: https://reviews.llvm.org/D92797
Reviewed By: martong
2023-08-17 08:49:35 -07:00