NO

Author Topic: Macro expansion  (Read 5720 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Macro expansion
« on: August 22, 2010, 09:10:08 AM »
Is this bug in PellesC ?

MacroTest.c(7): warning #2001: Syntax error: expected ')' but found 'QUOTEME'.
Code: [Select]
#define QUOTEME_(x) #x
#define QUOTEME(x) QUOTEME_(x)

#ifdef _MSC_VER
 #pragma message( "Version: "QUOTEME(_MSC_VER) )
#elif __POCC__
 #pragma message( "Version: "QUOTEME(__POCC__) )
#elif __WATCOMC__
 #pragma message( "Version: "QUOTEME(__WATCOMC__) )
#elif __DMC__
 #pragma message( "Version: "QUOTEME(__DMC__) )
#endif
May the source be with you

JohnF

  • Guest
Re: Macro expansion
« Reply #1 on: August 24, 2010, 08:02:29 AM »
I think it is.

EDIT: However, having said that I can't get it to work in other compilers either.

John
« Last Edit: August 24, 2010, 10:43:25 AM by JohnF »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Macro expansion
« Reply #2 on: August 24, 2010, 12:16:32 PM »
Code: [Select]
#define QUOTEME_(x) #x
#define QUOTEME(x) QUOTEME_(x)

#define VERSION(x) #x"="QUOTEME_(x)

#ifdef _MSC_VER
 #pragma message( "Version: "QUOTEME(_MSC_VER) )
 #pragma message( "Version: "VERSION(_MSC_VER) )
#elif __POCC__
 #pragma message( "Version: "QUOTEME(__POCC__) )
 #pragma message( "Version: "VERSION(__POCC__) )
#elif __WATCOMC__
 #pragma message( "Version: "QUOTEME(__WATCOMC__) )
 #pragma message( "Version: "VERSION(__WATCOMC__) )
#elif __DMC__
 #pragma message( "Version: "QUOTEME(__DMC__) )
 #pragma message( "Version: "VERSION(__DMC__) )
#endif
Code: [Select]
C:\code\msvc9>cl.exe -c MacroTest.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

MacroTest.c
Version: 1500
Version: _MSC_VER=1500
Code: [Select]
C:\code\PellesC\test-forum>pocc.exe MacroTest.c
Version:
MacroTest.c(10): warning #2001: Syntax error: expected ')' but found 'QUOTEME'.
Version:
MacroTest.c(11): warning #2001: Syntax error: expected ')' but found 'VERSION'.
warning #2024: [ISO] Empty input file.
Code: [Select]
C:\code\OWatcom>wcc386.exe MacroTest.c
Open Watcom C32 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
MacroTest.c(6) :
Version: 1290
Version: __WATCOMC__=1290
MacroTest.c: 24 lines, 0 warnings, 0 errors
Code size: 0
Code: [Select]
c:\code\DigitalMars>dmc.exe -c MacroTest.c
Version: 0x852
Version: __DMC__=0x852
Code: [Select]
C:\code\msvc71>cl.exe -c MacroTest.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

MacroTest.c
Version: 1310
Version: _MSC_VER=1310
May the source be with you

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Macro expansion
« Reply #3 on: August 24, 2010, 01:43:24 PM »
Seems that message only supports string literals.

The first two don't give an error, but the last four do.
Code: [Select]
#elif __POCC__
 #pragma message( "Version: 600" )
 #pragma message( "Version: __POCC__=600" )
 #pragma message( QUOTEME(__POCC__) )
 #pragma message( VERSION(__POCC__) )
 #pragma message( "Version: "QUOTEME(__POCC__) )
 #pragma message( "Version: "VERSION(__POCC__) )
---
Stefan

Proud member of the UltraDefrag Development Team

JohnF

  • Guest
Re: Macro expansion
« Reply #4 on: August 24, 2010, 03:20:05 PM »
Seems that message only supports string literals.

The first two don't give an error, but the last four do.
Code: [Select]
#elif __POCC__
 #pragma message( "Version: 600" )
 #pragma message( "Version: __POCC__=600" )
 #pragma message( QUOTEME(__POCC__) )
 #pragma message( VERSION(__POCC__) )
 #pragma message( "Version: "QUOTEME(__POCC__) )
 #pragma message( "Version: "VERSION(__POCC__) )

Yes that's right, but it should work with the macro, shouldn't it?

John

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Macro expansion
« Reply #5 on: August 24, 2010, 03:49:03 PM »
Yes that's right, but it should work with the macro, shouldn't it?

Yes, since it doesn't make sense to not work with macros.

Sure it involves a little more parsing to support dynamic text, but it is really handy to expand and display preprocessor symbols during compile time, which adds another level of debug details to it.

The current workaround is to hard code the values and use static messages, not really handy, isn't it.
---
Stefan

Proud member of the UltraDefrag Development Team