1 UNIFDEF(1) Programmer's Manual UNIFDEF(1)
4 unifdef, unifdefall -- remove preprocessor conditionals from code
7 unifdef [-bBcdeKknsStV] [-Ipath] [-Dsym[=val]] [-Usym] [-iDsym[=val]]
8 [-iUsym] ... [-o outfile] [infile]
9 unifdefall [-Ipath] ... file
12 The unifdef utility selectively processes conditional cpp(1) directives.
13 It removes from a file both the directives and any additional text that
14 they specify should be removed, while otherwise leaving the file alone.
16 The unifdef utility acts on #if, #ifdef, #ifndef, #elif, #else, and
17 #endif lines. A directive is only processed if the symbols specified on
18 the command line are sufficient to allow unifdef to get a definite value
19 for its control expression. If the result is false, the directive and
20 the following lines under its control are removed. If the result is
21 true, only the directive is removed. An #ifdef or #ifndef directive is
22 passed through unchanged if its controlling symbol is not specified on
23 the command line. Any #if or #elif control expression that has an
24 unknown value or that unifdef cannot parse is passed through unchanged.
25 By default, unifdef ignores #if and #elif lines with constant expres-
26 sions; it can be told to process them by specifying the -k flag on the
29 It understands a commonly-used subset of the expression syntax for #if
30 and #elif lines: integer constants, integer values of symbols defined on
31 the command line, the defined() operator, the operators !, <, >, <=, >=,
32 ==, !=, &&, ||, and parenthesized expressions. A kind of ``short
33 circuit'' evaluation is used for the && operator: if either operand is
34 definitely false then the result is false, even if the value of the other
35 operand is unknown. Similarly, if either operand of || is definitely
36 true then the result is true.
38 In most cases, the unifdef utility does not distinguish between object-
39 like macros (without arguments) and function-like arguments (with argu-
40 ments). If a macro is not explicitly defined, or is defined with the -D
41 flag on the command-line, its arguments are ignored. If a macro is
42 explicitly undefined on the command line with the -U flag, it may not
43 have any arguments since this leads to a syntax error.
45 The unifdef utility understands just enough about C to know when one of
46 the directives is inactive because it is inside a comment, or affected by
47 a backslash-continued line. It spots unusually-formatted preprocessor
48 directives and knows when the layout is too odd for it to handle.
50 A script called unifdefall can be used to remove all conditional cpp(1)
51 directives from a file. It uses unifdef -s and cpp -dM to get lists of
52 all the controlling symbols and their definitions (or lack thereof), then
53 invokes unifdef with appropriate arguments to process the file.
57 Specify that a symbol is defined to a given value which is used
58 when evaluating #if and #elif control expressions.
60 -Dsym Specify that a symbol is defined to the value 1.
62 -Usym Specify that a symbol is undefined. If the same symbol appears
63 in more than one argument, the last occurrence dominates.
65 -b Replace removed lines with blank lines instead of deleting them.
66 Mutually exclusive with the -B option.
68 -B Compress blank lines around a deleted section. Mutually exclu-
69 sive with the -b option.
71 -c If the -c flag is specified, then the operation of unifdef is
72 complemented, i.e., the lines that would have been removed or
73 blanked are retained and vice versa.
75 -d Turn on printing of degugging messages.
77 -e Because unifdef processes its input one line at a time, it cannot
78 remove preprocessor directives that span more than one line. The
79 most common example of this is a directive with a multi-line com-
80 ment hanging off its right hand end. By default, if unifdef has
81 to process such a directive, it will complain that the line is
82 too obfuscated. The -e option changes the behaviour so that,
83 where possible, such lines are left unprocessed instead of
86 -K Always treat the result of && and || operators as unknown if
87 either operand is unknown, instead of short-circuiting when
88 unknown operands can't affect the result. This option is for
89 compatibility with older versions of unifdef.
91 -k Process #if and #elif lines with constant expressions. By
92 default, sections controlled by such lines are passed through
93 unchanged because they typically start ``#if 0'' and are used as
94 a kind of comment to sketch out future or past development. It
95 would be rude to strip them out, just as it would be for normal
98 -n Add #line directives to the output following any deleted lines,
99 so that errors produced when compiling the output file correspond
100 to line numbers in the input file.
103 Write output to the file outfile instead of the standard output.
104 If outfile is the same as the input file, the output is written
105 to a temporary file which is renamed into place when unifdef com-
108 -s Instead of processing the input file as usual, this option causes
109 unifdef to produce a list of symbols that appear in expressions
110 that unifdef understands. It is useful in conjunction with the
111 -dM option of cpp(1) for creating unifdef command lines.
113 -S Like the -s option, but the nesting depth of each symbol is also
114 printed. This is useful for working out the number of possible
115 combinations of interdependent defined/undefined symbols.
117 -t Disables parsing for C comments and line continuations, which is
118 useful for plain text.
121 -iUsym Ignore #ifdefs. If your C code uses #ifdefs to delimit non-C
122 lines, such as comments or code which is under construction, then
123 you must tell unifdef which symbols are used for that purpose so
124 that it will not try to parse comments and line continuations
125 inside those #ifdefs. You can specify ignored symbols with
126 -iDsym[=val] and -iUsym similar to -Dsym[=val] and -Usym above.
128 -Ipath Specifies to unifdefall an additional place to look for #include
129 files. This option is ignored by unifdef for compatibility with
130 cpp(1) and to simplify the implementation of unifdefall.
132 -V Print version details.
134 The unifdef utility copies its output to stdout and will take its input
135 from stdin if no file argument is given.
137 The unifdef utility works nicely with the -Dsym option of diff(1).
140 The unifdef utility exits 0 if the output is an exact copy of the input,
141 1 if not, and 2 if in trouble.
144 Too many levels of nesting.
146 Inappropriate #elif, #else or #endif.
148 Obfuscated preprocessor control line.
150 Premature EOF (with the line number of the most recent unterminated #if).
158 The unifdef command appeared in 2.9BSD. ANSI C support was added in
162 The original implementation was written by Dave Yost <Dave@Yost.com>.
163 Tony Finch <dot@dotat.at> rewrote it to support ANSI C.
166 Expression evaluation is very limited.
168 Preprocessor control lines split across more than one physical line
169 (because of comments or backslash-newline) cannot be handled in every
172 Trigraphs are not recognized.
174 There is no support for symbols with different definitions at different
175 points in the source file.
177 The text-mode and ignore functionality does not correspond to modern