/// The handler type for MLIR diagnostics. This function takes a diagnostic as
/// input, and returns success if the handler has fully processed this
/// diagnostic. Returns failure otherwise.
- using HandlerTy = std::function<LogicalResult(Diagnostic &)>;
+ using HandlerTy = llvm::unique_function<LogicalResult(Diagnostic &)>;
/// A handle to a specific registered handler object.
using HandlerID = uint64_t;
/// handlers will process diagnostics first. This function returns a unique
/// identifier for the registered handler, which can be used to unregister
/// this handler at a later time.
- HandlerID registerHandler(const HandlerTy &handler);
+ HandlerID registerHandler(HandlerTy handler);
/// Set the diagnostic handler with a function that returns void. This is a
/// convenient wrapper for handlers that always completely process the given
assert(handler && "unexpected null diagnostic handler");
if (deleteUserData == nullptr)
deleteUserData = deleteUserDataNoop;
- std::shared_ptr<void> sharedUserData(userData, deleteUserData);
DiagnosticEngine::HandlerID id =
unwrap(context)->getDiagEngine().registerHandler(
- [handler, sharedUserData](Diagnostic &diagnostic) {
- return unwrap(handler(wrap(diagnostic), sharedUserData.get()));
+ [handler,
+ ownedUserData = std::unique_ptr<void, decltype(deleteUserData)>(
+ userData, deleteUserData)](Diagnostic &diagnostic) {
+ return unwrap(handler(wrap(diagnostic), ownedUserData.get()));
});
return static_cast<MlirDiagnosticHandlerID>(id);
}
/// Register a new handler for diagnostics to the engine. This function returns
/// a unique identifier for the registered handler, which can be used to
/// unregister this handler at a later time.
-auto DiagnosticEngine::registerHandler(const HandlerTy &handler) -> HandlerID {
+auto DiagnosticEngine::registerHandler(HandlerTy handler) -> HandlerID {
llvm::sys::SmartScopedLock<true> lock(impl->mutex);
auto uniqueID = impl->uniqueHandlerId++;
- impl->handlers.insert({uniqueID, handler});
+ impl->handlers.insert({uniqueID, std::move(handler)});
return uniqueID;
}