[clang] [clang] Apply internal buffering to clang diagnostics printing (PR #113440)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 6 07:12:22 PST 2025


Fznamznon wrote:

@tahonermann thank you for your investigation, and sorry for the delay.

> Is it known which platforms are affected by this issue?

The issue is reported on Linux with a makefile as a reproducer. I'm not aware if that is the case on Windows.

> I think it is worth cleaning this up to see if that 1) fixes the reported problem, and 2) causes any regressions (which would then prompt improving the comments to better explain the intent).

Well, simply commenting out the code preventing buffering for console
```
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -854,8 +854,8 @@ size_t raw_fd_ostream::preferred_buffer_size() const {
   // If this is a terminal, don't use buffering. Line buffering
   // would be a more traditional thing to do, but it's not worth
   // the complexity.
-  if (S_ISCHR(statbuf.st_mode) && is_displayed())
-    return 0;
+  // if (S_ISCHR(statbuf.st_mode) && is_displayed())
+  //   return 0;
   // Return the preferred block size.
   return statbuf.st_blksize;
 #endif
```
and setting incoming OS in TextDiagnostic to be buffered 
```
 TextDiagnostic::TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
                                DiagnosticOptions *DiagOpts,
                                const Preprocessor *PP)
-    : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS), PP(PP) {}
+    : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS), PP(PP) {
+    OS.SetBuffered();}

-TextDiagnostic::~TextDiagnostic() {}
+TextDiagnostic::~TextDiagnostic() {OS.SetUnbuffered();
+}

```
does help to make the output more stable, however **I think** due to llvm::errs being easily accessible by any parts of LLVM, I'm currently having troubles with sporadic memory corruption in clang tooling tests. I suppose this happens due to setting llvm::errs to buffered/unbuffered in several threads. So, either raw_ostream needs to become thread-safe somehow and/or setting llvm::errs to buffer does seem really unsafe. I feel I'm stuck. WDYT?

https://github.com/llvm/llvm-project/pull/113440


More information about the cfe-commits mailing list