File: c2x-typeof.c

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,235,796 kB
  • sloc: cpp: 7,617,614; ansic: 1,433,901; asm: 1,058,726; python: 252,096; f90: 94,671; objc: 70,753; lisp: 42,813; pascal: 18,401; sh: 10,032; ml: 5,111; perl: 4,720; awk: 3,523; makefile: 3,401; javascript: 2,272; xml: 892; fortran: 770
file content (54 lines) | stat: -rw-r--r-- 2,546 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// RUN: %clang_cc1 -verify -std=c2x %s

// Demonstrate that we don't support the expression form without parentheses in
// C2x mode.
typeof 0 int i = 12;         // expected-error {{expected '(' after 'typeof'}} expected-error {{expected identifier or '('}}
typeof 0 j = 12;             // expected-error {{expected '(' after 'typeof'}} expected-error {{expected identifier or '('}}
typeof_unqual 0 k = 12;      // expected-error {{expected '(' after 'typeof_unqual'}} expected-error {{expected identifier or '('}}
typeof_unqual 0 int l = 12;  // expected-error {{expected '(' after 'typeof_unqual'}} expected-error {{expected identifier or '('}}

// Show that combining typeof with another type specifier fails, but otherwise
// the expression and type forms are both parsed properly.
typeof(0) int a = 12;        // expected-error {{cannot combine with previous 'typeof' declaration specifier}}
typeof(0) b = 12;
typeof_unqual(0) int c = 12; // expected-error {{cannot combine with previous 'typeof_unqual' declaration specifier}}
typeof_unqual(0) d = 12;
typeof(int) e = 12;
typeof_unqual(int) f = 12;

// Show that we can parse nested constructs of both forms.
typeof(typeof(0)) w;
typeof_unqual(typeof(0)) x;
typeof(typeof_unqual(0)) y;
typeof_unqual(typeof_unqual(0)) z;

// Show that you can spell the type in functions, structures, or as the base
// type of an enumeration.
typeof(b) func1(typeof(b) c);
typeof_unqual(b) func2(typeof_unqual(b) c);

struct S {
  typeof(b) i;
  typeof_unqual(b) j;
} s;

enum E1 : typeof(b) { FirstZero };
enum E2 : typeof_unqual(b) { SecondZero };

// Show that you can use this type in place of another type and everything
// works as expected.
_Static_assert(__builtin_offsetof(typeof(struct S), i) == 0);
_Static_assert(__builtin_offsetof(typeof(s), i) == 0);
_Static_assert(__builtin_offsetof(typeof_unqual(struct S), i) == 0);
_Static_assert(__builtin_offsetof(typeof_unqual(s), i) == 0);

// Show that typeof and typeof_unqual can be used in the underlying type of an
// enumeration even when given the type form. Note, this can look like a
// compound literal expression, which caused GH146351.
enum E3 : typeof(int) { ThirdZero }; // (int) {}; is not a compound literal!
enum E4 : typeof_unqual(int) { FourthZero }; // Same here

// Ensure that this invalid construct is diagnosed instead of being treated
// as typeof((int){ 0 }).
typeof(int) { 0 } x; // expected-error {{a type specifier is required for all declarations}} \
                        expected-error {{expected identifier or '('}}