Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libgo / go / go / printer / testdata / statements.golden
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package statements
6
7 var expr bool
8
9 func use(x interface{}) {}
10
11 // Formatting of multi-line return statements.
12 func _f() {
13         return
14         return x, y, z
15         return T{}
16         return T{1, 2, 3},
17                 x, y, z
18         return T{1, 2, 3},
19                 x, y,
20                 z
21         return T{1,
22                 2,
23                 3}
24         return T{1,
25                 2,
26                 3,
27         }
28         return T{
29                 1,
30                 2,
31                 3}
32         return T{
33                 1,
34                 2,
35                 3,
36         }
37         return T{
38                 1,
39                 T{1, 2, 3},
40                 3,
41         }
42         return T{
43                 1,
44                 T{1,
45                         2, 3},
46                 3,
47         }
48         return T{
49                 1,
50                 T{1,
51                         2,
52                         3},
53                 3,
54         }
55         return T{
56                 1,
57                 2,
58         }, nil
59         return T{
60                         1,
61                         2,
62                 },
63                 T{
64                         x:      3,
65                         y:      4,
66                 }, nil
67         return T{
68                         1,
69                         2,
70                 },
71                 nil
72         return T{
73                         1,
74                         2,
75                 },
76                 T{
77                         x:      3,
78                         y:      4,
79                 },
80                 nil
81         return x + y +
82                 z
83         return func() {}
84         return func() {
85                         _ = 0
86                 }, T{
87                         1, 2,
88                 }
89         return func() {
90                 _ = 0
91         }
92         return func() T {
93                 return T{
94                         1, 2,
95                 }
96         }
97 }
98
99 // Formatting of multi-line returns: test cases from issue 1207.
100 func F() (*T, os.Error) {
101         return &T{
102                         X:      1,
103                         Y:      2,
104                 },
105                 nil
106 }
107
108 func G() (*T, *T, os.Error) {
109         return &T{
110                         X:      1,
111                         Y:      2,
112                 },
113                 &T{
114                         X:      3,
115                         Y:      4,
116                 },
117                 nil
118 }
119
120 func _() interface{} {
121         return &fileStat{
122                 name:           basename(file.name),
123                 size:           mkSize(d.FileSizeHigh, d.FileSizeLow),
124                 modTime:        mkModTime(d.LastWriteTime),
125                 mode:           mkMode(d.FileAttributes),
126                 sys:            mkSysFromFI(&d),
127         }, nil
128 }
129
130 // Formatting of if-statement headers.
131 func _() {
132         if true {
133         }
134         if true {
135         }       // no semicolon printed
136         if expr {
137         }
138         if expr {
139         }       // no semicolon printed
140         if expr {
141         }       // no parens printed
142         if expr {
143         }       // no semicolon and parens printed
144         if x := expr; true {
145                 use(x)
146         }
147         if x := expr; expr {
148                 use(x)
149         }
150 }
151
152 // Formatting of switch-statement headers.
153 func _() {
154         switch {
155         }
156         switch {
157         }       // no semicolon printed
158         switch expr {
159         }
160         switch expr {
161         }       // no semicolon printed
162         switch expr {
163         }       // no parens printed
164         switch expr {
165         }       // no semicolon and parens printed
166         switch x := expr; {
167         default:
168                 use(
169                         x)
170         }
171         switch x := expr; expr {
172         default:
173                 use(x)
174         }
175 }
176
177 // Formatting of switch statement bodies.
178 func _() {
179         switch {
180         }
181
182         switch x := 0; x {
183         case 1:
184                 use(x)
185                 use(x)  // followed by an empty line
186
187         case 2: // followed by an empty line
188
189                 use(x)  // followed by an empty line
190
191         case 3: // no empty lines
192                 use(x)
193                 use(x)
194         }
195
196         switch x {
197         case 0:
198                 use(x)
199         case 1: // this comment should have no effect on the previous or next line
200                 use(x)
201         }
202
203         switch x := 0; x {
204         case 1:
205                 x = 0
206                 // this comment should be indented
207         case 2:
208                 x = 0
209         // this comment should not be indented, it is aligned with the next case
210         case 3:
211                 x = 0
212                 /* indented comment
213                    aligned
214                    aligned
215                 */
216                 // bla
217                 /* and more */
218         case 4:
219                 x = 0
220         /* not indented comment
221            aligned
222            aligned
223         */
224         // bla
225         /* and more */
226         case 5:
227         }
228 }
229
230 // Formatting of selected select statements.
231 func _() {
232         select {}
233         select { /* this comment should not be tab-aligned because the closing } is on the same line */
234         }
235         select {        /* this comment should be tab-aligned */
236         }
237         select {        // this comment should be tab-aligned
238         }
239         select {
240         case <-c:
241         }
242 }
243
244 // Formatting of for-statement headers for single-line for-loops.
245 func _() {
246         for {
247         }
248         for expr {
249         }
250         for expr {
251         }       // no parens printed
252         for {
253         }       // no semicolons printed
254         for x := expr; ; {
255                 use(x)
256         }
257         for expr {
258         }       // no semicolons printed
259         for expr {
260         }       // no semicolons and parens printed
261         for ; ; expr = false {
262         }
263         for x := expr; expr; {
264                 use(x)
265         }
266         for x := expr; ; expr = false {
267                 use(x)
268         }
269         for ; expr; expr = false {
270         }
271         for x := expr; expr; expr = false {
272                 use(x)
273         }
274         for x := range []int{} {
275                 use(x)
276         }
277         for x := range []int{} {
278                 use(x)
279         }       // no parens printed
280 }
281
282 // Formatting of for-statement headers for multi-line for-loops.
283 func _() {
284         for {
285         }
286         for expr {
287         }
288         for expr {
289         }       // no parens printed
290         for {
291         }       // no semicolons printed
292         for x := expr; ; {
293                 use(x)
294         }
295         for expr {
296         }       // no semicolons printed
297         for expr {
298         }       // no semicolons and parens printed
299         for ; ; expr = false {
300         }
301         for x := expr; expr; {
302                 use(x)
303         }
304         for x := expr; ; expr = false {
305                 use(x)
306         }
307         for ; expr; expr = false {
308         }
309         for x := expr; expr; expr = false {
310                 use(x)
311         }
312         for x := range []int{} {
313                 use(x)
314         }
315         for x := range []int{} {
316                 use(x)
317         }       // no parens printed
318 }
319
320 // Formatting of selected short single- and multi-line statements.
321 func _() {
322         if cond {
323         }
324         if cond {
325         }       // multiple lines
326         if cond {
327         } else {
328         }       // else clause always requires multiple lines
329
330         for {
331         }
332         for i := 0; i < len(a); 1++ {
333         }
334         for i := 0; i < len(a); 1++ {
335                 a[i] = i
336         }
337         for i := 0; i < len(a); 1++ {
338                 a[i] = i
339         }       // multiple lines
340
341         for i := range a {
342         }
343         for i := range a {
344                 a[i] = i
345         }
346         for i := range a {
347                 a[i] = i
348         }       // multiple lines
349
350         go func() {
351                 for {
352                         a <- <-b
353                 }
354         }()
355         defer func() {
356                 if x := recover(); x != nil {
357                         err = fmt.Sprintf("error: %s", x.msg)
358                 }
359         }()
360 }
361
362 // Don't remove mandatory parentheses around composite literals in control clauses.
363 func _() {
364         // strip parentheses - no composite literals or composite literals don't start with a type name
365         if x {
366         }
367         if x {
368         }
369         if []T{} {
370         }
371         if []T{} {
372         }
373         if []T{} {
374         }
375
376         for x {
377         }
378         for x {
379         }
380         for []T{} {
381         }
382         for []T{} {
383         }
384         for []T{} {
385         }
386
387         switch x {
388         }
389         switch x {
390         }
391         switch []T{} {
392         }
393         switch []T{} {
394         }
395
396         for _ = range []T{T{42}} {
397         }
398
399         // leave parentheses - composite literals start with a type name
400         if (T{}) {
401         }
402         if (T{}) {
403         }
404         if (T{}) {
405         }
406
407         for (T{}) {
408         }
409         for (T{}) {
410         }
411         for (T{}) {
412         }
413
414         switch (T{}) {
415         }
416         switch (T{}) {
417         }
418
419         for _ = range (T1{T{42}}) {
420         }
421
422         if x == (T{42}[0]) {
423         }
424         if (x == T{42}[0]) {
425         }
426         if x == (T{42}[0]) {
427         }
428         if x == (T{42}[0]) {
429         }
430         if x == (T{42}[0]) {
431         }
432         if x == a+b*(T{42}[0]) {
433         }
434         if (x == a+b*T{42}[0]) {
435         }
436         if x == a+b*(T{42}[0]) {
437         }
438         if x == a+(b * (T{42}[0])) {
439         }
440         if x == a+b*(T{42}[0]) {
441         }
442         if (a + b*(T{42}[0])) == x {
443         }
444         if (a + b*(T{42}[0])) == x {
445         }
446
447         if struct{ x bool }{false}.x {
448         }
449         if (struct{ x bool }{false}.x) == false {
450         }
451         if struct{ x bool }{false}.x == false {
452         }
453 }
454
455 // Extra empty lines inside functions. Do respect source code line
456 // breaks between statement boundaries but print at most one empty
457 // line at a time.
458 func _() {
459
460         const _ = 0
461
462         const _ = 1
463         type _ int
464         type _ float
465
466         var _ = 0
467         var x = 1
468
469         // Each use(x) call below should have at most one empty line before and after.
470         // Known bug: The first use call may have more than one empty line before
471         //            (see go/printer/nodes.go, func linebreak).
472
473         use(x)
474
475         if x < x {
476
477                 use(x)
478
479         } else {
480
481                 use(x)
482
483         }
484 }
485
486 // Formatting around labels.
487 func _() {
488 L:
489 }
490
491 func _() {
492         // this comment should be indented
493 L:      // no semicolon needed
494 }
495
496 func _() {
497         switch 0 {
498         case 0:
499         L0:
500                 ;       // semicolon required
501         case 1:
502         L1:
503                 ;       // semicolon required
504         default:
505         L2:     // no semicolon needed
506         }
507 }
508
509 func _() {
510         f()
511 L1:
512         f()
513 L2:
514         ;
515 L3:
516 }
517
518 func _() {
519         // this comment should be indented
520 L:
521 }
522
523 func _() {
524 L:
525         _ = 0
526 }
527
528 func _() {
529         // this comment should be indented
530 L:
531         _ = 0
532 }
533
534 func _() {
535         for {
536         L1:
537                 _ = 0
538         L2:
539                 _ = 0
540         }
541 }
542
543 func _() {
544         // this comment should be indented
545         for {
546         L1:
547                 _ = 0
548         L2:
549                 _ = 0
550         }
551 }
552
553 func _() {
554         if true {
555                 _ = 0
556         }
557         _ = 0   // the indentation here should not be affected by the long label name
558 AnOverlongLabel:
559         _ = 0
560
561         if true {
562                 _ = 0
563         }
564         _ = 0
565
566 L:
567         _ = 0
568 }
569
570 func _() {
571         for {
572                 goto L
573         }
574 L:
575
576         MoreCode()
577 }
578
579 func _() {
580         for {
581                 goto L
582         }
583 L:      // A comment on the same line as the label, followed by a single empty line.
584         // Known bug: There may be more than one empty line before MoreCode()
585         //            (see go/printer/nodes.go, func linebreak).
586
587         MoreCode()
588 }
589
590 func _() {
591         for {
592                 goto L
593         }
594 L:
595
596         // There should be a single empty line before this comment.
597         MoreCode()
598 }
599
600 func _() {
601         for {
602                 goto AVeryLongLabelThatShouldNotAffectFormatting
603         }
604 AVeryLongLabelThatShouldNotAffectFormatting:
605         // There should be a single empty line after this comment.
606
607         // There should be a single empty line before this comment.
608         MoreCode()
609 }
610
611 // Formatting of empty statements.
612 func _() {
613
614 }
615
616 func _() {
617 }
618
619 func _() {
620 }
621
622 func _() {
623         f()
624 }
625
626 func _() {
627 L:
628         ;
629 }
630
631 func _() {
632 L:
633         ;
634         f()
635 }