Skip to content

[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

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2431,6 +2431,19 @@ DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument(
return DefaultArgumentKind::EmptyArray;
}
}
} else if (const clang::TypedefType *typedefType =
type->getAs<clang::TypedefType>()) {
Copy link
Contributor

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 😕)

Copy link
Contributor Author

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 😕)

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 ).

// Get the AvailabilityAttr that would be set from CF/NS_OPTIONS
if (importer::isUnavailableInSwift(typedefType->getDecl(), nullptr, true)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually use if (isUnavailableInSwift(typdefType->getDecl())) here because you're in the Implementation (in my patch I had to use this because of layering).

Copy link
Contributor

Choose a reason for hiding this comment

The 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",
Expand Down
8 changes: 8 additions & 0 deletions test/Interop/Cxx/enum/Inputs/c-enums-withOptions-omit.h
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
5 changes: 5 additions & 0 deletions test/Interop/Cxx/enum/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ module AnonymousWithSwiftName {
header "anonymous-with-swift-name.h"
requires cplusplus
}

module CenumsWithOptionsOmit {
header "c-enums-withOptions-omit.h"
requires cplusplus
}
9 changes: 9 additions & 0 deletions test/Interop/Cxx/enum/c-enums-withOptions-omit.swift
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
// 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:)")