blob: d462af06d730a8adc954232bbc7ae401f9c48d88 [file] [log] [blame]
John McCallddb0b4d2010-05-12 00:58:13 +00001// RUN: %clang_cc1 -fsyntax-only -verify -fblocks %s -Wno-unreachable-code
2
3namespace test0 {
4 struct D { ~D(); };
5
6 int f(bool b) {
7 if (b) {
8 D d;
9 goto end;
10 }
11
12 end:
13 return 1;
14 }
15}
16
17namespace test1 {
18 struct C { C(); };
19
20 int f(bool b) {
21 if (b)
22 goto foo; // expected-error {{illegal goto into protected scope}}
23 C c; // expected-note {{jump bypasses variable initialization}}
24 foo:
25 return 1;
26 }
27}
28
29namespace test2 {
30 struct C { C(); };
31
32 int f(void **ip) {
33 static void *ips[] = { &&lbl1, &&lbl2 };
34
35 C c;
36 goto *ip;
37 lbl1:
38 return 0;
39 lbl2:
40 return 1;
41 }
42}
43
44namespace test3 {
45 struct C { C(); };
46
47 int f(void **ip) {
48 static void *ips[] = { &&lbl1, &&lbl2 };
49
50 goto *ip;
51 lbl1: {
52 C c;
53 return 0;
54 }
55 lbl2:
56 return 1;
57 }
58}
59
60namespace test4 {
61 struct C { C(); };
62 struct D { ~D(); };
63
64 int f(void **ip) {
65 static void *ips[] = { &&lbl1, &&lbl2 };
66
67 C c0;
68
John McCall95c225d2010-10-28 08:53:48 +000069 goto *ip; // expected-error {{indirect goto might cross protected scopes}}
John McCallddb0b4d2010-05-12 00:58:13 +000070 C c1; // expected-note {{jump bypasses variable initialization}}
71 lbl1: // expected-note {{possible target of indirect goto}}
72 return 0;
73 lbl2:
74 return 1;
75 }
76}
77
78namespace test5 {
79 struct C { C(); };
80 struct D { ~D(); };
81
82 int f(void **ip) {
83 static void *ips[] = { &&lbl1, &&lbl2 };
84 C c0;
85
86 goto *ip;
87 lbl1: // expected-note {{possible target of indirect goto}}
88 return 0;
89 lbl2:
90 if (ip[1]) {
91 D d; // expected-note {{jump exits scope of variable with non-trivial destructor}}
92 ip += 2;
John McCall95c225d2010-10-28 08:53:48 +000093 goto *ip; // expected-error {{indirect goto might cross protected scopes}}
John McCallddb0b4d2010-05-12 00:58:13 +000094 }
95 return 1;
96 }
97}
98
99namespace test6 {
100 struct C { C(); };
101
102 unsigned f(unsigned s0, unsigned s1, void **ip) {
103 static void *ips[] = { &&lbl1, &&lbl2, &&lbl3, &&lbl4 };
104 C c0;
105
106 goto *ip;
107 lbl1:
108 s0++;
109 goto *++ip;
110 lbl2:
111 s0 -= s1;
112 goto *++ip;
113 lbl3: {
114 unsigned tmp = s0;
115 s0 = s1;
116 s1 = tmp;
117 goto *++ip;
118 }
119 lbl4:
120 return s0;
121 }
122}
123
John McCalle46f62c2010-08-01 01:24:59 +0000124// C++0x says it's okay to skip non-trivial initializers on static
125// locals, and we implement that in '03 as well.
126namespace test7 {
127 struct C { C(); };
128
129 void test() {
130 goto foo;
131 static C c;
132 foo:
133 return;
134 }
135}
John McCall97ba4812010-08-02 23:33:14 +0000136
137// PR7789
138namespace test8 {
139 void test1(int c) {
140 switch (c) {
141 case 0:
142 int x = 56; // expected-note {{jump bypasses variable initialization}}
143 case 1: // expected-error {{switch case is in protected scope}}
144 x = 10;
145 }
146 }
147
148 void test2() {
149 goto l2; // expected-error {{goto into protected scope}}
150 l1: int x = 5; // expected-note {{jump bypasses variable initialization}}
151 l2: x++;
152 }
153}