[Bug preprocessor/29836] New: Concatenation operator ## doesn't work with this: / ## /

michael dot bishop at gdcanada dot com gcc-bugzilla@gcc.gnu.org
Tue Nov 14 20:03:00 GMT 2006


To whom it may concern,

I am trying to use the macro concatenation operator to make a "conditional
comment".  I want a symbol that I can sprinkle throught my C code that will be
replaced either be a C++ style // comment, or it will be replaced with nothing.
 This is useful for turning on and off debug messages and other debug code.

The basic idea is to use the ## concatenation operator in a macro definition to
paste two forward slash characters ('/') together to make a C++ style comment
of the form "//".  I tried this on two other C compilers and it "worked" on
them.  One compiler is MinGW running on my PC which is gcc version 3.2.3 (mingw
special 20030504-1).  The other one is the latest Keil C compiler for the 8051
microcontroller.  Both of these compiled the code as I expected them to.

The problem is that the gcc compilers on our Sun system and on our HP Unix
system give errors when I try to compile the same code.  The HP Unix system has
gcc version 3.4.3.

The C code file (called t.c) is:

     #define DEBUG      / ## /    // disable debug code
  // #define DEBUG  //  / ## /    // enable debug code

  int
  main ( void )
  {
    int i ;
    DEBUG int j ;  // DEBUG is either "empty" or //

    DEBUG j = i ;

    return ( 0 ) ;
  }

The compiler error messages are:

  t.c:8:1: pasting "/" and "/" does not give a valid preprocessing token
  t.c:10:1: pasting "/" and "/" does not give a valid preprocessing token

The output from the preprocessor (using the -E command line switch) is:

  int
  main ( void )
  {
    int i ;
    / / int j ;

    / / j = i ;

    return ( 0 ) ;
  }

I find it strange that the preprocessor puts a blank space between the two '/'
characters.  This looks like a bug to me.  I don't think there should be a
blank space between the characters.

Sincerely,

Michael Bishop
michael.bishop@gdcanada.com

P.S. I know that there are many other ways to make conditional debug code, and
I have tried several of them over the last 20 years.  I am hoping that this
method can be made to work in a portable way.


-- 
           Summary: Concatenation operator ## doesn't work with this:  / ##
                    /
           Product: gcc
           Version: 3.4.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: preprocessor
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: michael dot bishop at gdcanada dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29836



More information about the Gcc-bugs mailing list