This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Multi-line Macros


During porting of code from NT, I had problems with multi-line macros that
would report the error "stray '\' in program".  I traced the problem to
MSDOS/Windows line terminators (CRLF) as opposed to Unix line terminators
(LF).  Apparently the preprocessor is interpreting the CR as an extra
character after the backslash.  Since this is the only problem I found that
resulted from the difference in terminators, it seems to me that this one
problem in the preprocessor should be fixed.

The following code demonstrates the problem.  Just make sure foo.h uses CRLF
terminators (Code Crusader makes this very easy).  Demonstration code could
be made much simpler, but this is the code I posted on
comp.os.linux.programming before I understood the cause (see "Stray '\' in a
macro???" on 01/20/00) and it does illustrate that other code is not
affected.

File foo.h

class foo
{
public:
foo() {};

#define DECLARE_HANDLER(classname,handlername) \
static void* handlername(void* arg) \
{ return ((classname*)arg)->handlername(); } \
virtual void* handlername(void);

    DECLARE_HANDLER(foo,Handler);

    int StartThread(void);
};

File foo.cpp

void* foo::Handler(void)
{
    //do something interesting...
return 0;
}

which is called by something like:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
     void* (*start_routine)(void*), void *arg);

int foo::StartThread(void)
{
    pthread_t  id;
    return pthread_create(&id, NULL, foo::Handler, this);
}

or

int function(void)
{
foo fooinst;
pthread_t  id;
return pthread_create(&id, NULL, foo::Handler, &fooinst);
}















Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]