|
|
|||
File indexing completed on 2026-05-10 08:37:11
0001 //===--- IncludeStyle.h - Style of C++ #include directives -------*- C++-*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H 0010 #define LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H 0011 0012 #include "llvm/Support/YAMLTraits.h" 0013 #include <string> 0014 #include <vector> 0015 0016 namespace clang { 0017 namespace tooling { 0018 0019 /// Style for sorting and grouping C++ #include directives. 0020 struct IncludeStyle { 0021 /// Styles for sorting multiple ``#include`` blocks. 0022 enum IncludeBlocksStyle { 0023 /// Sort each ``#include`` block separately. 0024 /// \code 0025 /// #include "b.h" into #include "b.h" 0026 /// 0027 /// #include <lib/main.h> #include "a.h" 0028 /// #include "a.h" #include <lib/main.h> 0029 /// \endcode 0030 IBS_Preserve, 0031 /// Merge multiple ``#include`` blocks together and sort as one. 0032 /// \code 0033 /// #include "b.h" into #include "a.h" 0034 /// #include "b.h" 0035 /// #include <lib/main.h> #include <lib/main.h> 0036 /// #include "a.h" 0037 /// \endcode 0038 IBS_Merge, 0039 /// Merge multiple ``#include`` blocks together and sort as one. 0040 /// Then split into groups based on category priority. See 0041 /// ``IncludeCategories``. 0042 /// \code 0043 /// #include "b.h" into #include "a.h" 0044 /// #include "b.h" 0045 /// #include <lib/main.h> 0046 /// #include "a.h" #include <lib/main.h> 0047 /// \endcode 0048 IBS_Regroup, 0049 }; 0050 0051 /// Dependent on the value, multiple ``#include`` blocks can be sorted 0052 /// as one and divided based on category. 0053 /// \version 6 0054 IncludeBlocksStyle IncludeBlocks; 0055 0056 /// See documentation of ``IncludeCategories``. 0057 struct IncludeCategory { 0058 /// The regular expression that this category matches. 0059 std::string Regex; 0060 /// The priority to assign to this category. 0061 int Priority; 0062 /// The custom priority to sort before grouping. 0063 int SortPriority; 0064 /// If the regular expression is case sensitive. 0065 bool RegexIsCaseSensitive; 0066 bool operator==(const IncludeCategory &Other) const { 0067 return Regex == Other.Regex && Priority == Other.Priority && 0068 RegexIsCaseSensitive == Other.RegexIsCaseSensitive; 0069 } 0070 }; 0071 0072 /// Regular expressions denoting the different ``#include`` categories 0073 /// used for ordering ``#includes``. 0074 /// 0075 /// `POSIX extended 0076 /// <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>`_ 0077 /// regular expressions are supported. 0078 /// 0079 /// These regular expressions are matched against the filename of an include 0080 /// (including the <> or "") in order. The value belonging to the first 0081 /// matching regular expression is assigned and ``#includes`` are sorted first 0082 /// according to increasing category number and then alphabetically within 0083 /// each category. 0084 /// 0085 /// If none of the regular expressions match, INT_MAX is assigned as 0086 /// category. The main header for a source file automatically gets category 0. 0087 /// so that it is generally kept at the beginning of the ``#includes`` 0088 /// (https://llvm.org/docs/CodingStandards.html#include-style). However, you 0089 /// can also assign negative priorities if you have certain headers that 0090 /// always need to be first. 0091 /// 0092 /// There is a third and optional field ``SortPriority`` which can used while 0093 /// ``IncludeBlocks = IBS_Regroup`` to define the priority in which 0094 /// ``#includes`` should be ordered. The value of ``Priority`` defines the 0095 /// order of ``#include blocks`` and also allows the grouping of ``#includes`` 0096 /// of different priority. ``SortPriority`` is set to the value of 0097 /// ``Priority`` as default if it is not assigned. 0098 /// 0099 /// Each regular expression can be marked as case sensitive with the field 0100 /// ``CaseSensitive``, per default it is not. 0101 /// 0102 /// To configure this in the .clang-format file, use: 0103 /// \code{.yaml} 0104 /// IncludeCategories: 0105 /// - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 0106 /// Priority: 2 0107 /// SortPriority: 2 0108 /// CaseSensitive: true 0109 /// - Regex: '^((<|")(gtest|gmock|isl|json)/)' 0110 /// Priority: 3 0111 /// - Regex: '<[[:alnum:].]+>' 0112 /// Priority: 4 0113 /// - Regex: '.*' 0114 /// Priority: 1 0115 /// SortPriority: 0 0116 /// \endcode 0117 /// \version 3.8 0118 std::vector<IncludeCategory> IncludeCategories; 0119 0120 /// Specify a regular expression of suffixes that are allowed in the 0121 /// file-to-main-include mapping. 0122 /// 0123 /// When guessing whether a #include is the "main" include (to assign 0124 /// category 0, see above), use this regex of allowed suffixes to the header 0125 /// stem. A partial match is done, so that: 0126 /// * ``""`` means "arbitrary suffix" 0127 /// * ``"$"`` means "no suffix" 0128 /// 0129 /// For example, if configured to ``"(_test)?$"``, then a header a.h would be seen 0130 /// as the "main" include in both a.cc and a_test.cc. 0131 /// \version 3.9 0132 std::string IncludeIsMainRegex; 0133 0134 /// Specify a regular expression for files being formatted 0135 /// that are allowed to be considered "main" in the 0136 /// file-to-main-include mapping. 0137 /// 0138 /// By default, clang-format considers files as "main" only when they end 0139 /// with: ``.c``, ``.cc``, ``.cpp``, ``.c++``, ``.cxx``, ``.m`` or ``.mm`` 0140 /// extensions. 0141 /// For these files a guessing of "main" include takes place 0142 /// (to assign category 0, see above). This config option allows for 0143 /// additional suffixes and extensions for files to be considered as "main". 0144 /// 0145 /// For example, if this option is configured to ``(Impl\.hpp)$``, 0146 /// then a file ``ClassImpl.hpp`` is considered "main" (in addition to 0147 /// ``Class.c``, ``Class.cc``, ``Class.cpp`` and so on) and "main 0148 /// include file" logic will be executed (with *IncludeIsMainRegex* setting 0149 /// also being respected in later phase). Without this option set, 0150 /// ``ClassImpl.hpp`` would not have the main include file put on top 0151 /// before any other include. 0152 /// \version 10 0153 std::string IncludeIsMainSourceRegex; 0154 0155 /// Character to consider in the include directives for the main header. 0156 enum MainIncludeCharDiscriminator : int8_t { 0157 /// Main include uses quotes: ``#include "foo.hpp"`` (the default). 0158 MICD_Quote, 0159 /// Main include uses angle brackets: ``#include <foo.hpp>``. 0160 MICD_AngleBracket, 0161 /// Main include uses either quotes or angle brackets. 0162 MICD_Any 0163 }; 0164 0165 /// When guessing whether a #include is the "main" include, only the include 0166 /// directives that use the specified character are considered. 0167 /// \version 19 0168 MainIncludeCharDiscriminator MainIncludeChar; 0169 }; 0170 0171 } // namespace tooling 0172 } // namespace clang 0173 0174 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::IncludeStyle::IncludeCategory) 0175 0176 namespace llvm { 0177 namespace yaml { 0178 0179 template <> 0180 struct MappingTraits<clang::tooling::IncludeStyle::IncludeCategory> { 0181 static void mapping(IO &IO, 0182 clang::tooling::IncludeStyle::IncludeCategory &Category); 0183 }; 0184 0185 template <> 0186 struct ScalarEnumerationTraits< 0187 clang::tooling::IncludeStyle::IncludeBlocksStyle> { 0188 static void 0189 enumeration(IO &IO, clang::tooling::IncludeStyle::IncludeBlocksStyle &Value); 0190 }; 0191 0192 template <> 0193 struct ScalarEnumerationTraits< 0194 clang::tooling::IncludeStyle::MainIncludeCharDiscriminator> { 0195 static void enumeration( 0196 IO &IO, 0197 clang::tooling::IncludeStyle::MainIncludeCharDiscriminator &Value); 0198 }; 0199 0200 } // namespace yaml 0201 } // namespace llvm 0202 0203 #endif // LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|