xref: /llvm-project/libcxx/test/support/set_windows_crt_report_mode.h (revision cd1b8be8de91bc1c43bac3eea7ebf3b5643b031c)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
11 #define SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
12 
13 // A few tests are built in C mode or in C++03 mode. The initialization
14 // of init_crt_anchor is a C++ feature, and <crtdbg.h> ends up including
15 // MSVC header code which breaks in C++03 mode. Therefore, only expand
16 // the body of this header when included in C++ >= 11 mode. As this file
17 // is included in every single translation unit, we're intentionally not
18 // including test_macros.h (for TEST_STD_VER) but try to keep it to the
19 // bare minimum.
20 #if defined(__cplusplus) && __cplusplus > 199711L
21 #ifndef _DEBUG
22 #error _DEBUG must be defined when using this header
23 #endif
24 
25 #ifndef _WIN32
26 #error This header can only be used when targeting Windows
27 #endif
28 
29 #include <crtdbg.h>
30 
31 // On Windows in debug builds the default assertion handler opens a new dialog
32 // window which must be dismissed manually by the user. This function overrides
33 // that setting and instead changes the assertion handler to log to stderr
34 // instead.
init_crt_report_mode()35 inline int init_crt_report_mode() {
36   _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
37   _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
38   _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
39   _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
40   _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
41   _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
42   return 0;
43 }
44 
45 static int init_crt_anchor = init_crt_report_mode();
46 #endif // defined(__cplusplus) && __cplusplus > 199711L
47 
48 #endif // SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
49