]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/unifdef/unifdef.1
Upgrade our copy of llvm/clang to trunk r162107. With thanks to
[FreeBSD/FreeBSD.git] / usr.bin / unifdef / unifdef.1
1 .\" Copyright (c) 1985, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\" Copyright (c) 2002 - 2010 Tony Finch <dot@dotat.at>.  All rights reserved.
4 .\"
5 .\" This code is derived from software contributed to Berkeley by
6 .\" Dave Yost. It was rewritten to support ANSI C by Tony Finch.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\" $FreeBSD$
33 .\"
34 .Dd March 11, 2010
35 .Dt UNIFDEF 1
36 .Os
37 .Sh NAME
38 .Nm unifdef , unifdefall
39 .Nd remove preprocessor conditionals from code
40 .Sh SYNOPSIS
41 .Nm
42 .Op Fl bBcdeKknsStV
43 .Op Fl I Ns Ar path
44 .Op Fl D Ns Ar sym Ns Op = Ns Ar val
45 .Op Fl U Ns Ar sym
46 .Op Fl iD Ns Ar sym Ns Op = Ns Ar val
47 .Op Fl iU Ns Ar sym
48 .Ar ...
49 .Op Fl o Ar outfile
50 .Op Ar infile
51 .Nm unifdefall
52 .Op Fl I Ns Ar path
53 .Ar ...
54 .Ar file
55 .Sh DESCRIPTION
56 The
57 .Nm
58 utility selectively processes conditional
59 .Xr cpp 1
60 directives.
61 It removes from a file
62 both the directives
63 and any additional text that they specify should be removed,
64 while otherwise leaving the file alone.
65 .Pp
66 The
67 .Nm
68 utility acts on
69 .Ic #if , #ifdef , #ifndef , #elif , #else ,
70 and
71 .Ic #endif
72 lines.
73 A directive is only processed
74 if the symbols specified on the command line are sufficient to allow
75 .Nm
76 to get a definite value for its control expression.
77 If the result is false,
78 the directive and the following lines under its control are removed.
79 If the result is true,
80 only the directive is removed.
81 An
82 .Ic #ifdef
83 or
84 .Ic #ifndef
85 directive is passed through unchanged
86 if its controlling symbol is not specified on the command line.
87 Any
88 .Ic #if
89 or
90 .Ic #elif
91 control expression that has an unknown value or that
92 .Nm
93 cannot parse is passed through unchanged.
94 By default,
95 .Nm
96 ignores
97 .Ic #if
98 and
99 .Ic #elif
100 lines with constant expressions;
101 it can be told to process them by specifying the
102 .Fl k
103 flag on the command line.
104 .Pp
105 It understands a commonly-used subset
106 of the expression syntax for
107 .Ic #if
108 and
109 .Ic #elif
110 lines:
111 integer constants,
112 integer values of symbols defined on the command line,
113 the
114 .Fn defined
115 operator,
116 the operators
117 .Ic \&! , < , > , <= , >= , == , != , && , || ,
118 and parenthesized expressions.
119 A kind of
120 .Dq "short circuit"
121 evaluation is used for the
122 .Ic &&
123 operator:
124 if either operand is definitely false then the result is false,
125 even if the value of the other operand is unknown.
126 Similarly,
127 if either operand of
128 .Ic ||
129 is definitely true then the result is true.
130 .Pp
131 In most cases, the
132 .Nm
133 utility does not distinguish between object-like macros
134 (without arguments) and function-like arguments (with arguments).
135 If a macro is not explicitly defined, or is defined with the
136 .Fl D
137 flag on the command-line, its arguments are ignored.
138 If a macro is explicitly undefined on the command line with the
139 .Fl U
140 flag, it may not have any arguments since this leads to a syntax error.
141 .Pp
142 The
143 .Nm
144 utility understands just enough about C
145 to know when one of the directives is inactive
146 because it is inside
147 a comment,
148 or affected by a backslash-continued line.
149 It spots unusually-formatted preprocessor directives
150 and knows when the layout is too odd for it to handle.
151 .Pp
152 A script called
153 .Nm unifdefall
154 can be used to remove all conditional
155 .Xr cpp 1
156 directives from a file.
157 It uses
158 .Nm Fl s
159 and
160 .Nm cpp Fl dM
161 to get lists of all the controlling symbols
162 and their definitions (or lack thereof),
163 then invokes
164 .Nm
165 with appropriate arguments to process the file.
166 .Sh OPTIONS
167 .Bl -tag -width indent -compact
168 .It Fl D Ns Ar sym Ns = Ns Ar val
169 Specify that a symbol is defined to a given value
170 which is used when evaluating
171 .Ic #if
172 and
173 .Ic #elif
174 control expressions.
175 .Pp
176 .It Fl D Ns Ar sym
177 Specify that a symbol is defined to the value 1.
178 .Pp
179 .It Fl U Ns Ar sym
180 Specify that a symbol is undefined.
181 If the same symbol appears in more than one argument,
182 the last occurrence dominates.
183 .Pp
184 .It Fl b
185 Replace removed lines with blank lines
186 instead of deleting them.
187 Mutually exclusive with the
188 .Fl B
189 option.
190 .Pp
191 .It Fl B
192 Compress blank lines around a deleted section.
193 Mutually exclusive with the
194 .Fl b
195 option.
196 .Pp
197 .It Fl c
198 If the
199 .Fl c
200 flag is specified,
201 then the operation of
202 .Nm
203 is complemented,
204 i.e., the lines that would have been removed or blanked
205 are retained and vice versa.
206 .Pp
207 .It Fl d
208 Turn on printing of debugging messages.
209 .Pp
210 .It Fl e
211 Because
212 .Nm
213 processes its input one line at a time,
214 it cannot remove preprocessor directives that span more than one line.
215 The most common example of this is a directive with a multi-line
216 comment hanging off its right hand end.
217 By default,
218 if
219 .Nm
220 has to process such a directive,
221 it will complain that the line is too obfuscated.
222 The
223 .Fl e
224 option changes the behaviour so that,
225 where possible,
226 such lines are left unprocessed instead of reporting an error.
227 .Pp
228 .It Fl K
229 Always treat the result of
230 .Ic &&
231 and
232 .Ic ||
233 operators as unknown if either operand is unknown,
234 instead of short-circuiting when unknown operands can't affect the result.
235 This option is for compatibility with older versions of
236 .Nm .
237 .Pp
238 .It Fl k
239 Process
240 .Ic #if
241 and
242 .Ic #elif
243 lines with constant expressions.
244 By default, sections controlled by such lines are passed through unchanged
245 because they typically start
246 .Dq Li "#if 0"
247 and are used as a kind of comment to sketch out future or past development.
248 It would be rude to strip them out, just as it would be for normal comments.
249 .Pp
250 .It Fl n
251 Add
252 .Li #line
253 directives to the output following any deleted lines,
254 so that errors produced when compiling the output file correspond to
255 line numbers in the input file.
256 .Pp
257 .It Fl o Ar outfile
258 Write output to the file
259 .Ar outfile
260 instead of the standard output.
261 If
262 .Ar outfile
263 is the same as the input file,
264 the output is written to a temporary file
265 which is renamed into place when
266 .Nm
267 completes successfully.
268 .Pp
269 .It Fl s
270 Instead of processing the input file as usual,
271 this option causes
272 .Nm
273 to produce a list of symbols that appear in expressions
274 that
275 .Nm
276 understands.
277 It is useful in conjunction with the
278 .Fl dM
279 option of
280 .Xr cpp 1
281 for creating
282 .Nm
283 command lines.
284 .Pp
285 .It Fl S
286 Like the
287 .Fl s
288 option, but the nesting depth of each symbol is also printed.
289 This is useful for working out the number of possible combinations
290 of interdependent defined/undefined symbols.
291 .Pp
292 .It Fl t
293 Disables parsing for C comments
294 and line continuations,
295 which is useful
296 for plain text.
297 .Pp
298 .It Fl iD Ns Ar sym Ns Op = Ns Ar val
299 .It Fl iU Ns Ar sym
300 Ignore
301 .Ic #ifdef Ns s .
302 If your C code uses
303 .Ic #ifdef Ns s
304 to delimit non-C lines,
305 such as comments
306 or code which is under construction,
307 then you must tell
308 .Nm
309 which symbols are used for that purpose so that it will not try to parse
310 comments
311 and line continuations
312 inside those
313 .Ic #ifdef Ns s .
314 You can specify ignored symbols with
315 .Fl iD Ns Ar sym Ns Oo = Ns Ar val Oc
316 and
317 .Fl iU Ns Ar sym
318 similar to
319 .Fl D Ns Ar sym Ns Op = Ns Ar val
320 and
321 .Fl U Ns Ar sym
322 above.
323 .Pp
324 .It Fl I Ns Ar path
325 Specifies to
326 .Nm unifdefall
327 an additional place to look for
328 .Ic #include
329 files.
330 This option is ignored by
331 .Nm
332 for compatibility with
333 .Xr cpp 1
334 and to simplify the implementation of
335 .Nm unifdefall .
336 .Pp
337 .It Fl V
338 Print version details.
339 .El
340 .Pp
341 The
342 .Nm
343 utility copies its output to
344 .Em stdout
345 and will take its input from
346 .Em stdin
347 if no
348 .Ar file
349 argument is given.
350 .Pp
351 The
352 .Nm
353 utility works nicely with the
354 .Fl D Ns Ar sym
355 option of
356 .Xr diff 1 .
357 .Sh EXIT STATUS
358 The
359 .Nm
360 utility exits 0 if the output is an exact copy of the input,
361 1 if not, and 2 if in trouble.
362 .Sh DIAGNOSTICS
363 .Bl -item
364 .It
365 Too many levels of nesting.
366 .It
367 Inappropriate
368 .Ic #elif ,
369 .Ic #else
370 or
371 .Ic #endif .
372 .It
373 Obfuscated preprocessor control line.
374 .It
375 Premature
376 .Tn EOF
377 (with the line number of the most recent unterminated
378 .Ic #if ) .
379 .It
380 .Tn EOF
381 in comment.
382 .El
383 .Sh SEE ALSO
384 .Xr cpp 1 ,
385 .Xr diff 1
386 .Sh HISTORY
387 The
388 .Nm
389 command appeared in
390 .Bx 2.9 .
391 .Tn ANSI\~C
392 support was added in
393 .Fx 4.7 .
394 .Sh AUTHORS
395 The original implementation was written by
396 .An Dave Yost Aq Dave@Yost.com .
397 .An Tony Finch Aq dot@dotat.at
398 rewrote it to support
399 .Tn ANSI\~C .
400 .Sh BUGS
401 Expression evaluation is very limited.
402 .Pp
403 Preprocessor control lines split across more than one physical line
404 (because of comments or backslash-newline)
405 cannot be handled in every situation.
406 .Pp
407 Trigraphs are not recognized.
408 .Pp
409 There is no support for symbols with different definitions at
410 different points in the source file.
411 .Pp
412 The text-mode and ignore functionality does not correspond to modern
413 .Xr cpp 1
414 behaviour.