Skip to content

Commit 56e024d

Browse files
authored
Merge pull request #42412 from plotfi/c-enums-withOptions-omit
[C++-Interop] Teach omitNeedlessWords to handle raw integer C-enums in C++
2 parents 2266a57 + a0985a0 commit 56e024d

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,6 +2465,19 @@ DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument(
24652465
return DefaultArgumentKind::EmptyArray;
24662466
}
24672467
}
2468+
} else if (const clang::TypedefType *typedefType =
2469+
type->getAs<clang::TypedefType>()) {
2470+
// Get the AvailabilityAttr that would be set from CF/NS_OPTIONS
2471+
if (importer::isUnavailableInSwift(typedefType->getDecl(), nullptr, true)) {
2472+
// If we've taken this branch it means we have an enum type, and it is
2473+
// likely an integer or NSInteger that is being used by NS/CF_OPTIONS to
2474+
// behave like a C enum in the presence of C++.
2475+
auto enumName = typedefType->getDecl()->getDeclName().getAsString();
2476+
for (auto word : llvm::reverse(camel_case::getWords(enumName))) {
2477+
if (camel_case::sameWordIgnoreFirstCase(word, "options"))
2478+
return DefaultArgumentKind::EmptyArray;
2479+
}
2480+
}
24682481
}
24692482

24702483
// NSDictionary arguments default to [:] (or nil, if nullable) if "options",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Enum usage that is bitwise-able and assignable in C++, aka how CF_OPTIONS
2+
// does things.
3+
typedef int __attribute__((availability(swift, unavailable))) NSEnumerationOptions;
4+
enum : NSEnumerationOptions { NSEnumerationConcurrent, NSEnumerationReverse };
5+
6+
@interface NSSet
7+
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts ;
8+
@end

test/Interop/Cxx/enum/Inputs/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ module AnonymousWithSwiftName {
1212
header "anonymous-with-swift-name.h"
1313
requires cplusplus
1414
}
15+
16+
module CenumsWithOptionsOmit {
17+
header "c-enums-withOptions-omit.h"
18+
requires cplusplus
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=CenumsWithOptionsOmit -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
2+
// REQUIRES: objc_interop
3+
4+
import CenumsWithOptionsOmit
5+
6+
// CHECK: class NSSet {
7+
// CHECK-NEXT: class func enumerateObjects(options
8+
// CHECK-NEXT: func enumerateObjects(options
9+
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "enumerateObjects(options:)")

0 commit comments

Comments
 (0)