-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[ClangImporter] Import constant values for 'const' globals #80749
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
kubamracek
merged 7 commits into
swiftlang:main
from
kubamracek:clangimport-const-values
Apr 18, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a39f60b
[ClangImporter] Import constant values for 'const' globals
kubamracek ec24b74
[ClangImporter] Add more test coverage in ClangImporter/const_values.…
kubamracek 936e911
[ClangImporter] Fix importing values for constant globals of enum types
kubamracek dc65773
[ClangImporter] Add more const values importing tests cases
kubamracek 8e6071e
[ClangImporter] Skip value importing on partial/dependent C++ decls, …
kubamracek 26cc36b
[ClangImporter] Handle swift_wrapper annotated typedefs, skip CGFloat…
kubamracek 0319aa4
[ClangImporter] Relax expectations in ClangImporter/const_values.swif…
kubamracek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// RUN: %empty-directory(%t/src) | ||
// RUN: split-file %s %t/src | ||
|
||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %t/src/main.swift \ | ||
// RUN: -import-bridging-header %t/src/test.h \ | ||
// RUN: -module-name main -I %t -emit-sil -serialize-diagnostics -serialize-diagnostics-path %t/test.diag | %FileCheck %s | ||
|
||
// RUN: c-index-test -read-diagnostics %t/test.diag 2>&1 | %FileCheck --check-prefix CHECK-DIAG %t/src/test.h | ||
|
||
//--- test.h | ||
#include <stdbool.h> | ||
|
||
#define MACRO_INT 42 | ||
|
||
const int const_int = 42; | ||
static int static_int = 42; | ||
static const int static_const_int = 42; | ||
|
||
static const bool static_const_bool = true; | ||
static const char static_const_char = 42; | ||
static const char static_const_char_with_long_literal = 42ull; | ||
static const char static_const_char_with_overflow_literal = 777ull; // CHECK-DIAG: [[@LINE]]:{{.*}}: warning: implicit conversion from 'unsigned long long' to 'char' changes value from 777 to 9 | ||
static const long static_const_long = 42; | ||
static const float static_const_float = 42.0; | ||
static const double static_const_double = 42.0; | ||
|
||
static const char static_const_char_array[4] = {1, 2, 3, 4}; | ||
static const char *static_const_pointer = 0; | ||
|
||
static const char static_const_char_referencing_other_const = 1 + static_const_char; | ||
|
||
typedef enum MyEnum: char { | ||
MyEnumCase0 = 0, MyEnumCase1, MyEnumCase2 | ||
} MyEnum; | ||
static const MyEnum static_const_enum = MyEnumCase1; | ||
|
||
struct MyStruct { | ||
int field; | ||
}; | ||
__attribute__((swift_name("MyStruct.static_const_int_as_a_member"))) | ||
static const int static_const_int_as_a_member = 42; | ||
|
||
typedef int ImportAsStruct __attribute__((swift_wrapper(struct))); | ||
static const ImportAsStruct ImportAsStructFoo = 123; | ||
typedef int ImportAsEnum __attribute__((swift_wrapper(enum))); | ||
static const ImportAsEnum ImportAsEnumFoo = 123; | ||
|
||
//--- main.swift | ||
func foo() { | ||
print(MACRO_INT) | ||
|
||
print(const_int) | ||
print(static_int) | ||
print(static_const_int) | ||
|
||
print(static_const_bool) | ||
print(static_const_char) | ||
print(static_const_char_with_long_literal) | ||
print(static_const_char_with_overflow_literal) | ||
print(static_const_long) | ||
print(static_const_float) | ||
print(static_const_double) | ||
|
||
print(static_const_char_array) | ||
print(static_const_pointer) | ||
|
||
print(static_const_char_referencing_other_const) | ||
|
||
print(static_const_enum) | ||
|
||
print(MyStruct.static_const_int_as_a_member) | ||
|
||
print(ImportAsStruct.foo) | ||
print(ImportAsEnum.foo) | ||
} | ||
|
||
// Globals that don't get their value imported stay as public_external: | ||
// CHECK: sil_global public_external @static_int : $Int32 | ||
// CHECK: sil_global public_external [let] @static_const_char_array : $(Int8, Int8, Int8, Int8) | ||
// CHECK: sil_global public_external @static_const_pointer : $Optional<UnsafePointer<Int8>> | ||
|
||
// CHECK: sil shared [transparent] @$sSC9MACRO_INTs5Int32Vvg : $@convention(thin) () -> Int32 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %1 = struct $Int32 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo9const_ints5Int32Vvg : $@convention(thin) () -> Int32 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %1 = struct $Int32 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo16static_const_ints5Int32Vvg : $@convention(thin) () -> Int32 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %1 = struct $Int32 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo17static_const_boolSbvg : $@convention(thin) () -> Bool { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1 | ||
// CHECK-NEXT: %1 = struct $Bool (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo17static_const_chars4Int8Vvg : $@convention(thin) () -> Int8 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 42 | ||
// CHECK-NEXT: %1 = struct $Int8 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo35static_const_char_with_long_literals4Int8Vvg : $@convention(thin) () -> Int8 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 42 | ||
// CHECK-NEXT: %1 = struct $Int8 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo39static_const_char_with_overflow_literals4Int8Vvg : $@convention(thin) () -> Int8 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 9 | ||
// CHECK-NEXT: %1 = struct $Int8 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo17static_const_long{{.*}} : $@convention(thin) () -> {{.*}} { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal {{.*}}, 42 | ||
// CHECK-NEXT: %1 = struct {{.*}} (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo18static_const_floatSfvg : $@convention(thin) () -> Float { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = float_literal $Builtin.FPIEEE32, 0x42280000 // 42 | ||
// CHECK-NEXT: %1 = struct $Float (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo19static_const_doubleSdvg : $@convention(thin) () -> Double { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = float_literal $Builtin.FPIEEE64, 0x4045000000000000 // 42 | ||
// CHECK-NEXT: %1 = struct $Double (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo036static_const_char_referencing_other_B0s4Int8Vvg : $@convention(thin) () -> Int8 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 43 | ||
// CHECK-NEXT: %1 = struct $Int8 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo17static_const_enumSo6MyEnumVvg : $@convention(thin) () -> MyEnum { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 1 | ||
// CHECK-NEXT: %1 = struct $Int8 (%0) | ||
// CHECK-NEXT: %2 = struct $MyEnum (%1) | ||
// CHECK-NEXT: return %2 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo8MyStructV28static_const_int_as_a_members5Int32VvgZ : $@convention(method) (@thin MyStruct.Type) -> Int32 { | ||
// CHECK-NEXT: // %0 "self" | ||
// CHECK-NEXT: bb0(%0 : $@thin MyStruct.Type): | ||
// CHECK-NEXT: debug_value %0, let, name "self", argno 1 | ||
// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %3 = struct $Int32 (%2) | ||
// CHECK-NEXT: return %3 | ||
// CHECK-NEXT: } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// RUN: %empty-directory(%t/src) | ||
// RUN: split-file %s %t/src | ||
|
||
// RUN: %target-swiftxx-frontend %t/src/main.swift \ | ||
// RUN: -import-bridging-header %t/src/test.h \ | ||
// RUN: -module-name main -I %t -emit-sil | %FileCheck %s | ||
|
||
//--- test.h | ||
|
||
const int const_int = 42; | ||
namespace Namespace { | ||
const int namespaced_const_int = 42; | ||
} | ||
|
||
static inline int foo() { return 42; } | ||
const int not_really_constant = foo(); | ||
|
||
constexpr int constexpr_func() { return 42; } | ||
constexpr int constexpr_int = constexpr_func(); | ||
static constexpr int static_constexpr_int = constexpr_func(); | ||
|
||
class MyClass { | ||
public: | ||
const int class_const_int = 42; // member field, don't expect to import as a global, no CHECKs for this | ||
static const int class_static_const_int = 42; | ||
}; | ||
|
||
namespace OtherNamespace { | ||
constexpr int namespaced_constexpr_int = constexpr_func(); | ||
static constexpr int namespaced_static_constexpr_int = constexpr_func(); | ||
} | ||
class OtherClass { | ||
static constexpr int class_static_constexpr_int = 42; | ||
}; | ||
|
||
template <int N, int M> | ||
inline const int template_gcd = template_gcd<M, N % M>; | ||
|
||
template <int N> | ||
inline const int template_gcd<N, 0> = N; | ||
|
||
|
||
//--- main.swift | ||
func foo() { | ||
print(const_int) | ||
print(Namespace.namespaced_const_int) | ||
print(not_really_constant) | ||
print(constexpr_int) | ||
print(static_constexpr_int) | ||
|
||
// TODO: Non-top-level constexpr variables currently are not imported at all (would be imported as static members) | ||
//print(OtherNamespace.namespaced_constexpr_int) | ||
//print(OtherNamespace.namespaced_static_constexpr_int) | ||
//print(MyClass.class_static_constexpr_int) | ||
|
||
print(MyClass().class_const_int) | ||
print(MyClass.class_static_const_int) | ||
|
||
// TODO: This seems to be incorrectly imported, this test here is just to check that the compiler doesn't crash. | ||
print(template_gcd) | ||
} | ||
|
||
// Only imported as external declarations: | ||
// CHECK: sil_global public_external [let] @not_really_constant : $Int32 | ||
|
||
// CHECK: sil shared [transparent] @$sSo9const_ints5Int32Vvg : $@convention(thin) () -> Int32 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %1 = struct $Int32 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo9NamespaceO20namespaced_const_ints5Int32VvgZ : $@convention(method) (@thin Namespace.Type) -> Int32 { | ||
// CHECK-NEXT: // %0 "self" | ||
// CHECK-NEXT: bb0(%0 : $@thin Namespace.Type): | ||
// CHECK-NEXT: debug_value %0, let, name "self", argno 1 | ||
// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %3 = struct $Int32 (%2) | ||
// CHECK-NEXT: return %3 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo13constexpr_ints5Int32Vvg : $@convention(thin) () -> Int32 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %1 = struct $Int32 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo20static_constexpr_ints5Int32Vvg : $@convention(thin) () -> Int32 { | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %1 = struct $Int32 (%0) | ||
// CHECK-NEXT: return %1 | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: sil shared [transparent] @$sSo7MyClassV22class_static_const_ints5Int32VvgZ : $@convention(method) (@thin MyClass.Type) -> Int32 { | ||
// CHECK-NEXT: // %0 "self" | ||
// CHECK-NEXT: bb0(%0 : $@thin MyClass.Type): | ||
// CHECK-NEXT: debug_value %0, let, name "self", argno 1 | ||
// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, 42 | ||
// CHECK-NEXT: %3 = struct $Int32 (%2) | ||
// CHECK-NEXT: return %3 | ||
// CHECK-NEXT: } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// RUN: %empty-directory(%t/src) | ||
// RUN: split-file %s %t/src | ||
|
||
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) %t/src/main.swift \ | ||
// RUN: -import-bridging-header %t/src/test.h \ | ||
// RUN: -module-name main -I %t -emit-sil -serialize-diagnostics -serialize-diagnostics-path %t/test.diag | ||
|
||
// RUN: c-index-test -read-diagnostics %t/test.diag 2>&1 | %FileCheck --check-prefix CHECK-DIAG %t/src/test.h | ||
|
||
//--- test.h | ||
static char other = 42; | ||
static const char static_const_char_that_is_not_const = 1 + other; | ||
// CHECK-DIAG: [[@LINE-1]]:{{.*}}: error: initializer element is not a compile-time constant | ||
// CHECK-DIAG: error: failed to import bridging header | ||
|
||
//--- main.swift | ||
func foo() { | ||
print(static_const_char_that_is_not_const) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.