File: c2x-auto.c

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (139 lines) | stat: -rw-r--r-- 5,394 bytes parent folder | download | duplicates (7)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// RUN: %clang_cc1 -std=c2x -verify -pedantic -Wno-comments %s
// RUN: %clang_cc1 -std=c2x -verify -pedantic -Wno-comments %s -fexperimental-new-constant-interpreter

void test_basic_types(void) {
  auto undefined;     // expected-error {{declaration of variable 'undefined' with deduced type 'auto' requires an initializer}}
  auto auto_int = 4;
  auto auto_long = 4UL;
  auto int auto_int_ts = 12;
  signed auto a = 1L; // expected-error {{'auto' cannot be signed or unsigned}}

  _Static_assert(_Generic(auto_int, int : 1));
  _Static_assert(_Generic(auto_long, unsigned long : 1));
}

void test_complex_types(void) {
  _Complex auto i = 12.0; // expected-error {{'_Complex auto' is invalid}}
}

void test_gnu_extensions(void) {
  auto t = ({ // expected-warning {{use of GNU statement expression extension}}
    auto b = 12;
    b;
  });
  _Static_assert(_Generic(t, int : 1));
}

void test_sizeof_typeof(void) {
  auto auto_size = sizeof(auto);  // expected-error {{expected expression}}
  typeof(auto) tpof = 4;          // expected-error {{expected expression}}
}

void test_casts(void) {
  auto int_cast = (int)(4 + 3);
  auto double_cast = (double)(1 / 3);
  auto long_cast = (long)(4UL + 3UL);
  auto auto_cast = (auto)(4 + 3); // expected-error {{expected expression}}

  _Static_assert(_Generic(int_cast, int : 1));
  _Static_assert(_Generic(double_cast, double : 1));
  _Static_assert(_Generic(long_cast, long : 1));
}

void test_compound_literral(void) {
  auto int_cl = (int){13};
  auto double_cl = (double){2.5};
  auto array[] = { 1, 2, 3 }; // expected-error {{cannot use 'auto' with array in C}}

  auto auto_cl = (auto){13};  // expected-error {{expected expression}}

  _Static_assert(_Generic(int_cl, int : 1));
  _Static_assert(_Generic(double_cl, double : 1));
}

void test_array_pointers(void) {
  double array[3] = { 0 };
  auto a = array;
  auto b = &array;

  _Static_assert(_Generic(array, double * : 1));
  _Static_assert(_Generic(a, double * : 1));
  _Static_assert(_Generic(b, double (*)[3] : 1));
}

void test_typeof() {
  int typeof_target();
  auto result = (typeof(typeof_target())){12};

  _Static_assert(_Generic(result, int : 1));
}

void test_qualifiers(const int y) {
  const auto a = 12;
  auto b = y;
  static auto c = 1UL;
  int* pa = &a; // expected-warning {{initializing 'int *' with an expression of type 'const int *' discards qualifiers}}
  const int* pb = &b;
  int* pc = &c; // expected-warning {{incompatible pointer types initializing 'int *' with an expression of type 'unsigned long *'}}

  _Static_assert(_Generic(a, int : 1));
  _Static_assert(_Generic(b, int : 1));
  _Static_assert(_Generic(c, unsigned long : 1));
  _Static_assert(_Generic(pa, int * : 1));
  _Static_assert(_Generic(pb, const int * : 1));
  _Static_assert(_Generic(pc, int * : 1));
}

void test_strings(void) {
  auto str = "this is a string";
  auto str2[] = "this is a string";       // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
  auto (str3) = "this is a string";
  auto (((str4))) = "this is a string";

  _Static_assert(_Generic(str, char * : 1));
  _Static_assert(_Generic(str2, char * : 1));
  _Static_assert(_Generic(str3, char * : 1));
  _Static_assert(_Generic(str4, char * : 1));
}

void test_pointers(void) {
  auto a = 12;
  auto *ptr = &a;                         // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
  auto *str = "this is a string";         // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
  const auto *str2 = "this is a string";  // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
  auto *b = &a;                           // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
  *b = &a;                                // expected-error {{incompatible pointer to integer conversion assigning to 'int' from 'int *'; remove &}}
  auto nptr = nullptr;

  _Static_assert(_Generic(a, int : 1));
  _Static_assert(_Generic(ptr, int * : 1));
  _Static_assert(_Generic(str, char * : 1));
  _Static_assert(_Generic(str2, const char * : 1));
  _Static_assert(_Generic(b, int * : 1));
  _Static_assert(_Generic(nptr, typeof(nullptr) : 1));
}

void test_prototypes(void) {
  extern void foo(int a, int array[({ auto x = 12; x;})]);  // expected-warning {{use of GNU statement expression extension}}
}

void test_scopes(void) {
  double a = 7;
  double b = 9;
  {
    auto a = a * a; // expected-error {{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}} \
                       expected-error {{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}}
  }
  {
    auto b = a * a;
    auto a = b;

    _Static_assert(_Generic(b, double : 1));
    _Static_assert(_Generic(a, double : 1));
  }
}

[[clang::overloadable]] auto test(auto x) { // expected-error {{'auto' not allowed in function prototype}} \
                                               expected-error {{'auto' not allowed in function return type}}
  return x;
}