From 29f7fcc515c14eb42429cec53ae1c888403ddc7e Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 3 Nov 2021 16:56:08 +0100
Subject: [PATCH] COMP: workaround clang-13 optimization issue (vtk handling)

---
 src/fileFormats/vtk/file/foamVtkFileWriter.C   | 18 +++++++++++++++---
 src/fileFormats/vtk/file/foamVtkFileWriter.H   |  3 +++
 .../vtk/mesh/foamVtkInternalMeshWriter.C       |  5 +++++
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/src/fileFormats/vtk/file/foamVtkFileWriter.C b/src/fileFormats/vtk/file/foamVtkFileWriter.C
index 3cc8b7e72a5..48afd2f3146 100644
--- a/src/fileFormats/vtk/file/foamVtkFileWriter.C
+++ b/src/fileFormats/vtk/file/foamVtkFileWriter.C
@@ -49,6 +49,18 @@ Foam::vtk::fileWriter::stateNames
 
 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
 
+void Foam::vtk::fileWriter::checkFormatterValidity() const
+{
+    // In parallel can be unallocated on non-master nodes
+    if ((parallel_ ? Pstream::master() : true) && !format_)
+    {
+        FatalErrorInFunction
+            << "unallocated formatter" << endl
+            << exit(FatalError);
+    }
+}
+
+
 Foam::Ostream& Foam::vtk::fileWriter::reportBadState
 (
     Ostream& os,
@@ -285,7 +297,7 @@ Foam::vtk::fileWriter::fileWriter
     nCellData_(0),
     nPointData_(0),
     outputFile_(),
-    format_(),
+    format_(nullptr),
     os_()
 {
     // We do not currently support append mode at all
@@ -313,7 +325,7 @@ bool Foam::vtk::fileWriter::open(const fileName& file, bool parallel)
 
     if (format_)
     {
-        format_.clear();
+        format_.reset(nullptr);
         os_.close();
     }
     nCellData_ = nPointData_ = 0;
@@ -368,7 +380,7 @@ void Foam::vtk::fileWriter::close()
 
     if (format_)
     {
-        format_.clear();
+        format_.reset(nullptr);
         os_.close();
     }
 
diff --git a/src/fileFormats/vtk/file/foamVtkFileWriter.H b/src/fileFormats/vtk/file/foamVtkFileWriter.H
index ea25b303232..cb9b6a371d0 100644
--- a/src/fileFormats/vtk/file/foamVtkFileWriter.H
+++ b/src/fileFormats/vtk/file/foamVtkFileWriter.H
@@ -116,6 +116,9 @@ protected:
 
     // Protected Member Functions
 
+        //- Verify that formatter in either allocated or not required
+        void checkFormatterValidity() const;
+
         //- Generate message reporting bad writer state
         Ostream& reportBadState(Ostream&, outputState expected) const;
 
diff --git a/src/meshTools/output/vtk/mesh/foamVtkInternalMeshWriter.C b/src/meshTools/output/vtk/mesh/foamVtkInternalMeshWriter.C
index a5ee5abd525..adae39e5609 100644
--- a/src/meshTools/output/vtk/mesh/foamVtkInternalMeshWriter.C
+++ b/src/meshTools/output/vtk/mesh/foamVtkInternalMeshWriter.C
@@ -315,6 +315,11 @@ void Foam::vtk::internalMeshWriter::writeCellsConnectivity
         }
         else
         {
+// FIXME: clang-13 optimization jumps into incorrect branch
+            #ifdef __clang__
+            checkFormatterValidity();
+            #endif
+
             vtk::writeList(format(), cellTypes);
         }
 
-- 
GitLab