xref: /aosp_15_r20/external/clang/docs/ClangTools.rst (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li========
2*67e74705SXin LiOverview
3*67e74705SXin Li========
4*67e74705SXin Li
5*67e74705SXin LiClang Tools are standalone command line (and potentially GUI) tools
6*67e74705SXin Lidesigned for use by C++ developers who are already using and enjoying
7*67e74705SXin LiClang as their compiler. These tools provide developer-oriented
8*67e74705SXin Lifunctionality such as fast syntax checking, automatic formatting,
9*67e74705SXin Lirefactoring, etc.
10*67e74705SXin Li
11*67e74705SXin LiOnly a couple of the most basic and fundamental tools are kept in the
12*67e74705SXin Liprimary Clang Subversion project. The rest of the tools are kept in a
13*67e74705SXin Liside-project so that developers who don't want or need to build them
14*67e74705SXin Lidon't. If you want to get access to the extra Clang Tools repository,
15*67e74705SXin Lisimply check it out into the tools tree of your Clang checkout and
16*67e74705SXin Lifollow the usual process for building and working with a combined
17*67e74705SXin LiLLVM/Clang checkout:
18*67e74705SXin Li
19*67e74705SXin Li-  With Subversion:
20*67e74705SXin Li
21*67e74705SXin Li   -  ``cd llvm/tools/clang/tools``
22*67e74705SXin Li   -  ``svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra``
23*67e74705SXin Li
24*67e74705SXin Li-  Or with Git:
25*67e74705SXin Li
26*67e74705SXin Li   -  ``cd llvm/tools/clang/tools``
27*67e74705SXin Li   -  ``git clone http://llvm.org/git/clang-tools-extra.git extra``
28*67e74705SXin Li
29*67e74705SXin LiThis document describes a high-level overview of the organization of
30*67e74705SXin LiClang Tools within the project as well as giving an introduction to some
31*67e74705SXin Liof the more important tools. However, it should be noted that this
32*67e74705SXin Lidocument is currently focused on Clang and Clang Tool developers, not on
33*67e74705SXin Liend users of these tools.
34*67e74705SXin Li
35*67e74705SXin LiClang Tools Organization
36*67e74705SXin Li========================
37*67e74705SXin Li
38*67e74705SXin LiClang Tools are CLI or GUI programs that are intended to be directly
39*67e74705SXin Liused by C++ developers. That is they are *not* primarily for use by
40*67e74705SXin LiClang developers, although they are hopefully useful to C++ developers
41*67e74705SXin Liwho happen to work on Clang, and we try to actively dogfood their
42*67e74705SXin Lifunctionality. They are developed in three components: the underlying
43*67e74705SXin Liinfrastructure for building a standalone tool based on Clang, core
44*67e74705SXin Lishared logic used by many different tools in the form of refactoring and
45*67e74705SXin Lirewriting libraries, and the tools themselves.
46*67e74705SXin Li
47*67e74705SXin LiThe underlying infrastructure for Clang Tools is the
48*67e74705SXin Li:doc:`LibTooling <LibTooling>` platform. See its documentation for much
49*67e74705SXin Limore detailed information about how this infrastructure works. The
50*67e74705SXin Licommon refactoring and rewriting toolkit-style library is also part of
51*67e74705SXin LiLibTooling organizationally.
52*67e74705SXin Li
53*67e74705SXin LiA few Clang Tools are developed along side the core Clang libraries as
54*67e74705SXin Liexamples and test cases of fundamental functionality. However, most of
55*67e74705SXin Lithe tools are developed in a side repository to provide easy separation
56*67e74705SXin Lifrom the core libraries. We intentionally do not support public
57*67e74705SXin Lilibraries in the side repository, as we want to carefully review and
58*67e74705SXin Lifind good APIs for libraries as they are lifted out of a few tools and
59*67e74705SXin Liinto the core Clang library set.
60*67e74705SXin Li
61*67e74705SXin LiRegardless of which repository Clang Tools' code resides in, the
62*67e74705SXin Lidevelopment process and practices for all Clang Tools are exactly those
63*67e74705SXin Liof Clang itself. They are entirely within the Clang *project*,
64*67e74705SXin Liregardless of the version control scheme.
65*67e74705SXin Li
66*67e74705SXin LiCore Clang Tools
67*67e74705SXin Li================
68*67e74705SXin Li
69*67e74705SXin LiThe core set of Clang tools that are within the main repository are
70*67e74705SXin Litools that very specifically complement, and allow use and testing of
71*67e74705SXin Li*Clang* specific functionality.
72*67e74705SXin Li
73*67e74705SXin Li``clang-check``
74*67e74705SXin Li---------------
75*67e74705SXin Li
76*67e74705SXin Li:doc:`ClangCheck` combines the LibTooling framework for running a
77*67e74705SXin LiClang tool with the basic Clang diagnostics by syntax checking specific files
78*67e74705SXin Liin a fast, command line interface. It can also accept flags to re-display the
79*67e74705SXin Lidiagnostics in different formats with different flags, suitable for use driving
80*67e74705SXin Lian IDE or editor. Furthermore, it can be used in fixit-mode to directly apply
81*67e74705SXin Lifixit-hints offered by clang. See :doc:`HowToSetupToolingForLLVM` for
82*67e74705SXin Liinstructions on how to setup and used `clang-check`.
83*67e74705SXin Li
84*67e74705SXin Li``clang-format``
85*67e74705SXin Li----------------
86*67e74705SXin Li
87*67e74705SXin LiClang-format is both a :doc:`library <LibFormat>` and a :doc:`stand-alone tool
88*67e74705SXin Li<ClangFormat>` with the goal of automatically reformatting C++ sources files
89*67e74705SXin Liaccording to configurable style guides.  To do so, clang-format uses Clang's
90*67e74705SXin Li``Lexer`` to transform an input file into a token stream and then changes all
91*67e74705SXin Lithe whitespace around those tokens.  The goal is for clang-format to serve both
92*67e74705SXin Lias a user tool (ideally with powerful IDE integrations) and as part of other
93*67e74705SXin Lirefactoring tools, e.g. to do a reformatting of all the lines changed during a
94*67e74705SXin Lirenaming.
95*67e74705SXin Li
96*67e74705SXin Li
97*67e74705SXin LiExtra Clang Tools
98*67e74705SXin Li=================
99*67e74705SXin Li
100*67e74705SXin LiAs various categories of Clang Tools are added to the extra repository,
101*67e74705SXin Lithey'll be tracked here. The focus of this documentation is on the scope
102*67e74705SXin Liand features of the tools for other tool developers; each tool should
103*67e74705SXin Liprovide its own user-focused documentation.
104*67e74705SXin Li
105*67e74705SXin Li``clang-tidy``
106*67e74705SXin Li--------------
107*67e74705SXin Li
108*67e74705SXin Li`clang-tidy <http://clang.llvm.org/extra/clang-tidy/>`_ is a clang-based C++
109*67e74705SXin Lilinter tool. It provides an extensible framework for building compiler-based
110*67e74705SXin Listatic analyses detecting and fixing bug-prone patterns, performance,
111*67e74705SXin Liportability and maintainability issues.
112*67e74705SXin Li
113*67e74705SXin Li
114*67e74705SXin LiIdeas for new Tools
115*67e74705SXin Li===================
116*67e74705SXin Li
117*67e74705SXin Li* C++ cast conversion tool.  Will convert C-style casts (``(type) value``) to
118*67e74705SXin Li  appropriate C++ cast (``static_cast``, ``const_cast`` or
119*67e74705SXin Li  ``reinterpret_cast``).
120*67e74705SXin Li* Non-member ``begin()`` and ``end()`` conversion tool.  Will convert
121*67e74705SXin Li  ``foo.begin()`` into ``begin(foo)`` and similarly for ``end()``, where
122*67e74705SXin Li  ``foo`` is a standard container.  We could also detect similar patterns for
123*67e74705SXin Li  arrays.
124*67e74705SXin Li* ``tr1`` removal tool.  Will migrate source code from using TR1 library
125*67e74705SXin Li  features to C++11 library.  For example:
126*67e74705SXin Li
127*67e74705SXin Li  .. code-block:: c++
128*67e74705SXin Li
129*67e74705SXin Li    #include <tr1/unordered_map>
130*67e74705SXin Li    int main()
131*67e74705SXin Li    {
132*67e74705SXin Li        std::tr1::unordered_map <int, int> ma;
133*67e74705SXin Li        std::cout << ma.size () << std::endl;
134*67e74705SXin Li        return 0;
135*67e74705SXin Li    }
136*67e74705SXin Li
137*67e74705SXin Li  should be rewritten to:
138*67e74705SXin Li
139*67e74705SXin Li  .. code-block:: c++
140*67e74705SXin Li
141*67e74705SXin Li    #include <unordered_map>
142*67e74705SXin Li    int main()
143*67e74705SXin Li    {
144*67e74705SXin Li        std::unordered_map <int, int> ma;
145*67e74705SXin Li        std::cout << ma.size () << std::endl;
146*67e74705SXin Li        return 0;
147*67e74705SXin Li    }
148*67e74705SXin Li
149*67e74705SXin Li* A tool to remove ``auto``.  Will convert ``auto`` to an explicit type or add
150*67e74705SXin Li  comments with deduced types.  The motivation is that there are developers
151*67e74705SXin Li  that don't want to use ``auto`` because they are afraid that they might lose
152*67e74705SXin Li  control over their code.
153*67e74705SXin Li
154*67e74705SXin Li* C++14: less verbose operator function objects (`N3421
155*67e74705SXin Li  <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3421.htm>`_).
156*67e74705SXin Li  For example:
157*67e74705SXin Li
158*67e74705SXin Li  .. code-block:: c++
159*67e74705SXin Li
160*67e74705SXin Li    sort(v.begin(), v.end(), greater<ValueType>());
161*67e74705SXin Li
162*67e74705SXin Li  should be rewritten to:
163*67e74705SXin Li
164*67e74705SXin Li  .. code-block:: c++
165*67e74705SXin Li
166*67e74705SXin Li    sort(v.begin(), v.end(), greater<>());
167*67e74705SXin Li
168