MLIR  20.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 <cassert>
13 #include <string>
14 
15 #include "mlir-c/Diagnostics.h"
16 #include "mlir-c/IR.h"
17 #include "llvm/ADT/StringRef.h"
18 
19 namespace mlir {
20 namespace python {
21 
22 /// RAII scope intercepting all diagnostics into a string. The message must be
23 /// checked before this goes out of scope.
25 public:
26  explicit CollectDiagnosticsToStringScope(MlirContext ctx) : context(ctx) {
27  handlerID = mlirContextAttachDiagnosticHandler(ctx, &handler, &errorMessage,
28  /*deleteUserData=*/nullptr);
29  }
31  assert(errorMessage.empty() && "unchecked error message");
32  mlirContextDetachDiagnosticHandler(context, handlerID);
33  }
34 
35  [[nodiscard]] std::string takeMessage() { return std::move(errorMessage); }
36 
37 private:
38  static MlirLogicalResult handler(MlirDiagnostic diag, void *data) {
39  auto printer = +[](MlirStringRef message, void *data) {
40  *static_cast<std::string *>(data) +=
41  llvm::StringRef(message.data, message.length);
42  };
43  MlirLocation loc = mlirDiagnosticGetLocation(diag);
44  *static_cast<std::string *>(data) += "at ";
45  mlirLocationPrint(loc, printer, data);
46  *static_cast<std::string *>(data) += ": ";
47  mlirDiagnosticPrint(diag, printer, data);
48  return mlirLogicalResultSuccess();
49  }
50 
51  MlirContext context;
52  MlirDiagnosticHandlerID handlerID;
53  std::string errorMessage = "";
54 };
55 
56 } // namespace python
57 } // namespace mlir
58 
59 #endif // MLIR_BINDINGS_PYTHON_DIAGNOSTICS_H
static std::string diag(const llvm::Value &value)
RAII scope intercepting all diagnostics into a string.
Definition: Diagnostics.h:24
MLIR_CAPI_EXPORTED void mlirDiagnosticPrint(MlirDiagnostic diagnostic, MlirStringCallback callback, void *userData)
Prints a diagnostic using the provided callback.
Definition: Diagnostics.cpp:18
MLIR_CAPI_EXPORTED MlirDiagnosticHandlerID mlirContextAttachDiagnosticHandler(MlirContext context, MlirDiagnosticHandler handler, void *userData, void(*deleteUserData)(void *))
Attaches the diagnostic handler to the context.
Definition: Diagnostics.cpp:56
MLIR_CAPI_EXPORTED void mlirContextDetachDiagnosticHandler(MlirContext context, MlirDiagnosticHandlerID id)
Detaches an attached diagnostic handler from the context given its identifier.
Definition: Diagnostics.cpp:72
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.
Definition: Diagnostics.cpp:24
MLIR_CAPI_EXPORTED void mlirLocationPrint(MlirLocation location, MlirStringCallback callback, void *userData)
Prints a location by sending chunks of the string representation and forwarding userData tocallback`.
Definition: IR.cpp:300
static MlirLogicalResult mlirLogicalResultSuccess(void)
Creates a logical result representing a success.
Definition: Support.h:132
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:116
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:73
const char * data
Pointer to the first symbol.
Definition: Support.h:74
size_t length
Length of the fragment.
Definition: Support.h:75