ClangTools.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ========
  2. Overview
  3. ========
  4. NOTE: this document applies to the original Clang project, not the DirectX
  5. Compiler. It's made available for informational purposes only.
  6. Clang Tools are standalone command line (and potentially GUI) tools
  7. designed for use by C++ developers who are already using and enjoying
  8. Clang as their compiler. These tools provide developer-oriented
  9. functionality such as fast syntax checking, automatic formatting,
  10. refactoring, etc.
  11. Only a couple of the most basic and fundamental tools are kept in the
  12. primary Clang Subversion project. The rest of the tools are kept in a
  13. side-project so that developers who don't want or need to build them
  14. don't. If you want to get access to the extra Clang Tools repository,
  15. simply check it out into the tools tree of your Clang checkout and
  16. follow the usual process for building and working with a combined
  17. LLVM/Clang checkout:
  18. - With Subversion:
  19. - ``cd llvm/tools/clang/tools``
  20. - ``svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra``
  21. - Or with Git:
  22. - ``cd llvm/tools/clang/tools``
  23. - ``git clone http://llvm.org/git/clang-tools-extra.git extra``
  24. This document describes a high-level overview of the organization of
  25. Clang Tools within the project as well as giving an introduction to some
  26. of the more important tools. However, it should be noted that this
  27. document is currently focused on Clang and Clang Tool developers, not on
  28. end users of these tools.
  29. Clang Tools Organization
  30. ========================
  31. Clang Tools are CLI or GUI programs that are intended to be directly
  32. used by C++ developers. That is they are *not* primarily for use by
  33. Clang developers, although they are hopefully useful to C++ developers
  34. who happen to work on Clang, and we try to actively dogfood their
  35. functionality. They are developed in three components: the underlying
  36. infrastructure for building a standalone tool based on Clang, core
  37. shared logic used by many different tools in the form of refactoring and
  38. rewriting libraries, and the tools themselves.
  39. The underlying infrastructure for Clang Tools is the
  40. :doc:`LibTooling <LibTooling>` platform. See its documentation for much
  41. more detailed information about how this infrastructure works. The
  42. common refactoring and rewriting toolkit-style library is also part of
  43. LibTooling organizationally.
  44. A few Clang Tools are developed along side the core Clang libraries as
  45. examples and test cases of fundamental functionality. However, most of
  46. the tools are developed in a side repository to provide easy separation
  47. from the core libraries. We intentionally do not support public
  48. libraries in the side repository, as we want to carefully review and
  49. find good APIs for libraries as they are lifted out of a few tools and
  50. into the core Clang library set.
  51. Regardless of which repository Clang Tools' code resides in, the
  52. development process and practices for all Clang Tools are exactly those
  53. of Clang itself. They are entirely within the Clang *project*,
  54. regardless of the version control scheme.
  55. Core Clang Tools
  56. ================
  57. The core set of Clang tools that are within the main repository are
  58. tools that very specifically complement, and allow use and testing of
  59. *Clang* specific functionality.
  60. ``clang-check``
  61. ---------------
  62. :doc:`ClangCheck` combines the LibTooling framework for running a
  63. Clang tool with the basic Clang diagnostics by syntax checking specific files
  64. in a fast, command line interface. It can also accept flags to re-display the
  65. diagnostics in different formats with different flags, suitable for use driving
  66. an IDE or editor. Furthermore, it can be used in fixit-mode to directly apply
  67. fixit-hints offered by clang. See :doc:`HowToSetupToolingForLLVM` for
  68. instructions on how to setup and used `clang-check`.
  69. ``clang-format``
  70. ~~~~~~~~~~~~~~~~
  71. Clang-format is both a :doc:`library <LibFormat>` and a :doc:`stand-alone tool
  72. <ClangFormat>` with the goal of automatically reformatting C++ sources files
  73. according to configurable style guides. To do so, clang-format uses Clang's
  74. ``Lexer`` to transform an input file into a token stream and then changes all
  75. the whitespace around those tokens. The goal is for clang-format to serve both
  76. as a user tool (ideally with powerful IDE integrations) and as part of other
  77. refactoring tools, e.g. to do a reformatting of all the lines changed during a
  78. renaming.
  79. ``clang-modernize``
  80. ~~~~~~~~~~~~~~~~~~~
  81. ``clang-modernize`` migrates C++ code to use C++11 features where appropriate.
  82. Currently it can:
  83. * convert loops to range-based for loops;
  84. * convert null pointer constants (like ``NULL`` or ``0``) to C++11 ``nullptr``;
  85. * replace the type specifier in variable declarations with the ``auto`` type specifier;
  86. * add the ``override`` specifier to applicable member functions.
  87. Extra Clang Tools
  88. =================
  89. As various categories of Clang Tools are added to the extra repository,
  90. they'll be tracked here. The focus of this documentation is on the scope
  91. and features of the tools for other tool developers; each tool should
  92. provide its own user-focused documentation.
  93. Ideas for new Tools
  94. ===================
  95. * C++ cast conversion tool. Will convert C-style casts (``(type) value``) to
  96. appropriate C++ cast (``static_cast``, ``const_cast`` or
  97. ``reinterpret_cast``).
  98. * Non-member ``begin()`` and ``end()`` conversion tool. Will convert
  99. ``foo.begin()`` into ``begin(foo)`` and similarly for ``end()``, where
  100. ``foo`` is a standard container. We could also detect similar patterns for
  101. arrays.
  102. * ``make_shared`` / ``make_unique`` conversion. Part of this transformation
  103. can be incorporated into the ``auto`` transformation. Will convert
  104. .. code-block:: c++
  105. std::shared_ptr<Foo> sp(new Foo);
  106. std::unique_ptr<Foo> up(new Foo);
  107. func(std::shared_ptr<Foo>(new Foo), bar());
  108. into:
  109. .. code-block:: c++
  110. auto sp = std::make_shared<Foo>();
  111. auto up = std::make_unique<Foo>(); // In C++14 mode.
  112. // This also affects correctness. For the cases where bar() throws,
  113. // make_shared() is safe and the original code may leak.
  114. func(std::make_shared<Foo>(), bar());
  115. * ``tr1`` removal tool. Will migrate source code from using TR1 library
  116. features to C++11 library. For example:
  117. .. code-block:: c++
  118. #include <tr1/unordered_map>
  119. int main()
  120. {
  121. std::tr1::unordered_map <int, int> ma;
  122. std::cout << ma.size () << std::endl;
  123. return 0;
  124. }
  125. should be rewritten to:
  126. .. code-block:: c++
  127. #include <unordered_map>
  128. int main()
  129. {
  130. std::unordered_map <int, int> ma;
  131. std::cout << ma.size () << std::endl;
  132. return 0;
  133. }
  134. * A tool to remove ``auto``. Will convert ``auto`` to an explicit type or add
  135. comments with deduced types. The motivation is that there are developers
  136. that don't want to use ``auto`` because they are afraid that they might lose
  137. control over their code.
  138. * C++14: less verbose operator function objects (`N3421
  139. <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3421.htm>`_).
  140. For example:
  141. .. code-block:: c++
  142. sort(v.begin(), v.end(), greater<ValueType>());
  143. should be rewritten to:
  144. .. code-block:: c++
  145. sort(v.begin(), v.end(), greater<>());