forked from include-what-you-use/include-what-you-use
-
Notifications
You must be signed in to change notification settings - Fork 5
/
include-what-you-use.1
346 lines (344 loc) · 9.71 KB
/
include-what-you-use.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
.\" t -*- coding: utf-8 -*-
.\" Man page for include-what-you-use
.\"
.\" This file is distributed under the University of Illinois Open Source
.\" License. See LICENSE.TXT for details.
.\"
.TH INCLUDE-WHAT-YOU-USE 1 "2022-02-21" include-what-you-use "User Commands"
.SH NAME
include-what-you-use \- analyze includes in C and C++ source files.
.SH SYNOPSIS
.SY include-what-you-use
.RB [ \-Xiwyu
.IR option "]\&.\|.\|. [" clang-options "] " file
.YS
.SH DESCRIPTION
\(lqInclude what you use\(rq means this: for every symbol (type, function,
variable, or macro) that you use in
.IR foo.cpp ,
either
.IR foo.cpp " or " foo.h
should include a header file that exports the declaration of that symbol.
.B include-what-you-use
is a tool to analyze includes of source files to find
\(lqinclude-what-you-use\(rq violations, and suggest fixes for them.
.PP
The main goal of
.B include-what-you-use
is to remove superfluous includes.
It does this both by figuring out what includes are not actually needed for this
file (for both source and header files), and by replacing includes with forward
declarations when possible.
.SH OPTIONS
Options for
.B include-what-you-use
have to be preceded with
.BR \-Xiwyu .
All other options are interpreted as
.BR clang (1)
compiler options.
.TP
.BI \-\-check_also= glob
Print \(lqinclude-what-you-use\(rq-violation info for all files matching the
given glob pattern (in addition to the default of reporting for the input
source file and associated header files).
This flag may be specified multiple times to specify multiple glob patterns.
.TP
.BI \-\-comment_style= verbosity
Controls the style and verbosity of \(lqwhy\(rq comments at the end of
suggested includes. Options for
.I verbosity
are:
.RS
.TP
.B none
No \(lqwhy\(rq comments.
.TP
.B short
\(lqWhy\(rq comments include symbol names, but no namespaces. This is the
default.
.TP
.B long
\(lqWhy\(rq comments include symbol names with namespaces.
.RE
.TP
.B \-\-cxx17ns
Suggest the more concise syntax for nested namespaces introduced in C++17.
.TP
.BR \-\-error [ =\fIN ]
Exit with error code
.IR N
(defaults to 1 if omitted) if there are \(lqinclude-what-you-use\(rq
violations.
.TP
.BR \-\-error_always [ =\fIN ]
Exit with error code
.IR N
(defaults to 1 if omitted) whether there are \(lqinclude-what-you-use\(rq
violations or not (for use with \fBmake\fR(1)).
.TP
.BI \-\-keep= glob
Always keep the includes matched by
.IR glob .
This flag may be used multiple times to specify more than one glob pattern.
.TP
.BI \-\-mapping_file= filename
Use the given mapping file.
.TP
.B \-\-max_line_length
Maximum line length for includes.
Note that this only affects the comments and their alignment, the maximum line
length can still be exceeded with long filenames (default: 80).
.TP
.B \-\-no_comments
Do not add comments after includes about which symbols the header was required
for.
.TP
.B \-\-no_default_mappings
Do not use the default mappings.
.TP
.B \-\-no_fwd_decls
Do not use forward declarations, and instead always include the required header.
.TP
.B \-\-pch_in_code
Mark the first include in a translation unit as a precompiled header. Use
.B \-\-pch_in_code
to prevent removal of necessary PCH includes. Although
.BR clang (1)
forces PCHs to be listed as prefix headers, the PCH-in-code pattern can be used
with
.BR gcc (1).
.TP
.BI \-\-prefix_header_includes= value
Controls how includes and forward declarations involving prefix headers should
be handled.
Prefix headers are files included via the command-line option
.BR -include .
This option specifies what to do if a prefix header makes includes or forward
declarations obsolete.
The following
.IR value s
are allowed:
.RS
.TP
.B add
New includes are added. This is the default.
.TP
.B keep
No new includes are added, existing ones are kept intact.
.TP
.B remove
No new includes are added, existing ones are removed.
.RE
.TP
.B \-\-quoted_includes_first
When sorting includes, place quoted includes first.
.TP
.BI \-\-regex= dialect
Use the specified regex
.IR dialect
for matching in IWYU:
.RS
.TP
.B llvm
Fast and simple extended regular expressions. This is the default.
.TP
.B ecmascript
Slower but more full-featured regular expressions with support for lookaround
assertions, etc.
.RE
.TP
.B \-\-transitive_includes_only
Do not suggest that a file should add
.IR foo.h " unless " foo.h
is already visible in the file's transitive includes.
.TP
.BI \-\-update_comments
Print full include list with comments, even if there are no
\(lqinclude-what-you-use\(rq violations.
.TP
.BI \-\-verbose= level
Set verbosity. At the highest level, this will dump the AST of the source file
and explain all decisions.
.SH EXIT STATUS
By default
.B include-what-you-use
exits with zero exit code unless there's a critical error, but
.B \-\-error
or
.B \-\-error_always
can be used to customize the exit code depending on invoker expectations.
For an example see below.
.SH MAPPING FILES
Sometimes headers are not meant to be included directly,
and sometimes headers are guaranteed to include other headers.
Since this is hard to tell from the source code alone,
these relationships have to be provided via mapping files or pragma comments.
.PP
A mapping file consists of a comma-separated list of rules enclosed by square
brackets
.BR [] .
A rule can be any of the following:
.TP
.BI "{ include: [" header ", " header "] }"
Declares that instead of the first
.I header
the second can be used.
A header can appear on the left-hand side in multiple rules,
meaning that any of the right-hand side headers can be used instead.
.TP
.BI "{ symbol: [" symbol ", " header "] }"
Declares that to use a
.IR symbol ,
a certain
.I header
should be included.
.TP
.BI "{ ref: " mapping-file " }"
Includes the contents of another
.IR mapping-file .
.PP
The descriptions of headers and symbols are as follows:
.TP
.IB "header\fR := " \(dq include-spec "\(dq, " visibility
Describes a header file. The
.I include-spec
specifies the header file and
.I visibility
specifies whether the header is
.BR public " or " private .
Private headers are not allowed to be included directly.
So every private header file should appear on the left-hand side of a mapping
at least once.
The visibility of a header file has to be the same for all rules it appears in!
.TP
.IB "include-spec\fR := " < system-header-file > \
"\fR | " \e\(dq project-header-file \e\(dq
How the header is
.BR #include d
in a source file.
Quotation marks need to be escaped.
.TP
.IB "symbol\fR := " \(dq symbol-name "\(dq, " visibility
Describes a symbol, for example a type, function or macro. The
.I visibility
is ignored, by convention
.B private
is used.
.PP
Lines starting with
.B #
are treated as comments.
.SH PRAGMA COMMENTS
Pragma comments provide information about the relations between source and
header files and allow to whitelist or blacklist
.BR #include s
and forward declarations.
.PP
All arguments should be enclosed in quotation marks.
.TP
.B // IWYU pragma: keep
Used after
.B #include
directives or forward declarations it ensures that they won't be removed.
.TP
.BR "// IWYU pragma: begin_keep" , " // IWYU pragma: end_keep"
Has the same effect as the previous pragma comment, but applies to a range of
.BR #include s
and forward declarations instead of a single line.
.TP
.B // IWYU pragma: export
Used after an
.B #include
directive or forward declaration it indicates that the current file is
considered to be a provider of any symbol from the included file or declaration.
.TP
.BR "// IWYU pragma: begin_exports" , " // IWYU pragma: end_exports"
Has the same effect as the previous pragma comment, but applies to a range of
.BR #include s
or forward declarations instead of a single line.
.TP
.BR "// IWYU pragma: private" [ ", include \fIheader" ]
Indicates that the current file is considered private,
and (optionally) that any symbol will be provided by
.IR header .
.TP
.BI "// IWYU pragma: no_include " header
States that
.I header
should not be suggested for inclusion.
.TP
.BI "// IWYU pragma: no_forward_declare "symbol
States that
.I symbol
should not be forward-declared.
.TP
.BI "// IWYU pragma: friend " regex
Used in a private header, this indicates that all files matching
.I regex
are allowed to
.B #include
it.
.TP
.B // IWYU pragma: associated
Used in a source file after an
.B #include
directive, this marks the header as associated to the source file.
This is required if source and header filename differ in more than their ending.
Includes from an associated header are assumed in the source file.
.SH FILES
.I /usr/share/include-what-you-use
.RS
Directory containing the standard mapping files.
.SH BUGS
See the
.UR https://github.com/include-what-you-use/include-what-you-use/issues
issue tracker
.UE
on GitHub.
.SH EXAMPLE
It is possible to put
.B include-what-you-use
in place of your compiler to process all source files known to your build system
.PP
.RS
.EX
make \-k CC=include-what-you-use CFLAGS="-Xiwyu --error_always"
.EE
.EX
make \-k CXX=include-what-you-use CXXFLAGS="-Xiwyu --error_always"
.EE
.RE
.PP
With
.B -Xiwyu --error_always
the program always exits with an error code, so
the build system knows that it didn't build an object file. Hence the need for
.BR -k .
It only analyzes source files built by
.BR make (1)
along with their corresponding header files.
If a project has a header file with no corresponding source file,
.B include-what-you-use
will ignore it unless you use the
.B \-\-check_also
option to add it for analysis together with a source file.
.PP
CMake has built-in support for
.B include-what-you-use
as of version 3.3. With the
.B CMAKE_CXX_INCLUDE_WHAT_YOU_USE
option, CMake runs it on every source file after compilation:
.PP
.RS
.EX
cmake \-DCMAKE_CXX_INCLUDE_WHAT_YOU_USE="include-what-you-use <args>" ..
.EE
.RE
.PP
The option is supported for both C and C++, so use
.B CMAKE_C_INCLUDE_WHAT_YOU_USE
for C code.
.SH "SEE ALSO"
.BR clang (1),
.BR make (1)