lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master a5716af 3/6: Define LMI_GCC symbol and use it


From: Greg Chicares
Subject: [lmi-commits] [lmi] master a5716af 3/6: Define LMI_GCC symbol and use it for gcc-specific parts
Date: Fri, 16 Apr 2021 18:12:49 -0400 (EDT)

branch: master
commit a5716af3459022b6c0689ee2a49aacc15ce16ce1
Author: Vadim Zeitlin <vadim@tt-solutions.com>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Define LMI_GCC symbol and use it for gcc-specific parts
    
    Make it simpler to check if lmi is being compiled with the real gcc or
    just with a mostly gcc-compatible compiler, such as clang.
    
    This allows to simplify the checks around the pragmas disabling
    gcc-specific (and not present in clang) warnings.
    
    No real changes.
---
 config.hpp          |  9 +++++++++
 getopt.cpp          |  8 ++++----
 md5.cpp             |  4 ++--
 numeric_io_test.cpp |  8 ++++----
 pchfile_wx.hpp      |  2 ++
 snprintf_test.cpp   | 16 ++++++++--------
 6 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/config.hpp b/config.hpp
index 261cb9e..1aa5108 100644
--- a/config.hpp
+++ b/config.hpp
@@ -108,6 +108,15 @@ namespace fs = boost::filesystem;
 //
 #include "platform_dependent.hpp"
 
+// Testing for the predefined __GNUC__ is not always the right thing
+// to do, as it is also defined by mostly gcc-compatible compilers
+// such as clang or (not currently supported) icc, so define a symbol
+// which is only defined for gcc itself, but not any others, to be
+// used where the difference between them matters.
+#if defined __GNUC__ && !defined __clang__
+#   define LMI_GCC
+#endif // defined __GNUC__ && !defined __clang__
+
 // It is impossible to compile lmi with g++ prior to version 3, though
 // old versions of gcc would be adequate for C translation units.
 
diff --git a/getopt.cpp b/getopt.cpp
index e98dfd5..f4beb31 100644
--- a/getopt.cpp
+++ b/getopt.cpp
@@ -735,19 +735,19 @@ GetOpt::operator()()
 
   {
     int c = *nextchar++;
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic push
 #   pragma GCC diagnostic ignored "-Wuseless-cast"
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
     // i686-w64-mingw32-g++-7.3 flags this cast as "useless", but
     // that seems to be a defect: the first argument is const, so
     // the return value is also const. Perhaps the presence of C99's
     // 'char* strchr(char const*, int);' prototype confuses g++, but
     // it's still a defect.
     char* temp = const_cast<char*>(std::strchr(noptstring.c_str(), c));
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic pop
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
 
     // Increment 'optind' when we start to process its last character.
     if(*nextchar == 0)
diff --git a/md5.cpp b/md5.cpp
index f362c89..7c4e94c 100644
--- a/md5.cpp
+++ b/md5.cpp
@@ -88,10 +88,10 @@
 # define SWAP(n) (n)
 #endif // !defined WORDS_BIGENDIAN
 
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic ignored "-Wold-style-cast"
 #   pragma GCC diagnostic ignored "-Wuseless-cast"
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
 
 /* This array contains the bytes used to pad the buffer to the next
  * 64-byte boundary. (RFC 1321, 3.1: Step 1)
diff --git a/numeric_io_test.cpp b/numeric_io_test.cpp
index c828d66..d4c68ed 100644
--- a/numeric_io_test.cpp
+++ b/numeric_io_test.cpp
@@ -236,10 +236,10 @@ int test_main(int, char*[])
     LMI_TEST_EQUAL( "Z ", numeric_io_cast<std::string>( "Z "));
     LMI_TEST_EQUAL(" Z ", numeric_io_cast<std::string>(" Z "));
 
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic push
 #   pragma GCC diagnostic ignored "-Wuseless-cast"
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
 
     test_interconvertibility(static_cast<         char>(   1), "1", __FILE__, 
__LINE__);
     test_interconvertibility(static_cast<         char>('\1'), "1", __FILE__, 
__LINE__);
@@ -297,9 +297,9 @@ int test_main(int, char*[])
     LMI_TEST_EQUAL(numeric_io_cast<long 
double>("3.36210314311209350626e-4932"), std::numeric_limits<long 
double>::min());
 #endif // !defined LMI_MSVCRT
 
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic pop
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
 
     test_interconvertibility(std::string("  as  df  "), "  as  df  ", 
__FILE__, __LINE__);
     // The converse
diff --git a/pchfile_wx.hpp b/pchfile_wx.hpp
index fefe173..33c5d15 100644
--- a/pchfile_wx.hpp
+++ b/pchfile_wx.hpp
@@ -34,6 +34,8 @@
 // Even if precompiled headers are not really being used, use this header to
 // disable some warnings which are enabled for the rest of lmi code but have to
 // be disabled for the code using wxWidgets as they occur in wxWidgets headers.
+// Note carefully that 'config.hpp' has not been included yet, and therefore
+// the following conditionals must not use 'LMI_GCC'.
 #if defined __GNUC__ && !defined __clang__
 //#   pragma GCC diagnostic ignored "-Wcast-qual"
 //#   pragma GCC diagnostic ignored "-Wdouble-promotion"
diff --git a/snprintf_test.cpp b/snprintf_test.cpp
index 927b53c..59d27f8 100644
--- a/snprintf_test.cpp
+++ b/snprintf_test.cpp
@@ -43,19 +43,19 @@ int test_main(int, char*[])
     LMI_TEST_EQUAL(4, len);
 
     // All tests in this group fail with the defective msvc rtl.
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic push
 #   pragma GCC diagnostic ignored "-Wformat-truncation"
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
     len = std::snprintf(buf, 3, "%4d", 1234);
     LMI_TEST_EQUAL(4, len);
     // This test fails with borland C++ 5.5.1 .
     LMI_TEST_EQUAL(std::string(buf, 9), std::string("12\0zzzzzz\0", 9));
 
     len = std::snprintf(buf, 4, "%4d", 1234);
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic pop
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
     LMI_TEST_EQUAL(4, len);
     // This test fails with the defective msvc rtl and also
     // with borland C++ 5.5.1 .
@@ -66,18 +66,18 @@ int test_main(int, char*[])
     LMI_TEST_EQUAL(std::string(buf, 9), std::string("1234\0zzzz\0", 9));
 
     long double z = 2.718281828459045L;
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic push
 #   pragma GCC diagnostic ignored "-Wformat-truncation"
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
     len = std::snprintf(buf, 5, "%.5Lf", z);
     LMI_TEST_EQUAL(7, len);
     // This should truncate to 2.71, not round to 2.72 .
     LMI_TEST_EQUAL(std::string(buf, 9), std::string("2.71\0zzzz\0", 9));
     len = std::snprintf(buf, 7, "%.5Lf", z);
-#if defined __GNUC__ && !defined __clang__
+#if defined LMI_GCC
 #   pragma GCC diagnostic pop
-#endif // defined __GNUC__ && !defined __clang__
+#endif // defined LMI_GCC
     LMI_TEST_EQUAL(7, len);
     LMI_TEST_EQUAL(std::string(buf, 9), std::string("2.7182\0zz\0", 9));
     len = std::snprintf(buf,       0, "%1.12Lf", z);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]