Disappearing function call

Chris Torek chris at umcp-cs.UUCP
Wed Oct 8 10:01:22 AEST 1986


The problem: to include debug calls, with variable argument lists,
such that they may be conditionally compiled (a la #if), but without
cluttering the source with `#ifdef'.

Two standard solutions, and their problems:

1:	#ifdef DEBUG
	#define	debug(x)	debug_ x
	#else
	#define	debug(x)	/* null */
	#endif
	...
	foo(foostring, foovalue)
		char *foostring;
		int foovalue;
	{
		debug(("foo(%s, %d)", foostring, foovalue));
		...

Problem: The call to `debug' `looks funny'.  (This is not all that
trivial; style is important to readability.)

2:	#ifdef DEBUG
	#define	debug
	#else
	#define debug debug_
	#endif
	...
		debug("foo(%s, %d)", foostring, foovalue);
		...

Problems: Some preprocessors apparently gripe about argument
mismatches, requiring an extra space: `debug ("foo(%s, %d)", ...)'.
Other compilers generate code and/or data even when DEBUG is turned
off, since this preprocesses to

		("foo(%s, %d)", foostring, foovalue);

A third solution, which no one seems to have proposed here, is to
write your own `pre-prepocessor processor'.  Here for your amusment
and/or edification is a lex specification that matches C code
inputs.  I used it for `grim', a program pessimiser.  (An optimiser
makes code better, so a pessimiser makes code worse.  It was fun to
write.)

Caveat: the lexer matches comments in their entirety, and thus
requires a large lex buffer.  This can be avoided by using `BEGIN'
and extra lex states, but I wanted the comment text too.  It also
does not match C perfectly, only as well as I needed.  It should,
however, suffice for a debug remover.

%{
#undef YYLMAX
#define YYLMAX		32768
#define	yywrap()	1
%}
id				[a-zA-Z_][a-zA-Z0-9_]*
whitespace			[ \t\n]
comment				\/\*(\*[^/]|[^*])*\*+\/
charconst			\'(\\\'|[^'])*\'
strings				\"(\\\"|[^"])*\"
number				[0-9]+
%%
^#[ \t]*define[ \t]+.*	{ MungeNumberDefine(); }
^#.*			{ printf("%s", yytext); }
{comment}		{ MungeComment(); }
{strings}		{ printf("%s", yytext); }
{charconst}		{ printf("%s", yytext); }
{id}			{ MungeID();}
{whitespace}		{ putchar(yytext[0]); }
{number}[lL]		{ printf("%s", yytext); }
{number}		{ printf("%s", yytext); }
.			{ putchar(yytext[0]); }
%%
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list