1UNIFDEF(1)                BSD General Commands Manual               UNIFDEF(1)
2

NAME

4     unifdef, unifdefall — remove preprocessor conditionals from code
5

SYNOPSIS

7     unifdef [-BbcdeKknst] [-Ipath] [-Dsym[=val]] [-Usym] [-iDsym[=val]]
8             [-iUsym] ... [-o outfile] [infile]
9     unifdefall [-Ipath] ... file
10

DESCRIPTION

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.
15
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
27     command line.
28
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 circuit”
33     evaluation is used for the && operator: if either operand is definitely
34     false then the result is false, even if the value of the other operand is
35     unknown.  Similarly, if either operand of || is definitely true then the
36     result is true.
37
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.
44
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.
49
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.
54

OPTIONS

56     -Dsym[=val]
57             Specify that a symbol is defined, and optionally specify what
58             value to give it for the purpose of handling #if and #elif direc‐
59             tives.
60
61     -Usym   Specify that a symbol is undefined.  If the same symbol appears
62             in more than one argument, the last occurrence dominates.
63
64     -B      Compress blank lines around a deleted section.  Mutually exclu‐
65             sive with the -b option.
66
67     -b      Replace removed lines with blank lines instead of deleting them.
68             Mutually exclusive with the -B option.
69
70     -c      If the -c flag is specified, then the operation of unifdef is
71             complemented, i.e., the lines that would have been removed or
72             blanked are retained and vice versa.
73
74     -d      Turn on printing of degugging messages.
75
76     -e      Because unifdef processes its input one line at a time, it cannot
77             remove preprocessor directives that span more than one line.  The
78             most common example of this is a directive with a multi-line com‐
79             ment hanging off its right hand end.  By default, if unifdef has
80             to process such a directive, it will complain that the line is
81             too obfuscated.  The -e option changes the behaviour so that,
82             where possible, such lines are left unprocessed instead of
83             reporting an error.
84
85     -K      Always treat the result of && and || operators as unknown if
86             either operand is unknown, instead of short-circuiting when
87             unknown operands can't affect the result.  This option is for
88             compatibility with older versions of unifdef.
89
90     -k      Process #if and #elif lines with constant expressions.  By
91             default, sections controlled by such lines are passed through
92             unchanged because they typically start “#if 0” and are used as a
93             kind of comment to sketch out future or past development.  It
94             would be rude to strip them out, just as it would be for normal
95             comments.
96
97     -n      Add #line directives to the output following any deleted lines,
98             so that errors produced when compiling the output file correspond
99             to line numbers in the input file.
100
101     -o outfile
102             Write output to the file outfile instead of the standard output.
103             If outfile is the same as the input file, the output is written
104             to a temporary file which is renamed into place when unifdef com‐
105             pletes successfully.
106
107     -s      Instead of processing the input file as usual, this option causes
108             unifdef to produce a list of symbols that appear in expressions
109             that unifdef understands.  It is useful in conjunction with the
110             -dM option of cpp(1) for creating unifdef command lines.
111
112     -t      Disables parsing for C comments and line continuations, which is
113             useful for plain text.
114
115     -iDsym[=val]
116     -iUsym  Ignore #ifdefs.  If your C code uses #ifdefs to delimit non-C
117             lines, such as comments or code which is under construction, then
118             you must tell unifdef which symbols are used for that purpose so
119             that it will not try to parse comments and line continuations
120             inside those #ifdefs.  You can specify ignored symbols with
121             -iDsym[=val] and -iUsym similar to -Dsym[=val] and -Usym above.
122
123     -Ipath  Specifies to unifdefall an additional place to look for #include
124             files.  This option is ignored by unifdef for compatibility with
125             cpp(1) and to simplify the implementation of unifdefall.
126
127     The unifdef utility copies its output to stdout and will take its input
128     from stdin if no file argument is given.
129
130     The unifdef utility works nicely with the -Dsym option of diff(1).
131

EXIT STATUS

133     The unifdef utility exits 0 if the output is an exact copy of the input,
134     1 if not, and 2 if in trouble.
135

DIAGNOSTICS

137     Too many levels of nesting.
138
139     Inappropriate #elif, #else or #endif.
140
141     Obfuscated preprocessor control line.
142
143     Premature EOF (with the line number of the most recent unterminated #if).
144
145     EOF in comment.
146

SEE ALSO

148     cpp(1), diff(1)
149

HISTORY

151     The unifdef command appeared in 2.9BSD.  ANSI C support was added in
152     FreeBSD 4.7.
153

AUTHORS

155     The original implementation was written by Dave Yost <Dave@Yost.com>.
156     Tony Finch <dot@dotat.at> rewrote it to support ANSI C.
157

BUGS

159     Expression evaluation is very limited.
160
161     Preprocessor control lines split across more than one physical line
162     (because of comments or backslash-newline) cannot be handled in every
163     situation.
164
165     Trigraphs are not recognized.
166
167     There is no support for symbols with different definitions at different
168     points in the source file.
169
170     The text-mode and ignore functionality does not correspond to modern
171     cpp(1) behaviour.
172
173BSD                            January 19, 2010                            BSD
Impressum