This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] [test] Fix running tests with Clang-cl in Debug mode
ClosedPublic

Authored by mstorsjo on Jul 18 2023, 12:41 AM.

Details

Summary

When building in debug mode, the debug version of the MSVC CRT
gets linked in. (This is the default in CMake in general. In the case
of libcxx, we manually link the CRT though - and in debug mode,
we pick the debug version of the CRT.) When building the tests,
we need to use the same version of the CRT as was used for building
the library.

Additionally; the debug CRT defaults to pop up a dialog box when
asserts fail, which blocks running tests. By including the
set_windows_crt_report_mode.h helper header, we change the assert
behaviour back to that of release mode - printing a message and
exiting immediately.

This was supported by the old libcxx test system, where support for
it was added in 7e3ee09ad24cbca3ea7687c50b53be5269127fb1. When porting
over to the newer test setup, this mechanism wasn't brought over (and the
old test infrastructure was removed in
a48f018bb7d8fadc67c08e71409c31713daa0071).

Thus: In debug mode, link against the debug versions of msvcrt and
msvcprt, define _DEBUG (enabling CRT debug mode code patterns),
and include the set_windows_crt_report_mode.h header.

Based on a patch by Andrew Ng.

Linking of the debug version of the CRT can also be done by using
the new -fms-runtime-lib= Clang option. However that option was
added in Clang 16, and libcxx only requires Clang 15 for now;
therefore doing the CRT linking entirely manually for now (just as
before).

Additionally, adjust set_windows_crt_report_mode.h to avoid including
the body of the file when building in C mode or in C++03 mode.
This fixes the following two tests:

libcxx/include_as_c.sh.cpp
libcxx/selftest/dsl/dsl.sh.py

The former test is built in C mode. The latter tries compiling things
as C++03. Some of the vcruntime headers that we include break in
C++03 mode when MS CRT debug mode is enabled.

Diff Detail

Event Timeline

mstorsjo created this revision.Jul 18 2023, 12:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 18 2023, 12:41 AM
Herald added a subscriber: arichardson. · View Herald Transcript
mstorsjo requested review of this revision.Jul 18 2023, 12:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 18 2023, 12:41 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Herald added a subscriber: wangpc. · View Herald Transcript

In general LGTM, but one question.

libcxx/test/support/set_windows_crt_report_mode.h
13

Why do we need to test for C+98? If needed can you use our normal TEST_STD_VER macro?

mstorsjo added inline comments.Jul 18 2023, 11:46 AM
libcxx/test/support/set_windows_crt_report_mode.h
13

One testcase in libcxx/selftest/dsl/dsl.sh.py tests compiling in C++03 mode. And for some reason, some of the MSVC headers that get included from <crtdbg.h> break if compiled in C++03 mode.

I can look into changing this to include test_macros.h and checking TEST_STD_VER >= 11 instead. But since this header is force-included in every test compilation, we should ideally keep it as minimal as possible, so including test_macros.h might not be ideal (even if most tests themselves do include it). And I presume test_macros.h fail if included in C mode, so we possibly need to check against defined(__cplusplus) anyway?

Mordante accepted this revision.Jul 18 2023, 12:10 PM

LGTM modulo some nits.

libcxx/test/support/set_windows_crt_report_mode.h
13

Thanks for the explanation. Can you add this information as comment. I'm fine with not including test_macros.h

43
This revision is now accepted and ready to land.Jul 18 2023, 12:10 PM
mstorsjo added inline comments.Jul 18 2023, 12:53 PM
libcxx/test/support/set_windows_crt_report_mode.h
13

Ok, I'll amend this with a comment.

43

Thanks, will fix.

andrewng added inline comments.Jul 19 2023, 7:43 AM
libcxx/test/support/set_windows_crt_report_mode.h
36–38

Does this work for you? I had to change this in my patch otherwise the pop-up for which version of Visual Studio to use would come up and stall testing, IIRC. Perhaps this depends on some specific Windows environment/configuration?

mstorsjo added inline comments.Jul 19 2023, 2:28 PM
libcxx/test/support/set_windows_crt_report_mode.h
36–38

Yeah, it does seem to work for me as is. If it needs to be changed, we can do that in a separate change (ideally by figuring out why/when it’s needed), but this patch is enough for me (and the CI environment) for running the tests in debug mode.

I also tested specifically tests with failing asserts, and they get printed on the console as expected, without any blocking dialog.

ldionne added a subscriber: ldionne.EditedJul 19 2023, 4:39 PM

What would this patch look like if we supported Clang 16 and Clang 17 but not Clang 15? It would be a lot simpler right? I am asking because we're branching in less than a week, so it might be worth punting this change to after that.

Edit: Nevermind, I just saw D155562.

What would this patch look like if we supported Clang 16 and Clang 17 but not Clang 15? It would be a lot simpler right? I am asking because we're branching in less than a week, so it might be worth punting this change to after that.

Edit: Nevermind, I just saw D155562.

Yeah, indeed. Using the new Clang 16 option doesn't make a big difference (it actually makes for slightly more code changes), but it is structurally more of the right thing to do in the long run. So it doesn't really make any difference for this patch whether we have that allowed yet or not.

andrewng added inline comments.Jul 20 2023, 4:22 AM
libcxx/test/support/set_windows_crt_report_mode.h
36–38

According to the docs for _CrtSetReportMode, _CRTDBG_MODE_DEBUG means "Writes the message to the debugger's output window.". I'm guessing that what happens in this mode might depend on debugger settings. The code in my patch explicitly set _CrtSetReportMode to output to STDERR.

mstorsjo added inline comments.Jul 20 2023, 5:12 AM
libcxx/test/support/set_windows_crt_report_mode.h
36–38

I see, thanks. I can split out that bit and submit as a separate patch with that description then.