[libcxx-commits] [libcxx] r371864 - Only initialize the streams cout/wcout/cerr/wcerr etc once, rather than any time Init::Init is called. Fixes PR#43300
Marshall Clow via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Sep 13 08:28:06 PDT 2019
Author: marshall
Date: Fri Sep 13 08:28:06 2019
New Revision: 371864
URL: http://llvm.org/viewvc/llvm-project?rev=371864&view=rev
Log:
Only initialize the streams cout/wcout/cerr/wcerr etc once, rather than any time Init::Init is called. Fixes PR#43300
Added:
libcxx/trunk/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.multiple.pass.cpp
Modified:
libcxx/trunk/src/iostream.cpp
Modified: libcxx/trunk/src/iostream.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/iostream.cpp?rev=371864&r1=371863&r2=371864&view=diff
==============================================================================
--- libcxx/trunk/src/iostream.cpp (original)
+++ libcxx/trunk/src/iostream.cpp Fri Sep 13 08:28:06 2019
@@ -97,7 +97,13 @@ static void force_locale_initialization(
#endif
}
-ios_base::Init::Init()
+class DoIOSInit {
+public:
+ DoIOSInit();
+ ~DoIOSInit();
+};
+
+DoIOSInit::DoIOSInit()
{
force_locale_initialization();
@@ -126,7 +132,7 @@ ios_base::Init::Init()
#endif
}
-ios_base::Init::~Init()
+DoIOSInit::~DoIOSInit()
{
#ifndef _LIBCPP_HAS_NO_STDOUT
ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
@@ -141,4 +147,13 @@ ios_base::Init::~Init()
wclog_ptr->flush();
}
+ios_base::Init::Init()
+{
+ static DoIOSInit init_the_streams; // gets initialized once
+}
+
+ios_base::Init::~Init()
+{
+}
+
_LIBCPP_END_NAMESPACE_STD
Added: libcxx/trunk/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.multiple.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.multiple.pass.cpp?rev=371864&view=auto
==============================================================================
--- libcxx/trunk/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.multiple.pass.cpp (added)
+++ libcxx/trunk/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.multiple.pass.cpp Fri Sep 13 08:28:06 2019
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <iostream>
+#include <cassert>
+
+#include "test_macros.h"
+
+// Test to make sure that the streams only get initialized once
+// Taken from https://bugs.llvm.org/show_bug.cgi?id=43300
+
+int main(int, char**)
+{
+
+ std::cout << "Hello!";
+ std::ios_base::fmtflags stock_flags = std::cout.flags();
+
+ std::cout << std::boolalpha << true;
+ std::ios_base::fmtflags ba_flags = std::cout.flags();
+ assert(stock_flags != ba_flags);
+
+ std::ios_base::Init init_streams;
+ std::ios_base::fmtflags after_init = std::cout.flags();
+ assert(after_init == ba_flags);
+
+ return 0;
+}
More information about the libcxx-commits
mailing list