IncludeStyle.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- IncludeStyle.h - Style of C++ #include directives -------*- C++-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H
  14. #define LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H
  15. #include "llvm/Support/YAMLTraits.h"
  16. #include <string>
  17. #include <vector>
  18. namespace clang {
  19. namespace tooling {
  20. /// Style for sorting and grouping C++ #include directives.
  21. struct IncludeStyle {
  22. /// Styles for sorting multiple ``#include`` blocks.
  23. enum IncludeBlocksStyle {
  24. /// Sort each ``#include`` block separately.
  25. /// \code
  26. /// #include "b.h" into #include "b.h"
  27. ///
  28. /// #include <lib/main.h> #include "a.h"
  29. /// #include "a.h" #include <lib/main.h>
  30. /// \endcode
  31. IBS_Preserve,
  32. /// Merge multiple ``#include`` blocks together and sort as one.
  33. /// \code
  34. /// #include "b.h" into #include "a.h"
  35. /// #include "b.h"
  36. /// #include <lib/main.h> #include <lib/main.h>
  37. /// #include "a.h"
  38. /// \endcode
  39. IBS_Merge,
  40. /// Merge multiple ``#include`` blocks together and sort as one.
  41. /// Then split into groups based on category priority. See
  42. /// ``IncludeCategories``.
  43. /// \code
  44. /// #include "b.h" into #include "a.h"
  45. /// #include "b.h"
  46. /// #include <lib/main.h>
  47. /// #include "a.h" #include <lib/main.h>
  48. /// \endcode
  49. IBS_Regroup,
  50. };
  51. /// Dependent on the value, multiple ``#include`` blocks can be sorted
  52. /// as one and divided based on category.
  53. /// \version 6
  54. IncludeBlocksStyle IncludeBlocks;
  55. /// See documentation of ``IncludeCategories``.
  56. struct IncludeCategory {
  57. /// The regular expression that this category matches.
  58. std::string Regex;
  59. /// The priority to assign to this category.
  60. int Priority;
  61. /// The custom priority to sort before grouping.
  62. int SortPriority;
  63. /// If the regular expression is case sensitive.
  64. bool RegexIsCaseSensitive;
  65. bool operator==(const IncludeCategory &Other) const {
  66. return Regex == Other.Regex && Priority == Other.Priority &&
  67. RegexIsCaseSensitive == Other.RegexIsCaseSensitive;
  68. }
  69. };
  70. /// Regular expressions denoting the different ``#include`` categories
  71. /// used for ordering ``#includes``.
  72. ///
  73. /// `POSIX extended
  74. /// <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>`_
  75. /// regular expressions are supported.
  76. ///
  77. /// These regular expressions are matched against the filename of an include
  78. /// (including the <> or "") in order. The value belonging to the first
  79. /// matching regular expression is assigned and ``#includes`` are sorted first
  80. /// according to increasing category number and then alphabetically within
  81. /// each category.
  82. ///
  83. /// If none of the regular expressions match, INT_MAX is assigned as
  84. /// category. The main header for a source file automatically gets category 0.
  85. /// so that it is generally kept at the beginning of the ``#includes``
  86. /// (https://llvm.org/docs/CodingStandards.html#include-style). However, you
  87. /// can also assign negative priorities if you have certain headers that
  88. /// always need to be first.
  89. ///
  90. /// There is a third and optional field ``SortPriority`` which can used while
  91. /// ``IncludeBlocks = IBS_Regroup`` to define the priority in which
  92. /// ``#includes`` should be ordered. The value of ``Priority`` defines the
  93. /// order of ``#include blocks`` and also allows the grouping of ``#includes``
  94. /// of different priority. ``SortPriority`` is set to the value of
  95. /// ``Priority`` as default if it is not assigned.
  96. ///
  97. /// Each regular expression can be marked as case sensitive with the field
  98. /// ``CaseSensitive``, per default it is not.
  99. ///
  100. /// To configure this in the .clang-format file, use:
  101. /// \code{.yaml}
  102. /// IncludeCategories:
  103. /// - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
  104. /// Priority: 2
  105. /// SortPriority: 2
  106. /// CaseSensitive: true
  107. /// - Regex: '^((<|")(gtest|gmock|isl|json)/)'
  108. /// Priority: 3
  109. /// - Regex: '<[[:alnum:].]+>'
  110. /// Priority: 4
  111. /// - Regex: '.*'
  112. /// Priority: 1
  113. /// SortPriority: 0
  114. /// \endcode
  115. /// \version 3.8
  116. std::vector<IncludeCategory> IncludeCategories;
  117. /// Specify a regular expression of suffixes that are allowed in the
  118. /// file-to-main-include mapping.
  119. ///
  120. /// When guessing whether a #include is the "main" include (to assign
  121. /// category 0, see above), use this regex of allowed suffixes to the header
  122. /// stem. A partial match is done, so that:
  123. /// - "" means "arbitrary suffix"
  124. /// - "$" means "no suffix"
  125. ///
  126. /// For example, if configured to "(_test)?$", then a header a.h would be seen
  127. /// as the "main" include in both a.cc and a_test.cc.
  128. /// \version 3.9
  129. std::string IncludeIsMainRegex;
  130. /// Specify a regular expression for files being formatted
  131. /// that are allowed to be considered "main" in the
  132. /// file-to-main-include mapping.
  133. ///
  134. /// By default, clang-format considers files as "main" only when they end
  135. /// with: ``.c``, ``.cc``, ``.cpp``, ``.c++``, ``.cxx``, ``.m`` or ``.mm``
  136. /// extensions.
  137. /// For these files a guessing of "main" include takes place
  138. /// (to assign category 0, see above). This config option allows for
  139. /// additional suffixes and extensions for files to be considered as "main".
  140. ///
  141. /// For example, if this option is configured to ``(Impl\.hpp)$``,
  142. /// then a file ``ClassImpl.hpp`` is considered "main" (in addition to
  143. /// ``Class.c``, ``Class.cc``, ``Class.cpp`` and so on) and "main
  144. /// include file" logic will be executed (with *IncludeIsMainRegex* setting
  145. /// also being respected in later phase). Without this option set,
  146. /// ``ClassImpl.hpp`` would not have the main include file put on top
  147. /// before any other include.
  148. /// \version 10
  149. std::string IncludeIsMainSourceRegex;
  150. };
  151. } // namespace tooling
  152. } // namespace clang
  153. LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::IncludeStyle::IncludeCategory)
  154. namespace llvm {
  155. namespace yaml {
  156. template <>
  157. struct MappingTraits<clang::tooling::IncludeStyle::IncludeCategory> {
  158. static void mapping(IO &IO,
  159. clang::tooling::IncludeStyle::IncludeCategory &Category);
  160. };
  161. template <>
  162. struct ScalarEnumerationTraits<
  163. clang::tooling::IncludeStyle::IncludeBlocksStyle> {
  164. static void
  165. enumeration(IO &IO, clang::tooling::IncludeStyle::IncludeBlocksStyle &Value);
  166. };
  167. } // namespace yaml
  168. } // namespace llvm
  169. #endif // LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H
  170. #ifdef __GNUC__
  171. #pragma GCC diagnostic pop
  172. #endif