[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.
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
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.
Re: Defining a macro to disable warnings with pragma
I looking for a standard approach rather than platform specific.
Thanks.
Re: Defining a macro to disable warnings with pragma
it's ide specific, not platform specific, right?
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)
Re: Defining a macro to disable warnings with pragma
Quote:
Originally Posted by
Amleto
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.
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.
Re: Defining a macro to disable warnings with pragma
Quote:
Originally Posted by
_Superman_
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
_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.
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
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 )
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..
Re: Defining a macro to disable warnings with pragma
Quote:
Originally Posted by
Kheun
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.
Re: Defining a macro to disable warnings with pragma
Quote:
Originally Posted by
Amleto
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
Re: Defining a macro to disable warnings with pragma