Welcome, Guest. Please login or register.

Author Topic: Possible C preprocessor bug in VBCC...  (Read 1591 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Possible C preprocessor bug in VBCC...
« on: March 28, 2007, 10:39:25 PM »
I've stripped down (with many sweat, blood and almost tears, macro debugging is not for the fainthearted I tell you:() an error I was having in a big complicated macro I'm doing to the code bellow. It runs perfectly in SASC(68k) but not on VBCC, wich complains about an invalid created token. Don't have GCC installed to try but it worked on a PC with Borland compiler;).

So, can someone confirm if this is a bug in VBCC, a nonstandard feature that SASC and Borland's preprocessors happend to have, or what ? Asking this as I want the macro to run on any C compliant compiler (whatever that means:)).

Oh, and since we're at it maybe someone could also try it on GCC, I don't fell like installing it.

Cheers fellow coders :pint:

Code: [Select]

#include<stdio.h>

#define EXTRACTBRCKTELMNT(Terminator, Elmnt) #Elmnt, #Terminator

#define GETBRCKTELMNT(ToExtract) GETELMNTEXPAND(EXTRACTBRCKTELMNT##ToExtract)
#define GETELMNTEXPAND(a) GETELMNTEXPAND2 (a)
#define GETELMNTEXPAND2(a, b) a, b


int main (int argc, char **argv)
{ printf(&quot;%s %s\n&quot;, GETBRCKTELMNT((YYY, XXX)));
 
  getchar();
  return (0);
}

\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Possible C preprocessor bug in VBCC...
« Reply #1 on: March 29, 2007, 08:06:54 PM »
I think you can get VBCC to show you the emitted C code after the preprocessing stage (just as you can get it to show you the asm before and after going through the instruction scheduler)

I'll give it a try in a few different compilers tomorrow if I get a minute at work ;-)

Quote
Asking this as I want the macro to run on any C compliant compiler


This is a bit of a misnomer really. Macro syntax really isn't anything whatsoever to do with the C language - no more than your favourite macro assembler is anything to do with 68K assembly language ;-)

That said, preprocessor behaviour tends to be well defined in the majority of cases, but I have always been of the opinion that overuse of function-like macros in C is a bad idea.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Possible C preprocessor bug in VBCC...
« Reply #2 on: March 29, 2007, 08:20:19 PM »
Incidentally, got this from gcc:

macro.c:12:45: pasting "EXTRACTBRCKTELMNT" and "(" does not give a valid preprocessing token


int p; // A
 

Offline CannonFodder

  • Hero Member
  • *****
  • Join Date: Sep 2003
  • Posts: 1115
    • Show only replies by CannonFodder
Re: Possible C preprocessor bug in VBCC...
« Reply #3 on: March 29, 2007, 08:49:00 PM »
Quote

Karlos wrote:
Incidentally, got this from gcc:

macro.c:12:45: pasting "EXTRACTBRCKTELMNT" and "(" does not give a valid preprocessing token



gcc 4.1 on x86 Linux gives

Code: [Select]

test.c:11:1: error: pasting &quot;EXTRACTBRCKTELMNT&quot; and &quot;(&quot; does not give a valid preprocessing token


gcc 2.95.3 on MorphOS gives no errors or warnings at all.

when the resulting executable is run it returns "XXX YYY".
People are hostile to what they do not understand - Imam Ali ibn Abi Talib(AS)
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Possible C preprocessor bug in VBCC...
« Reply #4 on: March 29, 2007, 08:56:42 PM »
Gah, I forgot to say I saw it in both gcc 3.4 and 4.1.
int p; // A
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Possible C preprocessor bug in VBCC...
« Reply #5 on: March 30, 2007, 09:46:10 AM »
@Karlos
" I think you can get VBCC to show you the emitted C code after the preprocessing stage (just as you can get it to show you the asm before and after going through the instruction scheduler)"

The -E option, doesn't allways work as expected...

"I'll give it a try in a few different compilers tomorrow if I get a minute at work "

Cool:)

"That said, preprocessor behaviour tends to be well defined in the majority of cases.. "

I know that at least C99 finally "standardized" varargs macros so there must be some standard.

@CannonFodder

Yes, that's the expected result (on 2.95.3/MOS that is)



So can this be considered a preprocessor bug because obviously I'm not pasting "EXTRACTBRCKTELMNT" and "(", but "EXTRACTBRCKTELMNT" and "(YYY, XXX)" wich IS a valid token  :getmad:
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Possible C preprocessor bug in VBCC...
« Reply #6 on: March 30, 2007, 10:23:14 AM »
Difficult to say if it is a bug or not. It looks like it is, but without a solid preprocessor specification to compare against, I can't say with certainty.

Given this:

#define EXTRACTBRCKTELMNT(Terminator, Elmnt) #Elmnt, #Terminator

#define GETBRCKTELMNT(ToExtract) GETELMNTEXPAND(EXTRACTBRCKTELMNT##ToExtract)
#define GETELMNTEXPAND(a) GETELMNTEXPAND2 (a)
#define GETELMNTEXPAND2(a, b) a, b

let's see a manual expansion:

GETBRCKTELMNT((YYY, XXX))

becomes (expanding GETBRCKTELMNT)

GETELMNTEXPAND(EXTRACTBRCKTELMNT##(YYY, XXX))

becomes (and this is presumably the step it doesnt like)

GETELMNTEXPAND(EXTRACTBRCKTELMNT(YYY, XXX))

becomes (expanding EXTRACTBRCKTELMNT)

GETELMNTEXPAND("YYY", "XXX")

becomes

GETELMNTEXPAND2("YYY", "XXX")

becomes

"YYY", "XXX"

...
int p; // A
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Possible C preprocessor bug in VBCC...
« Reply #7 on: March 30, 2007, 08:40:27 PM »
The C Preprocessor for GCC (Richard Stallman)

I've read this a while ago (the parts about Macros and Pitfalls and Subtleties of Macros) and can't see why it shouldn't work.
Have you tried with different compilers at work ? Should be interesting...8-)

:pint:
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Possible C preprocessor bug in VBCC...
« Reply #8 on: March 30, 2007, 08:56:05 PM »
I couldn't get it to work on any x86 gcc from 3.0 up, it spat out the same error.

That in itself is not necessarily that surprising. Most of the changes over successive versions occur to the compiler itself rather than the preprocessor component.
int p; // A
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Possible C preprocessor bug in VBCC...
« Reply #9 on: March 30, 2007, 08:59:08 PM »
Right, thx for testing it. I'm gonna check where in the GCC dev forums I should submit this...
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Possible C preprocessor bug in VBCC...
« Reply #10 on: March 30, 2007, 09:01:34 PM »
One thing I thought of trying was simply preprocessing the source using cpp from 2.95.3 and then compiling the output in a higher compiler version.

That's not exactly a practical thing to write a decent makefile for, though.
int p; // A