-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[C++-Interop] Teach omitNeedlessWords to handle raw integer C-enums in C++ #42412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2431,6 +2431,19 @@ DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument( | |
return DefaultArgumentKind::EmptyArray; | ||
} | ||
} | ||
} else if (const clang::TypedefType *typedefType = | ||
type->getAs<clang::TypedefType>()) { | ||
// Get the AvailabilityAttr that would be set from CF/NS_OPTIONS | ||
if (importer::isUnavailableInSwift(typedefType->getDecl(), nullptr, true)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can actually use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this is a static method, so never mind. |
||
// If we've taken this branch it means we have an enum type, and it is | ||
// likely an integer or NSInteger that is being used by NS/CF_OPTIONS to | ||
// behave like a C enum in the presence of C++. | ||
auto enumName = typedefType->getDecl()->getDeclName().getAsString(); | ||
for (auto word : llvm::reverse(camel_case::getWords(enumName))) { | ||
if (camel_case::sameWordIgnoreFirstCase(word, "options")) | ||
return DefaultArgumentKind::EmptyArray; | ||
} | ||
} | ||
} | ||
|
||
// NSDictionary arguments default to [:] (or nil, if nullable) if "options", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Enum usage that is bitwise-able and assignable in C++, aka how CF_OPTIONS | ||
// does things. | ||
typedef int __attribute__((availability(swift, unavailable))) NSEnumerationOptions; | ||
enum : NSEnumerationOptions { NSEnumerationConcurrent, NSEnumerationReverse }; | ||
|
||
@interface NSSet | ||
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts ; | ||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUN: %target-swift-ide-test -print-module -module-to-print=CenumsWithOptionsOmit -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s | ||
plotfi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// REQUIRES: objc_interop | ||
|
||
import CenumsWithOptionsOmit | ||
|
||
// CHECK: class NSSet { | ||
// CHECK-NEXT: class func enumerateObjects(options | ||
// CHECK-NEXT: func enumerateObjects(options | ||
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "enumerateObjects(options:)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is maybe another reason that we should try harder to link typedef -> enum with some clang attr (this will be hard without naming the enum 😕)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was thinking something similar. Maybe we need another attr or something to link things. It would be potentially problematic though because it would assume folks are putting additional attrs on their enums (unless they just use CF_OPTIONS ).