blob: 708b0627fff9ce1ffa09217a3515de9b06c91663 [file] [log] [blame]
Edwin Vanee4e7c242013-03-09 03:33:50 +00001.. index:: Loop Convert Transform
2
3======================
4Loop Convert Transform
5======================
6
7The 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
9with the :option:`-loop-convert` option of :program:`cpp11-migrate`.
10
11Three 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
17Risk
18====
19
20TODO: Add code examples for which incorrect transformations are performed
21when the risk level is set to "Risky" or "Reasonable".
22
23Risky
24^^^^^
25
26In loops where the container expression is more complex than just a
27reference to a declared expression (a variable, function, enum, etc.),
28and some part of it appears elsewhere in the loop, we lower our confidence
29in the transformation due to the increased risk of changing semantics.
30Transformations for these loops are marked as `risky`, and thus will only
31be 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
44Reasonable (Default)
45^^^^^^^^^^^^^^^^^^^^
46
47If a loop calls ``.end()`` or ``.size()`` after each iteration, the
48transformation for that loop is marked as `reasonable`, and thus will
49be 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
58Safe
59^^^^
60
61Any 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
63if 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
72Example
73=======
74
75Original:
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
98After 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