13#ifndef MLIR_IR_DIAGNOSTICS_H
14#define MLIR_IR_DIAGNOSTICS_H
66 T val, std::enable_if_t<std::is_signed<T>::value &&
67 std::numeric_limits<T>::is_integer &&
68 sizeof(T) <=
sizeof(
int64_t)> * =
nullptr)
73 T val, std::enable_if_t<std::is_unsigned<T>::value &&
74 std::numeric_limits<T>::is_integer &&
75 sizeof(T) <=
sizeof(uint64_t)> * =
nullptr)
164 : loc(loc), severity(severity) {}
179 template <
typename Arg>
180 std::enable_if_t<!std::is_convertible<Arg, StringRef>::value &&
181 std::is_constructible<DiagnosticArgument, Arg>::value,
215 template <
typename EnumT>
217 std::is_enum_v<EnumT> &&
218 std::is_convertible_v<decltype(stringifyEnum(std::declval<EnumT>())),
222 return *
this << stringifyEnum(val);
226 template <
typename T,
typename ValueT = llvm::detail::ValueOfRange<T>>
227 std::enable_if_t<!std::is_constructible<DiagnosticArgument, T>::value,
235 template <
typename T>
238 c, [
this](
const auto &a) { *
this << a; }, [&]() { *
this << delim; });
243 template <
typename Arg1,
typename Arg2,
typename... Args>
245 append(std::forward<Arg1>(arg1));
246 return append(std::forward<Arg2>(arg2), std::forward<Args>(args)...);
249 template <
typename Arg>
251 *this << std::forward<Arg>(arg);
259 std::optional<int64_t> messagePartIndex = std::nullopt)
const;
263 std::string
str()
const;
276 llvm::pointee_iterator<NoteVector::const_iterator>;
280 return llvm::make_pointee_range(notes);
283 return llvm::make_pointee_range(notes);
287 operator LogicalResult()
const;
290 operator ParseResult()
const {
return ParseResult(LogicalResult(*
this)); }
294 template <
typename T>
295 operator FailureOr<T>()
const {
356 : owner(rhs.owner),
impl(std::move(rhs.
impl)) {
367 template <
typename Arg>
369 return append(std::forward<Arg>(arg));
371 template <
typename Arg>
373 return std::move(
append(std::forward<Arg>(arg)));
384 return std::move(manip(*
this));
388 template <
typename... Args>
390 assert(isActive() &&
"diagnostic not active");
392 impl->append(std::forward<Args>(args)...);
395 template <
typename... Args>
397 return std::move(
append(std::forward<Args>(args)...));
402 assert(isActive() &&
"diagnostic not active");
403 return impl->attachNote(noteLoc);
418 operator LogicalResult()
const;
422 operator ParseResult()
const {
return ParseResult(LogicalResult(*
this)); }
427 template <
typename T>
428 operator FailureOr<T>()
const {
436 : owner(owner),
impl(std::move(rhs)) {}
440 bool isActive()
const {
return impl.has_value(); }
443 bool isInFlight()
const {
return owner; }
446 friend DiagnosticEngine;
450 DiagnosticEngine *owner =
nullptr;
453 std::optional<Diagnostic>
impl;
457 diag.impl->startNewMessagePart();
469class DiagnosticEngine {
501 template <typename FuncTy, typename RetT = decltype(std::declval<FuncTy>()(
502 std::declval<Diagnostic &>()))>
503 std::enable_if_t<std::is_same<RetT, void>::value,
HandlerID>
517 "notes should not be emitted directly");
531 std::unique_ptr<detail::DiagnosticEngineImpl>
impl;
551template <
typename... Args>
557template <
typename... Args>
563template <
typename... Args>
580 template <
typename FuncTy>
582 : handlerID(0), ctx(ctx) {
589 template <
typename FuncTy>
591 auto &diagEngine = ctx->getDiagEngine();
593 diagEngine.eraseHandler(handlerID);
594 handlerID = diagEngine.registerHandler(std::forward<FuncTy>(handler));
610struct SourceMgrDiagnosticHandlerImpl;
634 bool displaySourceLine =
true);
663 std::optional<Location> findLocToShow(
Location loc);
666 unsigned callStackLimit = 10;
668 std::unique_ptr<detail::SourceMgrDiagnosticHandlerImpl>
impl;
676struct SourceMgrDiagnosticVerifierHandlerImpl;
699 [[nodiscard]] std::unique_ptr<ScopedDiagnosticHandler>
709 std::unique_ptr<detail::SourceMgrDiagnosticVerifierHandlerImpl>
impl;
717struct ParallelDiagnosticHandlerImpl;
752 std::unique_ptr<detail::ParallelDiagnosticHandlerImpl>
impl;
Attributes are known-constant values of operations.
A variant type that holds a single argument for a diagnostic.
DiagnosticArgumentKind
Enum that represents the different kinds of diagnostic arguments supported.
StringRef getAsString() const
Returns this argument as a string.
double getAsDouble() const
Returns this argument as a double.
DiagnosticArgument(Attribute attr)
Note: The constructors below are only exposed due to problems accessing constructors from type traits...
Type getAsType() const
Returns this argument as a Type.
int64_t getAsInteger() const
Returns this argument as a signed integer.
DiagnosticArgument(double val)
DiagnosticArgumentKind getKind() const
Returns the kind of this argument.
Attribute getAsAttribute() const
Returns this argument as an Attribute.
DiagnosticArgument(T val, std::enable_if_t< std::is_unsigned< T >::value &&std::numeric_limits< T >::is_integer &&sizeof(T)<=sizeof(uint64_t)> *=nullptr)
void print(raw_ostream &os) const
Outputs this argument to a stream.
DiagnosticArgument(float val)
uint64_t getAsUnsigned() const
Returns this argument as an unsigned integer.
DiagnosticArgument(StringRef val)
DiagnosticArgument(T val, std::enable_if_t< std::is_signed< T >::value &&std::numeric_limits< T >::is_integer &&sizeof(T)<=sizeof(int64_t)> *=nullptr)
This class is the main interface for diagnostics.
uint64_t HandlerID
A handle to a specific registered handler object.
std::enable_if_t< std::is_same< RetT, void >::value, HandlerID > registerHandler(FuncTy &&handler)
Set the diagnostic handler with a function that returns void.
friend class MLIRContextImpl
InFlightDiagnostic emit(Location loc, DiagnosticSeverity severity)
Create a new inflight diagnostic with the given location and severity.
void eraseHandler(HandlerID id)
Erase the registered diagnostic handler with the given identifier.
llvm::unique_function< LogicalResult(Diagnostic &)> HandlerTy
The handler type for MLIR diagnostics.
HandlerID registerHandler(HandlerTy handler)
Register a new handler for diagnostics to the engine.
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
DiagnosticSeverity getSeverity() const
Returns the severity of this diagnostic.
std::string str() const
Converts the diagnostic to a string, concatenating all the parts if there are multiples.
Diagnostic & operator<<(const char(&val)[n])
Stream in a string literal.
void startNewMessagePart()
Starts a new message part.
Diagnostic & append(Arg &&arg)
Append one argument to the diagnostic.
Diagnostic & operator<<(Operation *op)
Location getLocation() const
Returns the source location for this diagnostic.
SmallVector< std::string > strs() const
Converts each message part to a separate string.
Diagnostic & attachNote(std::optional< Location > noteLoc=std::nullopt)
Attaches a note to this diagnostic.
MutableArrayRef< DiagnosticArgument > getArguments()
Returns the current list of diagnostic arguments.
llvm::pointee_iterator< NoteVector::iterator > note_iterator
iterator_range< note_iterator > getNotes()
Returns the notes held by this diagnostic.
Diagnostic(Location loc, DiagnosticSeverity severity)
std::enable_if_t< std::is_enum_v< EnumT > &&std::is_convertible_v< decltype(stringifyEnum(std::declval< EnumT >())), StringRef >, Diagnostic & > operator<<(EnumT val)
Stream in an enum that has a stringifyEnum function.
std::enable_if_t<!std::is_convertible< Arg, StringRef >::value &&std::is_constructible< DiagnosticArgument, Arg >::value, Diagnostic & > operator<<(Arg &&val)
Stream operator for inserting new diagnostic arguments.
void print(raw_ostream &os, std::optional< int64_t > messagePartIndex=std::nullopt) const
Outputs this diagnostic to a stream.
iterator_range< const_note_iterator > getNotes() const
llvm::pointee_iterator< NoteVector::const_iterator > const_note_iterator
Diagnostic & append(Arg1 &&arg1, Arg2 &&arg2, Args &&...args)
Append arguments to the diagnostic.
SmallVectorImpl< DiagnosticArgument > & getMetadata()
Returns the current list of diagnostic metadata.
ArrayRef< DiagnosticArgument > getArguments() const
Diagnostic & appendRange(const T &c, const char *delim=", ")
Append a range to the diagnostic.
Diagnostic & operator=(Diagnostic &&)=default
Diagnostic(Diagnostic &&)=default
Diagnostic & appendOp(Operation &op, const OpPrintingFlags &flags)
Append an operation with the given printing flags.
An instance of this location represents a tuple of file, line number, and column number.
This class represents a diagnostic that is inflight and set to be reported.
InFlightDiagnostic & operator<<(Arg &&arg) &
Stream operator for new diagnostic arguments.
InFlightDiagnostic && append(Args &&...args) &&
InFlightDiagnostic & append(Args &&...args) &
Append arguments to the diagnostic.
Diagnostic * getUnderlyingDiagnostic()
Returns the underlying diagnostic or nullptr if this diagnostic isn't active.
InFlightDiagnostic && operator<<(Arg &&arg) &&
InFlightDiagnostic()=default
void report()
Reports the diagnostic to the engine.
Diagnostic & attachNote(std::optional< Location > noteLoc=std::nullopt)
Attaches a note to this diagnostic.
InFlightDiagnostic(InFlightDiagnostic &&rhs)
void abandon()
Abandons this diagnostic so that it will no longer be reported.
Location objects represent source locations information in MLIR.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
Set of flags used to control the behavior of the various IR print methods (e.g.
A wrapper class that allows for printing an operation with a set of flags, useful to act as a "stream...
Operation is the basic unit of execution within MLIR.
void eraseOrderIDForThread()
Remove the order id for the current thread.
~ParallelDiagnosticHandler()
ParallelDiagnosticHandler(MLIRContext *ctx)
void setOrderIDForThread(size_t orderID)
Set the order id for the current thread.
~ScopedDiagnosticHandler()
ScopedDiagnosticHandler(MLIRContext *ctx)
ScopedDiagnosticHandler(MLIRContext *ctx, FuncTy &&handler)
void setHandler(FuncTy &&handler)
Set the handler to manage via RAII.
void setCallStackLimit(unsigned limit)
Set the maximum depth that a call stack will be printed. Defaults to 10.
void emitDiagnostic(Location loc, Twine message, DiagnosticSeverity kind, bool displaySourceLine=true)
Emit the given diagnostic information with the held source manager.
raw_ostream & os
The output stream to use when printing diagnostics.
SourceMgrDiagnosticHandler(llvm::SourceMgr &mgr, MLIRContext *ctx, raw_ostream &os, ShouldShowLocFn &&shouldShowLocFn={})
~SourceMgrDiagnosticHandler()
ShouldShowLocFn shouldShowLocFn
A functor used when determining if a location for a diagnostic should be shown.
const llvm::MemoryBuffer * getBufferForFile(StringRef filename)
Get a memory buffer for the given file, or nullptr if no file is available.
llvm::SourceMgr & mgr
The source manager that we are wrapping.
llvm::unique_function< bool(Location)> ShouldShowLocFn
This type represents a functor used to filter out locations when printing a diagnostic.
~SourceMgrDiagnosticVerifierHandler()
SourceMgrDiagnosticVerifierHandler(llvm::SourceMgr &srcMgr, MLIRContext *ctx, raw_ostream &out, Level level=Level::All)
std::unique_ptr< ScopedDiagnosticHandler > registerInContext(MLIRContext *ctx)
Register this handler with ctx and return a scoped registration.
LogicalResult verify()
Returns the status of the handler and verifies that all expected diagnostics were emitted.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
The OpAsmOpInterface, see OpAsmInterface.td for more details.
InFlightDiagnostic & next(InFlightDiagnostic &diag)
Starts a new message part in an in-flight diagnostic.
Include the generated interface declarations.
InFlightDiagnostic emitWarning(Location loc)
Utility method to emit a warning message using this location.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
LogicalResult emitOptionalRemark(std::optional< Location > loc, Args &&...args)
LogicalResult emitOptionalError(std::optional< Location > loc, Args &&...args)
Overloads of the above emission functions that take an optionally null location.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
InFlightDiagnostic emitRemark(Location loc)
Utility method to emit a remark message using this location.
LogicalResult emitOptionalWarning(std::optional< Location > loc, Args &&...args)