[PATCH] D81525: [Support] Ensure errs() is constructed after outs() and don't rerun tie when errs() is called

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 08:44:21 PDT 2020


MaskRay updated this revision to Diff 269864.
MaskRay retitled this revision from "[Support] Initialize outs() errs() nulls() once" to "[Support] Ensure errs() is constructed after outs() and don't rerun tie when errs() is called".
MaskRay edited the summary of this revision.
MaskRay added a comment.

Avoid global constructors

Add comments from labath


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81525/new/

https://reviews.llvm.org/D81525

Files:
  llvm/lib/Support/raw_ostream.cpp


Index: llvm/lib/Support/raw_ostream.cpp
===================================================================
--- llvm/lib/Support/raw_ostream.cpp
+++ llvm/lib/Support/raw_ostream.cpp
@@ -876,10 +876,17 @@
 }
 
 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;
+  // Ensure outs() is constructed before the unbuffered stderr stream so that
+  // outs() will be destructed after outs() and the TiedStream field of Init.S
+  // will not become dangling. The tie call is called once, thus the user can
+  // opt out the behavior.
+  static struct InitializeAndTie {
+    raw_fd_ostream S;
+    InitializeAndTie(raw_ostream &TieTo) : S(STDERR_FILENO, false, true) {
+      S.tie(&TieTo);
+    }
+  } Init(outs());
+  return Init.S;
 }
 
 /// nulls() - This returns a reference to a raw_ostream which discards output.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81525.269864.patch
Type: text/x-patch
Size: 954 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200610/5b44a758/attachment.bin>


More information about the llvm-commits mailing list