99#ifndef MLIR_BINDINGS_PYTHON_DIAGNOSTICS_H
1010#define MLIR_BINDINGS_PYTHON_DIAGNOSTICS_H
1111
12+ #include " mlir-c/Diagnostics.h"
13+ #include " mlir-c/IR.h"
14+
1215#include < cassert>
1316#include < cstdint>
17+ #include < sstream>
1418#include < string>
1519
16- #include " mlir-c/Diagnostics.h"
17- #include " mlir-c/IR.h"
18- #include " llvm/ADT/StringRef.h"
19-
2020namespace mlir {
2121namespace python {
2222
@@ -29,25 +29,28 @@ class CollectDiagnosticsToStringScope {
2929 /* deleteUserData=*/ nullptr );
3030 }
3131 ~CollectDiagnosticsToStringScope () {
32- assert (errorMessage.empty () && " unchecked error message" );
3332 mlirContextDetachDiagnosticHandler (context, handlerID);
3433 }
3534
36- [[nodiscard]] std::string takeMessage () { return std::move (errorMessage); }
35+ [[nodiscard]] std::string takeMessage () {
36+ std::ostringstream stream;
37+ std::swap (stream, errorMessage);
38+ return stream.str ();
39+ }
3740
3841private:
3942 static MlirLogicalResult handler (MlirDiagnostic diag, void *data) {
4043 auto printer = +[](MlirStringRef message, void *data) {
41- *static_cast <std::string *>(data) +=
42- llvm::StringRef (message.data , message.length );
44+ *static_cast <std::ostringstream *>(data)
45+ << std::string_view (message.data , message.length );
4346 };
4447 MlirLocation loc = mlirDiagnosticGetLocation (diag);
45- *static_cast <std::string *>(data) += " at " ;
48+ *static_cast <std::ostringstream *>(data) << " at " ;
4649 mlirLocationPrint (loc, printer, data);
47- *static_cast <std::string *>(data) += " : " ;
50+ *static_cast <std::ostringstream *>(data) << " : " ;
4851 mlirDiagnosticPrint (diag, printer, data);
4952 for (intptr_t i = 0 ; i < mlirDiagnosticGetNumNotes (diag); i++) {
50- *static_cast <std::string *>(data) += " \n " ;
53+ *static_cast <std::ostringstream *>(data) << " \n " ;
5154 MlirDiagnostic note = mlirDiagnosticGetNote (diag, i);
5255 handler (note, data);
5356 }
@@ -56,7 +59,7 @@ class CollectDiagnosticsToStringScope {
5659
5760 MlirContext context;
5861 MlirDiagnosticHandlerID handlerID;
59- std::string errorMessage = " " ;
62+ std::ostringstream errorMessage;
6063};
6164
6265} // namespace python
0 commit comments