Debian Codegraph @

gcc/testsuite/g++.dg/parse/pr18770.C

raw
1 /* { dg-do compile } */
2
3 /* The ISO C++ standard says, in Section 3.3.2 sentence 4, that a name
4 declared in the for-init-statement or in the condition of an if, for
5 while, or switch statement can't be redeclared in the outermost block
6 of the controlled statement. (Note, this is not an error in C.) */
7
8 extern void foo (int);
9 extern int j;
10
11 void
12 e0 (void)
13 {
14 for (int i = 0; // { dg-message "previously declared here" "prev" }
15 i < 10; ++i)
16 {
17 int i = 2; // { dg-error "redeclaration" "redecl" }
18 foo (i);
19 }
20 }
21
22 void
23 e1 (void)
24 {
25 int i;
26 for (i = 0;
27 int k = j; i++) // { dg-message "previously declared here" "prev" }
28 {
29 int k = 2; // { dg-error "redeclaration" "redecl" }
30 foo (k);
31 }
32 }
33
34 void
35 e2 (void)
36 {
37 if (int i = 1) // { dg-message "previously declared here" "prev" }
38 {
39 int i = 2; // { dg-error "redeclaration" "redecl" }
40 foo (i);
41 }
42 }
43
44 void
45 e3 (void)
46 {
47 if (int i = 1) // { dg-message "previously declared here" "prev" }
48 {
49 foo (i);
50 }
51 else
52 {
53 int i = 2; // { dg-error "redeclaration" "redecl" }
54 foo (i);
55 }
56 }
57
58 void
59 e4 (void)
60 {
61 while (int i = 1) // { dg-message "previously declared here" "prev" }
62 {
63 int i = 2; // { dg-error "redeclaration" "redecl" }
64 foo (i);
65 }
66 }
67
68 void
69 e5 (void)
70 {
71 switch (int i = j) // { dg-message "previously declared here" "prev" }
72 {
73 int i; // { dg-error "redeclaration" "redecl" }
74 default:
75 {
76 i = 2;
77 foo (i);
78 }
79 }
80 }
81
82 void
83 f0 (void)
84 {
85 for (int i = 0; i < 10; ++i)
86 {
87 foo (i);
88 {
89 int i = 2; // OK, not outermost block.
90 foo (i);
91 }
92 }
93 }
94
95 void
96 f1 (void)
97 {
98 int i;
99 for (i = 0; int k = j; i++)
100 {
101 foo (k);
102 {
103 int k = 2; // OK, not outermost block.
104 foo (k);
105 }
106 }
107 }
108
109 void
110 f2 (void)
111 {
112 if (int i = 1)
113 {
114 foo (i);
115 {
116 int i = 2; // OK, not outermost block.
117 foo (i);
118 }
119 }
120 }
121
122 void
123 f3 (void)
124 {
125 if (int i = 1)
126 {
127 foo (i);
128 }
129 else
130 {
131 foo (i+2);
132 {
133 int i = 2; // OK, not outermost block.
134 foo (i);
135 }
136 }
137 }
138
139 void
140 f4 (void)
141 {
142 while (int i = 1)
143 {
144 foo (i);
145 {
146 int i = 2; // OK, not outermost block.
147 foo (i);
148 }
149 }
150 }
151
152 void
153 f5 (void)
154 {
155 switch (int i = j)
156 {
157 default:
158 {
159 int i = 2; // OK, not outermost block.
160 foo (i);
161 }
162 }
163 }
164
165 void
166 f6 (void)
167 {
168 int i = 1;
169
170 for (int j = 0; j < 10; j++)
171 {
172 int i = 2; // OK, not variable from for-init.
173 foo (i);
174 }
175 }