Thread: Preprocessor error

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Preprocessor error

    Hi,
    I have a piece of code as pasted below.

    I get warnings like

    test1.c:29:32: warning: pasting "(" and ""ERROR:"" does not give a valid preprocessing token
    test1.c: In function `main':
    test1.c:29: `__MODULE__' undeclared (first use in this function)
    test1.c:29: (Each undeclared identifier is reported only once
    test1.c:29: for each function it appears in.)

    What does warning: pasting "(" and ""ERROR:"" does not give a valid preprocessing token mean?

    And where is __MODULE__ defined?

    Please provide me some solution.
    Taklu.

    --------------------------------------------------------
    #include <stdio.h>
    #include <stdarg.h>

    #define DL1 "%s:"
    #define DR1 ,__MODULE__
    #define DL2 "%d:"
    #define DR2 ,__LINE__


    void Trace(char *format, ...);

    #define Debug(ARGS...) Trace(##ARGS)


    #define Error(ARGS,...) \
    Debug("ERROR:"DL1 DL2 ARGS "\n" DR1 DR2, ## __VA_ARGS__)


    void Trace(char *format, ...){
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
    return;
    }

    main(){
    int i = 10;
    Error("Print from main");
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm curious as to how you managed to post code without seeing this popup.
    Please explain.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    I'm curious as to how you managed to post code without seeing this popup.
    Please explain.
    My guess would be "disabling JavaScript".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I believe you are trying to implement variadic macros, a C99, objective.


    Below code may help in setting that up.

    Code:
    #define debug(args...) if(bug) printf(args)
    
    debug ("starting %s is %s\n", ip_type, start.ip_first);

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, the disabling javascript would do the trick

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    a token is a minimal construct a parser can use to analyse its input, like a name, a number or simply a punctuation character. When you use ## between 2 tokens, the preprocessor expects to get a new valid token, which is not the case with '(' and "Error:". So rework your macros...

    you can use __FILE__ __LINE__ __DATE__ __FUNCTION__(c99) and perhaps a few more, but I don't think there is a __MODULE__
    Last edited by root4; 11-24-2008 at 02:12 PM.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Review how you are to actually display this error. Where is your printf?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by slingerland3g View Post
    Review how you are to actually display this error. Where is your printf?
    Code:
    vprintf(format, args);
    perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Then I would think the order of their #define statement may be the issue. I am not sure on this one.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    the definition of Debug(ARGS...) as Trace(##ARGS) makes no sense, that's all.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    int main(void)
    {
    return 0;
    }

    Like that, Salem? And no. I did NOT Disable JavaScript.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Thanks

    Thanks for the solutions.
    The error will be displayed using vprintf.

    I did not see the popup while pasting the code.

    master5001, the Real issue is with the preprocessor errors.
    If you can provide me a solution with that, it would be fine.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe instead of __MODULE__ you could use __FILE__ which translates to the name of the source file [undefined whether that includes the path or not - when I've used it recently, I've got different results from two different compilers recently].

    I personally would probably put the commas in the DEBUG macro, rather than in the DR1 and DR2:
    Code:
    ...
    #define DR1 __MODULE__
    ....
    #define DR2 __LINE__
    Debug("ERROR:"DL1 DL2 ARGS "\n", DR1, DR2, ## __VA_ARGS__)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed