Chandler Carruth | d9063c4 | 2013-09-04 17:35:07 +0000 | [diff] [blame] | 1 | .. index:: clang-modernize |
| 2 | |
Alexander Kornienko | f4e8b92 | 2015-09-10 09:42:01 +0000 | [diff] [blame] | 3 | .. note:: |
| 4 | |
| 5 | **Deprecation** |
| 6 | |
| 7 | As of September 2015 all :program:`clang-modernize` transforms have been |
| 8 | ported to :doc:`clang-tidy/index`. :program:`clang-modernize` is deprecated |
| 9 | and is going to be removed soon. |
| 10 | |
| 11 | |
Chandler Carruth | d9063c4 | 2013-09-04 17:35:07 +0000 | [diff] [blame] | 12 | ================================== |
| 13 | Clang C++ Modernizer User's Manual |
| 14 | ================================== |
| 15 | |
| 16 | .. toctree:: |
| 17 | :hidden: |
| 18 | |
| 19 | UseAutoTransform |
| 20 | UseNullptrTransform |
| 21 | LoopConvertTransform |
| 22 | AddOverrideTransform |
| 23 | PassByValueTransform |
| 24 | ReplaceAutoPtrTransform |
| 25 | ModernizerUsage |
Edwin Vane | 5a816f0 | 2013-09-06 19:27:19 +0000 | [diff] [blame] | 26 | cpp11-migrate |
| 27 | MigratorUsage |
Chandler Carruth | d9063c4 | 2013-09-04 17:35:07 +0000 | [diff] [blame] | 28 | |
| 29 | :program:`clang-modernize` is a standalone tool used to automatically convert |
| 30 | C++ code written against old standards to use features of the newest C++ |
| 31 | standard where appropriate. |
| 32 | |
| 33 | Getting Started |
| 34 | =============== |
| 35 | |
| 36 | To build from source: |
| 37 | |
| 38 | 1. Read `Getting Started with the LLVM System`_ and `Clang Tools |
| 39 | Documentation`_ for information on getting sources for LLVM, Clang, and |
| 40 | Clang Extra Tools. |
| 41 | |
| 42 | 2. `Getting Started with the LLVM System`_ and `Building LLVM with CMake`_ give |
| 43 | directions for how to build. With sources all checked out into the |
| 44 | right place the LLVM build will build Clang Extra Tools and their |
| 45 | dependencies automatically. |
| 46 | |
| 47 | * If using CMake, you can also use the ``clang-modernize`` target to build |
| 48 | just the Modernizer and its dependencies. |
| 49 | |
| 50 | Before continuing, take a look at :doc:`ModernizerUsage` to see how to invoke |
| 51 | the Modernizer. |
| 52 | |
| 53 | Before running the Modernizer on code you'll need the arguments you'd normally |
| 54 | pass to the compiler. If you're migrating a single file with few compiler |
| 55 | arguments, it might be easier to pass the compiler args on the command line |
| 56 | after ``--``. If you don't have any compiler arguments then ``--`` is not needed. |
| 57 | If you're working with multiple files or even a single file with many compiler |
| 58 | args, it's probably best to use a *compilation database*. |
| 59 | |
| 60 | A `compilation database`_ contains the command-line arguments for multiple |
| 61 | files. If the code you want to transform can be built with CMake, you can |
| 62 | generate this database easily by running CMake with the |
Justin Bogner | e3acc8b | 2015-05-18 05:05:49 +0000 | [diff] [blame] | 63 | ``-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`` option. The Ninja_ build system, since |
Chandler Carruth | d9063c4 | 2013-09-04 17:35:07 +0000 | [diff] [blame] | 64 | v1.2, can create this file too using the *compdb* tool: ``ninja -t compdb``. If |
| 65 | you're not already using either of these tools or cannot easily make use of |
| 66 | them you might consider looking into Bear_. |
| 67 | |
| 68 | In addition to the compiler arguments you usually build your code with, you must |
| 69 | provide the option for enabling C++11 features. For clang and versions of gcc |
| 70 | ≥ v4.8 this is ``-std=c++11``. |
| 71 | |
Edwin Vane | c76c0dd | 2013-10-07 15:56:25 +0000 | [diff] [blame] | 72 | With compiler arguments in hand, the modernizer can be applied to sources. Each |
| 73 | transform is applied to all sources before the next transform. All the changes |
| 74 | generated by each transform pass are serialized to disk and applied using |
| 75 | ``clang-apply-replacements``. This executable must be located on the ``PATH`` |
| 76 | or be present in the same directory as the ``clang-modernizer`` executable. If |
| 77 | any changes fail to apply, the modernizer will **not** proceed to the next |
| 78 | transform and will halt. |
Chandler Carruth | d9063c4 | 2013-09-04 17:35:07 +0000 | [diff] [blame] | 79 | |
Edwin Vane | c76c0dd | 2013-10-07 15:56:25 +0000 | [diff] [blame] | 80 | There's a small chance that changes made by a transform will produce code that |
| 81 | doesn't compile, also causing the modernizer to halt. This can happen with |
| 82 | bugs in the transforms or use of the pre-processor to make the same code behave |
| 83 | differently between translation units. Before logging a bug, be sure which |
| 84 | situation you are dealing with. |
Chandler Carruth | d9063c4 | 2013-09-04 17:35:07 +0000 | [diff] [blame] | 85 | |
| 86 | .. _Ninja: http://martine.github.io/ninja/ |
| 87 | .. _Bear: https://github.com/rizsotto/Bear |
| 88 | .. _compilation database: http://clang.llvm.org/docs/JSONCompilationDatabase.html |
| 89 | .. _Getting Started with the LLVM System: http://llvm.org/docs/GettingStarted.html |
| 90 | .. _Building LLVM with CMake: http://llvm.org/docs/CMake.html |
| 91 | .. _Clang Tools Documentation: http://clang.llvm.org/docs/ClangTools.html |
| 92 | |
Chandler Carruth | d9063c4 | 2013-09-04 17:35:07 +0000 | [diff] [blame] | 93 | |
| 94 | .. _transforms: |
| 95 | |
| 96 | Transformations |
| 97 | =============== |
| 98 | |
| 99 | The Modernizer is a collection of independent transforms which can be |
| 100 | independently enabled. The transforms currently implemented are: |
| 101 | |
| 102 | * :doc:`LoopConvertTransform` |
| 103 | |
| 104 | * :doc:`UseNullptrTransform` |
| 105 | |
| 106 | * :doc:`UseAutoTransform` |
| 107 | |
| 108 | * :doc:`AddOverrideTransform` |
| 109 | |
| 110 | * :doc:`PassByValueTransform` |
| 111 | |
| 112 | * :doc:`ReplaceAutoPtrTransform` |