Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

80%
+6 −0
Q&A Scheme for cross-platform warning control?

Since C++11 the standard mandates the _Pragma operator which is intended for use inside macros. With this you can improve the situation by wrapping all this compiler switching into a central macro...

posted 3y ago by Someone‭  ·  edited 3y ago by Someone‭

Answer
#4: Post edited by user avatar Someone‭ · 2021-01-22T20:06:10Z (over 3 years ago)
Simplified code, using Lundin's comment; added limitations
  • Since C++11 the standard mandates the [`_Pragma` operator](https://en.cppreference.com/w/cpp/preprocessor/impl) which is intended for use inside macros.
  • With this you can improve the situation by wrapping all this compiler switching into a central macro definition (**MSVC is untested!**):
  • ```
  • // see https://stackoverflow.com/a/45783809
  • #define DO_PRAGMA_(x) _Pragma (#x)
  • #define DO_PRAGMA(x) DO_PRAGMA_(x)
  • #if defined(_MSC_VER)
  • #define PUSH_WARNING_STATE DO_PRAGMA(warnings(push)) # untested!
  • #elif defined(__clang__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(clang diagnostic push)
  • #elif defined(__GNUC__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(GCC diagnostic push)
  • #endif
  • #if defined(_MSC_VER)
  • #define POP_WARNING_STATE DO_PRAGMA(warnings(poph)) # untested!
  • #elif defined(__clang__)
  • #define POP_WARNING_STATE DO_PRAGMA(clang diagnostic pop)
  • #elif defined(__GNUC__)
  • #define POP_WARNING_STATE DO_PRAGMA(GCC diagnostic pop)
  • #endif
  • #if defined(_MSC_VER)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(warnings(disable : WARNING_NAME##_MSC)
  • #elif defined(__clang__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(clang diagnostic ignored WARNING_NAME##_CLANG)
  • #elif defined(__GNUC__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(GCC diagnostic ignored WARNING_NAME##_GCC)
  • #endif
  • ```
  • Which can be used like:
  • ```
  • // find some custom central naming for your warnings
  • #define WARN_UNINITIALIZED_CLANG "-Wuninitialized"
  • #define WARN_UNINITIALIZED_GCC "-Wuninitialized"
  • int main(int argc, char **argv) {
  • float a = 0, b;
  • PUSH_WARNING_STATE
  • DISABLE_WARNING(WARN_UNINITIALIZED);
  • if(a != b) {
  • POP_WARNING_STATE
  • return a == b;
  • }
  • }
  • ```
  • Compiling gives only the second error, as intended:
  • ```
  • $ g++ -Wall -pedantic test.cpp
  • test.cpp: In function 'int main(int, char**)':
  • test.cpp:39:12: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  • 39 | return a == b;
  • | ~~^~~~
  • ```
  • Since C++11 the standard mandates the [`_Pragma` operator](https://en.cppreference.com/w/cpp/preprocessor/impl) which is intended for use inside macros.
  • With this you can improve the situation by wrapping all this compiler switching into a central macro definition (**MSVC is untested!**):
  • ```
  • // see https://stackoverflow.com/a/45783809
  • #define DO_PRAGMA_(x) _Pragma (#x)
  • #define DO_PRAGMA(x) DO_PRAGMA_(x)
  • #define PUSH_WARNING_STATE DO_PRAGMA(warnings(push)) \
  • DO_PRAGMA(clang diagnostic push) \
  • DO_PRAGMA(GCC diagnostic push)
  • #define POP_WARNING_STATE DO_PRAGMA(warnings(pop)) \
  • DO_PRAGMA(clang diagnostic pop) \
  • DO_PRAGMA(GCC diagnostic pop)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(warnings(disable : WARNING_NAME##_MSC) ) \
  • DO_PRAGMA(clang diagnostic ignored WARNING_NAME##_CLANG) \
  • DO_PRAGMA(GCC diagnostic ignored WARNING_NAME##_GCC)
  • ```
  • Which can be used like:
  • ```
  • // find some custom central naming for your warnings
  • #define WARN_UNINITIALIZED_CLANG "-Wuninitialized"
  • #define WARN_UNINITIALIZED_GCC "-Wuninitialized"
  • int main(int argc, char **argv) {
  • float a = 0, b;
  • PUSH_WARNING_STATE
  • DISABLE_WARNING(WARN_UNINITIALIZED);
  • if(a != b) {
  • POP_WARNING_STATE
  • return a == b;
  • }
  • }
  • ```
  • Compiling gives only the second error, as intended:
  • ```
  • $ g++ -Wall -pedantic test.cpp
  • test.cpp: In function 'int main(int, char**)':
  • test.cpp:39:12: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  • 39 | return a == b;
  • | ~~^~~~
  • ```
  • Limitations:
  • - problem with warnings that can only be disabled on some platforms (-> pragma might trigger a warning)
#3: Post edited by user avatar Someone‭ · 2021-01-21T13:03:52Z (over 3 years ago)
  • Since C++11 the standard mandates the [`_Pragma` operator](https://en.cppreference.com/w/cpp/preprocessor/impl) which is intended for use inside macros.
  • With this you can improve the situation by wrapping all this compiler switching into a central macro definition:
  • ```
  • // see https://stackoverflow.com/a/45783809
  • #define DO_PRAGMA_(x) _Pragma (#x)
  • #define DO_PRAGMA(x) DO_PRAGMA_(x)
  • #if defined(_MSC_VER)
  • #define PUSH_WARNING_STATE DO_PRAGMA(warnings(push)) # untested!
  • #elif defined(__clang__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(clang diagnostic push)
  • #elif defined(__GNUC__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(GCC diagnostic push)
  • #endif
  • #if defined(_MSC_VER)
  • #define POP_WARNING_STATE DO_PRAGMA(warnings(poph)) # untested!
  • #elif defined(__clang__)
  • #define POP_WARNING_STATE DO_PRAGMA(clang diagnostic pop)
  • #elif defined(__GNUC__)
  • #define POP_WARNING_STATE DO_PRAGMA(GCC diagnostic pop)
  • #endif
  • #if defined(_MSC_VER)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(warnings(disable : WARNING_NAME##_MSC)
  • #elif defined(__clang__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(clang diagnostic ignored WARNING_NAME##_CLANG)
  • #elif defined(__GNUC__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(GCC diagnostic ignored WARNING_NAME##_GCC)
  • #endif
  • ```
  • Which can be used like:
  • ```
  • // find some custom central naming for your warnings
  • #define WARN_UNINITIALIZED_CLANG "-Wuninitialized"
  • #define WARN_UNINITIALIZED_GCC "-Wuninitialized"
  • int main(int argc, char **argv) {
  • float a = 0, b;
  • PUSH_WARNING_STATE
  • DISABLE_WARNING(WARN_UNINITIALIZED);
  • if(a != b) {
  • POP_WARNING_STATE
  • return a == b;
  • }
  • }
  • ```
  • Compiling gives only the second error, as intended:
  • ```
  • $ g++ -Wall -pedantic test.cpp
  • test.cpp: In function 'int main(int, char**)':
  • test.cpp:39:12: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  • 39 | return a == b;
  • | ~~^~~~
  • ```
  • Since C++11 the standard mandates the [`_Pragma` operator](https://en.cppreference.com/w/cpp/preprocessor/impl) which is intended for use inside macros.
  • With this you can improve the situation by wrapping all this compiler switching into a central macro definition (**MSVC is untested!**):
  • ```
  • // see https://stackoverflow.com/a/45783809
  • #define DO_PRAGMA_(x) _Pragma (#x)
  • #define DO_PRAGMA(x) DO_PRAGMA_(x)
  • #if defined(_MSC_VER)
  • #define PUSH_WARNING_STATE DO_PRAGMA(warnings(push)) # untested!
  • #elif defined(__clang__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(clang diagnostic push)
  • #elif defined(__GNUC__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(GCC diagnostic push)
  • #endif
  • #if defined(_MSC_VER)
  • #define POP_WARNING_STATE DO_PRAGMA(warnings(poph)) # untested!
  • #elif defined(__clang__)
  • #define POP_WARNING_STATE DO_PRAGMA(clang diagnostic pop)
  • #elif defined(__GNUC__)
  • #define POP_WARNING_STATE DO_PRAGMA(GCC diagnostic pop)
  • #endif
  • #if defined(_MSC_VER)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(warnings(disable : WARNING_NAME##_MSC)
  • #elif defined(__clang__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(clang diagnostic ignored WARNING_NAME##_CLANG)
  • #elif defined(__GNUC__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(GCC diagnostic ignored WARNING_NAME##_GCC)
  • #endif
  • ```
  • Which can be used like:
  • ```
  • // find some custom central naming for your warnings
  • #define WARN_UNINITIALIZED_CLANG "-Wuninitialized"
  • #define WARN_UNINITIALIZED_GCC "-Wuninitialized"
  • int main(int argc, char **argv) {
  • float a = 0, b;
  • PUSH_WARNING_STATE
  • DISABLE_WARNING(WARN_UNINITIALIZED);
  • if(a != b) {
  • POP_WARNING_STATE
  • return a == b;
  • }
  • }
  • ```
  • Compiling gives only the second error, as intended:
  • ```
  • $ g++ -Wall -pedantic test.cpp
  • test.cpp: In function 'int main(int, char**)':
  • test.cpp:39:12: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  • 39 | return a == b;
  • | ~~^~~~
  • ```
#2: Post edited by user avatar Someone‭ · 2021-01-21T13:02:05Z (over 3 years ago)
Added compiler output
  • Since C++11 the standard mandates the [`_Pragma` operator](https://en.cppreference.com/w/cpp/preprocessor/impl) which is intended for use inside macros.
  • With this you can improve the situation by wrapping all this compiler switching into a central macro definition:
  • ```
  • // see https://stackoverflow.com/a/45783809
  • #define DO_PRAGMA_(x) _Pragma (#x)
  • #define DO_PRAGMA(x) DO_PRAGMA_(x)
  • #if defined(_MSC_VER)
  • #define PUSH_WARNING_STATE DO_PRAGMA(warnings(push)) # untested!
  • #elif defined(__clang__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(clang diagnostic push)
  • #elif defined(__GNUC__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(GCC diagnostic push)
  • #endif
  • #if defined(_MSC_VER)
  • #define POP_WARNING_STATE DO_PRAGMA(warnings(poph)) # untested!
  • #elif defined(__clang__)
  • #define POP_WARNING_STATE DO_PRAGMA(clang diagnostic pop)
  • #elif defined(__GNUC__)
  • #define POP_WARNING_STATE DO_PRAGMA(GCC diagnostic pop)
  • #endif
  • #if defined(_MSC_VER)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(warnings(disable : WARNING_NAME##_MSC)
  • #elif defined(__clang__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(clang diagnostic ignored WARNING_NAME##_CLANG)
  • #elif defined(__GNUC__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(GCC diagnostic ignored WARNING_NAME##_GCC)
  • #endif
  • ```
  • Which can be used like:
  • ```
  • // find some custom central naming for your warnings
  • #define WARN_UNINITIALIZED_CLANG "-Wuninitialized"
  • #define WARN_UNINITIALIZED_GCC "-Wuninitialized"
  • int main(int argc, char **argv) {
  • float a = 0, b;
  • PUSH_WARNING_STATE
  • DISABLE_WARNING(WARN_UNINITIALIZED);
  • if(a != b) {
  • POP_WARNING_STATE
  • return a == b;
  • }
  • }
  • ```
  • Since C++11 the standard mandates the [`_Pragma` operator](https://en.cppreference.com/w/cpp/preprocessor/impl) which is intended for use inside macros.
  • With this you can improve the situation by wrapping all this compiler switching into a central macro definition:
  • ```
  • // see https://stackoverflow.com/a/45783809
  • #define DO_PRAGMA_(x) _Pragma (#x)
  • #define DO_PRAGMA(x) DO_PRAGMA_(x)
  • #if defined(_MSC_VER)
  • #define PUSH_WARNING_STATE DO_PRAGMA(warnings(push)) # untested!
  • #elif defined(__clang__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(clang diagnostic push)
  • #elif defined(__GNUC__)
  • #define PUSH_WARNING_STATE DO_PRAGMA(GCC diagnostic push)
  • #endif
  • #if defined(_MSC_VER)
  • #define POP_WARNING_STATE DO_PRAGMA(warnings(poph)) # untested!
  • #elif defined(__clang__)
  • #define POP_WARNING_STATE DO_PRAGMA(clang diagnostic pop)
  • #elif defined(__GNUC__)
  • #define POP_WARNING_STATE DO_PRAGMA(GCC diagnostic pop)
  • #endif
  • #if defined(_MSC_VER)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(warnings(disable : WARNING_NAME##_MSC)
  • #elif defined(__clang__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(clang diagnostic ignored WARNING_NAME##_CLANG)
  • #elif defined(__GNUC__)
  • #define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(GCC diagnostic ignored WARNING_NAME##_GCC)
  • #endif
  • ```
  • Which can be used like:
  • ```
  • // find some custom central naming for your warnings
  • #define WARN_UNINITIALIZED_CLANG "-Wuninitialized"
  • #define WARN_UNINITIALIZED_GCC "-Wuninitialized"
  • int main(int argc, char **argv) {
  • float a = 0, b;
  • PUSH_WARNING_STATE
  • DISABLE_WARNING(WARN_UNINITIALIZED);
  • if(a != b) {
  • POP_WARNING_STATE
  • return a == b;
  • }
  • }
  • ```
  • Compiling gives only the second error, as intended:
  • ```
  • $ g++ -Wall -pedantic test.cpp
  • test.cpp: In function 'int main(int, char**)':
  • test.cpp:39:12: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  • 39 | return a == b;
  • | ~~^~~~
  • ```
#1: Initial revision by user avatar Someone‭ · 2021-01-21T13:00:17Z (over 3 years ago)
Since C++11 the standard mandates the [`_Pragma` operator](https://en.cppreference.com/w/cpp/preprocessor/impl) which is intended for use inside macros.

With this you can improve the situation by wrapping all this compiler switching into a central macro definition:

```
// see https://stackoverflow.com/a/45783809
#define DO_PRAGMA_(x) _Pragma (#x)
#define DO_PRAGMA(x) DO_PRAGMA_(x)

#if defined(_MSC_VER)
#define PUSH_WARNING_STATE DO_PRAGMA(warnings(push)) # untested!
#elif defined(__clang__)
#define PUSH_WARNING_STATE DO_PRAGMA(clang diagnostic push)
#elif defined(__GNUC__)
#define PUSH_WARNING_STATE DO_PRAGMA(GCC diagnostic push)
#endif

#if defined(_MSC_VER)
#define POP_WARNING_STATE DO_PRAGMA(warnings(poph)) # untested!
#elif defined(__clang__)
#define POP_WARNING_STATE DO_PRAGMA(clang diagnostic pop)
#elif defined(__GNUC__)
#define POP_WARNING_STATE DO_PRAGMA(GCC diagnostic pop)
#endif

#if defined(_MSC_VER)
#define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(warnings(disable :  WARNING_NAME##_MSC)
#elif defined(__clang__)
#define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(clang diagnostic ignored WARNING_NAME##_CLANG)
#elif defined(__GNUC__)
#define DISABLE_WARNING(WARNING_NAME) DO_PRAGMA(GCC diagnostic ignored WARNING_NAME##_GCC)
#endif

```

Which can be used like:

```
// find some custom central naming for your warnings
#define WARN_UNINITIALIZED_CLANG "-Wuninitialized"
#define WARN_UNINITIALIZED_GCC "-Wuninitialized"

int main(int argc, char **argv) {
        float a = 0, b;
        PUSH_WARNING_STATE
        DISABLE_WARNING(WARN_UNINITIALIZED);
        if(a != b) {
                POP_WARNING_STATE
                return a == b;
        }
}
```