Skip to content

Commit 407ef9a

Browse files
authored
Merge pull request #83238 from kubamracek/const-cgfloat-typedef
[ClangImporter] Skip importing values for CGFloat typedefs on top of CGFloats direct
2 parents a8e7d7b + 6ec280d commit 407ef9a

File tree

7 files changed

+69
-12
lines changed

7 files changed

+69
-12
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,10 @@ namespace swift {
11231123
/// invocations directly from clang cc1 args.
11241124
bool ClangImporterDirectCC1Scan = false;
11251125

1126+
/// Whether we should import values (initializer expressions) of constant
1127+
/// globals.
1128+
bool EnableConstValueImporting = true;
1129+
11261130
/// Return a hash code of any components from these options that should
11271131
/// contribute to a Swift Bridging PCH hash.
11281132
llvm::hash_code getPCHHashComponents() const {

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ def disable_clangimporter_source_import : Flag<["-"],
534534
"disable-clangimporter-source-import">,
535535
HelpText<"Disable ClangImporter and forward all requests straight the DWARF importer.">;
536536

537+
def disable_const_value_importing : Flag<["-"],
538+
"disable-const-value-importing">,
539+
HelpText<"Disable constant value importing in ClangImporter.">;
540+
537541
def disable_implicit_concurrency_module_import : Flag<["-"],
538542
"disable-implicit-concurrency-module-import">,
539543
HelpText<"Disable the implicit import of the _Concurrency module.">;

lib/ClangImporter/ImportDecl.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4613,16 +4613,18 @@ namespace {
46134613
ValueDecl *result = nullptr;
46144614

46154615
bool initIsEvaluatable = false;
4616-
if (auto init = decl->getInit()) {
4617-
// Don't import values for partial specializations. TODO: Should we stop
4618-
// importing partially specialized variables completely?
4619-
bool partial = isa<clang::VarTemplatePartialSpecializationDecl>(decl);
4616+
if (Impl.SwiftContext.ClangImporterOpts.EnableConstValueImporting) {
4617+
if (auto init = decl->getInit()) {
4618+
// Don't import values for partial specializations. TODO: Should we
4619+
// stop importing partially specialized variables completely?
4620+
bool partial = isa<clang::VarTemplatePartialSpecializationDecl>(decl);
46204621

4621-
// Don't import values when type-dependent or value-dependent.
4622-
bool typeDependent = decl->getType()->isDependentType();
4623-
bool valueDependent = init->isValueDependent();
4622+
// Don't import values when type-dependent or value-dependent.
4623+
bool typeDependent = decl->getType()->isDependentType();
4624+
bool valueDependent = init->isValueDependent();
46244625

4625-
initIsEvaluatable = !partial && !typeDependent && !valueDependent;
4626+
initIsEvaluatable = !partial && !typeDependent && !valueDependent;
4627+
}
46264628
}
46274629

46284630
// If the variable is const (we're importing it as a let), and has an
@@ -4639,8 +4641,12 @@ namespace {
46394641
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
46404642
isInSystemModule(dc), Bridgeability::None, ImportTypeAttrs());
46414643

4642-
// FIXME: Handle CGFloat too.
4643-
if (type && !type->isCGFloat()) {
4644+
// Do not attempt to import CGFloat values, for now. Importing
4645+
// CGFloats is special cased in the importer, and needs more handling.
4646+
bool isCGFloat = (type && type->isCGFloat()) ||
4647+
(type && synthesizer.isCGFloat(type));
4648+
4649+
if (type && !isCGFloat) {
46444650
auto convertKind = ConstantConvertKind::None;
46454651
// Request conversions on enums, and swift_wrapper((enum/struct))
46464652
// types

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ Type SwiftDeclSynthesizer::getConstantLiteralType(
197197
}
198198
}
199199

200+
bool SwiftDeclSynthesizer::isCGFloat(Type type) {
201+
auto found = ImporterImpl.RawTypes.find(type->getAnyNominal());
202+
if (found == ImporterImpl.RawTypes.end()) {
203+
return false;
204+
}
205+
206+
Type importTy = found->second;
207+
return importTy->isCGFloat();
208+
}
209+
200210
ValueDecl *SwiftDeclSynthesizer::createConstant(Identifier name,
201211
DeclContext *dc, Type type,
202212
const clang::APValue &value,

lib/ClangImporter/SwiftDeclSynthesizer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ class SwiftDeclSynthesizer {
345345
/// function for the availability domain represented by `var`.
346346
FuncDecl *makeAvailabilityDomainPredicate(const clang::VarDecl *var);
347347

348+
bool isCGFloat(Type type);
349+
348350
private:
349351
Type getConstantLiteralType(Type type, ConstantConvertKind convertKind);
350352
};

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,9 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts, ArgList &Args,
21422142
Opts.DisableSourceImport |=
21432143
Args.hasArg(OPT_disable_clangimporter_source_import);
21442144

2145+
if (Args.hasArg(OPT_disable_const_value_importing))
2146+
Opts.EnableConstValueImporting = false;
2147+
21452148
Opts.ClangImporterDirectCC1Scan |=
21462149
Args.hasArg(OPT_experimental_clang_importer_direct_cc1_scan);
21472150
// Forward the FrontendOptions to clang importer option so it can be
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// RUN: %empty-directory(%t/src)
22
// RUN: split-file %s %t/src
33

4-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %t/src/main.swift \
4+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t/modules) -Xcc -DCGFLOAT_IN_COREFOUNDATION -DCGFLOAT_IN_COREFOUNDATION -emit-module -o %t/modules/CoreFoundation.swiftmodule %clang-importer-sdk-path/swift-modules/CoreFoundation.swift
5+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t/modules) -Xcc -DCGFLOAT_IN_COREFOUNDATION -DCGFLOAT_IN_COREFOUNDATION -emit-module -o %t/modules/CoreGraphics.swiftmodule %clang-importer-sdk-path/swift-modules/CoreGraphics.swift
6+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t/modules) -Xcc -DCGFLOAT_IN_COREFOUNDATION -DCGFLOAT_IN_COREFOUNDATION -emit-module -o %t/modules/Foundation.swiftmodule %clang-importer-sdk-path/swift-modules/Foundation.swift
7+
8+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk -I %t/modules) %t/src/main.swift \
59
// RUN: -import-bridging-header %t/src/test.h \
610
// RUN: -module-name main -I %t -emit-sil | %FileCheck %s
711

@@ -10,22 +14,46 @@
1014
//--- test.h
1115
#include <objc/objc.h>
1216

17+
@import CoreFoundation;
18+
@import CoreGraphics;
19+
@import Foundation;
20+
1321
@interface MyClass : NSObject
1422
@end
1523
1624
__attribute__((swift_name("MyClass.value")))
1725
static const int MyClassValue = -1;
1826

27+
static const CGFloat myFloatConstValue = 42.0;
28+
29+
typedef CGFloat MyFloatType __attribute__((swift_wrapper(struct)));
30+
static const MyFloatType MyFloatTypeValue1 = 10;
31+
static const MyFloatType MyFloatTypeValue2 = 20;
32+
static const MyFloatType MyFloatTypeValue3 = 30;
33+
1934
//--- main.swift
2035
func foo() {
2136
print(MyClass.value)
37+
print(myFloatConstValue)
38+
print(MyFloatType.value1)
39+
print(MyFloatType.value2)
40+
print(MyFloatType.value3)
2241
}
2342

24-
// CHECK: sil shared [transparent] @$sSo7MyClassC5values5Int32VvgZ : $@convention(method) (@thick MyClass.Type) -> Int32 {
43+
// CHECK: // static MyClass.value.getter
44+
// CHECK-NEXT: // Isolation: nonisolated
45+
// CHECK-NEXT: sil shared [transparent] @$sSo7MyClassC5values5Int32VvgZ : $@convention(method) (@thick MyClass.Type) -> Int32 {
2546
// CHECK-NEXT: // %0 "self"
2647
// CHECK-NEXT: bb0(%0 : $@thick MyClass.Type):
2748
// CHECK-NEXT: debug_value %0, let, name "self", argno 1
2849
// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, -1
2950
// CHECK-NEXT: %3 = struct $Int32 (%2)
3051
// CHECK-NEXT: return %3
3152
// CHECK-NEXT: }
53+
54+
// CGFloats are not imported:
55+
56+
// CHECK-NOT: // myFloatConstValue.getter
57+
// CHECK-NOT: // static MyFloatType.value1.getter
58+
// CHECK-NOT: // static MyFloatType.value2.getter
59+
// CHECK-NOT: // static MyFloatType.value3.getter

0 commit comments

Comments
 (0)