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