John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s |
| 2 | |
| 3 | // rdar://13784901 |
| 4 | |
| 5 | struct S0 { |
| 6 | int x; |
Richard Smith | df4994c | 2018-10-26 19:42:43 +0000 | [diff] [blame] | 7 | static const int test0 = __alignof__(x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}} |
| 8 | static const int test1 = __alignof__(S0::x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}} |
| 9 | auto test2() -> char(&)[__alignof__(x)]; // expected-error {{invalid application of 'alignof' to a field of a class still being defined}} |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 10 | }; |
| 11 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 12 | struct S1; // expected-note 6 {{forward declaration}} |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 13 | extern S1 s1; |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 14 | const int test3 = __alignof__(s1); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}} |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 15 | |
| 16 | struct S2 { |
| 17 | S2(); |
| 18 | S1 &s; |
| 19 | int x; |
| 20 | |
| 21 | int test4 = __alignof__(x); // ok |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 22 | int test5 = __alignof__(s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}} |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | const int test6 = __alignof__(S2::x); |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 26 | const int test7 = __alignof__(S2::s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}} |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 27 | |
| 28 | // Arguably, these should fail like the S1 cases do: the alignment of |
| 29 | // 's2.x' should depend on the alignment of both x-within-S2 and |
| 30 | // s2-within-S3 and thus require 'S3' to be complete. If we start |
| 31 | // doing the appropriate recursive walk to do that, we should make |
| 32 | // sure that these cases don't explode. |
| 33 | struct S3 { |
| 34 | S2 s2; |
| 35 | |
| 36 | static const int test8 = __alignof__(s2.x); |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 37 | static const int test9 = __alignof__(s2.s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}} |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 38 | auto test10() -> char(&)[__alignof__(s2.x)]; |
| 39 | static const int test11 = __alignof__(S3::s2.x); |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 40 | static const int test12 = __alignof__(S3::s2.s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}} |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 41 | auto test13() -> char(&)[__alignof__(s2.x)]; |
| 42 | }; |
| 43 | |
| 44 | // Same reasoning as S3. |
| 45 | struct S4 { |
| 46 | union { |
| 47 | int x; |
| 48 | }; |
| 49 | static const int test0 = __alignof__(x); |
| 50 | static const int test1 = __alignof__(S0::x); |
| 51 | auto test2() -> char(&)[__alignof__(x)]; |
| 52 | }; |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 53 | |
| 54 | // Regression test for asking for the alignment of a field within an invalid |
| 55 | // record. |
| 56 | struct S5 { |
| 57 | S1 s; // expected-error {{incomplete type}} |
| 58 | int x; |
| 59 | }; |
| 60 | const int test8 = __alignof__(S5::x); |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 61 | |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 62 | int test14[2]; |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 63 | |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 64 | static_assert(alignof(test14) == 4, "foo"); // expected-warning {{'alignof' applied to an expression is a GNU extension}} |
Richard Smith | c3fbf68 | 2014-06-10 21:11:26 +0000 | [diff] [blame] | 65 | |
| 66 | // PR19992 |
Richard Smith | c3fbf68 | 2014-06-10 21:11:26 +0000 | [diff] [blame] | 67 | static_assert(alignof(int[]) == alignof(int), ""); // ok |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 68 | |
| 69 | namespace alignof_array_expr { |
| 70 | alignas(32) extern int n[]; |
| 71 | static_assert(alignof(n) == 32, ""); // expected-warning {{GNU extension}} |
| 72 | |
| 73 | template<int> struct S { |
| 74 | static int a[]; |
| 75 | }; |
| 76 | template<int N> int S<N>::a[N]; |
| 77 | // ok, does not complete type of S<-1>::a |
| 78 | static_assert(alignof(S<-1>::a) == alignof(int), ""); // expected-warning {{GNU extension}} |
| 79 | } |
David Majnemer | 10fd83d | 2015-01-15 10:04:14 +0000 | [diff] [blame] | 80 | |
| 81 | template <typename T> void n(T) { |
| 82 | alignas(T) int T1; |
| 83 | char k[__alignof__(T1)]; |
| 84 | static_assert(sizeof(k) == alignof(long long), ""); |
| 85 | } |
| 86 | template void n(long long); |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame] | 87 | |
| 88 | namespace PR22042 { |
| 89 | template <typename T> |
| 90 | void Fun(T A) { |
| 91 | typedef int __attribute__((__aligned__(A))) T1; // expected-error {{requested alignment is dependent but declaration is not dependent}} |
| 92 | int k1[__alignof__(T1)]; |
| 93 | } |
| 94 | |
| 95 | template <int N> |
| 96 | struct S { |
| 97 | typedef __attribute__((aligned(N))) int Field[sizeof(N)]; // expected-error {{requested alignment is dependent but declaration is not dependent}} |
| 98 | }; |
| 99 | } |
Matt Davis | c50240d | 2018-11-05 17:25:26 +0000 | [diff] [blame] | 100 | |
| 101 | typedef int __attribute__((aligned(16))) aligned_int; |
| 102 | template <typename> |
| 103 | using template_alias = aligned_int; |
| 104 | static_assert(alignof(template_alias<void>) == 16, "Expected alignment of 16" ); |