MLIR 23.0.0git
Diagnostics.h
Go to the documentation of this file.
1//===- Diagnostics.h - Helpers for diagnostics in Python bindings ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_BINDINGS_PYTHON_DIAGNOSTICS_H
10#define MLIR_BINDINGS_PYTHON_DIAGNOSTICS_H
11
12#include "mlir-c/Diagnostics.h"
13#include "mlir-c/IR.h"
14
15#include <cassert>
16#include <cstdint>
17#include <sstream>
18#include <string>
19
20namespace mlir {
21namespace python {
22
23/// RAII scope intercepting all diagnostics into a string. The message must be
24/// checked before this goes out of scope.
26public:
27 explicit CollectDiagnosticsToStringScope(MlirContext ctx) : context(ctx) {
28 handlerID =
29 mlirContextAttachDiagnosticHandler(ctx, &handler, &messageStream,
30 /*deleteUserData=*/nullptr);
31 }
33 assert(messageStream.str().empty() && "unchecked error message");
34 mlirContextDetachDiagnosticHandler(context, handlerID);
35 }
36
37 [[nodiscard]] std::string takeMessage() {
38 std::string newMessage = messageStream.str();
39 messageStream.str("");
40 messageStream.clear();
41 return newMessage;
42 }
43
44private:
45 static MlirLogicalResult handler(MlirDiagnostic diag, void *data) {
46 auto printer = +[](MlirStringRef message, void *data) {
47 *static_cast<std::ostringstream *>(data)
48 << std::string_view(message.data, message.length);
49 };
50 MlirLocation loc = mlirDiagnosticGetLocation(diag);
51 *static_cast<std::ostringstream *>(data) << "at ";
52 mlirLocationPrint(loc, printer, data);
53 *static_cast<std::ostringstream *>(data) << ": ";
54 mlirDiagnosticPrint(diag, printer, data);
55 for (intptr_t i = 0; i < mlirDiagnosticGetNumNotes(diag); i++) {
56 *static_cast<std::ostringstream *>(data) << "\n";
58 handler(note, data);
59 }
61 }
62
63 MlirContext context;
65
66 std::ostringstream messageStream;
67};
68
69} // namespace python
70} // namespace mlir
71
72#endif // MLIR_BINDINGS_PYTHON_DIAGNOSTICS_H
void mlirLocationPrint(MlirLocation location, MlirStringCallback callback, void *userData)
Definition IR.cpp:414
static std::string diag(const llvm::Value &value)
MLIR_CAPI_EXPORTED intptr_t mlirDiagnosticGetNumNotes(MlirDiagnostic diagnostic)
Returns the number of notes attached to the diagnostic.
MLIR_CAPI_EXPORTED void mlirDiagnosticPrint(MlirDiagnostic diagnostic, MlirStringCallback callback, void *userData)
Prints a diagnostic using the provided callback.
MLIR_CAPI_EXPORTED MlirDiagnostic mlirDiagnosticGetNote(MlirDiagnostic diagnostic, intptr_t pos)
Returns pos-th note attached to the diagnostic.
MLIR_CAPI_EXPORTED MlirDiagnosticHandlerID mlirContextAttachDiagnosticHandler(MlirContext context, MlirDiagnosticHandler handler, void *userData, void(*deleteUserData)(void *))
Attaches the diagnostic handler to the context.
MLIR_CAPI_EXPORTED void mlirContextDetachDiagnosticHandler(MlirContext context, MlirDiagnosticHandlerID id)
Detaches an attached diagnostic handler from the context given its identifier.
uint64_t MlirDiagnosticHandlerID
Opaque identifier of a diagnostic handler, useful to detach a handler.
Definition Diagnostics.h:41
MLIR_CAPI_EXPORTED MlirLocation mlirDiagnosticGetLocation(MlirDiagnostic diagnostic)
Returns the location at which the diagnostic is reported.
static MlirLogicalResult mlirLogicalResultSuccess(void)
Creates a logical result representing a success.
Definition Support.h:137
Include the generated interface declarations.
An opaque reference to a diagnostic, always owned by the diagnostics engine (context).
Definition Diagnostics.h:26
A logical result value, essentially a boolean with named states.
Definition Support.h:121
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78
const char * data
Pointer to the first symbol.
Definition Support.h:79
size_t length
Length of the fragment.
Definition Support.h:80