diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/lib/Support/raw_ostream.cpp @@ -867,25 +867,39 @@ // outs(), errs(), nulls() //===----------------------------------------------------------------------===// +alignas(raw_fd_ostream) static char out[sizeof(raw_fd_ostream)]; +alignas(raw_fd_ostream) static char err[sizeof(raw_fd_ostream)]; +alignas(raw_null_ostream) static char null[sizeof(raw_null_ostream)]; + +namespace { +struct ostream_init { + ostream_init() { + // Set buffer settings to model stdout behavior. + std::error_code EC; + auto *out_ptr = ::new (out) raw_fd_ostream("-", EC, sys::fs::OF_None); + assert(!EC); + + // Set standard error to be unbuffered and tied to outs() by default. + auto *err_ptr = ::new (err) raw_fd_ostream(STDERR_FILENO, false, true); + err_ptr->tie(out_ptr); + + ::new (null) raw_null_ostream(); + } + ~ostream_init() { reinterpret_cast(out)->flush(); } +} initializer; +} // namespace + raw_fd_ostream &llvm::outs() { - // Set buffer settings to model stdout behavior. - std::error_code EC; - static raw_fd_ostream S("-", EC, sys::fs::OF_None); - assert(!EC); - return S; + return *reinterpret_cast(out); } raw_fd_ostream &llvm::errs() { - // Set standard error to be unbuffered and tied to outs() by default. - static raw_fd_ostream S(STDERR_FILENO, false, true); - S.tie(&outs()); - return S; + return *reinterpret_cast(err); } /// nulls() - This returns a reference to a raw_ostream which discards output. raw_ostream &llvm::nulls() { - static raw_null_ostream S; - return S; + return *reinterpret_cast(null); } //===----------------------------------------------------------------------===//