blob: b2cdcfddf39c35854ef22cd76ddc09adac24dd28 [file] [log] [blame]
Chandler Carruthd9063c42013-09-04 17:35:07 +00001.. index:: clang-modernize
2
Alexander Kornienkof4e8b922015-09-10 09:42:01 +00003.. 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 Carruthd9063c42013-09-04 17:35:07 +000012==================================
13Clang 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 Vane5a816f02013-09-06 19:27:19 +000026 cpp11-migrate
27 MigratorUsage
Chandler Carruthd9063c42013-09-04 17:35:07 +000028
29:program:`clang-modernize` is a standalone tool used to automatically convert
30C++ code written against old standards to use features of the newest C++
31standard where appropriate.
32
33Getting Started
34===============
35
36To build from source:
37
381. 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
422. `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
50Before continuing, take a look at :doc:`ModernizerUsage` to see how to invoke
51the Modernizer.
52
53Before running the Modernizer on code you'll need the arguments you'd normally
54pass to the compiler. If you're migrating a single file with few compiler
55arguments, it might be easier to pass the compiler args on the command line
56after ``--``. If you don't have any compiler arguments then ``--`` is not needed.
57If you're working with multiple files or even a single file with many compiler
58args, it's probably best to use a *compilation database*.
59
60A `compilation database`_ contains the command-line arguments for multiple
61files. If the code you want to transform can be built with CMake, you can
62generate this database easily by running CMake with the
Justin Bognere3acc8b2015-05-18 05:05:49 +000063``-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`` option. The Ninja_ build system, since
Chandler Carruthd9063c42013-09-04 17:35:07 +000064v1.2, can create this file too using the *compdb* tool: ``ninja -t compdb``. If
65you're not already using either of these tools or cannot easily make use of
66them you might consider looking into Bear_.
67
68In addition to the compiler arguments you usually build your code with, you must
69provide the option for enabling C++11 features. For clang and versions of gcc
70 v4.8 this is ``-std=c++11``.
71
Edwin Vanec76c0dd2013-10-07 15:56:25 +000072With compiler arguments in hand, the modernizer can be applied to sources. Each
73transform is applied to all sources before the next transform. All the changes
74generated by each transform pass are serialized to disk and applied using
75``clang-apply-replacements``. This executable must be located on the ``PATH``
76or be present in the same directory as the ``clang-modernizer`` executable. If
77any changes fail to apply, the modernizer will **not** proceed to the next
78transform and will halt.
Chandler Carruthd9063c42013-09-04 17:35:07 +000079
Edwin Vanec76c0dd2013-10-07 15:56:25 +000080There's a small chance that changes made by a transform will produce code that
81doesn't compile, also causing the modernizer to halt. This can happen with
82bugs in the transforms or use of the pre-processor to make the same code behave
83differently between translation units. Before logging a bug, be sure which
84situation you are dealing with.
Chandler Carruthd9063c42013-09-04 17:35:07 +000085
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 Carruthd9063c42013-09-04 17:35:07 +000093
94.. _transforms:
95
96Transformations
97===============
98
99The Modernizer is a collection of independent transforms which can be
100independently 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`