clang  10.0.0svn
IncludeStyle.h
Go to the documentation of this file.
1 //===--- IncludeStyle.h - Style of C++ #include directives -------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H
10 #define LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H
11 
12 #include "llvm/Support/YAMLTraits.h"
13 #include <string>
14 #include <vector>
15 
16 namespace clang {
17 namespace tooling {
18 
19 /// Style for sorting and grouping C++ #include directives.
20 struct IncludeStyle {
21  /// Styles for sorting multiple ``#include`` blocks.
23  /// Sort each ``#include`` block separately.
24  /// \code
25  /// #include "b.h" into #include "b.h"
26  ///
27  /// #include <lib/main.h> #include "a.h"
28  /// #include "a.h" #include <lib/main.h>
29  /// \endcode
31  /// Merge multiple ``#include`` blocks together and sort as one.
32  /// \code
33  /// #include "b.h" into #include "a.h"
34  /// #include "b.h"
35  /// #include <lib/main.h> #include <lib/main.h>
36  /// #include "a.h"
37  /// \endcode
39  /// Merge multiple ``#include`` blocks together and sort as one.
40  /// Then split into groups based on category priority. See
41  /// ``IncludeCategories``.
42  /// \code
43  /// #include "b.h" into #include "a.h"
44  /// #include "b.h"
45  /// #include <lib/main.h>
46  /// #include "a.h" #include <lib/main.h>
47  /// \endcode
49  };
50 
51  /// Dependent on the value, multiple ``#include`` blocks can be sorted
52  /// as one and divided based on category.
54 
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.
63  bool operator==(const IncludeCategory &Other) const {
64  return Regex == Other.Regex && Priority == Other.Priority;
65  }
66  };
67 
68  /// Regular expressions denoting the different ``#include`` categories
69  /// used for ordering ``#includes``.
70  ///
71  /// `POSIX extended
72  /// <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>`_
73  /// regular expressions are supported.
74  ///
75  /// These regular expressions are matched against the filename of an include
76  /// (including the <> or "") in order. The value belonging to the first
77  /// matching regular expression is assigned and ``#includes`` are sorted first
78  /// according to increasing category number and then alphabetically within
79  /// each category.
80  ///
81  /// If none of the regular expressions match, INT_MAX is assigned as
82  /// category. The main header for a source file automatically gets category 0.
83  /// so that it is generally kept at the beginning of the ``#includes``
84  /// (https://llvm.org/docs/CodingStandards.html#include-style). However, you
85  /// can also assign negative priorities if you have certain headers that
86  /// always need to be first.
87  ///
88  /// To configure this in the .clang-format file, use:
89  /// \code{.yaml}
90  /// IncludeCategories:
91  /// - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
92  /// Priority: 2
93  /// - Regex: '^(<|"(gtest|gmock|isl|json)/)'
94  /// Priority: 3
95  /// - Regex: '<[[:alnum:].]+>'
96  /// Priority: 4
97  /// - Regex: '.*'
98  /// Priority: 1
99  /// \endcode
100  std::vector<IncludeCategory> IncludeCategories;
101 
102  /// Specify a regular expression of suffixes that are allowed in the
103  /// file-to-main-include mapping.
104  ///
105  /// When guessing whether a #include is the "main" include (to assign
106  /// category 0, see above), use this regex of allowed suffixes to the header
107  /// stem. A partial match is done, so that:
108  /// - "" means "arbitrary suffix"
109  /// - "$" means "no suffix"
110  ///
111  /// For example, if configured to "(_test)?$", then a header a.h would be seen
112  /// as the "main" include in both a.cc and a_test.cc.
113  std::string IncludeIsMainRegex;
114 };
115 
116 } // namespace tooling
117 } // namespace clang
118 
119 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::IncludeStyle::IncludeCategory)
120 
121 namespace llvm {
122 namespace yaml {
123 
124 template <>
125 struct MappingTraits<clang::tooling::IncludeStyle::IncludeCategory> {
126  static void mapping(IO &IO,
128 };
129 
130 template <>
131 struct ScalarEnumerationTraits<
133  static void
135 };
136 
137 } // namespace yaml
138 } // namespace llvm
139 
140 #endif // LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H
bool operator==(const IncludeCategory &Other) const
Definition: IncludeStyle.h:63
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
std::vector< IncludeCategory > IncludeCategories
Regular expressions denoting the different #include categories used for ordering #includes.
Definition: IncludeStyle.h:100
IncludeBlocksStyle
Styles for sorting multiple #include blocks.
Definition: IncludeStyle.h:22
int Priority
The priority to assign to this category.
Definition: IncludeStyle.h:60
int Category
Definition: Format.cpp:1810
Merge multiple #include blocks together and sort as one.
Definition: IncludeStyle.h:48
std::string Regex
The regular expression that this category matches.
Definition: IncludeStyle.h:58
Sort each #include block separately.
Definition: IncludeStyle.h:30
int SortPriority
The custom priority to sort before grouping.
Definition: IncludeStyle.h:62
Style for sorting and grouping C++ #include directives.
Definition: IncludeStyle.h:20
Dataflow Directional Tag Classes.
std::string IncludeIsMainRegex
Specify a regular expression of suffixes that are allowed in the file-to-main-include mapping...
Definition: IncludeStyle.h:113
See documentation of IncludeCategories.
Definition: IncludeStyle.h:56
IncludeBlocksStyle IncludeBlocks
Dependent on the value, multiple #include blocks can be sorted as one and divided based on category...
Definition: IncludeStyle.h:53
Merge multiple #include blocks together and sort as one.
Definition: IncludeStyle.h:38