Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++1z %s -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s |
| 2 | |
| 3 | struct Q { |
| 4 | // CHECK: @_ZN1Q1kE = linkonce_odr constant i32 5, comdat |
| 5 | static constexpr int k = 5; |
| 6 | }; |
| 7 | const int &r = Q::k; |
| 8 | |
| 9 | int f(); |
| 10 | |
| 11 | // const does not imply internal linkage. |
| 12 | // CHECK: @external_inline = linkonce_odr constant i32 5, comdat |
| 13 | inline const int external_inline = 5; |
| 14 | const int &use1 = external_inline; |
| 15 | |
| 16 | // static still does, though. |
| 17 | // CHECK: @_ZL15internal_inline = internal constant i32 5 |
| 18 | static inline const int internal_inline = 5; |
| 19 | const int &use2 = internal_inline; |
| 20 | |
| 21 | int a = f(); |
| 22 | // CHECK: @b = linkonce_odr global i32 0, comdat |
| 23 | // CHECK: @_ZGV1b = linkonce_odr global i64 0, comdat($b) |
| 24 | inline int b = f(); |
| 25 | int c = f(); |
| 26 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 27 | // For compatibility with C++11 and C++14, an out-of-line declaration of a |
| 28 | // static constexpr local variable promotes the variable to weak_odr. |
| 29 | struct compat { |
| 30 | static constexpr int a = 1; |
| 31 | static constexpr int b = 2; |
| 32 | static constexpr int c = 3; |
| 33 | static inline constexpr int d = 4; |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 34 | static const int e = 5; |
| 35 | static const int f = 6; |
| 36 | static const int g = 7; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 37 | }; |
| 38 | const int &compat_use_before_redecl = compat::b; |
| 39 | const int compat::a; |
| 40 | const int compat::b; |
| 41 | const int compat::c; |
| 42 | const int compat::d; |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 43 | const int compat::e; |
| 44 | constexpr int compat::f; |
| 45 | constexpr inline int compat::g; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 46 | const int &compat_use_after_redecl1 = compat::c; |
| 47 | const int &compat_use_after_redecl2 = compat::d; |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 48 | const int &compat_use_after_redecl3 = compat::g; |
| 49 | // CHECK-DAG: @_ZN6compat1bE = weak_odr constant i32 2 |
| 50 | // CHECK-DAG: @_ZN6compat1aE = weak_odr constant i32 1 |
| 51 | // CHECK-DAG: @_ZN6compat1cE = weak_odr constant i32 3 |
| 52 | // CHECK-DAG: @_ZN6compat1dE = linkonce_odr constant i32 4 |
| 53 | // CHECK-DAG: @_ZN6compat1eE = constant i32 5 |
| 54 | // CHECK-DAG: @_ZN6compat1fE = weak_odr constant i32 6 |
| 55 | // CHECK-DAG: @_ZN6compat1gE = linkonce_odr constant i32 7 |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 56 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 57 | template<typename T> struct X { |
| 58 | static int a; |
| 59 | static inline int b; |
| 60 | static int c; |
Richard Smith | 93ee9ca | 2018-01-10 23:08:26 +0000 | [diff] [blame] | 61 | static const int d; |
| 62 | static int e; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 63 | }; |
| 64 | // CHECK: @_ZN1XIiE1aE = linkonce_odr global i32 10 |
| 65 | // CHECK: @_ZN1XIiE1bE = global i32 20 |
| 66 | // CHECK-NOT: @_ZN1XIiE1cE |
Richard Smith | 93ee9ca | 2018-01-10 23:08:26 +0000 | [diff] [blame] | 67 | // CHECK: @_ZN1XIiE1dE = linkonce_odr constant i32 40 |
| 68 | // CHECK: @_ZN1XIiE1eE = linkonce_odr global i32 50 |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 69 | template<> inline int X<int>::a = 10; |
| 70 | int &use3 = X<int>::a; |
| 71 | template<> int X<int>::b = 20; |
| 72 | template<> inline int X<int>::c = 30; |
Richard Smith | 93ee9ca | 2018-01-10 23:08:26 +0000 | [diff] [blame] | 73 | template<typename T> constexpr int X<T>::d = 40; |
| 74 | template<typename T> inline int X<T>::e = 50; |
| 75 | const int *use_x_int_d = &X<int>::d; |
| 76 | const int *use_x_int_e = &X<int>::e; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 77 | |
Richard Smith | e124612 | 2017-11-03 01:26:01 +0000 | [diff] [blame] | 78 | template<typename T> struct Y; |
| 79 | template<> struct Y<int> { |
| 80 | static constexpr int a = 123; |
| 81 | static constexpr int b = 456; |
| 82 | static constexpr int c = 789; |
| 83 | }; |
| 84 | // CHECK: @_ZN1YIiE1aE = weak_odr constant i32 123 |
| 85 | constexpr int Y<int>::a; |
| 86 | // CHECK: @_ZN1YIiE1bE = linkonce_odr constant i32 456 |
| 87 | const int &yib = Y<int>::b; |
| 88 | // CHECK-NOT: @_ZN1YIiE1cE |
| 89 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 90 | // CHECK-LABEL: define {{.*}}global_var_init |
| 91 | // CHECK: call i32 @_Z1fv |
| 92 | |
| 93 | // CHECK-LABEL: define {{.*}}global_var_init |
| 94 | // CHECK-NOT: comdat |
| 95 | // CHECK-SAME: {{$}} |
| 96 | // CHECK: load atomic {{.*}} acquire |
| 97 | // CHECK: br |
| 98 | // CHECK: __cxa_guard_acquire(i64* @_ZGV1b) |
| 99 | // CHECK: br |
| 100 | // CHECK: call i32 @_Z1fv |
| 101 | // CHECK: __cxa_guard_release(i64* @_ZGV1b) |
| 102 | |
| 103 | // CHECK-LABEL: define {{.*}}global_var_init |
| 104 | // CHECK: call i32 @_Z1fv |
| 105 | |
| 106 | template<typename T> inline int d = f(); |
| 107 | int e = d<int>; |
| 108 | |
| 109 | // CHECK-LABEL: define {{.*}}global_var_init{{.*}}comdat |
| 110 | // CHECK: _ZGV1dIiE |
| 111 | // CHECK-NOT: __cxa_guard_acquire(i64* @_ZGV1b) |
| 112 | // CHECK: call i32 @_Z1fv |
| 113 | // CHECK-NOT: __cxa_guard_release(i64* @_ZGV1b) |