It is probably a bad idea to unconditionally process directives with

constant controlling expressions: in particular, removing #if 0 sections
is considered "rude". This commit changes the default so that such
things are passed through unchanged, and the old behaviour can be had
with the -k "kill konsts" flag.

Suggested by:	markm
MFC after:	3 weeks
This commit is contained in:
Tony Finch 2002-09-24 19:27:44 +00:00
parent 73be6d69ef
commit 352d0a6932
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=103906
2 changed files with 41 additions and 10 deletions

View File

@ -33,10 +33,10 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" @(#)unifdef.1 8.2 (Berkeley) 4/1/94 .\" @(#)unifdef.1 8.2 (Berkeley) 4/1/94
.\" $dotat: things/unifdef.1,v 1.23 2002/05/14 22:15:03 fanf Exp $ .\" $dotat: things/unifdef.1,v 1.25 2002/09/24 19:16:29 fanf2 Exp $
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd April 26, 2002 .Dd September 24, 2002
.Dt UNIFDEF 1 .Dt UNIFDEF 1
.Os .Os
.Sh NAME .Sh NAME
@ -44,7 +44,7 @@
.Nd remove preprocessor conditionals from code .Nd remove preprocessor conditionals from code
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl clst .Op Fl cklst
.Oo .Oo
.Fl I Ns Ar path .Fl I Ns Ar path
.Fl D Ns Ar sym Ns Op = Ns Ar val .Fl D Ns Ar sym Ns Op = Ns Ar val
@ -97,6 +97,14 @@ and
directives are only processed directives are only processed
if the symbol is specified on the command line, if the symbol is specified on the command line,
otherwise they are also passed through unchanged. otherwise they are also passed through unchanged.
Constant
.Ic #if
and
.Ic #elif
expressions are passed through by default,
or they may be processed by specifying the
.Fl k
flag.
.Pp .Pp
The The
.Nm .Nm
@ -153,6 +161,18 @@ is complemented,
i.e., the lines that would have been removed or blanked i.e., the lines that would have been removed or blanked
are retained and vice versa. are retained and vice versa.
.Pp .Pp
.It Fl k
Process
.Ic #if
and
.Ic #elif
lines with constant expressions.
By default, sections controlled by such lines are passed through unchanged
because they typically start
.Li #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.
.Pp
.It Fl l .It Fl l
Replace removed lines with blank lines Replace removed lines with blank lines
instead of deleting them. instead of deleting them.

View File

@ -44,7 +44,7 @@ static const char copyright[] =
#ifdef __IDSTRING #ifdef __IDSTRING
__IDSTRING(Berkeley, "@(#)unifdef.c 8.1 (Berkeley) 6/6/93"); __IDSTRING(Berkeley, "@(#)unifdef.c 8.1 (Berkeley) 6/6/93");
__IDSTRING(NetBSD, "$NetBSD: unifdef.c,v 1.8 2000/07/03 02:51:36 matt Exp $"); __IDSTRING(NetBSD, "$NetBSD: unifdef.c,v 1.8 2000/07/03 02:51:36 matt Exp $");
__IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.73 2002/05/21 17:33:41 fanf Exp $"); __IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.75 2002/09/24 19:16:29 fanf2 Exp $");
#endif #endif
#ifdef __FBSDID #ifdef __FBSDID
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
@ -184,6 +184,7 @@ const char *filename;
int linenum; /* current line number */ int linenum; /* current line number */
int stifline; /* start of current #if */ int stifline; /* start of current #if */
int stqcline; /* start of current coment or quote */ int stqcline; /* start of current coment or quote */
bool keepthis; /* ignore this #if's value 'cause it's const */
#define MAXLINE 1024 #define MAXLINE 1024
#define KWSIZE 8 #define KWSIZE 8
@ -193,6 +194,7 @@ char *keyword; /* used for editing #elif's */
bool complement; /* -c option in effect: do the complement */ bool complement; /* -c option in effect: do the complement */
bool debugging; /* -d option in effect: debugging reports */ bool debugging; /* -d option in effect: debugging reports */
bool killconsts; /* -k option in effect: eval constant #ifs */
bool lnblank; /* -l option in effect: blank deleted lines */ bool lnblank; /* -l option in effect: blank deleted lines */
bool symlist; /* -s option in effect: output symbol list */ bool symlist; /* -s option in effect: output symbol list */
bool text; /* -t option in effect: this is a text file */ bool text; /* -t option in effect: this is a text file */
@ -222,6 +224,7 @@ int findsym(const char *);
void flushline(bool); void flushline(bool);
int getline(char *, int, FILE *, bool); int getline(char *, int, FILE *, bool);
Linetype ifeval(const char **); Linetype ifeval(const char **);
int main(int, char **);
const char *skipcomment(const char *); const char *skipcomment(const char *);
const char *skipquote(const char *, Quote_state); const char *skipquote(const char *, Quote_state);
const char *skipsym(const char *); const char *skipsym(const char *);
@ -234,7 +237,7 @@ main(int argc, char *argv[])
{ {
int opt; int opt;
while ((opt = getopt(argc, argv, "i:D:U:I:cdlst")) != -1) while ((opt = getopt(argc, argv, "i:D:U:I:cdklst")) != -1)
switch (opt) { switch (opt) {
case 'i': /* treat stuff controlled by these symbols as text */ case 'i': /* treat stuff controlled by these symbols as text */
/* /*
@ -262,6 +265,9 @@ main(int argc, char *argv[])
case 'c': /* treat -D as -U and vice versa */ case 'c': /* treat -D as -U and vice versa */
complement = true; complement = true;
break; break;
case 'k': /* process constant #ifs */
killconsts = true;
break;
case 'd': case 'd':
debugging = true; debugging = true;
break; break;
@ -305,7 +311,7 @@ void
usage(void) usage(void)
{ {
fprintf (stderr, "usage: %s", fprintf (stderr, "usage: %s",
"unifdef [-cdlst] [[-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym]] ... [file]\n"); "unifdef [-cdklst] [[-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym]] ... [file]\n");
exit (2); exit (2);
} }
@ -535,20 +541,20 @@ checkline(int *cursym)
} else if (strcmp(kw, "if") == 0) { } else if (strcmp(kw, "if") == 0) {
retval = ifeval(&cp); retval = ifeval(&cp);
cp = skipcomment(cp); cp = skipcomment(cp);
if (*cp != '\n') if (*cp != '\n' || keepthis)
retval = LT_IF; retval = LT_IF;
*cursym = 0; *cursym = 0;
} else if (strcmp(kw, "elif") == 0) { } else if (strcmp(kw, "elif") == 0) {
retval = ifeval(&cp); retval = ifeval(&cp);
cp = skipcomment(cp); cp = skipcomment(cp);
if (*cp != '\n' || keepthis)
retval = LT_ELIF;
if (retval == LT_IF) if (retval == LT_IF)
retval = LT_ELIF; retval = LT_ELIF;
if (retval == LT_TRUE) if (retval == LT_TRUE)
retval = LT_ELTRUE; retval = LT_ELTRUE;
if (retval == LT_FALSE) if (retval == LT_FALSE)
retval = LT_ELFALSE; retval = LT_ELFALSE;
if (*cp != '\n')
retval = LT_ELIF;
*cursym = 0; *cursym = 0;
} else if (strcmp(kw, "else") == 0) } else if (strcmp(kw, "else") == 0)
retval = LT_ELSE; retval = LT_ELSE;
@ -605,6 +611,7 @@ elif2endif(void)
/* /*
* Function for evaluating the innermost parts of expressions, * Function for evaluating the innermost parts of expressions,
* viz. !expr (expr) defined(symbol) symbol number * viz. !expr (expr) defined(symbol) symbol number
* We reset the keepthis flag when we find a non-constant subexpression.
*/ */
Linetype Linetype
eval_unary(struct ops *ops, int *valp, const char **cpp) eval_unary(struct ops *ops, int *valp, const char **cpp)
@ -646,6 +653,7 @@ eval_unary(struct ops *ops, int *valp, const char **cpp)
cp = skipcomment(cp); cp = skipcomment(cp);
if (*cp++ != ')') if (*cp++ != ')')
return LT_IF; return LT_IF;
keepthis = false;
} else if (!endsym(*cp)) { } else if (!endsym(*cp)) {
debug("eval%d symbol", ops - eval_ops); debug("eval%d symbol", ops - eval_ops);
sym = findsym(cp); sym = findsym(cp);
@ -659,6 +667,7 @@ eval_unary(struct ops *ops, int *valp, const char **cpp)
return LT_IF; return LT_IF;
} }
cp = skipsym(cp); cp = skipsym(cp);
keepthis = false;
} else } else
return LT_IF; return LT_IF;
@ -703,13 +712,15 @@ eval_table(struct ops *ops, int *valp, const char **cpp)
/* /*
* Evaluate the expression on a #if or #elif line. If we can work out * Evaluate the expression on a #if or #elif line. If we can work out
* the result we return LT_TRUE or LT_FALSE accordingly, otherwise we * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
* return just a generic LT_IF. * return just a generic LT_IF. If the expression is constant and
* we are not processing constant #ifs then the keepthis flag is true.
*/ */
Linetype Linetype
ifeval(const char **cpp) ifeval(const char **cpp)
{ {
int val; int val;
debug("eval %s", *cpp); debug("eval %s", *cpp);
keepthis = killconsts ? false : true;
return eval_table(eval_ops, &val, cpp); return eval_table(eval_ops, &val, cpp);
} }