1111
1212#include " mlir-c/Diagnostics.h"
1313#include " mlir-c/IR.h"
14+ #include " llvm/Support/raw_ostream.h"
1415
1516#include < cassert>
1617#include < cstdint>
@@ -25,32 +26,33 @@ namespace python {
2526class CollectDiagnosticsToStringScope {
2627public:
2728 explicit CollectDiagnosticsToStringScope (MlirContext ctx) : context(ctx) {
28- handlerID = mlirContextAttachDiagnosticHandler (ctx, &handler, &errorMessage,
29- /* deleteUserData=*/ nullptr );
29+ handlerID =
30+ mlirContextAttachDiagnosticHandler (ctx, &handler, &messageStream,
31+ /* deleteUserData=*/ nullptr );
3032 }
3133 ~CollectDiagnosticsToStringScope () {
3234 mlirContextDetachDiagnosticHandler (context, handlerID);
3335 }
3436
3537 [[nodiscard]] std::string takeMessage () {
36- std::ostringstream stream ;
37- std::swap (stream, errorMessage );
38- return stream. str () ;
38+ std::string newMessage ;
39+ std::swap (message, newMessage );
40+ return newMessage ;
3941 }
4042
4143private:
4244 static MlirLogicalResult handler (MlirDiagnostic diag, void *data) {
4345 auto printer = +[](MlirStringRef message, void *data) {
44- *static_cast <std::ostringstream *>(data)
46+ *static_cast <llvm::raw_string_ostream *>(data)
4547 << std::string_view (message.data , message.length );
4648 };
4749 MlirLocation loc = mlirDiagnosticGetLocation (diag);
48- *static_cast <std::ostringstream *>(data) << " at " ;
50+ *static_cast <llvm::raw_string_ostream *>(data) << " at " ;
4951 mlirLocationPrint (loc, printer, data);
50- *static_cast <std::ostringstream *>(data) << " : " ;
52+ *static_cast <llvm::raw_string_ostream *>(data) << " : " ;
5153 mlirDiagnosticPrint (diag, printer, data);
5254 for (intptr_t i = 0 ; i < mlirDiagnosticGetNumNotes (diag); i++) {
53- *static_cast <std::ostringstream *>(data) << " \n " ;
55+ *static_cast <llvm::raw_string_ostream *>(data) << " \n " ;
5456 MlirDiagnostic note = mlirDiagnosticGetNote (diag, i);
5557 handler (note, data);
5658 }
@@ -59,7 +61,9 @@ class CollectDiagnosticsToStringScope {
5961
6062 MlirContext context;
6163 MlirDiagnosticHandlerID handlerID;
62- std::ostringstream errorMessage;
64+
65+ std::string message;
66+ llvm::raw_string_ostream messageStream{message};
6367};
6468
6569} // namespace python
0 commit comments