55 lines · c
1// RUN: %clang_cc1 -verify -std=c2x %s2 3// Demonstrate that we don't support the expression form without parentheses in4// C2x mode.5typeof 0 int i = 12; // expected-error {{expected '(' after 'typeof'}} expected-error {{expected identifier or '('}}6typeof 0 j = 12; // expected-error {{expected '(' after 'typeof'}} expected-error {{expected identifier or '('}}7typeof_unqual 0 k = 12; // expected-error {{expected '(' after 'typeof_unqual'}} expected-error {{expected identifier or '('}}8typeof_unqual 0 int l = 12; // expected-error {{expected '(' after 'typeof_unqual'}} expected-error {{expected identifier or '('}}9 10// Show that combining typeof with another type specifier fails, but otherwise11// the expression and type forms are both parsed properly.12typeof(0) int a = 12; // expected-error {{cannot combine with previous 'typeof' declaration specifier}}13typeof(0) b = 12;14typeof_unqual(0) int c = 12; // expected-error {{cannot combine with previous 'typeof_unqual' declaration specifier}}15typeof_unqual(0) d = 12;16typeof(int) e = 12;17typeof_unqual(int) f = 12;18 19// Show that we can parse nested constructs of both forms.20typeof(typeof(0)) w;21typeof_unqual(typeof(0)) x;22typeof(typeof_unqual(0)) y;23typeof_unqual(typeof_unqual(0)) z;24 25// Show that you can spell the type in functions, structures, or as the base26// type of an enumeration.27typeof(b) func1(typeof(b) c);28typeof_unqual(b) func2(typeof_unqual(b) c);29 30struct S {31 typeof(b) i;32 typeof_unqual(b) j;33} s;34 35enum E1 : typeof(b) { FirstZero };36enum E2 : typeof_unqual(b) { SecondZero };37 38// Show that you can use this type in place of another type and everything39// works as expected.40_Static_assert(__builtin_offsetof(typeof(struct S), i) == 0);41_Static_assert(__builtin_offsetof(typeof(s), i) == 0);42_Static_assert(__builtin_offsetof(typeof_unqual(struct S), i) == 0);43_Static_assert(__builtin_offsetof(typeof_unqual(s), i) == 0);44 45// Show that typeof and typeof_unqual can be used in the underlying type of an46// enumeration even when given the type form. Note, this can look like a47// compound literal expression, which caused GH146351.48enum E3 : typeof(int) { ThirdZero }; // (int) {}; is not a compound literal!49enum E4 : typeof_unqual(int) { FourthZero }; // Same here50 51// Ensure that this invalid construct is diagnosed instead of being treated52// as typeof((int){ 0 }).53typeof(int) { 0 } x; // expected-error {{a type specifier is required for all declarations}} \54 expected-error {{expected identifier or '('}}55