MLIR  20.0.0git
Diagnostics.cpp
Go to the documentation of this file.
1 //===- Diagnostics.cpp - C Interface for MLIR Diagnostics -----------------===//
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 #include "mlir-c/Diagnostics.h"
10 #include "mlir/CAPI/Diagnostics.h"
11 #include "mlir/CAPI/IR.h"
12 #include "mlir/CAPI/Support.h"
13 #include "mlir/CAPI/Utils.h"
14 #include "mlir/IR/Diagnostics.h"
15 
16 using namespace mlir;
17 
19  void *userData) {
20  detail::CallbackOstream stream(callback, userData);
21  unwrap(diagnostic).print(stream);
22 }
23 
24 MlirLocation mlirDiagnosticGetLocation(MlirDiagnostic diagnostic) {
25  return wrap(unwrap(diagnostic).getLocation());
26 }
27 
29  switch (unwrap(diagnostic).getSeverity()) {
31  return MlirDiagnosticError;
33  return MlirDiagnosticWarning;
35  return MlirDiagnosticNote;
37  return MlirDiagnosticRemark;
38  }
39  llvm_unreachable("unhandled diagnostic severity");
40 }
41 
42 // Notes are stored in a vector, so note iterator range is a pair of
43 // random access iterators, for which it is cheap to compute the size.
45  return static_cast<intptr_t>(llvm::size(unwrap(diagnostic).getNotes()));
46 }
47 
48 // Notes are stored in a vector, so the iterator is a random access iterator,
49 // cheap to advance multiple steps at a time.
51  return wrap(*std::next(unwrap(diagnostic).getNotes().begin(), pos));
52 }
53 
54 static void deleteUserDataNoop(void *userData) {}
55 
57  MlirContext context, MlirDiagnosticHandler handler, void *userData,
58  void (*deleteUserData)(void *)) {
59  assert(handler && "unexpected null diagnostic handler");
60  if (deleteUserData == nullptr)
61  deleteUserData = deleteUserDataNoop;
63  unwrap(context)->getDiagEngine().registerHandler(
64  [handler,
65  ownedUserData = std::unique_ptr<void, decltype(deleteUserData)>(
66  userData, deleteUserData)](Diagnostic &diagnostic) {
67  return unwrap(handler(wrap(diagnostic), ownedUserData.get()));
68  });
69  return static_cast<MlirDiagnosticHandlerID>(id);
70 }
71 
72 void mlirContextDetachDiagnosticHandler(MlirContext context,
74  unwrap(context)->getDiagEngine().eraseHandler(
75  static_cast<DiagnosticEngine::HandlerID>(id));
76 }
77 
78 void mlirEmitError(MlirLocation location, const char *message) {
79  emitError(unwrap(location)) << message;
80 }
MlirDiagnosticSeverity mlirDiagnosticGetSeverity(MlirDiagnostic diagnostic)
Returns the severity of the diagnostic.
Definition: Diagnostics.cpp:28
static void deleteUserDataNoop(void *userData)
Definition: Diagnostics.cpp:54
intptr_t mlirDiagnosticGetNumNotes(MlirDiagnostic diagnostic)
Returns the number of notes attached to the diagnostic.
Definition: Diagnostics.cpp:44
void mlirEmitError(MlirLocation location, const char *message)
Emits an error at the given location through the diagnostics engine.
Definition: Diagnostics.cpp:78
MlirLocation mlirDiagnosticGetLocation(MlirDiagnostic diagnostic)
Returns the location at which the diagnostic is reported.
Definition: Diagnostics.cpp:24
void mlirDiagnosticPrint(MlirDiagnostic diagnostic, MlirStringCallback callback, void *userData)
Prints a diagnostic using the provided callback.
Definition: Diagnostics.cpp:18
MlirDiagnostic mlirDiagnosticGetNote(MlirDiagnostic diagnostic, intptr_t pos)
Returns pos-th note attached to the diagnostic.
Definition: Diagnostics.cpp:50
void mlirContextDetachDiagnosticHandler(MlirContext context, MlirDiagnosticHandlerID id)
Detaches an attached diagnostic handler from the context given its identifier.
Definition: Diagnostics.cpp:72
MlirDiagnosticHandlerID mlirContextAttachDiagnosticHandler(MlirContext context, MlirDiagnosticHandler handler, void *userData, void(*deleteUserData)(void *))
Attaches the diagnostic handler to the context.
Definition: Diagnostics.cpp:56
uint64_t HandlerID
A handle to a specific registered handler object.
Definition: Diagnostics.h:434
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
Definition: Diagnostics.h:155
void print(raw_ostream &os) const
Outputs this diagnostic to a stream.
A simple raw ostream subclass that forwards write_impl calls to the user-supplied callback together w...
Definition: Utils.h:30
MlirDiagnosticSeverity
Severity of a diagnostic.
Definition: Diagnostics.h:32
@ MlirDiagnosticNote
Definition: Diagnostics.h:35
@ MlirDiagnosticRemark
Definition: Diagnostics.h:36
@ MlirDiagnosticWarning
Definition: Diagnostics.h:34
@ MlirDiagnosticError
Definition: Diagnostics.h:33
MlirLogicalResult(* MlirDiagnosticHandler)(MlirDiagnostic, void *userData)
Diagnostic handler type.
Definition: Diagnostics.h:49
uint64_t MlirDiagnosticHandlerID
Opaque identifier of a diagnostic handler, useful to detach a handler.
Definition: Diagnostics.h:41
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
void(* MlirStringCallback)(MlirStringRef, void *)
A callback for returning string references.
Definition: Support.h:105
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
An opaque reference to a diagnostic, always owned by the diagnostics engine (context).
Definition: Diagnostics.h:26