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 |
Chandler Carruth | d9063c4 | 2013-09-04 17:35:07 +0000 | [diff] [blame] | 9 | with the :option:`-loop-convert` option of :program:`clang-modernize`. |
Edwin Vane | e4e7c24 | 2013-03-09 03:33:50 +0000 | [diff] [blame] | 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 | |
Edwin Vane | e4e7c24 | 2013-03-09 03:33:50 +0000 | [diff] [blame] | 20 | Risky |
Edwin Vane | 8d28646 | 2013-06-14 15:14:20 +0000 | [diff] [blame] | 21 | ----- |
Edwin Vane | e4e7c24 | 2013-03-09 03:33:50 +0000 | [diff] [blame] | 22 | |
| 23 | In loops where the container expression is more complex than just a |
| 24 | reference to a declared expression (a variable, function, enum, etc.), |
| 25 | and some part of it appears elsewhere in the loop, we lower our confidence |
| 26 | in the transformation due to the increased risk of changing semantics. |
| 27 | Transformations for these loops are marked as `risky`, and thus will only |
| 28 | be converted if the acceptable risk level is set to ``-risk=risky``. |
| 29 | |
| 30 | .. code-block:: c++ |
| 31 | |
| 32 | int arr[10][20]; |
| 33 | int l = 5; |
| 34 | |
| 35 | for (int j = 0; j < 20; ++j) |
| 36 | int k = arr[l][j] + l; // using l outside arr[l] is considered risky |
| 37 | |
| 38 | for (int i = 0; i < obj.getVector().size(); ++i) |
| 39 | obj.foo(10); // using 'obj' is considered risky |
| 40 | |
Edwin Vane | 3cb833d | 2013-03-26 16:44:29 +0000 | [diff] [blame] | 41 | See |
| 42 | :ref:`Range-based loops evaluate end() only once<IncorrectRiskyTransformation>` |
| 43 | for an example of an incorrect transformation when the maximum acceptable risk |
| 44 | level is set to `risky`. |
| 45 | |
Edwin Vane | e4e7c24 | 2013-03-09 03:33:50 +0000 | [diff] [blame] | 46 | Reasonable (Default) |
Edwin Vane | 8d28646 | 2013-06-14 15:14:20 +0000 | [diff] [blame] | 47 | -------------------- |
Edwin Vane | e4e7c24 | 2013-03-09 03:33:50 +0000 | [diff] [blame] | 48 | |
| 49 | If a loop calls ``.end()`` or ``.size()`` after each iteration, the |
| 50 | transformation for that loop is marked as `reasonable`, and thus will |
| 51 | be converted if the acceptable risk level is set to ``-risk=reasonable`` |
| 52 | (default) or higher. |
| 53 | |
| 54 | .. code-block:: c++ |
| 55 | |
| 56 | // using size() is considered reasonable |
| 57 | for (int i = 0; i < container.size(); ++i) |
| 58 | cout << container[i]; |
| 59 | |
| 60 | Safe |
Edwin Vane | 8d28646 | 2013-06-14 15:14:20 +0000 | [diff] [blame] | 61 | ---- |
Edwin Vane | e4e7c24 | 2013-03-09 03:33:50 +0000 | [diff] [blame] | 62 | |
| 63 | Any other loops that do not match the above criteria to be marked as |
| 64 | `risky` or `reasonable` are marked `safe`, and thus will be converted |
| 65 | if the acceptable risk level is set to ``-risk=safe`` or higher. |
| 66 | |
| 67 | .. code-block:: c++ |
| 68 | |
| 69 | int arr[] = {1,2,3}; |
| 70 | |
| 71 | for (int i = 0; i < 3; ++i) |
| 72 | cout << arr[i]; |
| 73 | |
| 74 | Example |
| 75 | ======= |
| 76 | |
| 77 | Original: |
| 78 | |
| 79 | .. code-block:: c++ |
| 80 | |
| 81 | const int N = 5; |
| 82 | int arr[] = {1,2,3,4,5}; |
| 83 | vector<int> v; |
| 84 | v.push_back(1); |
| 85 | v.push_back(2); |
| 86 | v.push_back(3); |
| 87 | |
| 88 | // safe transform |
| 89 | for (int i = 0; i < N; ++i) |
| 90 | cout << arr[i]; |
| 91 | |
| 92 | // reasonable transform |
| 93 | for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) |
| 94 | cout << *it;* |
| 95 | |
| 96 | // reasonable transform |
| 97 | for (int i = 0; i < v.size(); ++i) |
| 98 | cout << v[i]; |
| 99 | |
| 100 | After transformation with risk level set to ``-risk=reasonable`` (default): |
| 101 | |
| 102 | .. code-block:: c++ |
| 103 | |
| 104 | const int N = 5; |
| 105 | int arr[] = {1,2,3,4,5}; |
| 106 | vector<int> v; |
| 107 | v.push_back(1); |
| 108 | v.push_back(2); |
| 109 | v.push_back(3); |
| 110 | |
| 111 | // safe transform |
| 112 | for (auto & elem : arr) |
| 113 | cout << elem; |
| 114 | |
| 115 | // reasonable transform |
| 116 | for (auto & elem : v) |
| 117 | cout << elem; |
| 118 | |
| 119 | // reasonable transform |
| 120 | for (auto & elem : v) |
| 121 | cout << elem; |
| 122 | |
Edwin Vane | 3cb833d | 2013-03-26 16:44:29 +0000 | [diff] [blame] | 123 | Limitations |
| 124 | =========== |
| 125 | |
| 126 | There are certain situations where the tool may erroneously perform |
| 127 | transformations that remove information and change semantics. Users of the tool |
| 128 | should be aware of the behaviour and limitations of the transform outlined by |
| 129 | the cases below. |
| 130 | |
| 131 | Comments inside loop headers |
Edwin Vane | 8d28646 | 2013-06-14 15:14:20 +0000 | [diff] [blame] | 132 | ---------------------------- |
Edwin Vane | 3cb833d | 2013-03-26 16:44:29 +0000 | [diff] [blame] | 133 | |
| 134 | Comments inside the original loop header are ignored and deleted when |
| 135 | transformed. |
| 136 | |
| 137 | .. code-block:: c++ |
| 138 | |
| 139 | for (int i = 0; i < N; /* This will be deleted */ ++i) { } |
| 140 | |
| 141 | Range-based loops evaluate end() only once |
Edwin Vane | 8d28646 | 2013-06-14 15:14:20 +0000 | [diff] [blame] | 142 | ------------------------------------------ |
Edwin Vane | 3cb833d | 2013-03-26 16:44:29 +0000 | [diff] [blame] | 143 | |
| 144 | The C++11 range-based for loop calls ``.end()`` only once during the |
| 145 | initialization of the loop. If in the original loop ``.end()`` is called after |
| 146 | each iteration the semantics of the transformed loop may differ. |
| 147 | |
| 148 | .. code-block:: c++ |
| 149 | |
| 150 | // The following is semantically equivalent to the C++11 range-based for loop, |
| 151 | // therefore the semantics of the header will not change. |
| 152 | for (iterator it = container.begin(), e = container.end(); it != e; ++it) { } |
| 153 | |
| 154 | // Instead of calling .end() after each iteration, this loop will be |
| 155 | // transformed to call .end() only once during the initialization of the loop, |
| 156 | // which may affect semantics. |
| 157 | for (iterator it = container.begin(); it != container.end(); ++it) { } |
| 158 | |
| 159 | .. _IncorrectRiskyTransformation: |
| 160 | |
| 161 | As explained above, calling member functions of the container in the body |
| 162 | of the loop is considered `risky`. If the called member function modifies the |
| 163 | container the semantics of the converted loop will differ due to ``.end()`` |
| 164 | being called only once. |
| 165 | |
| 166 | .. code-block:: c++ |
| 167 | |
| 168 | bool flag = false; |
| 169 | for (vector<T>::iterator it = vec.begin(); it != vec.end(); ++it) { |
| 170 | // Add a copy of the first element to the end of the vector. |
| 171 | if (!flag) { |
| 172 | // This line makes this transformation 'risky'. |
| 173 | vec.push_back(*it); |
| 174 | flag = true; |
| 175 | } |
| 176 | cout << *it; |
| 177 | } |
| 178 | |
| 179 | The original code above prints out the contents of the container including the |
| 180 | newly added element while the converted loop, shown below, will only print the |
| 181 | original contents and not the newly added element. |
| 182 | |
| 183 | .. code-block:: c++ |
| 184 | |
| 185 | bool flag = false; |
| 186 | for (auto & elem : vec) { |
| 187 | // Add a copy of the first element to the end of the vector. |
| 188 | if (!flag) { |
| 189 | // This line makes this transformation 'risky' |
| 190 | vec.push_back(elem); |
| 191 | flag = true; |
| 192 | } |
| 193 | cout << elem; |
| 194 | } |
| 195 | |
| 196 | Semantics will also be affected if ``.end()`` has side effects. For example, in |
| 197 | the case where calls to ``.end()`` are logged the semantics will change in the |
| 198 | transformed loop if ``.end()`` was originally called after each iteration. |
| 199 | |
| 200 | .. code-block:: c++ |
| 201 | |
| 202 | iterator end() { |
| 203 | num_of_end_calls++; |
| 204 | return container.end(); |
| 205 | } |
| 206 | |
| 207 | Overloaded operator->() with side effects |
Edwin Vane | 8d28646 | 2013-06-14 15:14:20 +0000 | [diff] [blame] | 208 | ----------------------------------------- |
Edwin Vane | 3cb833d | 2013-03-26 16:44:29 +0000 | [diff] [blame] | 209 | |
| 210 | Similarly, if ``operator->()`` was overloaded to have side effects, such as |
| 211 | logging, the semantics will change. If the iterator's ``operator->()`` was used |
| 212 | in the original loop it will be replaced with ``<container element>.<member>`` |
| 213 | instead due to the implicit dereference as part of the range-based for loop. |
| 214 | Therefore any side effect of the overloaded ``operator->()`` will no longer be |
| 215 | performed. |
| 216 | |
| 217 | .. code-block:: c++ |
| 218 | |
| 219 | for (iterator it = c.begin(); it != c.end(); ++it) { |
| 220 | it->func(); // Using operator->() |
| 221 | } |
| 222 | // Will be transformed to: |
| 223 | for (auto & elem : c) { |
| 224 | elem.func(); // No longer using operator->() |
| 225 | } |
| 226 | |
| 227 | Pointers and references to containers |
Edwin Vane | 8d28646 | 2013-06-14 15:14:20 +0000 | [diff] [blame] | 228 | ------------------------------------- |
Edwin Vane | 3cb833d | 2013-03-26 16:44:29 +0000 | [diff] [blame] | 229 | |
| 230 | While most of the transform's risk analysis is dedicated to determining whether |
| 231 | the iterator or container was modified within the loop, it is possible to |
| 232 | circumvent the analysis by accessing and modifying the container through a |
| 233 | pointer or reference. |
| 234 | |
| 235 | If the container were directly used instead of using the pointer or reference |
| 236 | the following transformation would have only been applied at the ``-risk=risky`` |
| 237 | level since calling a member function of the container is considered `risky`. |
| 238 | The transform cannot identify expressions associated with the container that are |
| 239 | different than the one used in the loop header, therefore the transformation |
| 240 | below ends up being performed at the ``-risk=safe`` level. |
| 241 | |
| 242 | .. code-block:: c++ |
| 243 | |
| 244 | vector<int> vec; |
| 245 | |
| 246 | vector<int> *ptr = &vec; |
| 247 | vector<int> &ref = vec; |
| 248 | |
| 249 | for (vector<int>::iterator it = vec.begin(), e = vec.end(); it != e; ++it) { |
| 250 | if (!flag) { |
| 251 | // Accessing and modifying the container is considered risky, but the risk |
| 252 | // level is not raised here. |
| 253 | ptr->push_back(*it); |
| 254 | ref.push_back(*it); |
| 255 | flag = true; |
| 256 | } |
| 257 | } |