110f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s 210f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 310f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall// rdar://13784901 410f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 510f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCallstruct S0 { 610f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall int x; 710f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall static const int test0 = __alignof__(x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}} 810f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall static const int test1 = __alignof__(S0::x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}} 910f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall auto test2() -> char(&)[__alignof__(x)]; // expected-error {{invalid application of 'alignof' to a field of a class still being defined}} 1010f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall}; 1110f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 12147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gaystruct S1; // expected-note 6 {{forward declaration}} 1310f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCallextern S1 s1; 1410f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCallconst int test3 = __alignof__(s1); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}} 1510f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 1610f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCallstruct S2 { 1710f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall S2(); 1810f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall S1 &s; 1910f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall int x; 2010f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 2110f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall int test4 = __alignof__(x); // ok 2210f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall int test5 = __alignof__(s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}} 2310f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall}; 2410f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 2510f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCallconst int test6 = __alignof__(S2::x); 2610f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCallconst int test7 = __alignof__(S2::s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}} 2710f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 2810f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall// Arguably, these should fail like the S1 cases do: the alignment of 2910f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall// 's2.x' should depend on the alignment of both x-within-S2 and 3010f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall// s2-within-S3 and thus require 'S3' to be complete. If we start 3110f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall// doing the appropriate recursive walk to do that, we should make 3210f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall// sure that these cases don't explode. 3310f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCallstruct S3 { 3410f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall S2 s2; 3510f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 3610f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall static const int test8 = __alignof__(s2.x); 3710f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall static const int test9 = __alignof__(s2.s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}} 3810f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall auto test10() -> char(&)[__alignof__(s2.x)]; 3910f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall static const int test11 = __alignof__(S3::s2.x); 4010f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall static const int test12 = __alignof__(S3::s2.s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}} 4110f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall auto test13() -> char(&)[__alignof__(s2.x)]; 4210f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall}; 4310f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall 4410f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall// Same reasoning as S3. 4510f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCallstruct S4 { 4610f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall union { 4710f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall int x; 4810f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall }; 4910f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall static const int test0 = __alignof__(x); 5010f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall static const int test1 = __alignof__(S0::x); 5110f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall auto test2() -> char(&)[__alignof__(x)]; 5210f6f065456a2cfb6c2ab5dfedefb930e5e52e9dJohn McCall}; 53147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gay 54147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gay// Regression test for asking for the alignment of a field within an invalid 55147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gay// record. 56147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gaystruct S5 { 57147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gay S1 s; // expected-error {{incomplete type}} 58147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gay int x; 59147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gay}; 60147fab970c9877273e6185f0e276f376887c967fMatt Beaumont-Gayconst int test8 = __alignof__(S5::x); 611c56c9d9c2eed9ade88afe93541cc6fd25932355Rafael Espindola 621c56c9d9c2eed9ade88afe93541cc6fd25932355Rafael Espindolalong long int test14[2]; 631c56c9d9c2eed9ade88afe93541cc6fd25932355Rafael Espindola 641c56c9d9c2eed9ade88afe93541cc6fd25932355Rafael Espindolastatic_assert(alignof(test14) == 8, "foo"); // expected-warning {{'alignof' applied to an expression is a GNU extension}} 65c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines 66c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines// PR19992 67c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hinesstatic_assert(alignof(int[]) == alignof(int), ""); // ok 68c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines 69c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hinesnamespace alignof_array_expr { 70c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines alignas(32) extern int n[]; 71c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines static_assert(alignof(n) == 32, ""); // expected-warning {{GNU extension}} 72c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines 73c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines template<int> struct S { 74c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines static int a[]; 75c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines }; 76c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines template<int N> int S<N>::a[N]; 77c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines // ok, does not complete type of S<-1>::a 78c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines static_assert(alignof(S<-1>::a) == alignof(int), ""); // expected-warning {{GNU extension}} 79c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines} 800e2c34f92f00628d48968dfea096d36381f494cbStephen Hines 810e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestemplate <typename T> void n(T) { 820e2c34f92f00628d48968dfea096d36381f494cbStephen Hines alignas(T) int T1; 830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines char k[__alignof__(T1)]; 840e2c34f92f00628d48968dfea096d36381f494cbStephen Hines static_assert(sizeof(k) == alignof(long long), ""); 850e2c34f92f00628d48968dfea096d36381f494cbStephen Hines} 860e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestemplate void n(long long); 8758878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar 8858878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainarnamespace PR22042 { 8958878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainartemplate <typename T> 9058878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainarvoid Fun(T A) { 9158878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar typedef int __attribute__((__aligned__(A))) T1; // expected-error {{requested alignment is dependent but declaration is not dependent}} 9258878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar int k1[__alignof__(T1)]; 9358878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar} 9458878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar 9558878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainartemplate <int N> 9658878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainarstruct S { 9758878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar typedef __attribute__((aligned(N))) int Field[sizeof(N)]; // expected-error {{requested alignment is dependent but declaration is not dependent}} 9858878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar}; 9958878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar} 100