xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/alignof.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc // rdar://13784901
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc struct S0 {
6f4a2713aSLionel Sambuc   int x;
7f4a2713aSLionel Sambuc   static const int test0 = __alignof__(x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
8f4a2713aSLionel Sambuc   static const int test1 = __alignof__(S0::x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
9f4a2713aSLionel Sambuc   auto test2() -> char(&)[__alignof__(x)]; // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
10f4a2713aSLionel Sambuc };
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc struct S1; // expected-note 6 {{forward declaration}}
13f4a2713aSLionel Sambuc extern S1 s1;
14f4a2713aSLionel Sambuc const int test3 = __alignof__(s1); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc struct S2 {
17f4a2713aSLionel Sambuc   S2();
18f4a2713aSLionel Sambuc   S1 &s;
19f4a2713aSLionel Sambuc   int x;
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc   int test4 = __alignof__(x); // ok
22f4a2713aSLionel Sambuc   int test5 = __alignof__(s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
23f4a2713aSLionel Sambuc };
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc const int test6 = __alignof__(S2::x);
26f4a2713aSLionel Sambuc const int test7 = __alignof__(S2::s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc // Arguably, these should fail like the S1 cases do: the alignment of
29f4a2713aSLionel Sambuc // 's2.x' should depend on the alignment of both x-within-S2 and
30f4a2713aSLionel Sambuc // s2-within-S3 and thus require 'S3' to be complete.  If we start
31f4a2713aSLionel Sambuc // doing the appropriate recursive walk to do that, we should make
32f4a2713aSLionel Sambuc // sure that these cases don't explode.
33f4a2713aSLionel Sambuc struct S3 {
34f4a2713aSLionel Sambuc   S2 s2;
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc   static const int test8 = __alignof__(s2.x);
37f4a2713aSLionel Sambuc   static const int test9 = __alignof__(s2.s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
38f4a2713aSLionel Sambuc   auto test10() -> char(&)[__alignof__(s2.x)];
39f4a2713aSLionel Sambuc   static const int test11 = __alignof__(S3::s2.x);
40f4a2713aSLionel Sambuc   static const int test12 = __alignof__(S3::s2.s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
41f4a2713aSLionel Sambuc   auto test13() -> char(&)[__alignof__(s2.x)];
42f4a2713aSLionel Sambuc };
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc // Same reasoning as S3.
45f4a2713aSLionel Sambuc struct S4 {
46f4a2713aSLionel Sambuc   union {
47f4a2713aSLionel Sambuc     int x;
48f4a2713aSLionel Sambuc   };
49f4a2713aSLionel Sambuc   static const int test0 = __alignof__(x);
50f4a2713aSLionel Sambuc   static const int test1 = __alignof__(S0::x);
51f4a2713aSLionel Sambuc   auto test2() -> char(&)[__alignof__(x)];
52f4a2713aSLionel Sambuc };
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc // Regression test for asking for the alignment of a field within an invalid
55f4a2713aSLionel Sambuc // record.
56f4a2713aSLionel Sambuc struct S5 {
57f4a2713aSLionel Sambuc   S1 s;  // expected-error {{incomplete type}}
58f4a2713aSLionel Sambuc   int x;
59f4a2713aSLionel Sambuc };
60f4a2713aSLionel Sambuc const int test8 = __alignof__(S5::x);
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc long long int test14[2];
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc static_assert(alignof(test14) == 8, "foo"); // expected-warning {{'alignof' applied to an expression is a GNU extension}}
65*0a6a1f1dSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc // PR19992
67*0a6a1f1dSLionel Sambuc static_assert(alignof(int[]) == alignof(int), ""); // ok
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc namespace alignof_array_expr {
70*0a6a1f1dSLionel Sambuc   alignas(32) extern int n[];
71*0a6a1f1dSLionel Sambuc   static_assert(alignof(n) == 32, ""); // expected-warning {{GNU extension}}
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc   template<int> struct S {
74*0a6a1f1dSLionel Sambuc     static int a[];
75*0a6a1f1dSLionel Sambuc   };
76*0a6a1f1dSLionel Sambuc   template<int N> int S<N>::a[N];
77*0a6a1f1dSLionel Sambuc   // ok, does not complete type of S<-1>::a
78*0a6a1f1dSLionel Sambuc   static_assert(alignof(S<-1>::a) == alignof(int), ""); // expected-warning {{GNU extension}}
79*0a6a1f1dSLionel Sambuc }
80