I'm trying to do something like this:
#ifdef _MSC_VER
#define DISABLE_WARNINGS() \
#pragma warning( push, 0 )
#elif __GNUC__
#define DISABLE_WARNINGS() \
#define DISABLE_WARNINGS \
#pragma GCC diagnostic push \
#pragma GCC diagnostic ignored "-Wall"
#endif
I would like to define a single macro such as "DISABLE_WARNINGS" in my code before including 3rd party headers that produce enormous amount of warnings on W4, and also ensure the code compiles on any platform.
For example:
DISABLE_WARNINGS
#include <gtkmm/buttonbox.h>
#include <gtkmm/box.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
ENABLE_WARNINGS
What would be the best way to achieve this with single macro?
In C99 mode, you can use _Pragma instead of #pragma:
#define DISABLE_WARNINGS \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wall\"")
Related
I have a legacy logging printf-like function in C:
namespace legacy
{
void DoLog(const char* format,...);
}
If is only visible if a corresponding macro is defined (this is legacy too):
#ifdef LOG
# define Log legacy::DoLog
#else
# define Log /* Nothing */
#endif
I have some C++ code, which calls this log like:
Log("Logging started!");
When the LOG macro is not defined, Log("Something"); turns into ("Something"); which is unused code.
I need to suppress these warnings in GCC, and do it only once, of course, so I wrap Log into MyLog as #define MyLog Log and try this:
#define MyLog(...) \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wunused-value\""); \
Log(__VA_ARGS__); \
_Pragma("GCC diagnostic pop");
But these pragmas for some reason do not have any effecthttps://gcc.gnu.org/onlinedocs/cpp/Pragmas.htmlhttps://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
I would better use inline C++ wrapper with templates for the perfect wrapping, but I cannot cope with it yet...
Any suggestions how to suppress these warnings?
Thank you in advance!
Use
#define Log(...)
and then Log("Logging started!"); expands to just ; instead of ("Logging started!");
I managed to come to the two following solutions:
C++ style inline function, with suppressed warnings and basic Windows and Linux platform definitions used:
template<typename... Args>
inline void MyLog(const char* format, Args... args)
{
#ifndef LOG
# ifdef _MSC_VER // Windows
# pragma warning(push)
# pragma warning(disable: 4548)
# elif defined(__GNUC__) // Linux
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-value"
# endif
#endif
Log(format, (args,...));
#ifndef LOG
# ifdef _MSC_VER // Windows
# pragma warning(pop)
# elif defined(__GNUC__) // Linux
# pragma GCC diagnostic pop
# endif
#endif
}
C style wrapper macro, even without any warnings suppression needed (thanks to #KamilCuk and #user253751):
#ifndef LOG
# define MyLog(...)
#else
# define MyLog(...) Log(__VA_ARGS__)
#endif
I'm using the following preprocessor definitions to disable specific warnings for different c++ compilers.
Here's the code
#if defined(_MSC_VER)
#define DISABLE_WARNING_PUSH __pragma(warning(push))
#define DISABLE_WARNING_POP __pragma(warning(pop))
#define DISABLE_WARNING(warningNumber) __pragma(warning(disable : (warningNumber)))
#define DISABLE_WARNING_DEPRECATED_FUNCTION DISABLE_WARNING(4996)
#elif defined(__GNUC__) || defined(__clang__)
#define DO_PRAGMA(X) _Pragma(#X)
#define DISABLE_WARNING_PUSH DO_PRAGMA(GCC diagnostic push)
#define DISABLE_WARNING_POP DO_PRAGMA(GCC diagnostic pop)
#define DISABLE_WARNING(warningName) DO_PRAGMA(GCC diagnostic ignored #warningName)
#define DISABLE_WARNING_DEPRECATED_FUNCTION DISABLE_WARNING(-Wdeprecated-declarations)
#else
#define DISABLE_WARNING_PUSH
#define DISABLE_WARNING_POP
#define DISABLE_WARNING_DEPRECATED_FUNCTION
#endif
These defines works for GCC and CLANG but not for MSVC compiler.
On MSVC compiler I get the following warning:
(23): warning C4081: expected ')'; found '('
You can see it here: https://godbolt.org/z/hTErEbG7W
How can I fix this warning for MSVC compiler?
I have cpp and h of class file. Class implementation contains wcsncpy function that only might be used if defined _CRT_SECURE_NO_WARNINGS. It works if I place #define _CRT_SECURE_NO_WARNINGS before all header inclusions. But why I can't place this line after headers or event just before wcsncpy usage?
UPD
According to my understanding I can do #define in code:
int _tmain(int argc, _TCHAR* argv[])
{
#define a
#ifdef a
std::wcout << _T("hello") << std::endl;
#endif
}
Why definition below not works?
int main(void)
{
...
#define _CRT_SECURE_NO_WARNINGS
wcsncpy(dest, src, 4);
...
}
You can suppress a warning (even this one is treated as error by default) locally with pragma warning:
#pragma warning(push)
#pragma warning(disable: 4996) // 'wcsncpy': This function or variable may be unsafe.
wcsncpy(dest, src, 4);
#pragma warning(pop)
It is even possible to wrap this in more readable macros with Microsoft-specific __pragma keyword:
#define DISABLE_CRT_SECURE_WARNINGS() \
__pragma(warning(push)) \
__pragma(warning(disable: 4996))
#define RESTORE_CRT_SECURE_WARNINGS() \
__pragma(warning(pop))
...
DISABLE_CRT_SECURE_WARNINGS();
wcsncpy(dst, src, 4);
RESTORE_CRT_SECURE_WARNINGS();
I am trying to define the following macro:
#if defined(_MSC_VER)
#define PRAGMA_PACK_PUSH(n) __pragma(pack(push, n))
#define PRAGMA_PACK_POP() __pragma(pack(pop))
#else
#define PRAGMA_PACK_PUSH(n) #pragma (pack(push, n))
#define PRAGMA_PACK_POP() #pragma (pack(pop))
#endif
But i get the following error on Linux -
error: '#' is not followed by a macro parameter
#define PRAGMA_PACK_PUSH(n) #pragma (pack(push, n))
and it points to the first ')' in the statment
How can i define a macro that contains a #?
Solution Update:
As stated in this thread Pragma in define macro the syntax that worked is:
#if defined(_MSC_VER)
#define PRAGMA_PACK_PUSH(n) __pragma(pack(push, n))
#define PRAGMA_PACK_POP() __pragma(pack(pop))
#else
#define PRAGMA_PACK_PUSH(n) _Pragma("pack(push, n)")
#define PRAGMA_PACK_POP() _Pragma("pack(pop)")
#endif
How can i define a macro that contains a #?
You can't (define a macro that contains a directive, that is. # can still be used in macros for stringization and as ## for token concatenation). That's why _Pragma was invented and standardized in C99. As for C++, it's definitely in the C++11 standard and presumably the later ones.
You can use it as follows:
#define PRAGMA(X) _Pragma(#X)
#define PRAGMA_PACK_PUSH(n) PRAGMA(pack(push,n))
#define PRAGMA_PACK_POP() PRAGMA(pack(pop))
With that,
PRAGMA_PACK_PUSH(1)
struct x{
int i;
double d;
};
PRAGMA_PACK_POP()
preprocesses to
# 10 "pack.c"
#pragma pack(push,1)
# 10 "pack.c"
struct x{
int i;
double d;
};
# 15 "pack.c"
#pragma pack(pop)
# 15 "pack.c"
As you can see, the _Pragmas are expanding to #pragma directives.
Since _Pragma is standard, you should be able to avoid the #ifdef here if Microsoft supports it.
I am trying to pack some structs with Borland C++Builder (XE6) (in the future: bcc).
I am using a library which uses the following construct to create structs:
#ifdef _MSC_VER
#define PACKED_BEGIN __pragma(pack(push, 1))
#define PACKED
#define PACKED_END __pragma(pack(pop))
#elif defined(__GNUC__)
#define PACKED_BEGIN
#define PACKED __attribute__((__packed__))
#define PACKED_END
#endif
PACKED_BEGIN
struct PACKED {
short someSampleShort;
char sampleByte;
int sampleInteger;
} structType_t;
PACKED_END
The bcc compiler does not like the MSC __pragma, and does not like preprocessor directives inside of macros although it is described on their website:
#define GETSTD #include <stdio.h>
My Question is: Is there any possibility to use this construct with the Borland Compiler for packing a struct without using:
#pragma pack(1)
to pack every struct?
Are there any workarounds for this?
As you stated, C++Builder does not support preprocessor statements inside of macros. This is documented on Embarcadero's site:
#define (C++)
After each individual macro expansion, a further scan is made of the newly expanded text. This allows for the possibility of nested macros: The expanded text can contain macro identifiers that are subject to replacement. However, if the macro expands into what looks like a preprocessing directive, the directive will not be recognized by the preprocessor.
The reason for that is because the # character inside of a macro is reserved for the preprocessor's stringizing operator.
Some compilers, including MSVC, get around that restriction with the __pragma() compiler extension, or the C99/C++x0 _Pragma() extension. C++Builder's Windows 32bit compiler does not support either of those. However, its Windows 64bit and mobile compilers (which are all based on clang and support C++11) DO support both of them. So you can add support for those compilers in the macros like this:
#if defined(__BORLANDC__)
#if defined(__clang__)
#define PACKED_BEGIN __pragma(pack(push, 1))
#define PACKED
#define PACKED_END __pragma(pack(pop))
#else
#error Cannot define PACKED macros for this compiler
#endif
#elif defined(_MSC_VER)
#define PACKED_BEGIN __pragma(pack(push, 1))
#define PACKED
#define PACKED_END __pragma(pack(pop))
#elif defined(__GNUC__)
#define PACKED_BEGIN
#define PACKED __attribute__((__packed__))
#define PACKED_END
#else
#error PACKED macros are not defined for this compiler
#endif
If you want to support the C++Builder Windows 32bit compiler, you will have to move the logic into .h files that use #pragma for it, and then you can #include those files where needed (at least until the compiler is updated to support clang/C++11 - which Embarcadero is currently working on):
pack1_begin.h:
#if defined(__BORLANDC__)
#define PACKED_BEGIN
#define PACKED
#define PACKED_END
#pragma pack(push, 1)
#elif defined(_MSC_VER)
#define PACKED_BEGIN __pragma(pack(push, 1))
#define PACKED
#define PACKED_END __pragma(pack(pop))
#elif defined(__GNUC__)
#define PACKED_BEGIN
#define PACKED __attribute__((__packed__))
#define PACKED_END
#else
#error PACKED macros are not defined for this compiler
#endif
pack_end.h:
#if defined(__BORLANDC__)
#pragma pack(pop)
#endif
Then you can do this:
#include "pack1_begin.h"
PACKED_BEGIN
struct PACKED {
short someSampleShort;
char sampleByte;
int sampleInteger;
} structType_t;
PACKED_END
#include "pack_end.h"
If you take this approach, you can just drop PACKED_BEGIN/PACKED_END altogether:
pack1_begin.h:
#if defined(__BORLANDC__) || defined(_MSC_VER)
#define PACKED
#pragma pack(push, 1)
#elif defined(__GNUC__)
#define PACKED __attribute__((__packed__))
#else
#error PACKED macro is not defined for this compiler
#endif
pack_end.h:
#if defined(__BORLANDC__) || defined(_MSC_VER)
#pragma pack(pop)
#endif
#include "pack1_begin.h"
struct PACKED {
short someSampleShort;
char sampleByte;
int sampleInteger;
} structType_t;
#include "pack_end.h"
The standard offers one extra alternative for writing pragmas: the _Pragma operator:
#define PACKED_BEGIN _Pragma("pack(push, 1)")
#define PACKED
#define PACKED_END _Pragma("pack(pop)")
If the Borland compiler supports it, it should work.