Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to suppress all clang-target-msvc test warning in CMAKE and other misc fixes #1151

Merged
merged 36 commits into from
May 11, 2019
Merged

Conversation

denchat
Copy link
Contributor

@denchat denchat commented May 8, 2019

  • Trying to suppress all clang-target-msvc warnings that are caused by gtest using CRT unsafe functions (_CRT_SECURE_NO_WARNINGS) and POSIX functions in CMAKE
  • Trying to automatically enable exceptions in MSVC in CMAKE file both fmt and test (Not sure if desirable?)
  • Fix char8_t in format-test.cc
  • Suppress clang-target-msvc warning in posix-mock-test.cc

I agree that my contributions are licensed under the {fmt} license, and agree to future changes to the licensing.

…larations

Suppress warning _CRT_SECURE_NO_WARNINGS in MSVC and -Wdeprecated-declarations of POSIX functions in Clang target MSVC.
Those functions are used by gtest.
User must manually turn on exceptions otherwise error occurs when compiling `format.cc` as below

fmt-master\include\fmt/format-inl.h(891,3): error: cannot use 'try' with exceptions disabled
  FMT_TRY {
  ^
@denchat denchat changed the title Trying to suppress all clang-target-msvc warning in CMAKE and other misc fixes Trying to suppress all clang-target-msvc test warning in CMAKE and other misc fixes May 8, 2019
Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR! A few comments inline.

CMakeLists.txt Outdated
@@ -161,6 +161,9 @@ endif ()
if (FMT_PEDANTIC)
target_compile_options(fmt PRIVATE ${PEDANTIC_COMPILE_FLAGS})
endif ()
if (MSVC)
target_compile_options(fmt PRIVATE /EHsc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exceptions are enabled by default on MSVC. Why is this needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also force the library into a specific exception handling model, which would be undesirable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitaut : My clang-target-msvc isn't defaulting /EHsc. Then I guess this should just trying to mirror /EHsc default in MSVC cl.exe. Perhaps...

if (MSVC)
should be changed into
if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" )

@eliaskosunen Would you be okay with narrowing to clang-target-msvc for mirroring the default /EHsc above?

Should there be FMT_MSVC_EH option that explicitly add custom /EHxx?

Copy link
Contributor Author

@denchat denchat May 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, perhaps we can have FMT_MSVC_EH like this :

if (MSVC)
  if (FMT_MSVC_EH)
    set(FMT_CUSTOM_MSVC_EH ${FMT_MSVC_EH})
  else ()
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
      set(FMT_CUSTOM_MSVC_EH /EHsc)
    else ()
      set(FMT_CUSTOM_MSVC_EH)
    endif ()
  endif ()
  target_compile_options(fmt PRIVATE ${FMT_CUSTOM_MSVC_EH})
endif ()

What do you guys think?

Copy link
Contributor Author

@denchat denchat May 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why TravisCI evaluates MSVC true in apple platform and linux clang platform.
So I try to workaround with

if (MSVC AND WIN32)
  if (FMT_MSVC_EH)
    set(FMT_CUSTOM_MSVC_EH ${FMT_MSVC_EH})
  else ()
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
      set(FMT_CUSTOM_MSVC_EH /EHsc)
    else ()
      set(FMT_CUSTOM_MSVC_EH)
    endif ()
  endif ()
  target_compile_options(fmt PRIVATE ${FMT_CUSTOM_MSVC_EH})
endif ()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it's because my excessing endif, not MSVC weirdness. Fixed it.

Copy link
Contributor

@vitaut vitaut May 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My clang-target-msvc isn't defaulting /EHsc.

So what is the default in clang-target-msvc? Are exceptions disabled by default or enabled with a different setting?

Copy link
Contributor Author

@denchat denchat May 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C:\Users\User\AppData\Roaming\fmt>ninja -j 2
[1/47] Building CXX object CMakeFiles\fmt.dir\src\posix.cc.obj
FAILED: CMakeFiles/fmt.dir/src/posix.cc.obj
C:\PROGRA~1\LLVM\bin\clang-cl.exe  /nologo -TP -DFMT_EXPORT -Dfmt_EXPORTS -IC:\Users\User\AppData\Roaming\fmt-master\include /MD /O2 /Ob2 /DNDEBUG   -std:c++latest /showIncludes /FoCMakeFiles\fmt.dir\src\posix.cc.obj /FdCMakeFiles\fmt.dir\ -c C:\Users\User\AppData\Roaming\fmt-master\src\posix.cc
In file included from C:\Users\User\AppData\Roaming\fmt-master\src\posix.cc:13:
In file included from C:\Users\User\AppData\Roaming\fmt-master\include\fmt/posix.h:28:
C:\Users\User\AppData\Roaming\fmt-master\include\fmt/format.h(121,10): error: cannot use 'throw' with exceptions disabled
  if (b) throw x;
         ^

It seems exceptions are disabled by default. Well, you are right, after take a look at => user manual, I found that users can explicitly state the configurations through very generous means. However, clang-cl.exe alone basically does not enable exceptions by default.

Nevertheless, if you think this compiler case wouldn't be worth for the added complexity, I will be fine with dropping these CMAKE changes for /EHsc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed conditional adding /EHsc from CMAKELists.txt in both root and test.

// type in this mode. If this is the case __cpp_char8_t will be defined.
#ifndef __cpp_char8_t
// A UTF-8 code unit type.
#define CHAR8_T fmt::char8_t
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest replacing a macro with a using declaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

With c++11 using, it looks way way better now. Thank you.

@@ -461,6 +461,10 @@ struct LocaleMock {
# ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4273)
# ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Winconsistent-dllimport"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you post the warnings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C:\Users\User\AppData\Roaming\fmt>ninja -j 2
[4/47] Linking CXX shared library bin\fmt.dll
   Creating library fmt.lib and object fmt.exp
[35/47] Building CXX object test\CMakeFiles\posix-mock-test.dir\posix-mock-test.cc.obj
C:\Users\User\AppData\Roaming\fmt-master\test\posix-mock-test.cc(469,11): warning: '_create_locale' redeclared without 'dllimport' attribute: 'dllexport' attribute added [-Winconsistent-dllimport]
_locale_t _create_locale(int category, const char* locale) {
          ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt\locale.h(105,32): note: previous declaration is here
    _ACRTIMP _locale_t __cdecl _create_locale(
                               ^
C:\Users\User\AppData\Roaming\fmt-master\test\posix-mock-test.cc(473,6): warning: '_free_locale' redeclared without 'dllimport' attribute: 'dllexport' attribute added [-Winconsistent-dllimport]
void _free_locale(_locale_t locale) {
     ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt\locale.h(110,27): note: previous declaration is here
    _ACRTIMP void __cdecl _free_locale(
                          ^
C:\Users\User\AppData\Roaming\fmt-master\test\posix-mock-test.cc(477,8): warning: '_strtod_l' redeclared without 'dllimport' attribute: 'dllexport' attribute added [-Winconsistent-dllimport]
double _strtod_l(const char* nptr, char** endptr, _locale_t locale) {
       ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt\stdlib.h(504,25): note: previous declaration is here
_ACRTIMP double __cdecl _strtod_l(
                        ^
3 warnings generated.

MSVC by default doesn't enable /EHsc also. Just keep it most simple.
@@ -2533,9 +2540,9 @@ TEST(FormatTest, EmphasisNonHeaderOnly) {
TEST(FormatTest, CharTraitsIsNotAmbiguous) {
// Test that we don't inject internal names into the std namespace.
using namespace std;
char_traits<char>::char_type c;
FMT_MAYBE_UNUSED char_traits<char>::char_type c;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest just doing

(void)c;

and removing the FMT_MAYBE_UNUSED macro because it is not very portable (requires C++17).

#if __cplusplus >= 201103L
std::string s;
begin(s);
FMT_MAYBE_UNUSED auto lval = begin(s);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@@ -683,7 +683,7 @@ template <int GRISU_VERSION> struct grisu_shortest_handler {
};

template <typename Double, FMT_ENABLE_IF_T(sizeof(Double) == sizeof(uint64_t))>
FMT_FUNC bool grisu_format(Double value, buffer<char>& buf, int precision,
FMT_FUNC FMT_API bool grisu_format(Double value, buffer<char>& buf, int precision,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FMT_API shouldn't be here because it's an internal symbol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In DLL mode, grisu_format is refered by custom-formatter-test.cc.

C:\Users\User\AppData\Roaming\fmt>ninja -j 2
[3/47] Linking CXX shared library bin\fmt.dll
   Creating library fmt.lib and object fmt.exp
[12/47] Linking CXX executable bin\custom-formatter-test.exe
FAILED: bin/custom-formatter-test.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=test\CMakeFiles\custom-formatter-test.dir --rc=C:\PROGRA~2\WINDOW~4\10\bin\100177~1.0\x64\rc.exe --mt=C:\PROGRA~2\WINDOW~4\10\bin\100177~1.0\x64\mt.exe --manifests  -- C:\PROGRA~2\MIB055~1\2019\BUILDT~1\VC\Tools\MSVC\1420~1.275\bin\Hostx64\x64\link.exe /nologo test\CMakeFiles\custom-formatter-test.dir\custom-formatter-test.cc.obj  /out:bin\custom-formatter-test.exe /implib:test\custom-formatter-test.lib /pdb:bin\custom-formatter-test.pdb /version:0.0  /machine:x64 /INCREMENTAL:NO  /subsystem:console  test\test-main.lib test\gmock.lib fmt.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK: command "C:\PROGRA~2\MIB055~1\2019\BUILDT~1\VC\Tools\MSVC\1420~1.275\bin\Hostx64\x64\link.exe /nologo test\CMakeFiles\custom-formatter-test.dir\custom-formatter-test.cc.obj /out:bin\custom-formatter-test.exe /implib:test\custom-formatter-test.lib /pdb:bin\custom-formatter-test.pdb /version:0.0 /machine:x64 /INCREMENTAL:NO /subsystem:console test\test-main.lib test\gmock.lib fmt.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:bin\custom-formatter-test.exe.manifest" failed (exit code 1120) with the following output:
custom-formatter-test.cc.obj : error LNK2019: unresolved external symbol "bool __cdecl fmt::v5::internal::grisu_format<double,0>(double,class fmt::v5::internal::buffer<char> &,int,unsigned int,int &)" (??$grisu_format@N$0A@@internal@v5@fmt@@YA_NNAEAV?$buffer@D@012@HIAEAH@Z) referenced in function "private: void __cdecl fmt::v5::basic_writer<class fmt::v5::back_insert_range<class fmt::v5::internal::buffer<char> > >::write_double<double>(double,struct fmt::v5::basic_format_specs<char> const &)" (??$write_double@N@?$basic_writer@V?$back_insert_range@V?$buffer@D@internal@v5@fmt@@@v5@fmt@@@v5@fmt@@AEAAXNAEBU?$basic_format_specs@D@12@@Z)
bin\custom-formatter-test.exe : fatal error LNK1120: 1 unresolved externals
[13/47] Building CXX object test\CMakeFiles\format-test.dir\format-test.cc.obj
ninja: build stopped: subcommand failed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you are right, it should be exported. However FMT_API is already on the declaration, so it shouldn't be needed here:

FMT_API bool grisu_format(Double, buffer<char>&, int, unsigned, int&);
. I wonder why MSVC is complaining.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, if it fixes the problem, it's fine to keep FMT_API here but please remove FMT_FUNC then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also removed FMT_API from the corresponding declaration in format.h

template <typename Double, FMT_ENABLE_IF(sizeof(Double) == sizeof(uint64_t))>
FMT_API bool grisu_format(Double, buffer<char>&, int, unsigned, int&);

Now it becomes

template <typename Double, FMT_ENABLE_IF(sizeof(Double) == sizeof(uint64_t))>
bool grisu_format(Double, buffer<char>&, int, unsigned, int&);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't refresh this page. Now I'm looking at it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In format.h, it's been rolled back to

template <typename Double, FMT_ENABLE_IF(sizeof(Double) == sizeof(uint64_t))>
FMT_API bool grisu_format(Double, buffer<char>&, int, unsigned, int&);

In format-inl.h, remove FMT_FUNC and add FMT_API

template <typename Double, typename std::enable_if<
                               sizeof(Double) == sizeof(uint64_t), int>::type>
FMT_API bool grisu_format(Double value, buffer<char>& buf, int precision,
                           unsigned options, int& exp) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In DLL mode, format-test.cc refers to

internal::count_digits<4,struct fmt::v5::internal::uintptr::uintptr_t>(struct fmt::v5::internal::uintptr::uintptr_t)

The linker can not find symbol :

C:\Users\User\AppData\Roaming\fmt>ninja -j 2
[3/47] Linking CXX shared library bin\fmt.dll
   Creating library fmt.lib and object fmt.exp
[15/47] Linking CXX executable bin\format-test.exe
FAILED: bin/format-test.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=test\CMakeFiles\format-test.dir --rc=C:\PROGRA~2\WINDOW~4\10\bin\100177~1.0\x64\rc.exe --mt=C:\PROGRA~2\WINDOW~4\10\bin\100177~1.0\x64\mt.exe --manifests  -- C:\PROGRA~2\MIB055~1\2019\BUILDT~1\VC\Tools\MSVC\1420~1.275\bin\Hostx64\x64\link.exe /nologo test\CMakeFiles\format-test.dir\format-test.cc.obj  /out:bin\format-test.exe /implib:test\format-test.lib /pdb:bin\format-test.pdb /version:0.0  /machine:x64 /INCREMENTAL:NO  /subsystem:console  test\test-main.lib test\gmock.lib fmt.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK: command "C:\PROGRA~2\MIB055~1\2019\BUILDT~1\VC\Tools\MSVC\1420~1.275\bin\Hostx64\x64\link.exe /nologo test\CMakeFiles\format-test.dir\format-test.cc.obj /out:bin\format-test.exe /implib:test\format-test.lib /pdb:bin\format-test.pdb /version:0.0 /machine:x64 /INCREMENTAL:NO /subsystem:console test\test-main.lib test\gmock.lib fmt.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:bin\format-test.exe.manifest" failed (exit code 1120) with the following output:
format-test.cc.obj : error LNK2019: unresolved external symbol "int __cdecl fmt::v5::internal::count_digits<4,struct fmt::v5::internal::uintptr::uintptr_t>(struct fmt::v5::internal::uintptr::uintptr_t)" (??$count_digits@$03Uuintptr_t@uintptr@internal@v5@fmt@@@internal@v5@fmt@@YAHUuintptr_t@uintptr@012@@Z) referenced in function "public: void __cdecl fmt::v5::basic_writer<class fmt::v5::back_insert_range<class fmt::v5::internal::buffer<char> > >::write_pointer<struct fmt::v5::internal::uintptr::uintptr_t>(struct fmt::v5::internal::uintptr::uintptr_t,struct fmt::v5::align_spec const *)" (??$write_pointer@Uuintptr_t@uintptr@internal@v5@fmt@@@?$basic_writer@V?$back_insert_range@V?$buffer@D@internal@v5@fmt@@@v5@fmt@@@v5@fmt@@QEAAXUuintptr_t@uintptr@internal@12@PEBUalign_spec@12@@Z)
bin\format-test.exe : fatal error LNK1120: 1 unresolved externals
[16/47] Building CXX object test\CMakeFiles\assert-test.dir\assert-test.cc.obj
ninja: build stopped: subcommand failed.

In format-test.cc , if I comment out

TEST(WriterTest, WriteUIntPtr) {
  memory_buffer buf;
  fmt::writer writer(buf);
  writer.write_pointer(fmt::internal::bit_cast<fmt::internal::uintptr_t>(
                           reinterpret_cast<void*>(0xface)),
                       FMT_NULL);
  EXPECT_EQ("0xface", to_string(buf));
}

All test are passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's already been fixed. I didn't see. Thank you.

@vitaut vitaut merged commit f4dfd6e into fmtlib:master May 11, 2019
@vitaut
Copy link
Contributor

vitaut commented May 11, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants