Skip to content
10 changes: 10 additions & 0 deletions src/NearbyInteraction/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@
// Copyright 2020 Microsoft Inc.
//

using System.ComponentModel;

namespace NearbyInteraction {

#if XAMCORE_5_0
[NoTV, NoMac, iOS (14, 0)]
#else
[NoTV, Mac (12, 0), iOS (14, 0)]
#endif
[MacCatalyst (14, 0)]
#if !__MACOS__
[ErrorDomain ("NIErrorDomain")]
#endif
[Native]
#if __MACOS__ && !XAMCORE_5_0
[Obsolete ("Not available on this platform.")]
[EditorBrowsable (EditorBrowsableState.Never)]
#endif
public enum NIErrorCode : long {
UnsupportedPlatform = -5889,
InvalidConfiguration = -5888,
Expand Down
1 change: 1 addition & 0 deletions src/Network/NWEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public enum NWReportResolutionSource {

[NoTV, NoiOS]
[NoMacCatalyst]
[NativeName ("nw_ethernet_channel_state_t")]
public enum NWEthernetChannelState {
Invalid = 0,
Waiting = 1,
Expand Down
14 changes: 14 additions & 0 deletions tests/sharpie/Tests/EnumTypedefAvailability.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// RUN objc: -x objective-c

// Regression test for https://github.com/dotnet/macios/issues/26166
// Clang attaches an availability attribute written after a `typedef enum { } Name;`
// declaration to the TypedefDecl that names the enum, not to the EnumDecl itself.
// Sharpie must still propagate that availability to the generated C# enum.

typedef enum {
MacUnavailableEnumOne,
MacUnavailableEnumTwo
} MacUnavailableEnum __attribute__((availability(macosx,unavailable)));
5 changes: 5 additions & 0 deletions tests/sharpie/Tests/EnumTypedefAvailability.objc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[NoMac]
public enum MacUnavailableEnum : uint {
One,
Two
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@
!missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaCharacteristic(System.String,System.Action`2<Foundation.NSArray`1<AVFoundation.AVMovieTrack>,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #1
!missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaType(System.String,System.Action`2<Foundation.NSArray`1<AVFoundation.AVMovieTrack>,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #0
!missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaType(System.String,System.Action`2<Foundation.NSArray`1<AVFoundation.AVMovieTrack>,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #1

# AVAudioRoutingArbitrationCategory is macOS-only natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by AVAudioRoutingArbiter.BeginArbitration.
!unknown-native-enum! AVAudioRoutingArbitrationCategory bound
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
## The following types are only available on watchOS (not iOS, so definitively not Catalyst)
!missing-enum! CMFallDetectionEventUserResolution not bound

## not available on Catalyst (tested with macOS 12 b3)
!missing-selector! +CMAltimeter::isAbsoluteAltitudeAvailable not bound
!missing-selector! CMAltimeter::startAbsoluteAltitudeUpdatesToQueue:withHandler: not bound
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Documentation as of Xcode 13 as macOS only
!missing-enum! NSFileProviderFileSystemFlags not bound
!missing-enum! NSFileProviderDomainTestingModes not bound
# Header clearly says this enum is not available on Mac Catalyst, not sure why xtro thinks it is.
!missing-enum! NSFileProviderContentPolicy not bound
# These enums are unavailable on Mac Catalyst natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API.
!unknown-native-enum! NSFileProviderItemCapabilities bound
!unknown-native-enum! NSFileProviderKnownFolders bound
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# This selector requires ARKit, which doesn't work on Mac Catalyst
!missing-selector! NISession::setARSession: not bound
# Headers are clear that these two enums are not supported on Mac Catalyst, don't know why xtro thinks they are
!missing-enum! NIDLTDOACoordinatesType not bound
!missing-enum! NIDLTDOAMeasurementType not bound
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
!missing-enum! nw_data_transfer_report_state_t not bound
!missing-enum! nw_endpoint_type_t not bound
!missing-enum! nw_error_domain_t not bound
!missing-enum! nw_ethernet_channel_state_t not bound
!missing-enum! nw_framer_start_result_t not bound
!missing-enum! nw_interface_radio_type_t not bound
!missing-enum! nw_interface_type_t not bound
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
!missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaType(System.String,System.Action`2<Foundation.NSArray`1<AVFoundation.AVMovieTrack>,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #0
!missing-null-allowed! 'System.Void AVFoundation.AVMutableMovie::LoadTracksWithMediaType(System.String,System.Action`2<Foundation.NSArray`1<AVFoundation.AVMovieTrack>,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #1

# AVAudioRoutingArbitrationCategory is macOS-only natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by AVAudioRoutingArbiter.BeginArbitration.
!unknown-native-enum! AVAudioRoutingArbitrationCategory bound

## bound selectors with no native declaration reachable from the class in the current SDK headers:
## private/SPI selectors, protocol/category members Apple inlines without declaring the conformance on
## the class, managed callback infrastructure, or API Apple removed/privatized - kept for compatibility
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
!missing-selector! AUAudioUnit::messageChannelFor: not bound
!missing-protocol! AUMessageChannel not bound

# this enum is declared as not available on iOS, not sure why it shows up (it's visionOS-only)
!missing-enum! CASoundStageSize not bound
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
!missing-enum! CMFallDetectionEventUserResolution not bound

## no generator support for FastEnumeration - https://bugzilla.xamarin.com/show_bug.cgi?id=4391
!missing-protocol-conformance! CMSensorDataList should conform to NSFastEnumeration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@
!missing-null-allowed! 'System.Void FileProvider.NSFileProviderManager::SignalEnumerator(System.String,System.Action`1<Foundation.NSError>)' is missing a '?' on parameter 'completion' block parameter #0
!missing-null-allowed! 'System.Void FileProvider.NSFileProviderManager::SignalErrorResolved(Foundation.NSError,System.Action`1<Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #0
!missing-null-allowed! 'System.Void FileProvider.NSFileProviderManager::WaitForChangesOnItemsBelowItem(System.String,System.Action`1<Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #0

# NSFileProviderKnownFolders is macOS-only natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by bound API.
!unknown-native-enum! NSFileProviderKnownFolders bound
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,3 @@
!missing-enum! ar_device_anchor_query_status_t not bound
!missing-enum! ar_device_anchor_tracking_state_t not bound
!missing-enum! ar_session_error_code_t not bound
!missing-enum! ar_world_tracking_error_code_t not bound
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Added but deprecated, so there is not point to add them
!missing-protocol-member! CLLocationManagerDelegate::locationManager:didRangeBeacons:inRegion: not found
!missing-protocol-member! CLLocationManagerDelegate::locationManager:rangingBeaconsDidFailForRegion:withError: not found

# CLServiceSessionAuthorizationRequirement is unavailable on macOS natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by CLServiceSession.
!unknown-native-enum! CLServiceSessionAuthorizationRequirement bound
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
## available in headers but not used in any (available) API
!missing-enum! CMAttitudeReferenceFrame not bound
!missing-enum! CMFallDetectionEventUserResolution not bound

## available in headers but do not make much sense for macOS
!missing-selector! CMAmbientPressureData::pressure not bound
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Headers are clear that these two enums are not supported on macOS, don't know why xtro thinks they are
!missing-enum! NIDLTDOACoordinatesType not bound
!missing-enum! NIDLTDOAMeasurementType not bound
# These enums are unavailable on macOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API.
!unknown-native-enum! NIAlgorithmConvergenceStatus bound
!unknown-native-enum! NINearbyObjectRemovalReason bound
!unknown-native-enum! NINearbyObjectVerticalDirectionEstimate bound
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@
!missing-type! AVCaptionRenderer not bound

!missing-null-allowed! 'System.Void AVFoundation.AVCapturePhotoOutput::SetPreparedPhotoSettings(AVFoundation.AVCapturePhotoSettings[],System.Action`2<System.Boolean,Foundation.NSError>)' is missing a '?' on parameter 'completionHandler' block parameter #1

# AVAudioRoutingArbitrationCategory is macOS-only natively (API_UNAVAILABLE on the typedef), but the managed enum is bound and used by AVAudioRoutingArbiter.BeginArbitration.
!unknown-native-enum! AVAudioRoutingArbitrationCategory bound
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@

# the enum is available on tvOS, but none of its values are, so just don't bind the enum itself
!missing-enum! AUAudioMixRenderingStyle not bound

# this enum is declared as not available on tvOS, not sure why it shows up (it's visionOS-only)
!missing-enum! CASoundStageSize not bound
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
!extra-enum-value! Managed value 1008 for ASAuthorizationError.CredentialExport not found in native headers
!extra-enum-value! Managed value 1009 for ASAuthorizationError.PreferSignInWithApple not found in native headers
!extra-enum-value! Managed value 1010 for ASAuthorizationError.DeviceNotConfiguredForPasskeyCreation not found in native headers

# These enums are unavailable on tvOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API.
!unknown-native-enum! ASAuthorizationPublicKeyCredentialAttachment bound
!unknown-native-enum! ASAuthorizationWebBrowserPublicKeyCredentialManagerAuthorizationState bound
!unknown-native-enum! ASPublicKeyCredentialClientDataCrossOriginValue bound
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These enums are unavailable on tvOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API.
!unknown-native-enum! CSSuggestionKind bound
!unknown-native-enum! CSUserInteraction bound
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
## Not used on tvOS (no camera)
!missing-enum! RPCameraPosition not bound

# SCStreamErrorDomain is from ScreenCaptureKit, but only macOS and Mac Catalyst.
# However, Apple seems to also have added it to ReplayKit for iOS and tvOS, which is why xtro detects it as such.
# It doesn't seem useful though in ReplayKit, so just skip binding it.
Expand Down
6 changes: 6 additions & 0 deletions tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@
!missing-null-allowed! 'System.Void UIKit.UIImageReader::GetImage(Foundation.NSData,System.Action`1<UIKit.UIImage>)' is missing a '?' on parameter 'completion' block parameter #0
!missing-null-allowed! 'System.Void UIKit.UIImageReader::GetImage(Foundation.NSUrl,System.Action`1<UIKit.UIImage>)' is missing a '?' on parameter 'completion' block parameter #0

# These enums are unavailable on tvOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API.
!unknown-native-enum! UIBehavioralStyle bound
!unknown-native-enum! UICornerCurve bound
!unknown-native-enum! UIFindSessionSearchResultDisplayStyle bound
!unknown-native-enum! UITextSearchMatchMethod bound

## bound selectors with no native declaration reachable from the class in the current SDK headers:
## private/SPI selectors, protocol/category members Apple inlines without declaring the conformance on
## the class, managed callback infrastructure, or API Apple removed/privatized - kept for compatibility
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# The headers are quite clear that these enums aren't available on tvOS; so not adding them to tvOS for now
!missing-enum! VTFrameRateConversionConfigurationQualityPrioritization not bound
!missing-enum! VTFrameRateConversionConfigurationRevision not bound
!missing-enum! VTFrameRateConversionParametersSubmissionMode not bound
!missing-enum! VTMotionBlurConfigurationQualityPrioritization not bound
!missing-enum! VTMotionBlurConfigurationRevision not bound
!missing-enum! VTMotionBlurParametersSubmissionMode not bound
!missing-enum! VTOpticalFlowConfigurationQualityPrioritization not bound
!missing-enum! VTOpticalFlowConfigurationRevision not bound
!missing-enum! VTOpticalFlowParametersSubmissionMode not bound

# These enums are unavailable on tvOS natively (API_UNAVAILABLE on the typedef), but the managed enums are bound and used by bound API.
!unknown-native-enum! VTSuperResolutionScalerConfigurationInputType bound
!unknown-native-enum! VTSuperResolutionScalerConfigurationModelStatus bound
!unknown-native-enum! VTSuperResolutionScalerConfigurationQualityPrioritization bound
!unknown-native-enum! VTSuperResolutionScalerConfigurationRevision bound
!unknown-native-enum! VTSuperResolutionScalerParametersSubmissionMode bound
38 changes: 38 additions & 0 deletions tests/xtro-sharpie/xtro-sharpie/EnumCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class ManagedValue {
Dictionary<object, ManagedValue> managed_values = new Dictionary<object, ManagedValue> ();
Dictionary<object, (string Name, EnumConstantDecl Decl)> native_values = new Dictionary<object, (string Name, EnumConstantDecl Decl)> ();

// Cache, per DeclContext, the names of the typedefs that name an enum and are unavailable.
// This avoids rescanning the whole DeclContext for every enum we visit (which would be
// O(enums × siblings)); each context is scanned once and every enum check is then O(1).
Dictionary<IDeclContext, HashSet<string>> unavailable_enum_typedefs = new Dictionary<IDeclContext, HashSet<string>> ();

public EnumCheck (BindingResult bindingResult)
: base (bindingResult)
{
Expand Down Expand Up @@ -67,6 +72,12 @@ public override void VisitEnumDecl (EnumDecl decl)
if (!decl.IsAvailable ())
return;

// The availability attribute (e.g. API_UNAVAILABLE(maccatalyst)) is sometimes attached to
// the typedef that names the enum instead of to the enum declaration itself, in which case
// decl.IsAvailable () above won't detect it. Look for the corresponding typedef and check it too.
if (IsUnavailableViaTypedef (decl))
return;

var framework = Helpers.GetFramework (decl);
if (framework is null)
return;
Expand Down Expand Up @@ -284,6 +295,33 @@ public override void VisitEnumDecl (EnumDecl decl)
Log.On (framework).Add ($"!wrong-enum-size! {name} managed {managed_size} vs native {native_size}");
}

// The availability attribute for an enum can be attached to the typedef that names the enum
// (e.g. `typedef NS_ENUM(NSInteger, Foo) { ... } API_UNAVAILABLE(maccatalyst);`) instead of to
// the enum declaration itself. In that case the attribute lands on the TypedefDecl, not the
// EnumDecl, so we look for the corresponding sibling typedef and check its availability. The
// per-context set of unavailable enum typedefs is computed once and cached (see the field).
bool IsUnavailableViaTypedef (EnumDecl decl)
{
var context = decl.DeclContext;
if (context is null)
return false;

if (!unavailable_enum_typedefs.TryGetValue (context, out var names)) {
names = new HashSet<string> (StringComparer.Ordinal);
foreach (var sibling in context.Decls) {
if (sibling is not TypedefDecl typedef)
continue;
if (typedef.UnderlyingType.UnqualifiedDesugaredType is not EnumType)
continue;
if (!typedef.IsAvailable ())
names.Add (typedef.Name);
}
unavailable_enum_typedefs [context] = names;
}

return decl.Name is string name && names.Contains (name);
}

static bool IsErrorEnum (string typeName)
{
if (typeName.EndsWith ("Error", StringComparison.Ordinal))
Expand Down
11 changes: 9 additions & 2 deletions tools/sharpie/Sharpie.Bind/BindingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,16 @@ public override void VisitTypedefDecl (TypedefDecl decl)
var underlyingDesugaredType = decl.UnderlyingType.UnqualifiedDesugaredType;
switch (underlyingDesugaredType?.TypeClass) {
case CX_TypeClass.CX_TypeClass_Enum:
var boundEnum = ((EnumType) underlyingDesugaredType).Decl.Annotation<TypeDeclaration> ();
if (boundEnum is not null)
var enumDecl = ((EnumType) underlyingDesugaredType).Decl;
var boundEnum = enumDecl.Annotation<TypeDeclaration> ();
if (boundEnum is not null) {
boundEnum.Name = decl.Name;
// The availability attributes for an enum may be attached to this typedef
// (e.g. `typedef NS_ENUM(NSInteger, Foo) { ... } API_UNAVAILABLE(maccatalyst);`)
// instead of to the enum declaration itself. Link the typedef to the enum so
// the AvailabilityMassager can pick up those attributes.
enumDecl.AddAnnotation (decl);
}
break;
case CX_TypeClass.CX_TypeClass_Record:
var boundRecord = ((RecordType) underlyingDesugaredType).Decl.Annotation<TypeDeclaration> ();
Expand Down
16 changes: 14 additions & 2 deletions tools/sharpie/Sharpie.Bind/Massagers/AvailabilityMassager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,24 @@ void Massage (EntityDeclaration entity)
if (decl is null)
return;

if (decl.Attrs.IsUnavailableAttr ()) {
// For enums, the availability attributes may be attached to the typedef that names
// the enum (e.g. `typedef NS_ENUM(NSInteger, Foo) { ... } API_UNAVAILABLE(maccatalyst);`)
// rather than to the enum declaration itself. BindingGenerator.VisitTypedefDecl links
// such typedefs to the enum, so include the attributes of the typedef that gives the
// enum its name here as well. An enum can be linked to more than one typedef (e.g. in
// typedef chains), so match by name to pick the one that actually names this enum and
// avoid applying availability from an unrelated typedef.
IEnumerable<Attr> attrs = decl.Attrs;
var typedef = decl.GetAnnotations<TypedefDecl> ().FirstOrDefault (t => t is not null && t.Name == entity.Name);
if (typedef is not null)
attrs = attrs.Concat (typedef.Attrs);

if (attrs.IsUnavailableAttr ()) {
entity.Remove ();
return;
}

foreach (var attr in decl.Attrs.GetAvailabilityAttributes ().SelectMany (AvailabilityBaseAttribute.FromClang))
foreach (var attr in attrs.GetAvailabilityAttributes ().SelectMany (AvailabilityBaseAttribute.FromClang))
entity.AddAttribute (attr);
}
}
Loading