aboutsummaryrefslogtreecommitdiff
path: root/test/Sema/block-misc.c
blob: 22604582db80804baf10c929d3dfe5dfacc97b94 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// RUN: %clang_cc1 -fsyntax-only -verify %s -fblocks
void donotwarn();

int (^IFP) ();
int (^II) (int);
int test1() {
  int (^PFR) (int) = 0; // OK
  PFR = II;             // OK

  if (PFR == II)        // OK
    donotwarn();

  if (PFR == IFP)       // OK
    donotwarn();

  if (PFR == (int (^) (int))IFP) // OK
    donotwarn();

  if (PFR == 0)         // OK
    donotwarn();

  if (PFR)              // OK
    donotwarn();

  if (!PFR)             // OK
    donotwarn();

  return PFR != IFP;    // OK
}

int test2(double (^S)()) {
  double (^I)(int)  = (void*) S;
  (void*)I = (void *)S; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}

  void *pv = I;

  pv = S;

  I(1);

  return (void*)I == (void *)S;
}

int^ x; // expected-error {{block pointer to non-function type is invalid}}
int^^ x1; // expected-error {{block pointer to non-function type is invalid}} expected-error {{block pointer to non-function type is invalid}}

void test3() {
  char *^ y; // expected-error {{block pointer to non-function type is invalid}}
}



enum {NSBIRLazilyAllocated = 0};

int test4(int argc) {  // rdar://6251437
  ^{
    switch (argc) {
      case NSBIRLazilyAllocated:  // is an integer constant expression.
      default:
        break;
    }
  }();
  return 0;
}


void bar(void*);
// rdar://6257721 - reference to static/global is byref by default.
static int test5g;
void test5() {
  bar(^{ test5g = 1; });
}

// rdar://6405429 - __func__ in a block refers to the containing function name.
const char*test6() {
  return ^{
    return __func__;
  } ();
}

// radr://6732116 - block comparisons
void (^test7a)();
int test7(void (^p)()) {
  return test7a == p;
}


void test8() {
somelabel:
  ^{ goto somelabel; }();   // expected-error {{use of undeclared label 'somelabel'}}
}

void test9() {
  goto somelabel;       // expected-error {{use of undeclared label 'somelabel'}}
  ^{ somelabel: ; }();
}

void test10(int i) {
  switch (i) {
  case 41: ;
  ^{ case 42: ; }();     // expected-error {{'case' statement not in switch statement}}
  }
}

void test11(int i) {
  switch (i) {
  case 41: ;
    ^{ break; }();     // expected-error {{'break' statement not in loop or switch statement}}
  }
  
  for (; i < 100; ++i)
    ^{ break; }();     // expected-error {{'break' statement not in loop or switch statement}}
}

void (^test12f)(void);
void test12() {
  test12f = ^test12f;  // expected-error {{type name requires a specifier or qualifier}} expected-error {{expected expression}}
}

// rdar://6808730
void *test13 = ^{
  int X = 32;

  void *P = ^{
    return X+4;  // References outer block's "X", so outer block is constant.
  };
};

void test14() {
  int X = 32;
  static void *P = ^{  // expected-error {{initializer element is not a compile-time constant}}

    void *Q = ^{
      // References test14's "X": outer block is non constant.
      return X+4;
    };
  };
}

enum { LESS };

void foo(long (^comp)()) { // expected-note{{passing argument to parameter 'comp' here}}
}

void (^test15f)(void);
void test15() {
  foo(^{ return LESS; }); // expected-error {{incompatible block pointer types passing 'int (^)(void)' to parameter of type 'long (^)()'}}
}

__block int test16i;  // expected-error {{__block attribute not allowed, only allowed on local variables}}

void test16(__block int i) { // expected-error {{__block attribute not allowed, only allowed on local variables}}
  int size = 5;
  extern __block double extern_var; // expected-error {{__block attribute not allowed, only allowed on local variables}}
  static __block char * pch; // expected-error {{__block attribute not allowed, only allowed on local variables}}
  __block int a[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
  __block int (*ap)[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
}

void f();

void test17() {
  void (^bp)(int);
  void (*rp)(int);
  void (^bp1)();
  void *vp = bp;

  f(1 ? bp : vp);
  f(1 ? vp : bp);
  f(1 ? bp : bp1);
  (void)(bp > rp); // expected-error {{invalid operands}}
  (void)(bp > 0); // expected-error {{invalid operands}}
  (void)(bp > bp); // expected-error {{invalid operands}}
  (void)(bp > vp); // expected-error {{invalid operands}}
  f(1 ? bp : rp); // expected-error {{incompatible operand types ('void (^)(int)' and 'void (*)(int)')}}
  (void)(bp == 1); // expected-error {{invalid operands to binary expression}}
  (void)(bp == 0);
  (void)(1 == bp); // expected-error {{invalid operands to binary expression}}
  (void)(0 == bp);
  (void)(bp < 1); // expected-error {{invalid operands to binary expression}}
  (void)(bp < 0); // expected-error {{invalid operands to binary expression}}
  (void)(1 < bp); // expected-error {{invalid operands to binary expression}}
  (void)(0 < bp); // expected-error {{invalid operands to binary expression}}
}

void test18() {
  void (^const  blockA)(void) = ^{ };
  blockA = ^{ }; // expected-error {{read-only variable is not assignable}}
}

// rdar://7072507
int test19() {
  goto L0;       // expected-error {{goto into protected scope}}
  
  __block int x; // expected-note {{jump bypasses setup of __block variable}}
L0:
  x = 0;
  ^(){ ++x; }();
  return x;
}

// radr://7438948
void test20() {
  int n = 7;
  int vla[n]; // expected-note {{declared here}}
  int (*vm)[n] = 0; // expected-note {{declared here}}
  vla[1] = 4341;
  ^{
    (void)vla[1];  // expected-error {{cannot refer to declaration with a variably modified type inside block}}
    (void)(vm+1);  // expected-error {{cannot refer to declaration with a variably modified type inside block}}
  }();
}

// radr://7438948
void test21() {
  int a[7]; // expected-note {{declared here}}
  __block int b[10]; // expected-note {{declared here}}
  a[1] = 1;
  ^{
    (void)a[1]; // expected-error {{cannot refer to declaration with an array type inside block}}
    (void)b[1]; // expected-error {{cannot refer to declaration with an array type inside block}}
  }();
}

// rdar ://8218839
const char * (^func)(void) = ^{ return __func__; };
const char * (^function)(void) = ^{ return __FUNCTION__; };
const char * (^pretty)(void) = ^{ return __PRETTY_FUNCTION__; };