On Wed, May 23, 2018 at 06:45:41PM +0200, Jakub Jelinek wrote: > On Wed, May 23, 2018 at 10:45:43AM -0400, Marek Polacek wrote: > > 2018-05-23 Marek Polacek <pola...@redhat.com> > > > > Implement P0614R1, Range-based for statements with initializer. > > * parser.c (cp_parser_range_based_for_with_init_p): New. > > (cp_parser_init_statement): Use it. Parse the optional init-statement > > for a range-based for loop. > > (cp_parser_skip_to_closing_parenthesis_1): Handle balancing ?:. > > > > * g++.dg/cpp2a/range-for1.C: New test. > > * g++.dg/cpp2a/range-for2.C: New test. > > * g++.dg/cpp2a/range-for3.C: New test. > > * g++.dg/cpp2a/range-for4.C: New test. > > * g++.dg/cpp2a/range-for5.C: New test. > > * g++.dg/cpp2a/range-for6.C: New test. > > * g++.dg/cpp2a/range-for7.C: New test. > > Could you please add some testcases that would test the handling of > structured bindings in these new forms of range for, like: > for (int i = 0; auto [ x, y ] : z) > but perhaps for completeness also in the init-stmt and perhaps both spots > too?
Sure. Tested on x86_64-linux, ok for trunk? 2018-05-23 Marek Polacek <pola...@redhat.com> * g++.dg/cpp2a/range-for8.C: New test. * g++.dg/cpp2a/range-for9.C: New test. * g++.dg/cpp2a/range-for10.C: New test. diff --git gcc/testsuite/g++.dg/cpp2a/range-for10.C gcc/testsuite/g++.dg/cpp2a/range-for10.C index e69de29bb2d..a0d0e6d085e 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for10.C +++ gcc/testsuite/g++.dg/cpp2a/range-for10.C @@ -0,0 +1,24 @@ +// P0614R1 +// { dg-do run } +// { dg-options "-std=c++2a" } + +struct A { int i; long long j; } a[64]; + +int +main () +{ + A b = { 1, 2 }; + for (auto & [ u, v ] : a) + { + u = 2; + v = 3; + } + + for (auto [x, y] = b; auto [ u, v ] : a) + if (y + u != x + v) + __builtin_abort (); + + for (auto [x, y] = b; auto & [ u, v ] : a) + if (y + u != x + v) + __builtin_abort (); +} diff --git gcc/testsuite/g++.dg/cpp2a/range-for8.C gcc/testsuite/g++.dg/cpp2a/range-for8.C index e69de29bb2d..204a63204ca 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for8.C +++ gcc/testsuite/g++.dg/cpp2a/range-for8.C @@ -0,0 +1,37 @@ +// P0614R1 +// { dg-do run } +// { dg-options "-std=c++2a" } + +struct A { int i; long long j; } a[64]; + +int +main () +{ + for (int i = 0; auto &x : a) + { + x.i = i; + x.j = 2 * i++; + } + for (auto & [ x, y ] : a) + { + x += 2; + y += 3; + } + for (int i = 0; const auto [ u, v ] : a) + { + if (u != i + 2 || v != 2 * i++ + 3) + __builtin_abort (); + } + for (int i = 0; auto [ x, y ] : a) + { + x += 4; + y += 5; + if (x != i + 6 || y != 2 * i++ + 8) + __builtin_abort (); + } + for (int i = 0; const auto x : a) + { + if (x.i != i + 2 || x.j != 2 * i++ + 3) + __builtin_abort (); + } +} diff --git gcc/testsuite/g++.dg/cpp2a/range-for9.C gcc/testsuite/g++.dg/cpp2a/range-for9.C index e69de29bb2d..74d71b67213 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for9.C +++ gcc/testsuite/g++.dg/cpp2a/range-for9.C @@ -0,0 +1,30 @@ +// P0614R1 +// { dg-do run } +// { dg-options "-std=c++2a" } + +struct A { int i, j; }; + +int +main () +{ + A a = { .i = 2, .j = 3 }; + int arr[] = { 1, 1, 1 }; + + for (auto & [ x, y ] = a; auto z : arr) + if (x + z != 3 || y + z != 4) + __builtin_abort (); + + for (int d = 1; auto &z : arr) + z += d; + + for (const auto [ x, y ] = a; auto z : arr) + if (x + z != 4 || y + z != 5) + __builtin_abort (); + + for (int d = 1; auto &z : arr) + z += d; + + for (auto [ x, y ] = a; auto z : arr) + if (x + z != 5 || y + z != 6) + __builtin_abort (); +}