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