17e3ee09aSEric Fiselier // -*- C++ -*-
27e3ee09aSEric Fiselier //===----------------------------------------------------------------------===//
37e3ee09aSEric Fiselier //
457b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
557b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
657b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77e3ee09aSEric Fiselier //
87e3ee09aSEric Fiselier //===----------------------------------------------------------------------===//
9480cd780SLouis Dionne
107e3ee09aSEric Fiselier #ifndef SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
117e3ee09aSEric Fiselier #define SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
127e3ee09aSEric Fiselier
13e346fd8aSMartin Storsjö // A few tests are built in C mode or in C++03 mode. The initialization
14e346fd8aSMartin Storsjö // of init_crt_anchor is a C++ feature, and <crtdbg.h> ends up including
15e346fd8aSMartin Storsjö // MSVC header code which breaks in C++03 mode. Therefore, only expand
16e346fd8aSMartin Storsjö // the body of this header when included in C++ >= 11 mode. As this file
17e346fd8aSMartin Storsjö // is included in every single translation unit, we're intentionally not
18e346fd8aSMartin Storsjö // including test_macros.h (for TEST_STD_VER) but try to keep it to the
19e346fd8aSMartin Storsjö // bare minimum.
20e346fd8aSMartin Storsjö #if defined(__cplusplus) && __cplusplus > 199711L
217e3ee09aSEric Fiselier #ifndef _DEBUG
227e3ee09aSEric Fiselier #error _DEBUG must be defined when using this header
237e3ee09aSEric Fiselier #endif
247e3ee09aSEric Fiselier
257e3ee09aSEric Fiselier #ifndef _WIN32
267e3ee09aSEric Fiselier #error This header can only be used when targeting Windows
277e3ee09aSEric Fiselier #endif
287e3ee09aSEric Fiselier
297e3ee09aSEric Fiselier #include <crtdbg.h>
307e3ee09aSEric Fiselier
317e3ee09aSEric Fiselier // On Windows in debug builds the default assertion handler opens a new dialog
327e3ee09aSEric Fiselier // window which must be dismissed manually by the user. This function overrides
337e3ee09aSEric Fiselier // that setting and instead changes the assertion handler to log to stderr
347e3ee09aSEric Fiselier // instead.
init_crt_report_mode()357e3ee09aSEric Fiselier inline int init_crt_report_mode() {
36*cd1b8be8SAndrew Ng _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
37*cd1b8be8SAndrew Ng _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
38*cd1b8be8SAndrew Ng _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
39*cd1b8be8SAndrew Ng _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
40*cd1b8be8SAndrew Ng _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
41*cd1b8be8SAndrew Ng _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
427e3ee09aSEric Fiselier return 0;
437e3ee09aSEric Fiselier }
447e3ee09aSEric Fiselier
457e3ee09aSEric Fiselier static int init_crt_anchor = init_crt_report_mode();
46e346fd8aSMartin Storsjö #endif // defined(__cplusplus) && __cplusplus > 199711L
477e3ee09aSEric Fiselier
487e3ee09aSEric Fiselier #endif // SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
49