58 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
Artem Chikin
a7091951f0
[APINotes] Add support for capturing all possible versioned APINotes without applying them
Swift-versioned API notes get applied at PCM constrution time relying on
'-fapinotes-swift-version=X' argument to pick the appropriate version.
This change adds a new APINotes application mode with
'-fswift-version-independent-apinotes' which causes *all* versioned API
notes to get recorded into the PCM wrapped in 'SwiftVersionedAttr'
instances. The expectation in this mode is that the Swift client will
perform the required transformations as per the API notes on the client
side, when loading the PCM, instead of them getting applied on the
producer side. This will allow the same PCM to be usable by Swift
clients building with different language versions.

In addition to versioned-wrapping the various existing API notes
annotations which are carried in declaration attributes, this change
adds a new attribute for two annotations which were previously applied
directly to the declaration at the PCM producer side: 1) Type and 2)
Nullability annotations with 'SwiftTypeAttr' and 'SwiftNullabilityAttr',
respectively. The logic to apply these two annotations to a declaration
is refactored into API.
2025-07-10 19:19:18 +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
Kazu Hirata
ed42e17298
[APINotes] Remove unused includes (NFC) (#142406)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-06-04 12:30:47 -07:00
Kazu Hirata
2a277f9ff1
[clang] Use llvm::is_sorted (NFC) (#140468) 2025-05-19 06:18:53 -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
Kazu Hirata
563c7c5539
[clang] Migrate away from PointerUnion::dyn_cast (NFC) (#124425)
Note that PointerUnion::dyn_cast has been soft deprecated in
PointerUnion.h:

  // FIXME: Replace the uses of is(), get() and dyn_cast() with
  //        isa<T>, cast<T> and the llvm::dyn_cast<T>

This patch migrates uses of PointerUnion::dyn_cast to
dyn_cast_if_present (see the definition of PointerUnion::dyn_cast).
Note that we cannot use dyn_cast in any of the migrations in this
patch; placing

  assert(!X.isNull());

just before any of dyn_cast_if_present in this patch triggers some
failure in check-clang.
2025-01-25 14:05:01 -08:00
Björn Pettersson
26f5d1ee9c
[APINotes] Avoid assertion failure with expensive checks (#120487)
Found assertion failures when using EXPENSIVE_CHECKS and running lit
tests for APINotes:
Assertion `left.first != right.first && "two entries for the same
version"' failed.

It seems like std::is_sorted is verifying that the comparison function
is reflective (comp(a,a)=false) when using expensive checks. So we would
get callbacks to the lambda used for comparison, even for vectors with a
single element in APINotesReader::VersionedInfo<T>::VersionedInfo, with
"left" and "right" being the same object. Therefore the assert checking
that we never found equal values would fail.

Fix makes sure that we skip the check for equal values when "left" and
"right" is the same object.
2024-12-18 23:36:45 +01:00
Kazu Hirata
d01c11df04
[clang] Migrate away from PointerUnion::{is,get} (NFC) (#119724)
Note that PointerUnion::{is,get} have been soft deprecated in
PointerUnion.h:

  // FIXME: Replace the uses of is(), get() and dyn_cast() with
  //        isa<T>, cast<T> and the llvm::dyn_cast<T>

I'm not touching PointerUnion::dyn_cast for now because it's a bit
complicated; we could blindly migrate it to dyn_cast_if_present, but
we should probably use dyn_cast when the operand is known to be
non-null.
2024-12-12 16:03:20 -08: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
416f101111
[clang] Use std::optional::value_or (NFC) (#109894) 2024-09-24 23:01:45 -07: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
4d5f81cad5
[APINotes] Support nested tags
This allows annotating C/C++ structs declared within other structs using
API Notes.

rdar://132083354
2024-07-23 12:28:00 +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
David Stone
aaa4ff88d6
[clang][Modules] Remove unnecessary includes of Module.h (#93417) 2024-06-03 14:49:04 +08: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
Kazu Hirata
89071f3559
[clang] Drop unaligned from calls to readNext (NFC) (#88842)
Now readNext defaults to unaligned accesses.  This patch drops
unaligned to improve readability.
2024-04-16 00:09:41 -07: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
96c8e2e88c
[APINotes] For a re-exported module, look for APINotes in the re-exporting module's apinotes file
This upstreams https://github.com/apple/llvm-project/pull/8063.

If module FooCore is re-exported through module Foo (by using
`export_as` in the modulemap), look for attributes of FooCore symbols in
Foo.apinotes file.

Swift bundles `std.apinotes` file that adds Swift-specific attributes to
the C++ stdlib symbols. In recent versions of libc++, module std got
split into multiple top-level modules, each of them is re-exported
through std. This change allows us to keep using a single modulemap file
for all supported C++ stdlibs.

rdar://121680760
2024-03-28 12:59:57 +00:00
Egor Zhdan
878097dff3
[APINotes] Fix failing tests after a PCM logic change
This fixes tests that are going to be upstreamed in the near future.
Currently they are failing downstream in the Apple open source fork.

Failing tests
  Clang :: APINotes/retain-count-convention.m
  Clang :: APINotes/types.m
  Clang :: APINotes/versioned-multi.c
  Clang :: APINotes/versioned.m

Since 2e5af56 got merged, Clang now enables `LangOpts.APINotesModules`
when reading a precompiled module that was built with API Notes enabled.
This is correct. The logic in APINotesManager needs to be adjusted to
handle this.

rdar://123526142
2024-03-11 12:02:29 +00:00
Jan Svoboda
864593b91d [clang][api-notes] NFC: Upstream some documentation 2024-03-01 16:53:12 -08: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
Kazu Hirata
0e039fc39e [APINotes] Use DenseMap::contains (NFC) 2023-12-22 14:23:25 -08:00
Kazu Hirata
f3dcc2351c
[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 08:54:13 -08:00
Egor Zhdan
f049395fc8
[APINotes] Upstream APINotesManager
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-17 13:28:51 +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
Egor Zhdan
bb352b6ead
[APINotes] Upstream APINotesReader
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-10-30 14:36:44 +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
86c31c1406
[APINotes] Remove unused OS-availability feature
iOS/OSX-specific availability is a legacy of compiled API Notes which
are not supported and were removed from Apple Clang in 2019 (see
30fc70f349).
2023-09-04 11:59:11 +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
Evan Wilde
e4ad1f976d
[NFC] Remove extra semicolons in clang/lib/APINotes/APINotesFormat.h
There are some trailing semicolons on namespaces in
clang/lib/APINotes/APINotesFormat.h. These result in warnings while
building and don't need to be there.

Reviewed By: compnerd

Differential Revision: https://reviews.llvm.org/D105049
2023-07-21 16:19:53 -07:00
Kazu Hirata
9b050e5011 [clang] Remove redundant initialization of std::optional (NFC) 2022-12-22 13:46:26 -08:00
Kazu Hirata
6eb0b0a045 Don't include Optional.h
These files no longer use llvm::Optional.
2022-12-14 21:16:22 -08:00
Krzysztof Parzyszek
c589730ad5 [YAML] Convert Optional to std::optional 2022-12-06 12:49:32 -08:00
Kazu Hirata
5891420e68 [clang] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 11:54:46 -08:00