UNIFDEF(1) | General Commands Manual | UNIFDEF(1) |
unifdef
,
unifdefall
— remove
preprocessor conditionals from code
unifdef |
[-bBcdeKknsStV ]
[-I path]
[-D sym[=val]]
[-U sym]
[-iD sym[=val]]
[-iU sym]
... [-o
outfile] [infile] |
unifdefall |
[-I path]
... file |
The unifdef
utility selectively processes
conditional cpp(1) directives. It removes
from a file both the directives and any additional text that they specify
should be removed, while otherwise leaving the file alone.
The unifdef
utility acts on
#if
, #ifdef
,
#ifndef
, #elif
,
#else
, and #endif
lines. A
directive is only processed if the symbols specified on the command line are
sufficient to allow unifdef
to get a definite value
for its control expression. If the result is false, the directive and the
following lines under its control are removed. If the result is true, only
the directive is removed. An #ifdef
or
#ifndef
directive is passed through unchanged if its
controlling symbol is not specified on the command line. Any
#if
or #elif
control
expression that has an unknown value or that unifdef
cannot parse is passed through unchanged. By default,
unifdef
ignores #if
and
#elif
lines with constant expressions; it can be
told to process them by specifying the -k
flag on
the command line.
It understands a commonly-used subset of the
expression syntax for #if
and
#elif
lines: integer constants, integer values of
symbols defined on the command line, the
defined
()
operator, the operators !
,
<
, >
,
<=
, >=
,
==
, !=
,
&&
, ||
, and
parenthesized expressions. A kind of “short circuit”
evaluation is used for the &&
operator: if
either operand is definitely false then the result is false, even if the
value of the other operand is unknown. Similarly, if either operand of
||
is definitely true then the result is true.
In most cases, the unifdef
utility does
not distinguish between object-like macros (without arguments) and
function-like arguments (with arguments). If a macro is not explicitly
defined, or is defined with the -D
flag on the
command-line, its arguments are ignored. If a macro is explicitly undefined
on the command line with the -U
flag, it may not
have any arguments since this leads to a syntax error.
The unifdef
utility understands just
enough about C to know when one of the directives is inactive because it is
inside a comment, or affected by a backslash-continued line. It spots
unusually-formatted preprocessor directives and knows when the layout is too
odd for it to handle.
A script called unifdefall
can be used to
remove all conditional cpp(1) directives
from a file. It uses unifdef
-s
and cpp
-dM
to get lists of all the controlling symbols and
their definitions (or lack thereof), then invokes
unifdef
with appropriate arguments to process the
file.
-D
sym=val#if
and #elif
control expressions.
-D
sym-U
sym-b
-B
option.
-B
-b
option.
-c
-c
flag is specified, then the operation of
unifdef
is complemented, i.e., the lines that
would have been removed or blanked are retained and vice versa.
-d
-e
unifdef
processes its input one line at a
time, it cannot remove preprocessor directives that span more than one
line. The most common example of this is a directive with a multi-line
comment hanging off its right hand end. By default, if
unifdef
has to process such a directive, it will
complain that the line is too obfuscated. The -e
option changes the behaviour so that, where possible, such lines are left
unprocessed instead of reporting an error.
-K
&&
and
||
operators as unknown if either operand is
unknown, instead of short-circuiting when unknown operands can't affect
the result. This option is for compatibility with older versions of
unifdef
.
-k
#if
and #elif
lines with constant expressions. By default, sections controlled by such
lines are passed through unchanged because they typically start
“#if 0
” and are used as a kind of
comment to sketch out future or past development. It would be rude to
strip them out, just as it would be for normal comments.
-n
#line
directives to the output following any
deleted lines, so that errors produced when compiling the output file
correspond to line numbers in the input file.
-o
outfileunifdef
completes successfully.
-s
unifdef
to produce a list of symbols that appear
in expressions that unifdef
understands. It is
useful in conjunction with the -dM
option of
cpp(1) for creating
unifdef
command lines.
-S
-s
option, but the nesting depth of each
symbol is also printed. This is useful for working out the number of
possible combinations of interdependent defined/undefined symbols.
-t
-iD
sym[=val]-iU
sym#ifdef
s. If your C code uses
#ifdef
s to delimit non-C lines, such as comments
or code which is under construction, then you must tell
unifdef
which symbols are used for that purpose so
that it will not try to parse comments and line continuations inside those
#ifdef
s. You can specify ignored symbols with
-iD
sym[=val]
and -iU
sym similar to
-D
sym[=val]
and -U
sym above.
-I
pathunifdefall
an additional place to
look for #include
files. This option is ignored by
unifdef
for compatibility with
cpp(1) and to simplify the
implementation of unifdefall
.
-V
The unifdef
utility copies its
output to stdout
and will take its input from
stdin if
no file argument is given.
The unifdef
utility works nicely with the
-D
sym option of
diff(1).
The unifdef
utility exits 0 if the output
is an exact copy of the input, 1 if not, and 2 if in trouble.
#elif
, #else
or #endif
.#if
).The unifdef
command appeared in
2.9BSD. ANSI C support was added in
FreeBSD 4.7.
The original implementation was written by Dave
Yost ⟨Dave@Yost.com⟩.
Tony Finch ⟨dot@dotat.at⟩ rewrote it to
support ANSI C.
Expression evaluation is very limited.
Preprocessor control lines split across more than one physical line (because of comments or backslash-newline) cannot be handled in every situation.
Trigraphs are not recognized.
There is no support for symbols with different definitions at different points in the source file.
The text-mode and ignore functionality does not correspond to modern cpp(1) behaviour.
March 11, 2010 | macOS 14.6 |