Edwin Vane | e4e7c24 | 2013-03-09 03:33:50 +0000 | [diff] [blame] | 1 | .. index:: Loop Convert Transform |
| 2 | |
| 3 | ====================== |
| 4 | Loop Convert Transform |
| 5 | ====================== |
| 6 | |
| 7 | The Loop Convert Transform is a transformation to convert ``for(...; ...; |
| 8 | ...)`` loops to use the new range-based loops in C++11. The transform is enabled |
| 9 | with the :option:`-loop-convert` option of :program:`cpp11-migrate`. |
| 10 | |
| 11 | Three kinds of loops can be converted: |
| 12 | |
| 13 | - Loops over statically allocated arrays |
| 14 | - Loops over containers, using iterators |
| 15 | - Loops over array-like containers, using ``operator[]`` and ``at()`` |
| 16 | |
| 17 | Risk |
| 18 | ==== |
| 19 | |
| 20 | TODO: Add code examples for which incorrect transformations are performed |
| 21 | when the risk level is set to "Risky" or "Reasonable". |
| 22 | |
| 23 | Risky |
| 24 | ^^^^^ |
| 25 | |
| 26 | In loops where the container expression is more complex than just a |
| 27 | reference to a declared expression (a variable, function, enum, etc.), |
| 28 | and some part of it appears elsewhere in the loop, we lower our confidence |
| 29 | in the transformation due to the increased risk of changing semantics. |
| 30 | Transformations for these loops are marked as `risky`, and thus will only |
| 31 | be converted if the acceptable risk level is set to ``-risk=risky``. |
| 32 | |
| 33 | .. code-block:: c++ |
| 34 | |
| 35 | int arr[10][20]; |
| 36 | int l = 5; |
| 37 | |
| 38 | for (int j = 0; j < 20; ++j) |
| 39 | int k = arr[l][j] + l; // using l outside arr[l] is considered risky |
| 40 | |
| 41 | for (int i = 0; i < obj.getVector().size(); ++i) |
| 42 | obj.foo(10); // using 'obj' is considered risky |
| 43 | |
| 44 | Reasonable (Default) |
| 45 | ^^^^^^^^^^^^^^^^^^^^ |
| 46 | |
| 47 | If a loop calls ``.end()`` or ``.size()`` after each iteration, the |
| 48 | transformation for that loop is marked as `reasonable`, and thus will |
| 49 | be converted if the acceptable risk level is set to ``-risk=reasonable`` |
| 50 | (default) or higher. |
| 51 | |
| 52 | .. code-block:: c++ |
| 53 | |
| 54 | // using size() is considered reasonable |
| 55 | for (int i = 0; i < container.size(); ++i) |
| 56 | cout << container[i]; |
| 57 | |
| 58 | Safe |
| 59 | ^^^^ |
| 60 | |
| 61 | Any other loops that do not match the above criteria to be marked as |
| 62 | `risky` or `reasonable` are marked `safe`, and thus will be converted |
| 63 | if the acceptable risk level is set to ``-risk=safe`` or higher. |
| 64 | |
| 65 | .. code-block:: c++ |
| 66 | |
| 67 | int arr[] = {1,2,3}; |
| 68 | |
| 69 | for (int i = 0; i < 3; ++i) |
| 70 | cout << arr[i]; |
| 71 | |
| 72 | Example |
| 73 | ======= |
| 74 | |
| 75 | Original: |
| 76 | |
| 77 | .. code-block:: c++ |
| 78 | |
| 79 | const int N = 5; |
| 80 | int arr[] = {1,2,3,4,5}; |
| 81 | vector<int> v; |
| 82 | v.push_back(1); |
| 83 | v.push_back(2); |
| 84 | v.push_back(3); |
| 85 | |
| 86 | // safe transform |
| 87 | for (int i = 0; i < N; ++i) |
| 88 | cout << arr[i]; |
| 89 | |
| 90 | // reasonable transform |
| 91 | for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) |
| 92 | cout << *it;* |
| 93 | |
| 94 | // reasonable transform |
| 95 | for (int i = 0; i < v.size(); ++i) |
| 96 | cout << v[i]; |
| 97 | |
| 98 | After transformation with risk level set to ``-risk=reasonable`` (default): |
| 99 | |
| 100 | .. code-block:: c++ |
| 101 | |
| 102 | const int N = 5; |
| 103 | int arr[] = {1,2,3,4,5}; |
| 104 | vector<int> v; |
| 105 | v.push_back(1); |
| 106 | v.push_back(2); |
| 107 | v.push_back(3); |
| 108 | |
| 109 | // safe transform |
| 110 | for (auto & elem : arr) |
| 111 | cout << elem; |
| 112 | |
| 113 | // reasonable transform |
| 114 | for (auto & elem : v) |
| 115 | cout << elem; |
| 116 | |
| 117 | // reasonable transform |
| 118 | for (auto & elem : v) |
| 119 | cout << elem; |
| 120 | |