[comp.sys.mac.programmer] ?Bug? in LsC Preprocessor.

goers@julius.cs.umn.edu (03/28/89)

Ho Netland,

I'm having a problem with a macro in LsC.  The following macro is preprocessed
"correctly" with the Sun and Sequent C preprocessors but in LsC I get
just a syntax error.  

#define check_size(name) \
	if (e_/**/name >= l_/**/name) { \
	    register nsize = l_/**/name-s_/**/name+400; \
	    name/**/buf = (char *) realloc(name/**/buf, nsize); \
	    e_/**/name = name/**/buf + (e_/**/name-s_/**/name) + 1; \
	    l_/**/name = name/**/buf + nsize - 5; \
	    s_/**/name = name/**/buf + 1; \
	}

I have tried to find any referances to whether this is
valid for a standard preprocessor but K&R don't get very specific about legal
substitution just say not in double quotes.  Could anyone on the net tell me 
what could be happening here?  Included are below are the unpreprocessed 
version, and the version after going through the preprocessor on a Sun system 
(I added indentation to it to make it readable, otherwise it just sat on one 
line).

Thanks for any help you can give.

Kenny Goers
goers@umn-cs.cs.umn.edu


/* Before Preprocess */
main()
{
	check_size(code);
}
---
/* After Preprocess on Sun 3.5 system */
main()
{
	if (e_code >= l_code) { 
		register nsize = l_code-s_code+400; 
		codebuf = (char *) realloc(codebuf, nsize); 
		e_code = codebuf + (e_code-s_code) + 1; 
		l_code = codebuf + nsize - 5; 
		s_code = codebuf + 1; 
	};
}

mesard@bbn.com (Wayne Mesard) (03/28/89)

In article <11796@umn-cs.CS.UMN.EDU> goers@umn-cs.cs.umn.edu (Kenny Goers) writes:
>Ho Netland,
>
>I'm having a problem with a macro in LsC.  The following macro is preprocessed
>"correctly" with the Sun and Sequent C preprocessors but in LsC I get
>just a syntax error.  
>
>#define check_size(name) \
>	if (e_/**/name >= l_/**/name) { \
>	    register nsize = l_/**/name-s_/**/name+400; \
>	    name/**/buf = (char *) realloc(name/**/buf, nsize); \
>	    e_/**/name = name/**/buf + (e_/**/name-s_/**/name) + 1; \
>	    l_/**/name = name/**/buf + nsize - 5; \
>	    s_/**/name = name/**/buf + 1; \
>	}
>

As dozens of others have, no doubt, said by now, using "/**/" to
concatenate to macro arguments is an artifact of BSD's pcc.  To the best
of my knowledge it was never intended, and has certainly never been
portable!

The good news is that the ever-emergent ANSI standard does provide a
portable way of doing this (although it's nearly as ugly): the double
pound sign.  E.g.:

	#define CONCAT(A,B)  A##B

This will work in LSC.

-- 
unsigned *Wayne_Mesard();    "Newborns are slimy!  I like nice clean
MESARD@BBN.COM                babies in front-packs."
BBN, Cambridge, MA                                         -DB.

spencer@eecs.umich.edu (Spencer W. Thomas) (03/28/89)

This is the sort of thing that is not guaranteed to work across C
preprocessors.  It works in pcc based compilers.  ANSI C has a
standard for "token concatenation" (which is what you are trying to
do) using ##.  Thus, your example would be

#define check_size(name) \
	if (e_##name >= l_##name) { \
	    register nsize = l_##name-s_##name+400; \
	    name##buf = (char *) realloc(name##buf, nsize); \
	    e_##name = name##buf + (e_##name-s_##name) + 1; \
	    l_##name = name##buf + nsize - 5; \
	    s_##name = name##buf + 1; \
	}

You might try this and see if LSC hacks it.  (I think LSC 3.0 is
supposed to be ANSI-compliant, so it should.)  Otherwise, you are out
of luck.

=Spencer (spencer@eecs.umich.edu)

jwhitnell@cup.portal.com (Jerry D Whitnell) (03/29/89)

|I'm having a problem with a macro in LsC.  The following macro is preprocessed
|"correctly" with the Sun and Sequent C preprocessors but in LsC I get
|just a syntax error.  
|
|#define check_size(name) \
|	if (e_/**/name >= l_/**/name) { \
|	etc.

What you see is a kludge based on the design of the UNIX C preprocessor.  LSC
supports the ANSI C ##, which concatenates two tokens together.  So try
the following:

#define check_size(name) \
        if (e_##name >= l_##name) { \
            register nsize = l_##name-s_##name+400; \
            name##buf = (char *) realloc(name##buf, nsize); \
            e_##name = name##buf + (e_##name-s_##name) + 1; \
            l_##name = name##buf + nsize - 5; \
            s_##name = name##buf + 1; \
        }
|
|Kenny Goers
|goers@umn-cs.cs.umn.edu

Jerry Whitnell
jwhitnel@cup.portal.com