Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -verify -fsyntax-only -triple=i686-linux-gnu |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2 | |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 3 | // Basic parsing/Sema tests for __c11_atomic_* |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 4 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 5 | typedef enum memory_order { |
| 6 | memory_order_relaxed, memory_order_consume, memory_order_acquire, |
| 7 | memory_order_release, memory_order_acq_rel, memory_order_seq_cst |
| 8 | } memory_order; |
| 9 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 10 | struct S { char c[3]; }; |
| 11 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 12 | _Static_assert(__GCC_ATOMIC_BOOL_LOCK_FREE == 2, ""); |
| 13 | _Static_assert(__GCC_ATOMIC_CHAR_LOCK_FREE == 2, ""); |
| 14 | _Static_assert(__GCC_ATOMIC_CHAR16_T_LOCK_FREE == 2, ""); |
| 15 | _Static_assert(__GCC_ATOMIC_CHAR32_T_LOCK_FREE == 2, ""); |
| 16 | _Static_assert(__GCC_ATOMIC_WCHAR_T_LOCK_FREE == 2, ""); |
| 17 | _Static_assert(__GCC_ATOMIC_SHORT_LOCK_FREE == 2, ""); |
| 18 | _Static_assert(__GCC_ATOMIC_INT_LOCK_FREE == 2, ""); |
| 19 | _Static_assert(__GCC_ATOMIC_LONG_LOCK_FREE == 2, ""); |
| 20 | _Static_assert(__GCC_ATOMIC_LLONG_LOCK_FREE == 1, ""); |
| 21 | _Static_assert(__GCC_ATOMIC_POINTER_LOCK_FREE == 2, ""); |
| 22 | |
| 23 | _Static_assert(__c11_atomic_is_lock_free(1), ""); |
| 24 | _Static_assert(__c11_atomic_is_lock_free(2), ""); |
| 25 | _Static_assert(__c11_atomic_is_lock_free(3), ""); // expected-error {{not an integral constant expression}} |
| 26 | _Static_assert(__c11_atomic_is_lock_free(4), ""); |
| 27 | _Static_assert(__c11_atomic_is_lock_free(8), ""); |
| 28 | _Static_assert(__c11_atomic_is_lock_free(16), ""); // expected-error {{not an integral constant expression}} |
| 29 | _Static_assert(__c11_atomic_is_lock_free(17), ""); // expected-error {{not an integral constant expression}} |
| 30 | |
| 31 | _Static_assert(__atomic_is_lock_free(1, 0), ""); |
| 32 | _Static_assert(__atomic_is_lock_free(2, 0), ""); |
| 33 | _Static_assert(__atomic_is_lock_free(3, 0), ""); // expected-error {{not an integral constant expression}} |
| 34 | _Static_assert(__atomic_is_lock_free(4, 0), ""); |
| 35 | _Static_assert(__atomic_is_lock_free(8, 0), ""); |
| 36 | _Static_assert(__atomic_is_lock_free(16, 0), ""); // expected-error {{not an integral constant expression}} |
| 37 | _Static_assert(__atomic_is_lock_free(17, 0), ""); // expected-error {{not an integral constant expression}} |
| 38 | |
| 39 | char i8; |
| 40 | short i16; |
| 41 | int i32; |
| 42 | int __attribute__((vector_size(8))) i64; |
| 43 | struct Incomplete *incomplete; |
| 44 | |
| 45 | _Static_assert(__atomic_is_lock_free(1, &i8), ""); |
| 46 | _Static_assert(__atomic_is_lock_free(1, &i64), ""); |
| 47 | _Static_assert(__atomic_is_lock_free(2, &i8), ""); // expected-error {{not an integral constant expression}} |
| 48 | _Static_assert(__atomic_is_lock_free(2, &i16), ""); |
| 49 | _Static_assert(__atomic_is_lock_free(2, &i64), ""); |
| 50 | _Static_assert(__atomic_is_lock_free(4, &i16), ""); // expected-error {{not an integral constant expression}} |
| 51 | _Static_assert(__atomic_is_lock_free(4, &i32), ""); |
| 52 | _Static_assert(__atomic_is_lock_free(4, &i64), ""); |
| 53 | _Static_assert(__atomic_is_lock_free(8, &i32), ""); // expected-error {{not an integral constant expression}} |
| 54 | _Static_assert(__atomic_is_lock_free(8, &i64), ""); |
| 55 | |
| 56 | _Static_assert(__atomic_always_lock_free(1, 0), ""); |
| 57 | _Static_assert(__atomic_always_lock_free(2, 0), ""); |
| 58 | _Static_assert(!__atomic_always_lock_free(3, 0), ""); |
| 59 | _Static_assert(__atomic_always_lock_free(4, 0), ""); |
| 60 | _Static_assert(__atomic_always_lock_free(8, 0), ""); |
| 61 | _Static_assert(!__atomic_always_lock_free(16, 0), ""); |
| 62 | _Static_assert(!__atomic_always_lock_free(17, 0), ""); |
| 63 | |
| 64 | _Static_assert(__atomic_always_lock_free(1, incomplete), ""); |
| 65 | _Static_assert(!__atomic_always_lock_free(2, incomplete), ""); |
| 66 | _Static_assert(!__atomic_always_lock_free(4, incomplete), ""); |
| 67 | |
| 68 | _Static_assert(__atomic_always_lock_free(1, &i8), ""); |
| 69 | _Static_assert(__atomic_always_lock_free(1, &i64), ""); |
| 70 | _Static_assert(!__atomic_always_lock_free(2, &i8), ""); |
| 71 | _Static_assert(__atomic_always_lock_free(2, &i16), ""); |
| 72 | _Static_assert(__atomic_always_lock_free(2, &i64), ""); |
| 73 | _Static_assert(!__atomic_always_lock_free(4, &i16), ""); |
| 74 | _Static_assert(__atomic_always_lock_free(4, &i32), ""); |
| 75 | _Static_assert(__atomic_always_lock_free(4, &i64), ""); |
| 76 | _Static_assert(!__atomic_always_lock_free(8, &i32), ""); |
| 77 | _Static_assert(__atomic_always_lock_free(8, &i64), ""); |
| 78 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 79 | void f(_Atomic(int) *i, _Atomic(int*) *p, _Atomic(float) *d, |
| 80 | int *I, int **P, float *D, struct S *s1, struct S *s2) { |
| 81 | __c11_atomic_init(I, 5); // expected-error {{pointer to _Atomic}} |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 82 | __c11_atomic_load(0); // expected-error {{too few arguments to function}} |
| 83 | __c11_atomic_load(0,0,0); // expected-error {{too many arguments to function}} |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 84 | __c11_atomic_store(0,0,0); // expected-error {{first argument to atomic builtin must be a pointer}} |
| 85 | __c11_atomic_store((int*)0,0,0); // expected-error {{first argument to atomic operation must be a pointer to _Atomic}} |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 86 | |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 87 | __c11_atomic_load(i, memory_order_seq_cst); |
| 88 | __c11_atomic_load(p, memory_order_seq_cst); |
| 89 | __c11_atomic_load(d, memory_order_seq_cst); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 90 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 91 | int load_n_1 = __atomic_load_n(I, memory_order_relaxed); |
| 92 | int *load_n_2 = __atomic_load_n(P, memory_order_relaxed); |
| 93 | float load_n_3 = __atomic_load_n(D, memory_order_relaxed); // expected-error {{must be a pointer to integer or pointer}} |
| 94 | __atomic_load_n(s1, memory_order_relaxed); // expected-error {{must be a pointer to integer or pointer}} |
| 95 | |
| 96 | __atomic_load(i, I, memory_order_relaxed); // expected-error {{must be a pointer to a trivially-copyable type}} |
| 97 | __atomic_load(I, i, memory_order_relaxed); // expected-warning {{passing '_Atomic(int) *' to parameter of type 'int *'}} |
| 98 | __atomic_load(I, *P, memory_order_relaxed); |
| 99 | __atomic_load(I, *P, memory_order_relaxed, 42); // expected-error {{too many arguments}} |
| 100 | (int)__atomic_load(I, I, memory_order_seq_cst); // expected-error {{operand of type 'void'}} |
| 101 | __atomic_load(s1, s2, memory_order_acquire); |
| 102 | |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 103 | __c11_atomic_store(i, 1, memory_order_seq_cst); |
| 104 | __c11_atomic_store(p, 1, memory_order_seq_cst); // expected-warning {{incompatible integer to pointer conversion}} |
| 105 | (int)__c11_atomic_store(d, 1, memory_order_seq_cst); // expected-error {{operand of type 'void'}} |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 106 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 107 | __atomic_store_n(I, 4, memory_order_release); |
| 108 | __atomic_store_n(I, 4.0, memory_order_release); |
| 109 | __atomic_store_n(I, P, memory_order_release); // expected-warning {{parameter of type 'int'}} |
| 110 | __atomic_store_n(i, 1, memory_order_release); // expected-error {{must be a pointer to integer or pointer}} |
| 111 | __atomic_store_n(s1, *s2, memory_order_release); // expected-error {{must be a pointer to integer or pointer}} |
| 112 | |
| 113 | __atomic_store(I, *P, memory_order_release); |
| 114 | __atomic_store(s1, s2, memory_order_release); |
| 115 | __atomic_store(i, I, memory_order_release); // expected-error {{trivially-copyable}} |
| 116 | |
| 117 | int exchange_1 = __c11_atomic_exchange(i, 1, memory_order_seq_cst); |
| 118 | int exchange_2 = __c11_atomic_exchange(I, 1, memory_order_seq_cst); // expected-error {{must be a pointer to _Atomic}} |
| 119 | int exchange_3 = __atomic_exchange_n(i, 1, memory_order_seq_cst); // expected-error {{must be a pointer to integer or pointer}} |
| 120 | int exchange_4 = __atomic_exchange_n(I, 1, memory_order_seq_cst); |
| 121 | |
| 122 | __atomic_exchange(s1, s2, s2, memory_order_seq_cst); |
| 123 | __atomic_exchange(s1, I, P, memory_order_seq_cst); // expected-warning 2{{parameter of type 'struct S *'}} |
| 124 | (int)__atomic_exchange(s1, s2, s2, memory_order_seq_cst); // expected-error {{operand of type 'void'}} |
| 125 | |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 126 | __c11_atomic_fetch_add(i, 1, memory_order_seq_cst); |
| 127 | __c11_atomic_fetch_add(p, 1, memory_order_seq_cst); |
| 128 | __c11_atomic_fetch_add(d, 1, memory_order_seq_cst); // expected-error {{must be a pointer to atomic integer or pointer}} |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 129 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 130 | __atomic_fetch_add(i, 3, memory_order_seq_cst); // expected-error {{pointer to integer or pointer}} |
| 131 | __atomic_fetch_sub(I, 3, memory_order_seq_cst); |
| 132 | __atomic_fetch_sub(P, 3, memory_order_seq_cst); |
| 133 | __atomic_fetch_sub(D, 3, memory_order_seq_cst); // expected-error {{must be a pointer to integer or pointer}} |
| 134 | __atomic_fetch_sub(s1, 3, memory_order_seq_cst); // expected-error {{must be a pointer to integer or pointer}} |
| 135 | |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 136 | __c11_atomic_fetch_and(i, 1, memory_order_seq_cst); |
| 137 | __c11_atomic_fetch_and(p, 1, memory_order_seq_cst); // expected-error {{must be a pointer to atomic integer}} |
| 138 | __c11_atomic_fetch_and(d, 1, memory_order_seq_cst); // expected-error {{must be a pointer to atomic integer}} |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 139 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 140 | __atomic_fetch_and(i, 3, memory_order_seq_cst); // expected-error {{pointer to integer}} |
| 141 | __atomic_fetch_or(I, 3, memory_order_seq_cst); |
| 142 | __atomic_fetch_xor(P, 3, memory_order_seq_cst); // expected-error {{must be a pointer to integer}} |
| 143 | __atomic_fetch_or(D, 3, memory_order_seq_cst); // expected-error {{must be a pointer to integer}} |
| 144 | __atomic_fetch_and(s1, 3, memory_order_seq_cst); // expected-error {{must be a pointer to integer}} |
| 145 | |
| 146 | _Bool cmpexch_1 = __c11_atomic_compare_exchange_strong(i, 0, 1, memory_order_seq_cst, memory_order_seq_cst); |
| 147 | _Bool cmpexch_2 = __c11_atomic_compare_exchange_strong(p, 0, (int*)1, memory_order_seq_cst, memory_order_seq_cst); |
| 148 | _Bool cmpexch_3 = __c11_atomic_compare_exchange_strong(d, (int*)0, 1, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{incompatible pointer types}} |
| 149 | |
| 150 | _Bool cmpexch_4 = __atomic_compare_exchange_n(I, I, 5, 1, memory_order_seq_cst, memory_order_seq_cst); |
| 151 | _Bool cmpexch_5 = __atomic_compare_exchange_n(I, P, 5, 0, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{; dereference with *}} |
| 152 | _Bool cmpexch_6 = __atomic_compare_exchange_n(I, I, P, 0, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{passing 'int **' to parameter of type 'int'}} |
| 153 | |
| 154 | _Bool cmpexch_7 = __atomic_compare_exchange(I, I, 5, 1, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{passing 'int' to parameter of type 'int *'}} |
| 155 | _Bool cmpexch_8 = __atomic_compare_exchange(I, P, I, 0, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{; dereference with *}} |
| 156 | _Bool cmpexch_9 = __atomic_compare_exchange(I, I, I, 0, memory_order_seq_cst, memory_order_seq_cst); |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 157 | |
| 158 | const volatile int flag_k = 0; |
| 159 | volatile int flag = 0; |
| 160 | (void)(int)__atomic_test_and_set(&flag_k, memory_order_seq_cst); // expected-warning {{passing 'const volatile int *' to parameter of type 'volatile void *'}} |
| 161 | (void)(int)__atomic_test_and_set(&flag, memory_order_seq_cst); |
| 162 | __atomic_clear(&flag_k, memory_order_seq_cst); // expected-warning {{passing 'const volatile int *' to parameter of type 'volatile void *'}} |
| 163 | __atomic_clear(&flag, memory_order_seq_cst); |
| 164 | (int)__atomic_clear(&flag, memory_order_seq_cst); // expected-error {{operand of type 'void'}} |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 165 | } |