1 // Test visualization of general branch constructs in C.
2 
3 
4 
5 
6 
simple_loops()7 void simple_loops() {
8   int i;
9   for (i = 0; i < 100; ++i) {
10   }
11   while (i > 0)
12     i--;
13   do {} while (i++ < 75);
14 
15 }
16 
conditionals()17 void conditionals() {
18   for (int i = 0; i < 100; ++i) {
19     if (i % 2) {
20       if (i) {}
21     } else if (i % 3) {
22       if (i) {}
23     } else {
24       if (i) {}
25     }
26 
27     if (1 && i) {}
28     if (0 || i) {}
29   }
30 
31 }
32 
early_exits()33 void early_exits() {
34   int i = 0;
35 
36   if (i) {}
37 
38   while (i < 100) {
39     i++;
40     if (i > 50)
41       break;
42     if (i % 2)
43       continue;
44   }
45 
46   if (i) {}
47 
48   do {
49     if (i > 75)
50       return;
51     else
52       i++;
53   } while (i < 100);
54 
55   if (i) {}
56 
57 }
58 
jumps()59 void jumps() {
60   int i;
61 
62   for (i = 0; i < 2; ++i) {
63     goto outofloop;
64     // Never reached -> no weights
65     if (i) {}
66   }
67 
68 outofloop:
69   if (i) {}
70 
71   goto loop1;
72 
73   while (i) {
74   loop1:
75     if (i) {}
76   }
77 
78   goto loop2;
79 first:
80 second:
81 third:
82   i++;
83   if (i < 3)
84     goto loop2;
85 
86   while (i < 3) {
87   loop2:
88     switch (i) {
89     case 0:
90       goto first;
91     case 1:
92       goto second;
93     case 2:
94       goto third;
95     }
96   }
97 
98   for (i = 0; i < 10; ++i) {
99     goto withinloop;
100     // never reached -> no weights
101     if (i) {}
102   withinloop:
103     if (i) {}
104   }
105 
106 }
107 
switches()108 void switches() {
109   static int weights[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
110 
111   // No cases -> no weights
112   switch (weights[0]) {
113   default:
114     break;
115   }
116 
117   for (int i = 0, len = sizeof(weights) / sizeof(weights[0]); i < len; ++i) {
118     switch (i[weights]) {
119     case 1:
120       if (i) {}
121       // fallthrough
122     case 2:
123       if (i) {}
124       break;
125     case 3:
126       if (i) {}
127       continue;
128     case 4:
129       if (i) {}
130       switch (i) {
131       case 6 ... 9:
132         if (i) {}
133         continue;
134       }
135 
136     default:
137       if (i == len - 1)
138         return;
139     }
140   }
141 
142   // Never reached -> no weights
143   if (weights[0]) {}
144 
145 }
146 
big_switch()147 void big_switch() {
148   for (int i = 0; i < 32; ++i) {
149     switch (1 << i) {
150     case (1 << 0):
151       if (i) {}
152       // fallthrough
153     case (1 << 1):
154       if (i) {}
155       break;
156     case (1 << 2) ... (1 << 12):
157       if (i) {}
158       break;
159       // The branch for the large case range above appears after the case body.
160 
161     case (1 << 13):
162       if (i) {}
163       break;
164     case (1 << 14) ... (1 << 28):
165       if (i) {}
166       break;
167     // The branch for the large case range above appears after the case body.
168 
169     case (1 << 29) ... ((1 << 29) + 1):
170       if (i) {}
171       break;
172     default:
173       if (i) {}
174       break;
175     }
176   }
177 
178 }
179 
boolean_operators()180 void boolean_operators() {
181   int v;
182   for (int i = 0; i < 100; ++i) {
183     v = i % 3 || i;
184 
185     v = i % 3 && i;
186 
187     v = i % 3 || i % 2 || i;
188 
189     v = i % 2 && i % 3 && i;
190   }
191 
192 }
193 
boolop_loops()194 void boolop_loops() {
195   int i = 100;
196 
197   while (i && i > 50)
198     i--;
199 
200   while ((i % 2) || (i > 0))
201     i--;
202 
203   for (i = 100; i && i > 50; --i);
204 
205   for (; (i % 2) || (i > 0); --i);
206 
207 }
208 
conditional_operator()209 void conditional_operator() {
210   int i = 100;
211 
212   int j = i < 50 ? i : 1;
213 
214   int k = i ?: 0;
215 
216 }
217 
do_fallthrough()218 void do_fallthrough() {
219   for (int i = 0; i < 10; ++i) {
220     int j = 0;
221     do {
222       // The number of exits out of this do-loop via the break statement
223       // exceeds the counter value for the loop (which does not include the
224       // fallthrough count). Make sure that does not violate any assertions.
225       if (i < 8) break;
226       j++;
227     } while (j < 2);
228   }
229 }
230 
static_func()231 static void static_func() {
232   for (int i = 0; i < 10; ++i) {
233   }
234 }
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
main(int argc,const char * argv[])245 int main(int argc, const char *argv[]) {
246   simple_loops();
247   conditionals();
248   early_exits();
249   jumps();
250   switches();
251   big_switch();
252   boolean_operators();
253   boolop_loops();
254   conditional_operator();
255   do_fallthrough();
256   static_func();
257   extern void __llvm_profile_write_file();
258   __llvm_profile_write_file();
259   return 0;
260 }
261