[RESOLVED] Defining a macro to disable warnings with pragma
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: [RESOLVED] Defining a macro to disable warnings with pragma

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [RESOLVED] Defining a macro to disable warnings with pragma

    Hey.

    I want to do this:
    Code:
    #ifndef DISABLE_WARNING_H
    #define DISABLE_WARNING_H
    
    #define PUSH_DISABLE_WARNING(warning)
    #pragma warning(push)
    #pragma warning(disable : 4800)
    
    #define POP_DISABLE_WARNING()
    #pragma warning(pop)
    
    #endif
    But it's clearly not working... it was a quick attempt since I know that I don't have a clue on how to code it...

    Any help?

    Cheers.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Defining a macro to disable warnings with pragma

    For #define, a new line is the terminating character.
    So put a backslash to indicate that the line continues to the next.

    Code:
    #ifndef DISABLE_WARNING_H
    #define DISABLE_WARNING_H
    
    #define PUSH_DISABLE_WARNING(warning) \
    #pragma warning(push) \
    #pragma warning(disable : 4800)
    
    #define POP_DISABLE_WARNING() \
    #pragma warning(pop)
    
    #endif
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Defining a macro to disable warnings with pragma

    _Superman_, the problem is not about writing a macro but on using preprocessor to suppress warning messages from the compiler on certain part of the code. One example would be reusing a legacy code that causing the compiler to generate warning message. As just any good programmer, we must not ignore the compiler warning. However, since you may not have the right to change the legacy code, you only want to suppress the compiler warning from the legacy code but from other part of the code.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Defining a macro to disable warnings with pragma

    I looking for a standard approach rather than platform specific.

    Thanks.
    Thanks for your help.

  5. #5
    Join Date
    Apr 2008
    Posts
    726

    Re: Defining a macro to disable warnings with pragma

    it's ide specific, not platform specific, right?

  6. #6
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Defining a macro to disable warnings with pragma

    I have done this many times and it did work.

    This is what I do.

    #pragma warning(disable:4800)

    Some code or #include which throws the warning.

    #pragma warning(default:4800)
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  7. #7
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Defining a macro to disable warnings with pragma

    Quote Originally Posted by Amleto View Post
    it's ide specific, not platform specific, right?
    I looking for standard approach rather than compiler/IDE specific.

    Does boost offer this kind of feature ?

    Thanks.
    Thanks for your help.

  8. #8
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Defining a macro to disable warnings with pragma

    I doubt so. As far as I know, #pragma preprocessor directive is compiler specific. I don't think it is part of the C++ language.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  9. #9
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Defining a macro to disable warnings with pragma

    Quote Originally Posted by _Superman_ View Post
    For #define, a new line is the terminating character.
    So put a backslash to indicate that the line continues to the next.

    Code:
    #ifndef DISABLE_WARNING_H
    #define DISABLE_WARNING_H
    
    #define PUSH_DISABLE_WARNING(warning) \
    #pragma warning(push) \
    #pragma warning(disable : 4800)
    
    #define POP_DISABLE_WARNING() \
    #pragma warning(pop)
    
    #endif
    Hey. I tried that and get errors:
    Code:
    #define PUSH_DISABLE_WARNING(warning_) \
    #pragma warning(push) \
    #pragma warning(disable : warning_)
    
    #define POP_DISABLE_WARNING() \
    #pragma warning(pop)
    
    int main(int argc, char* argv[])
    {
      return 0;
    }
    Code:
    Error	1	error C2162: expected macro formal parameter	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	2
    Error	2	error C2162: expected macro formal parameter	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	3
    Quote Originally Posted by Kheun View Post
    _Superman_, the problem is not about writing a macro but on using preprocessor to suppress warning messages from the compiler on certain part of the code. One example would be reusing a legacy code that causing the compiler to generate warning message. As just any good programmer, we must not ignore the compiler warning. However, since you may not have the right to change the legacy code, you only want to suppress the compiler warning from the legacy code but from other part of the code.
    Not quite sure what you meant to say here, as the problem is indeed how to write a macro that suppresses warning messages.

    Cheers.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  10. #10
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Defining a macro to disable warnings with pragma

    Thanks for clearing up.

    I found this link that may help but I haven't tried it.
    http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  11. #11
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Defining a macro to disable warnings with pragma

    Thanks for the link. I appear to be using it correctly, so I think my macro is coded wrong. From the MSDN link:
    Code:
    #pragma warning( push )
    #pragma warning( disable : 4705 )
    #pragma warning( disable : 4706 )
    #pragma warning( disable : 4707 )
    // Some code
    #pragma warning( pop )
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  12. #12
    Join Date
    Apr 2008
    Posts
    726

    Re: Defining a macro to disable warnings with pragma

    i think its expecting the thing after # in #pragma to be a macro argument. I dont know much about macros, but # and ## are used for token pasting and things like that.

    try this:

    Code:
    #define myPragma (#pragma)
    
    #define PUSH_DISABLE_WARNING(warning_) \
    myPragma warning(push)\
    myPragma warning(disable : warning_)
    
    #define POP_DISABLE_WARNING() \
    #pragma warning(pop)
    
    int main(int argc, char* argv[])
    {
      return 0;
    }
    don't know why it's not needed for the warning (pop) part though. I believe it is because no argument is given to the macro..
    Last edited by Amleto; May 31st, 2009 at 04:46 AM.

  13. #13
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Defining a macro to disable warnings with pragma

    Quote Originally Posted by Kheun View Post
    I doubt so. As far as I know, #pragma preprocessor directive is compiler specific. I don't think it is part of the C++ language.
    Although there aren't any language feature support it, but i think [preprocessor if] should be able to cope with it.
    Thanks for your help.

  14. #14
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Defining a macro to disable warnings with pragma

    Quote Originally Posted by Amleto View Post
    i think its expecting the thing after # in #pragma to be a macro argument. I dont know much about macros, but # and ## are used for token pasting and things like that.

    try this:

    Code:
    #define myPragma (#pragma)
    
    #define PUSH_DISABLE_WARNING(warning_) \
    myPragma warning(push)\
    myPragma warning(disable : warning_)
    
    #define POP_DISABLE_WARNING() \
    #pragma warning(pop)
    
    int main(int argc, char* argv[])
    {
      return 0;
    }
    don't know why it's not needed for the warning (pop) part though. I believe it is because no argument is given to the macro..
    Sorry for the late reply. Your code doesn't compile for me:
    Code:
    #define myPragma (#pragma)
    
    #define PUSH_DISABLE_WARNING(warning_) \
    myPragma warning(push) \
    myPragma warning(disable : warning_)
    
    #define POP_DISABLE_WARNING() \
    #pragma warning(pop)
    
    PUSH_DISABLE_WARNING(4800)
    
    bool f()
    {
        unsigned short x = 32;
        return x;
    }
    
    POP_DISABLE_WARNING()
    
    int main(int argc, char* argv[])
    {
      return 0;
    }
    Code:
    Error	1	error C2121: '#' : invalid character : possibly the result of a macro expansion	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	2	error C2146: syntax error : missing ';' before identifier 'warning'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	3	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	4	error C2065: 'push' : undeclared identifier	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	5	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	6	error C2143: syntax error : missing ';' before '('	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	7	error C2121: '#' : invalid character : possibly the result of a macro expansion	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	8	error C2059: syntax error : ')'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	9	error C2146: syntax error : missing ')' before identifier 'warning'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	10	error C2146: syntax error : missing ';' before identifier 'warning'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	11	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	12	error C2086: 'int pragma' : redefinition	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	13	error C2065: 'disable' : undeclared identifier	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	14	error C2143: syntax error : missing ')' before ':'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	15	error C2448: 'warning' : function-style initializer appears to be a function definition	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	16	error C2059: syntax error : 'constant'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	17	error C2059: syntax error : ')'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	10
    Error	18	error C2143: syntax error : missing ';' before '{'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	13
    Error	19	error C2447: '{' : missing function header (old-style formal list?)	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	13
    Error	20	error C2121: '#' : invalid character : possibly the result of a macro expansion	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	18
    Error	21	error C2146: syntax error : missing ';' before identifier 'warning'	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	18
    Error	22	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	18
    Error	23	error C2086: 'int pragma' : redefinition	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	18
    Error	24	error C2065: 'pop' : undeclared identifier	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	18
    Error	25	error C2448: 'warning' : function-style initializer appears to be a function definition	c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp	20
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  15. #15
    Join Date
    Apr 2008
    Posts
    726

    Re: Defining a macro to disable warnings with pragma


Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center