From 887b1da5e6161893ecec83c9bdc1c224fa752e92 Mon Sep 17 00:00:00 2001
From: Zsolt Parragi <zsolt.parragi@cancellar.hu>
Date: Wed, 29 May 2019 17:33:05 +0200
Subject: [PATCH] Fixing issues when building with clang 8.0 on Windows

* Clang with the MSVC STL requires C++14
* Deprecatoion warning suppressions were specific to MSVC and Intel
* An assignment inside a condition resulted in a warning
* kwsysProcessSetExitException was declared, but never defined or used
---
 CMakeLists.txt        | 12 ++++++++++--
 ProcessWin32.c        | 23 ++++++++++++++++++-----
 SystemInformation.cxx |  9 ++++++++-
 SystemTools.cxx       |  9 ++++++++-
 testConsoleBuf.cxx    |  9 ++++++++-
 5 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 120b5de3..79e813ee 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -120,7 +120,14 @@ SET_PROPERTY(DIRECTORY
 if(KWSYS_CXX_STANDARD)
   set(CMAKE_CXX_STANDARD "${KWSYS_CXX_STANDARD}")
 elseif(NOT DEFINED CMAKE_CXX_STANDARD AND NOT DEFINED KWSYS_CXX_STANDARD)
-  set(CMAKE_CXX_STANDARD 11)
+  if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
+     AND "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC"
+     AND "x${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "xGNU"
+    )
+    set(CMAKE_CXX_STANDARD 14)
+  else()
+    set(CMAKE_CXX_STANDARD 11)
+  endif()
 endif()
 
 # Select library components.
@@ -1005,7 +1012,8 @@ ENDIF()
 ADD_DEFINITIONS("-DKWSYS_NAMESPACE=${KWSYS_NAMESPACE}")
 
 # Disable deprecation warnings for standard C functions.
-IF(MSVC OR (WIN32 AND CMAKE_C_COMPILER_ID STREQUAL "Intel"))
+IF(MSVC OR (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "Intel" OR
+   (CMAKE_C_COMPILER_ID STREQUAL "Clang"  AND "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC"))))
   ADD_DEFINITIONS(
     -D_CRT_NONSTDC_NO_DEPRECATE
     -D_CRT_SECURE_NO_DEPRECATE
diff --git a/ProcessWin32.c b/ProcessWin32.c
index 2a2e7376..68c52185 100644
--- a/ProcessWin32.c
+++ b/ProcessWin32.c
@@ -117,7 +117,6 @@ static kwsysProcessTime kwsysProcessTimeAdd(kwsysProcessTime in1,
                                             kwsysProcessTime in2);
 static kwsysProcessTime kwsysProcessTimeSubtract(kwsysProcessTime in1,
                                                  kwsysProcessTime in2);
-static void kwsysProcessSetExitException(kwsysProcess* cp, int code);
 static void kwsysProcessSetExitExceptionByIndex(kwsysProcess* cp, int code,
                                                 int idx);
 static void kwsysProcessKillTree(int pid);
@@ -358,13 +357,20 @@ kwsysProcess* kwsysProcess_New(void)
 #  pragma warning(push)
 #  ifdef __INTEL_COMPILER
 #    pragma warning(disable : 1478)
+#  elif defined __clang__
+#    pragma clang diagnostic push
+#    pragma clang diagnostic ignored "-Wdeprecated-declarations"
 #  else
 #    pragma warning(disable : 4996)
 #  endif
 #endif
   GetVersionEx(&osv);
 #ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx
-#  pragma warning(pop)
+#  ifdef __clang__
+#    pragma clang diagnostic pop
+#  else
+#    pragma warning(pop)
+#  endif
 #endif
   if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
     /* Win9x no longer supported.  */
@@ -2269,13 +2275,20 @@ static kwsysProcess_List* kwsysProcess_List_New(void)
 #  pragma warning(push)
 #  ifdef __INTEL_COMPILER
 #    pragma warning(disable : 1478)
+#  elif defined __clang__
+#    pragma clang diagnostic push
+#    pragma clang diagnostic ignored "-Wdeprecated-declarations"
 #  else
 #    pragma warning(disable : 4996)
 #  endif
 #endif
   GetVersionEx(&osv);
 #ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx
-#  pragma warning(pop)
+#  ifdef __clang__
+#    pragma clang diagnostic pop
+#  else
+#    pragma warning(pop)
+#  endif
 #endif
   self->NT4 =
     (osv.dwPlatformId == VER_PLATFORM_WIN32_NT && osv.dwMajorVersion < 5) ? 1
@@ -2659,8 +2672,8 @@ static int kwsysProcessesAdd(HANDLE hProcess, DWORD dwProcessid,
     newSize = kwsysProcesses.Size ? kwsysProcesses.Size * 2 : 4;
 
     /* Try allocating the new block of memory.  */
-    if (newArray = (kwsysProcessInstance*)malloc(
-          newSize * sizeof(kwsysProcessInstance))) {
+    if ((newArray = (kwsysProcessInstance*)malloc(
+           newSize * sizeof(kwsysProcessInstance)))) {
       /* Copy the old process handles to the new memory.  */
       if (kwsysProcesses.Count > 0) {
         memcpy(newArray, kwsysProcesses.Processes,
diff --git a/SystemInformation.cxx b/SystemInformation.cxx
index 7b697c48..7dc6cf4b 100644
--- a/SystemInformation.cxx
+++ b/SystemInformation.cxx
@@ -5205,6 +5205,9 @@ bool SystemInformationImplementation::QueryOSInformation()
 #    pragma warning(push)
 #    ifdef __INTEL_COMPILER
 #      pragma warning(disable : 1478)
+#    elif defined __clang__
+#      pragma clang diagnostic push
+#      pragma clang diagnostic ignored "-Wdeprecated-declarations"
 #    else
 #      pragma warning(disable : 4996)
 #    endif
@@ -5217,7 +5220,11 @@ bool SystemInformationImplementation::QueryOSInformation()
     }
   }
 #  ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx
-#    pragma warning(pop)
+#    ifdef __clang__
+#      pragma clang diagnostic pop
+#    else
+#      pragma warning(pop)
+#    endif
 #  endif
 
   switch (osvi.dwPlatformId) {
diff --git a/SystemTools.cxx b/SystemTools.cxx
index e756cdcf..2135913f 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -4366,6 +4366,9 @@ std::string SystemTools::GetOperatingSystemNameAndVersion()
 #    pragma warning(push)
 #    ifdef __INTEL_COMPILER
 #      pragma warning(disable : 1478)
+#    elif defined __clang__
+#      pragma clang diagnostic push
+#      pragma clang diagnostic ignored "-Wdeprecated-declarations"
 #    else
 #      pragma warning(disable : 4996)
 #    endif
@@ -4375,7 +4378,11 @@ std::string SystemTools::GetOperatingSystemNameAndVersion()
     return 0;
   }
 #  ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx
-#    pragma warning(pop)
+#    ifdef __clang__
+#      pragma clang diagnostic pop
+#    else
+#      pragma warning(pop)
+#    endif
 #  endif
 
   switch (osvi.dwPlatformId) {
diff --git a/testConsoleBuf.cxx b/testConsoleBuf.cxx
index 15494405..b6ad1188 100644
--- a/testConsoleBuf.cxx
+++ b/testConsoleBuf.cxx
@@ -499,6 +499,9 @@ static int testConsole()
 #    pragma warning(push)
 #    ifdef __INTEL_COMPILER
 #      pragma warning(disable : 1478)
+#    elif defined __clang__
+#      pragma clang diagnostic push
+#      pragma clang diagnostic ignored "-Wdeprecated-declarations"
 #    else
 #      pragma warning(disable : 4996)
 #    endif
@@ -506,7 +509,11 @@ static int testConsole()
   const bool isVistaOrGreater =
     LOBYTE(LOWORD(GetVersion())) >= HIBYTE(_WIN32_WINNT_VISTA);
 #  ifdef KWSYS_WINDOWS_DEPRECATED_GetVersion
-#    pragma warning(pop)
+#    ifdef __clang__
+#      pragma clang diagnostic pop
+#    else
+#      pragma warning(pop)
+#    endif
 #  endif
   if (!isVistaOrGreater) {
     if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Console", 0, KEY_READ | KEY_WRITE,
-- 
GitLab