Skip to content

Commit 7b7bb7b

Browse files
authored
Merge pull request #9804 from DougGregor/printasobjc-typealias-nontypedecl
2 parents cb8dbd9 + b610a50 commit 7b7bb7b

File tree

7 files changed

+87
-6
lines changed

7 files changed

+87
-6
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,11 +1361,23 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
13611361
return;
13621362

13631363
if (alias->hasClangNode()) {
1364-
auto *clangTypeDecl = cast<clang::TypeDecl>(alias->getClangDecl());
1365-
os << clangTypeDecl->getName();
1366-
1367-
if (isClangPointerType(clangTypeDecl))
1364+
if (auto *clangTypeDecl =
1365+
dyn_cast<clang::TypeDecl>(alias->getClangDecl())) {
1366+
os << clangTypeDecl->getName();
1367+
1368+
if (isClangPointerType(clangTypeDecl))
1369+
printNullability(optionalKind);
1370+
} else if (auto *clangObjCClass
1371+
= dyn_cast<clang::ObjCInterfaceDecl>(alias->getClangDecl())){
1372+
os << clangObjCClass->getName() << " *";
1373+
printNullability(optionalKind);
1374+
} else {
1375+
auto *clangCompatAlias =
1376+
cast<clang::ObjCCompatibleAliasDecl>(alias->getClangDecl());
1377+
os << clangCompatAlias->getName() << " *";
13681378
printNullability(optionalKind);
1379+
}
1380+
13691381
return;
13701382
}
13711383

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This file is meant to be included with modules turned off, compiled against
2+
// the fake clang-importer-sdk.
3+
#import <Foundation.h>
4+
5+
@compatibility_alias StringCheese NSString;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Name: NestedClass
2+
SwiftVersions:
3+
- Version: 3.0
4+
Classes:
5+
- Name: InnerClass
6+
SwiftName: InnerClass
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This file is meant to be included with modules turned off, compiled against
2+
// the fake clang-importer-sdk.
3+
#import <Foundation.h>
4+
5+
@interface Outer : NSObject
6+
@end
7+
8+
__attribute__((swift_name("Outer.Inner")))
9+
@interface InnerClass : NSObject
10+
@end

test/PrintAsObjC/Inputs/custom-modules/module.map

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ module OverrideBase [system] {
3232
module OtherModule {
3333
// Deliberately empty. Used by depends-on-swift-framework.swift.
3434
}
35+
36+
module CompatibilityAlias {
37+
header "CompatibilityAlias.h"
38+
export *
39+
}
40+
41+
module NestedClass {
42+
header "NestedClass.h"
43+
export *
44+
}

test/PrintAsObjC/classes.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// RUN: %FileCheck --check-prefix=NEGATIVE %s < %t/classes.h
2020
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ %t/classes.h
2121
// RUN: not %check-in-clang -I %S/Inputs/custom-modules/ -fno-modules -Qunused-arguments %t/classes.h
22-
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ -fno-modules -Qunused-arguments %t/classes.h -include Foundation.h -include CoreFoundation.h -include objc_generics.h -include SingleGenericClass.h
22+
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ -fno-modules -Qunused-arguments %t/classes.h -include Foundation.h -include CoreFoundation.h -include objc_generics.h -include SingleGenericClass.h -include CompatibilityAlias.h
2323

2424
// CHECK-NOT: AppKit;
2525
// CHECK-NOT: Properties;
@@ -28,13 +28,15 @@
2828
// CHECK-NEXT: @import CoreGraphics;
2929
// CHECK-NEXT: @import CoreFoundation;
3030
// CHECK-NEXT: @import objc_generics;
31+
// CHECK-NEXT: @import CompatibilityAlias;
3132
// CHECK-NEXT: @import SingleGenericClass;
3233
// CHECK-NOT: AppKit;
3334
// CHECK-NOT: Swift;
3435
import Foundation
3536
import objc_generics
3637
import AppKit // only used in implementations
3738
import CoreFoundation
39+
import CompatibilityAlias
3840
import SingleGenericClass
3941

4042
// CHECK-LABEL: @interface A1{{$}}
@@ -724,6 +726,15 @@ public class NonObjCClass { }
724726

725727
@objc class Spoon: Fungible {}
726728

729+
// CHECK-LABEL: @interface UsesCompatibilityAlias
730+
@objc class UsesCompatibilityAlias : NSObject {
731+
// CHECK-NEXT: - (StringCheese * _Nullable)foo SWIFT_WARN_UNUSED_RESULT;
732+
@objc func foo() -> StringCheese? { return nil }
733+
734+
// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
735+
}
736+
// CHECK-NEXT: @end
737+
727738
// CHECK-LABEL: @interface UsesImportedGenerics
728739
@objc class UsesImportedGenerics {
729740
// CHECK: - (GenericClass<id> * _Nonnull)takeAndReturnGenericClass:(GenericClass<NSString *> * _Nullable)x SWIFT_WARN_UNUSED_RESULT;
@@ -739,4 +750,3 @@ public class NonObjCClass { }
739750
@objc func referenceSingleGenericClass(_: SingleImportedObjCGeneric<AnyObject>?) {}
740751
}
741752
// CHECK: @end
742-

test/PrintAsObjC/versioned.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// REQUIRES: objc_interop
2+
3+
// RUN: rm -rf %t
4+
// RUN: mkdir -p %t
5+
6+
// FIXME: BEGIN -enable-source-import hackaround
7+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/ObjectiveC.swift
8+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/CoreGraphics.swift
9+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/Foundation.swift
10+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/AppKit.swift
11+
// FIXME: END -enable-source-import hackaround
12+
13+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -I %S/Inputs/custom-modules -o %t %s -disable-objc-attr-requires-foundation-module -swift-version 3
14+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -parse-as-library %t/versioned.swiftmodule -typecheck -I %S/Inputs/custom-modules -emit-objc-header-path %t/versioned.h -import-objc-header %S/../Inputs/empty.h -disable-objc-attr-requires-foundation-module -swift-version 3
15+
// RUN: %FileCheck %s < %t/versioned.h
16+
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ %t/versioned.h
17+
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ -fno-modules -Qunused-arguments %t/versioned.h -include Foundation.h -include NestedClass.h
18+
19+
import NestedClass
20+
21+
// CHECK-LABEL: @interface UsesNestedClass
22+
@objc class UsesNestedClass : NSObject {
23+
// CHECK-NEXT: - (InnerClass * _Nullable)foo SWIFT_WARN_UNUSED_RESULT;
24+
@objc func foo() -> InnerClass? { return nil }
25+
26+
// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
27+
}
28+
// CHECK-NEXT: @end

0 commit comments

Comments
 (0)