xref: /llvm-project/clang/test/AST/ByteCode/atomic.c (revision a07aba5d44204a7ca0d891a3da05af9960081e4c)
1 // RUN: %clang_cc1 -fgnuc-version=4.2.1 -triple=i686-linux-gnu -ffreestanding -fexperimental-new-constant-interpreter -verify=both,expected %s
2 // RUN: %clang_cc1 -fgnuc-version=4.2.1 -triple=i686-linux-gnu -ffreestanding -verify=both,ref %s
3 
4 /// FIXME: Copied from test/Sema/atomic-expr.c.
5 /// this expression seems to be rejected for weird reasons,
6 /// but we imitate the current interpreter's behavior.
7 _Atomic int ai = 0;
8 // FIXME: &ai is an address constant, so this should be accepted as an
9 // initializer, but the bit-cast inserted due to the pointer conversion is
10 // tripping up the test for whether the initializer is a constant expression.
11 // The warning is correct but the error is not.
12 _Atomic(int *) aip3 = &ai; // both-warning {{incompatible pointer types initializing '_Atomic(int *)' with an expression of type '_Atomic(int) *'}} \
13                            // both-error {{initializer element is not a compile-time constant}}
14 
15 #include <stdatomic.h>
16 
17 
18 
19 _Static_assert(__GCC_ATOMIC_BOOL_LOCK_FREE == 2, "");
20 _Static_assert(__GCC_ATOMIC_BOOL_LOCK_FREE == __CLANG_ATOMIC_BOOL_LOCK_FREE, "");
21 _Static_assert(__GCC_ATOMIC_CHAR_LOCK_FREE == 2, "");
22 _Static_assert(__GCC_ATOMIC_CHAR_LOCK_FREE == __CLANG_ATOMIC_CHAR_LOCK_FREE, "");
23 _Static_assert(__GCC_ATOMIC_CHAR16_T_LOCK_FREE == 2, "");
24 _Static_assert(__GCC_ATOMIC_CHAR16_T_LOCK_FREE == __CLANG_ATOMIC_CHAR16_T_LOCK_FREE, "");
25 _Static_assert(__GCC_ATOMIC_CHAR32_T_LOCK_FREE == 2, "");
26 _Static_assert(__GCC_ATOMIC_CHAR32_T_LOCK_FREE == __CLANG_ATOMIC_CHAR32_T_LOCK_FREE, "");
27 _Static_assert(__GCC_ATOMIC_WCHAR_T_LOCK_FREE == 2, "");
28 _Static_assert(__GCC_ATOMIC_WCHAR_T_LOCK_FREE == __CLANG_ATOMIC_WCHAR_T_LOCK_FREE, "");
29 _Static_assert(__GCC_ATOMIC_SHORT_LOCK_FREE == 2, "");
30 _Static_assert(__GCC_ATOMIC_SHORT_LOCK_FREE == __CLANG_ATOMIC_SHORT_LOCK_FREE, "");
31 _Static_assert(__GCC_ATOMIC_INT_LOCK_FREE == 2, "");
32 _Static_assert(__GCC_ATOMIC_INT_LOCK_FREE == __CLANG_ATOMIC_INT_LOCK_FREE, "");
33 _Static_assert(__GCC_ATOMIC_LONG_LOCK_FREE == 2, "");
34 _Static_assert(__GCC_ATOMIC_LONG_LOCK_FREE == __CLANG_ATOMIC_LONG_LOCK_FREE, "");
35 _Static_assert(__GCC_ATOMIC_LLONG_LOCK_FREE == 2, "");
36 _Static_assert(__GCC_ATOMIC_LLONG_LOCK_FREE == __CLANG_ATOMIC_LLONG_LOCK_FREE, "");
37 _Static_assert(__GCC_ATOMIC_POINTER_LOCK_FREE == 2, "");
38 _Static_assert(__GCC_ATOMIC_POINTER_LOCK_FREE == __CLANG_ATOMIC_POINTER_LOCK_FREE, "");
39 
40 _Static_assert(__c11_atomic_is_lock_free(1), "");
41 _Static_assert(__c11_atomic_is_lock_free(2), "");
42 _Static_assert(__c11_atomic_is_lock_free(3), ""); // both-error {{not an integral constant expression}}
43 _Static_assert(__c11_atomic_is_lock_free(4), "");
44 _Static_assert(__c11_atomic_is_lock_free(8), "");
45 _Static_assert(__c11_atomic_is_lock_free(16), ""); // both-error {{not an integral constant expression}}
46 _Static_assert(__c11_atomic_is_lock_free(17), ""); // both-error {{not an integral constant expression}}
47 
48 _Static_assert(__atomic_is_lock_free(1, 0), "");
49 _Static_assert(__atomic_is_lock_free(2, 0), "");
50 _Static_assert(__atomic_is_lock_free(3, 0), ""); // both-error {{not an integral constant expression}}
51 _Static_assert(__atomic_is_lock_free(4, 0), "");
52 _Static_assert(__atomic_is_lock_free(8, 0), "");
53 _Static_assert(__atomic_is_lock_free(16, 0), ""); // both-error {{not an integral constant expression}}
54 _Static_assert(__atomic_is_lock_free(17, 0), ""); // both-error {{not an integral constant expression}}
55 
56 _Static_assert(atomic_is_lock_free((atomic_char*)0), "");
57 _Static_assert(atomic_is_lock_free((atomic_short*)0), "");
58 _Static_assert(atomic_is_lock_free((atomic_int*)0), "");
59 _Static_assert(atomic_is_lock_free((atomic_long*)0), "");
60 _Static_assert(atomic_is_lock_free(0 + (atomic_char*)0), "");
61 
62 _Static_assert(__atomic_always_lock_free(1, (void*)1), "");
63 _Static_assert(__atomic_always_lock_free(1, (void*)-1), "");
64 _Static_assert(!__atomic_always_lock_free(4, (void*)2), "");
65 _Static_assert(!__atomic_always_lock_free(4, (void*)-2), "");
66 _Static_assert(__atomic_always_lock_free(4, (void*)4), "");
67 _Static_assert(__atomic_always_lock_free(4, (void*)-4), "");
68 
69 _Static_assert(__atomic_always_lock_free(1, "string"), "");
70 _Static_assert(!__atomic_always_lock_free(2, "string"), "");
71 _Static_assert(__atomic_always_lock_free(2, (int[2]){}), "");
72 void dummyfn();
73 _Static_assert(__atomic_always_lock_free(2, dummyfn) || 1, "");
74