blob: 48ad12b41914328085f0f13202806e0817e58696 [file] [log] [blame]
Edwin Vane30f70042013-02-25 20:37:03 +00001===========================
2cpp11-migrate User's Manual
3===========================
4
Edwin Vane3b9f08d2013-03-08 23:26:00 +00005.. toctree::
6 :hidden:
7
8 UseAutoTransform
9
Edwin Vane30f70042013-02-25 20:37:03 +000010:program:`cpp11-migrate` is a standalone tool used to automatically convert
11C++98 and C++03 code to use features of the new C++11 standard where
12appropriate.
13
14Basic Usage
15===========
16
17``cpp11-migrate [options] <source0> [... <sourceN>]``
18
19``<source0>...`` specify the paths of files in the CMake source tree,
20with the same requirements as other tools built on LibTooling.
21
22Command Line Options
23--------------------
24
25.. option:: -fatal-assembler-warnings
26
27 Treat all compiler warnings as errors.
28
29.. option:: -help
30
31 Displays tool usage instructions and command line options.
32
33.. option:: -loop-convert
34
35 Makes use of C++11 range-based for loops where possible.
36 See :ref:`loop-convert-transformation`.
37
Edwin Vane3b9f08d2013-03-08 23:26:00 +000038.. option:: -use-auto
39
40 Replace the type specifier of variable declarations with the ``auto`` type
41 specifier. See :doc:`UseAutoTransform`.
42
Edwin Vane30f70042013-02-25 20:37:03 +000043.. option:: -p=<build-path>
44
45 ``<build-path>`` is a CMake build directory containing a file named
46 ``compile_commands.json`` which provides compiler options for building
47 each source file and can be generated by specifying
48 ``-DCMAKE_EXPORT_COMPILE_COMMANDS`` when running CMake. If ``<build-path>``
49 is not provided the ``compile_commands.json`` file is searched for through
50 all parent directories.
51
52 Alternatively, one can provide compile options to be applied to every
53 source file after the optional ``--``.
54
55.. option:: -risk=<risk-level>
56
57 Some transformations may cause a change in semantics. In such cases the
58 maximum acceptable risk level specified through the ``-risk`` command
59 line option decides whether or not a transformation is applied.
60
61 Three different risk level options are available:
62
63 ``-risk=safe``
64 Perform only safe transformations.
65 ``-risk=reasonable`` (default)
66 Enable transformations that may change semantics.
67 ``-risk=risky``
68 Enable transformations that are likely to change semantics.
69
70 See :ref:`transformations` for further details for
71 risk in individual transformations.
72
73.. option:: -use-nullptr
74
75 Makes use of the new C++11 keyword ``nullptr`` where possible.
76 See :ref:`nullptr-convert-transformation`.
77
78.. option:: -version
79
80 Displays the version information of this tool.
81
82.. _transformations:
83
84Transformations
85===============
86
87.. _loop-convert-transformation:
88
89Loop Convert
90------------
91
92Loop convert is a transformation to convert ``for(...; ...; ...)`` loops to use
93the new range-based loops in C++11.
94
95Three kinds of loops can be converted:
96
97- Loops over statically allocated arrays
98- Loops over containers, using iterators
99- Loops over array-like containers, using ``operator[]`` and ``at()``
100
101Risk
102^^^^
103
104TODO: Add code examples for which incorrect transformations are performed
105when the risk level is set to "Risky" or "Reasonable".
106
107Risky
108"""""
109
110In loops where the container expression is more complex than just a
111reference to a declared expression (a variable, function, enum, etc.),
112and some part of it appears elsewhere in the loop, we lower our confidence
113in the transformation due to the increased risk of changing semantics.
114Transformations for these loops are marked as `risky`, and thus will only
115be converted if the acceptable risk level is set to ``-risk=risky``.
116
117.. code-block:: c++
118
119 int arr[10][20];
120 int l = 5;
121
122 for (int j = 0; j < 20; ++j)
123 int k = arr[l][j] + l; // using l outside arr[l] is considered risky
124
125 for (int i = 0; i < obj.getVector().size(); ++i)
126 obj.foo(10); // using 'obj' is considered risky
127
128Reasonable (Default)
129""""""""""""""""""""
130
131If a loop calls ``.end()`` or ``.size()`` after each iteration, the
132transformation for that loop is marked as `reasonable`, and thus will
133be converted if the acceptable risk level is set to ``-risk=reasonable``
134(default) or higher.
135
136.. code-block:: c++
137
138 // using size() is considered reasonable
139 for (int i = 0; i < container.size(); ++i)
140 cout << container[i];
141
142Safe
143""""
144
145Any other loops that do not match the above criteria to be marked as
146`risky` or `reasonable` are marked `safe`, and thus will be converted
147if the acceptable risk level is set to ``-risk=safe`` or higher.
148
149.. code-block:: c++
150
151 int arr[] = {1,2,3};
152
153 for (int i = 0; i < 3; ++i)
154 cout << arr[i];
155
156Example
157^^^^^^^
158
159Original
160""""""""
161
162.. code-block:: c++
163
164 const int N = 5;
165 int arr[] = {1,2,3,4,5};
166 vector<int> v;
167 v.push_back(1);
168 v.push_back(2);
169 v.push_back(3);
170
171 // safe transform
172 for (int i = 0; i < N; ++i)
173 cout << arr[i];
174
175 // reasonable transform
176 for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
177 cout << *it;
178
179 // reasonable transform
180 for (int i = 0; i < v.size(); ++i)
181 cout << v[i];
182
183
184After transformation
185""""""""""""""""""""
186With risk level set to ``-risk=reasonable`` (default).
187
188.. code-block:: c++
189
190 const int N = 5;
191 int arr[] = {1,2,3,4,5};
192 vector<int> v;
193 v.push_back(1);
194 v.push_back(2);
195 v.push_back(3);
196
197 // safe transform
198 for (auto & elem : arr)
199 cout << elem;
200
201 // reasonable transform
202 for (auto & elem : v)
203 cout << elem;
204
205 // reasonable transform
206 for (auto & elem : v)
207 cout << elem;
208
209.. _nullptr-convert-transformation:
210
211Nullptr Convert
212---------------
213
214Nullptr convert is a transformation to convert the usage of null pointer
215constants (eg. ``NULL``, ``0``) to use the new C++11 ``nullptr`` keyword.
216
217Example
218^^^^^^^
219
220Original
221""""""""
222
223.. code-block:: c++
224
225 void assignment() {
226 char *a = NULL;
227 char *b = 0;
228 char c = 0;
229 }
230
231 int *ret_ptr() {
232 return 0;
233 }
234
235
236After transformation
237""""""""""""""""""""
238
239.. code-block:: c++
240
241 void assignment() {
242 char *a = nullptr;
243 char *b = nullptr;
244 char c = 0;
245 }
246
247 int *ret_ptr() {
248 return nullptr;
249 }