Skip to content

Commit

Permalink
Merge pull request #117 from biojppm/fix/winclang
Browse files Browse the repository at this point in the history
[fix] Windows: fix compilation with MSVC/clang
  • Loading branch information
biojppm authored May 15, 2023
2 parents b18c597 + 18b9abb commit 2332b5e
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 59 deletions.
53 changes: 50 additions & 3 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

#----------------------------------------------------------------------------
windows:
name: win/${{matrix.cxx}}/c++${{matrix.std}}/${{matrix.bt}}
name: ${{matrix.cxx}}/c++${{matrix.std}}/${{matrix.bt}}
continue-on-error: true
if: always() # https://stackoverflow.com/questions/62045967/github-actions-is-there-a-way-to-continue-on-error-while-still-getting-correct
runs-on: ${{matrix.os}}
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:

#----------------------------------------------------------------------------
windows_noexc:
name: win_noexc/${{matrix.cxx}}/c++${{matrix.std}}/${{matrix.bt}}
name: noexc/${{matrix.cxx}}/c++${{matrix.std}}/${{matrix.bt}}
continue-on-error: true
if: always() # https://stackoverflow.com/questions/62045967/github-actions-is-there-a-way-to-continue-on-error-while-still-getting-correct
runs-on: ${{matrix.os}}
Expand Down Expand Up @@ -125,10 +125,57 @@ jobs:
- {name: static32-run, run: source .github/setenv.sh && c4_run_test static32}
- {name: static32-pack, run: source .github/setenv.sh && c4_package static32}

#----------------------------------------------------------------------------
windows_clang:
name: clang/${{matrix.cxx}}/c++${{matrix.std}}/${{matrix.bt}}
continue-on-error: true
if: always() # https://stackoverflow.com/questions/62045967/github-actions-is-there-a-way-to-continue-on-error-while-still-getting-correct
runs-on: ${{matrix.os}}
strategy:
fail-fast: false
matrix:
include:
- {std: 11, cxx: vs2022, bt: Debug , os: windows-2022, bitlinks: shared64 static32}
- {std: 11, cxx: vs2022, bt: Release, os: windows-2022, bitlinks: shared64 static32}
- {std: 14, cxx: vs2022, bt: Debug , os: windows-2022, bitlinks: shared64 static32}
- {std: 14, cxx: vs2022, bt: Release, os: windows-2022, bitlinks: shared64 static32}
- {std: 17, cxx: vs2022, bt: Debug , os: windows-2022, bitlinks: shared64 static32}
- {std: 17, cxx: vs2022, bt: Release, os: windows-2022, bitlinks: shared64 static32}
- {std: 20, cxx: vs2022, bt: Debug , os: windows-2022, bitlinks: shared64 static32}
- {std: 20, cxx: vs2022, bt: Release, os: windows-2022, bitlinks: shared64 static32}
env: {STD: "${{matrix.std}}", CXX_: "${{matrix.cxx}}", BT: "${{matrix.bt}}", BITLINKS: "${{matrix.bitlinks}}", VG: "${{matrix.vg}}", SAN: "${{matrix.san}}", LINT: "${{matrix.lint}}", OS: "${{matrix.os}}",
BDIR: "build_${{matrix.arch}}_${{matrix.bt}}_${{matrix.std}}",
IDIR: "install_${{matrix.arch}}_${{matrix.bt}}_${{matrix.std}}"
}
steps:
- {name: checkout, uses: actions/checkout@v3, with: {submodules: recursive}}
- {name: show info, run: source .github/setenv.sh && c4_show_info}
- name: configure
run: |
set -x
echo BDIR=$BDIR
echo IDIR=$IDIR
cmake -S . -B $BDIR \
-G "Unix Makefiles" \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_INSTALL_PREFIX=$IDIR \
-DCMAKE_BUILD_TYPE=${{matrix.bt}} \
-DC4_CXX_STANDARD=${{matrix.std}} \
-DCXX_STANDARD=${{matrix.std}} \
-DC4CORE_DEV=ON \
-DC4CORE_BUILD_BENCHMARKS=OFF \
-DC4CORE_SANITIZE=OFF \
-DC4CORE_LINT=OFF \
-DC4CORE_VALGRIND=OFF
- name: build
run: cmake --build $BDIR -j --target c4core-test-build
- name: run
run: cmake --build $BDIR -j --target c4core-test-run

#----------------------------------------------------------------------------
# TODO how to run?
windows_arm:
name: win_arm/${{matrix.cxx}}/c++${{matrix.std}}/${{matrix.bt}}
name: arm/${{matrix.cxx}}/c++${{matrix.std}}/${{matrix.bt}}
continue-on-error: true
if: always() # https://stackoverflow.com/questions/62045967/github-actions-is-there-a-way-to-continue-on-error-while-still-getting-correct
runs-on: ${{matrix.os}}
Expand Down
1 change: 1 addition & 0 deletions changelog/current.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ assert(to_substr((char*)ptr).len == 3); // as before
- [PR#104](https://github.com/biojppm/c4core/pull/104)/[PR#112](https://github.com/biojppm/c4core/pull/112) - Fix pedantic warnings in gcc, clang and MSVC
- [PR#104](https://github.com/biojppm/c4core/pull/104) - Fix possible compile error when `__GNUC__` is not defined
- Inject explicit `#include <charconv>` on the amalgamated header. The amalgamation tool was filtering all prior includes, thus causing a compilation error. Addresses [rapidyaml#364](https://github.com/biojppm/rapidyaml/issues/364).
- [PR#117](https://github.com/biojppm/c4core/pull/117): Windows: fix compilation with MSVC/clang++.
- Windows: add missing `C4CORE_EXPORT` to `c4::base64_valid()`, `c4::base64_encode()` and `c4::base64_decode()`.
11 changes: 8 additions & 3 deletions src/c4/bitmask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include "c4/enum.hpp"
#include "c4/format.hpp"

#ifdef _MSC_VER
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4996) // 'strncpy', fopen, etc: This function or variable may be unsafe
#elif defined(__clang__)
#endif

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wold-style-cast"
#elif defined(__GNUC__)
Expand Down Expand Up @@ -325,7 +327,10 @@ typename std::underlying_type<Enum>::type str2bm(const char *str)

#ifdef _MSC_VER
# pragma warning(pop)
#elif defined(__clang__)
#endif

#if defined(__clang__)
# pragma clang diagnostic pop
#elif defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
Expand Down
12 changes: 8 additions & 4 deletions src/c4/charconv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@
#endif


#ifdef _MSC_VER
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable: 4996) // snprintf/scanf: this function or variable may be unsafe
# if C4_MSVC_VERSION != C4_MSVC_VERSION_2017
# pragma warning(disable: 4800) //'int': forcing value to bool 'true' or 'false' (performance warning)
# endif
# pragma warning(disable: 4996) // snprintf/scanf: this function or variable may be unsafe
#elif defined(__clang__)
#endif

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
# pragma clang diagnostic ignored "-Wformat-nonliteral"
Expand Down Expand Up @@ -2410,7 +2412,9 @@ inline size_t to_chars(substr buf, const char * C4_RESTRICT v) noexcept

#ifdef _MSC_VER
# pragma warning(pop)
#elif defined(__clang__)
#endif

#if defined(__clang__)
# pragma clang diagnostic pop
#elif defined(__GNUC__)
# pragma GCC diagnostic pop
Expand Down
2 changes: 1 addition & 1 deletion src/c4/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/** @see http://sourceforge.net/p/predef/wiki/Compilers/ for a list of compiler identifier macros */
/** @see https://msdn.microsoft.com/en-us/library/b0084kay.aspx for VS2013 predefined macros */

#if defined(_MSC_VER)// && (defined(C4_WIN) || defined(C4_XBOX) || defined(C4_UE4))
#if defined(_MSC_VER) && !defined(__clang__)
# define C4_MSVC
# define C4_MSVC_VERSION_2022 17
# define C4_MSVC_VERSION_2019 16
Expand Down
2 changes: 1 addition & 1 deletion src/c4/ext/fast_float.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _C4_EXT_FAST_FLOAT_HPP_
#define _C4_EXT_FAST_FLOAT_HPP_

#ifdef _MSC_VER
#if defined(_MSC_VER) && !defined(__clang__)
# pragma warning(push)
# pragma warning(disable: 4365) // '=': conversion from 'const _Ty' to 'fast_float::limb', signed/unsigned mismatch
# pragma warning(disable: 4996) // snprintf/scanf: this function or variable may be unsafe
Expand Down
75 changes: 35 additions & 40 deletions src/c4/language.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/* Detect C++ standard.
* @see http://stackoverflow.com/a/7132549/5875572 */
#ifndef C4_CPP
# ifdef _MSC_VER
# if defined(_MSC_VER) && !defined(__clang__)
# if _MSC_VER >= 1910 // >VS2015: VS2017, VS2019
# if (!defined(_MSVC_LANG))
# error _MSVC not defined
Expand Down Expand Up @@ -110,33 +110,27 @@
#endif

/** lifted from this answer: http://stackoverflow.com/a/20170989/5875572 */
#ifndef _MSC_VER
# if __cplusplus < 201103
#if defined(_MSC_VER) && !defined(__clang__)
# if _MSC_VER < 1900
# define C4_CONSTEXPR11
# define C4_CONSTEXPR14
//# define C4_NOEXCEPT
# elif __cplusplus == 201103
# elif _MSC_VER < 2000
# define C4_CONSTEXPR11 constexpr
# define C4_CONSTEXPR14
//# define C4_NOEXCEPT noexcept
# else
# define C4_CONSTEXPR11 constexpr
# define C4_CONSTEXPR14 constexpr
//# define C4_NOEXCEPT noexcept
# endif
#else // _MSC_VER
# if _MSC_VER < 1900
#else
# if __cplusplus < 201103
# define C4_CONSTEXPR11
# define C4_CONSTEXPR14
//# define C4_NOEXCEPT
# elif _MSC_VER < 2000
# elif __cplusplus == 201103
# define C4_CONSTEXPR11 constexpr
# define C4_CONSTEXPR14
//# define C4_NOEXCEPT noexcept
# else
# define C4_CONSTEXPR11 constexpr
# define C4_CONSTEXPR14 constexpr
//# define C4_NOEXCEPT noexcept
# endif
#endif // _MSC_VER

Expand All @@ -149,7 +143,7 @@
#define C4_INLINE_CONSTEXPR inline constexpr
#endif

#ifdef _MSC_VER
#if defined(_MSC_VER) && !defined(__clang__)
# if (defined(_CPPUNWIND) && (_CPPUNWIND == 1))
# define C4_EXCEPTIONS
# endif
Expand All @@ -167,7 +161,7 @@
# define C4_IF_EXCEPTIONS(exc_code, setjmp_code) do { setjmp_code } while(0)
#endif

#ifdef _MSC_VER
#if defined(_MSC_VER) && !defined(__clang__)
# if defined(_CPPRTTI)
# define C4_RTTI
# endif
Expand Down Expand Up @@ -203,7 +197,7 @@
//------------------------------------------------------------

#ifndef C4_API
# if defined(_MSC_VER)
# if defined(_MSC_VER) && !defined(__clang__)
# if defined(C4_EXPORT)
# define C4_API __declspec(dllexport)
# elif defined(C4_IMPORT)
Expand All @@ -216,7 +210,25 @@
# endif
#endif

#ifndef _MSC_VER ///< @todo assuming gcc-like compiler. check it is actually so.
#if defined(_MSC_VER) && !defined(__clang__)
# define C4_RESTRICT __restrict
# define C4_RESTRICT_FN __declspec(restrict)
# define C4_NO_INLINE __declspec(noinline)
# define C4_ALWAYS_INLINE inline __forceinline
/** these are not available in VS AFAIK */
# define C4_CONST
# define C4_PURE
# define C4_FLATTEN
# define C4_HOT /** @todo */
# define C4_COLD /** @todo */
# define C4_EXPECT(x, y) x /** @todo */
# define C4_LIKELY(x) x /** @todo */
# define C4_UNLIKELY(x) x /** @todo */
# define C4_UNREACHABLE() /** @todo */
# define C4_ATTR_FORMAT(...) /** */
# define C4_NORETURN /** @todo */
#else
///< @todo assuming gcc-like compiler. check it is actually so.
/** for function attributes in GCC,
* @see https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes */
/** for __builtin functions in GCC,
Expand All @@ -242,31 +254,14 @@
# define C4_UNREACHABLE() __builtin_unreachable()
# define C4_ATTR_FORMAT(...) //__attribute__((format (__VA_ARGS__))) ///< @see https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
# define C4_NORETURN __attribute__((noreturn))
#else
# define C4_RESTRICT __restrict
# define C4_RESTRICT_FN __declspec(restrict)
# define C4_NO_INLINE __declspec(noinline)
# define C4_ALWAYS_INLINE inline __forceinline
/** these are not available in VS AFAIK */
# define C4_CONST
# define C4_PURE
# define C4_FLATTEN
# define C4_HOT /** @todo */
# define C4_COLD /** @todo */
# define C4_EXPECT(x, y) x /** @todo */
# define C4_LIKELY(x) x /** @todo */
# define C4_UNLIKELY(x) x /** @todo */
# define C4_UNREACHABLE() /** @todo */
# define C4_ATTR_FORMAT(...) /** */
# define C4_NORETURN /** @todo */
#endif

#ifndef _MSC_VER
#ifdef _MSC_VER
# define C4_FUNC __FUNCTION__
# define C4_PRETTY_FUNC __PRETTY_FUNCTION__
# define C4_PRETTY_FUNC __FUNCSIG__
#else /// @todo assuming gcc-like compiler. check it is actually so.
# define C4_FUNC __FUNCTION__
# define C4_PRETTY_FUNC __FUNCSIG__
# define C4_PRETTY_FUNC __PRETTY_FUNCTION__
#endif

/** prevent compiler warnings about a specific var being unused */
Expand Down Expand Up @@ -296,10 +291,10 @@ void use_char_pointer(char const volatile*);

/** @def C4_KEEP_EMPTY_LOOP prevent an empty loop from being optimized out.
* @see http://stackoverflow.com/a/7084193/5875572 */
#ifndef _MSC_VER
# define C4_KEEP_EMPTY_LOOP { asm(""); }
#else
#if defined(_MSC_VER) && !defined(__clang__)
# define C4_KEEP_EMPTY_LOOP { char c; C4_DONT_OPTIMIZE(c); }
#else
# define C4_KEEP_EMPTY_LOOP { asm(""); }
#endif

/** @def C4_VA_LIST_REUSE_MUST_COPY
Expand Down
16 changes: 9 additions & 7 deletions src/c4/type_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/** @file type_name.hpp compile-time type name */

#include "c4/span.hpp"
#include "c4/compiler.hpp"

/// @cond dev
struct _c4t
Expand Down Expand Up @@ -36,26 +37,27 @@ C4_CONSTEXPR14 cspan<char> type_name()
{
const _c4t p = _c4tn<T>();

#if (0) // _C4_THIS_IS_A_DEBUG_SCAFFOLD
#if (0) // enable this to debug and find the offsets
for(size_t index = 0; index < p.sz; ++index)
{
printf(" %2c", p.str[index]);
}
printf("\n");
for(size_t index = 0; index < p.sz; ++index)
{
printf(" %2d", (int)index);
}
printf(" %2zu", index);
printf("\n");
#endif

#if defined(_MSC_VER)
# if defined(__clang__) // Visual Studio has the clang toolset
# if (_MSC_VER >= 1930) // do not use this: defined(C4_MSVC_2022)
// ..............................xxx.
// _c4t __cdecl _c4tn(void) [T = int]
enum : size_t { tstart = 30, tend = 1};
# else
// example:
// ..........................xxx.
// _c4t __cdecl _c4tn() [T = int]
enum : size_t { tstart = 26, tend = 1};

# endif
# elif defined(C4_MSVC_2015) || defined(C4_MSVC_2017) || defined(C4_MSVC_2019) || defined(C4_MSVC_2022)
// Note: subtract 7 at the end because the function terminates with ">(void)" in VS2015+
cspan<char>::size_type tstart = 26, tend = 7;
Expand Down

0 comments on commit 2332b5e

Please sign in to comment.