Article View: net.lang.c
Article #9521Re: Disappearing function call
From: Chris Torek
Date: Wed, 08 Oct 1986 00:01
Date: Wed, 08 Oct 1986 00:01
78 lines
2889 bytes
2889 bytes
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@umcp-cs ARPA: chris@mimsy.umd.edu
Technical Details
Message-ID:
<3731@umcp-cs.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!rutgers!seismo!umcp-cs!chrisReferences:
<361@cullvax.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!rutgers!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c Subject: Re: Disappearing function call Message-ID: <3731@umcp-cs.UUCP> Date: Tue, 7-Oct-86 20:01:22 EDT Article-I.D.: umcp-cs.3731 Posted: Tue Oct 7 20:01:22 1986 Date-Received: Thu, 9-Oct-86 04:51:56 EDT References: <361@cullvax.UUCP> Organization: Computer Sci. Dept, U of Maryland, College Park, MD Lines: 78