ClangTools.rst 5.8 KB

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