diff --git a/scripts/cxx-api/parser/utils/text_resolution.py b/scripts/cxx-api/parser/utils/text_resolution.py index 8d5e7c46ad2..e9b67f64203 100644 --- a/scripts/cxx-api/parser/utils/text_resolution.py +++ b/scripts/cxx-api/parser/utils/text_resolution.py @@ -90,6 +90,30 @@ def normalize_angle_brackets(text: str) -> str: return text +def normalize_nullability(text: str) -> str: + """Normalize Objective-C nullability annotations to a consistent form. + + There are three forms of nullability annotations in Objective-C: + - nonnull/nullable (context-sensitive keywords, typically used as prefix) + - _Nonnull/_Nullable (type qualifiers, can appear after the type) + - __nonnull/__nullable (legacy Apple macros, deprecated but still used) + + This function normalizes all forms to _Nonnull/_Nullable, which is the + most flexible form that can be used in any position. + """ + # Normalize __nonnull -> _Nonnull (must come before nonnull) + text = re.sub(r"\b__nonnull\b", "_Nonnull", text) + # Normalize nonnull -> _Nonnull (negative lookbehind to avoid _Nonnull) + text = re.sub(r"(? _Nullable (must come before nullable) + text = re.sub(r"\b__nullable\b", "_Nullable", text) + # Normalize nullable -> _Nullable (negative lookbehind to avoid _Nullable) + text = re.sub(r"(? str: """Normalize spacing around pointer (*) and reference (&, &&) symbols. @@ -179,7 +203,9 @@ def resolve_linked_text_name( name = name[1:-1].strip() return ( - normalize_pointer_spacing(normalize_angle_brackets(name.strip())), + normalize_nullability( + normalize_pointer_spacing(normalize_angle_brackets(name.strip())) + ), initialier_type, ) diff --git a/scripts/cxx-api/tests/snapshots/should_normalize_nullability/snapshot.api b/scripts/cxx-api/tests/snapshots/should_normalize_nullability/snapshot.api new file mode 100644 index 00000000000..dc16c53ac34 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_normalize_nullability/snapshot.api @@ -0,0 +1,10 @@ +interface NullabilityTest : public NSObject { + public @property (assign) NSString* nonnullProperty; + public @property (assign) NSString* nullableProperty; + public virtual NSString* _Nonnull legacyNonnullMethod(); + public virtual NSString* _Nonnull modernNonnullMethod(); + public virtual NSString* _Nullable legacyNullableMethod(); + public virtual NSString* _Nullable modernNullableMethod(); + public virtual _Nonnull NSString* nonnullMethod(); + public virtual _Nullable NSString* nullableMethod(); +} diff --git a/scripts/cxx-api/tests/snapshots/should_normalize_nullability/test.h b/scripts/cxx-api/tests/snapshots/should_normalize_nullability/test.h new file mode 100644 index 00000000000..b702b80276b --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_normalize_nullability/test.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +@interface NullabilityTest : NSObject + +// Different nullability annotation styles that should be normalized +- (nonnull NSString *)nonnullMethod; +- (nullable NSString *)nullableMethod; +- (NSString *__nonnull)legacyNonnullMethod; +- (NSString *__nullable)legacyNullableMethod; +- (NSString *_Nonnull)modernNonnullMethod; +- (NSString *_Nullable)modernNullableMethod; + +@property (nonatomic, nonnull) NSString *nonnullProperty; +@property (nonatomic, nullable) NSString *nullableProperty; + +@end