tvm
diagnostic.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef TVM_IR_DIAGNOSTIC_H_
27 #define TVM_IR_DIAGNOSTIC_H_
28 
29 #include <tvm/ffi/reflection/registry.h>
30 #include <tvm/ir/module.h>
31 
32 #include <sstream>
33 #include <string>
34 
35 namespace tvm {
36 
38 enum class DiagnosticLevel : int {
39  kBug = 10,
40  kError = 20,
41  kWarning = 30,
42  kNote = 40,
43  kHelp = 50,
44 };
45 
46 class DiagnosticBuilder;
47 
49 class Diagnostic;
50 
52 class DiagnosticNode : public Object {
53  public:
65  ObjectRef loc;
67  ffi::String message;
69  ffi::String error_kind{"InternalError"};
70 
71  static void RegisterReflection() {
72  namespace refl = tvm::ffi::reflection;
73  refl::ObjectDef<DiagnosticNode>()
74  .def_ro("level", &DiagnosticNode::level)
75  .def_ro("span", &DiagnosticNode::span)
76  .def_ro("message", &DiagnosticNode::message)
77  .def_ro("error_kind", &DiagnosticNode::error_kind);
78  }
79 
80  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
82 };
83 
84 class Diagnostic : public ObjectRef {
85  public:
86  TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message);
87  TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message,
88  const std::string& error_kind);
89 
90  static DiagnosticBuilder Bug(Span span);
93  static DiagnosticBuilder Note(Span span);
94  static DiagnosticBuilder Help(Span span);
95  // variants uses object location
96  static DiagnosticBuilder Bug(ObjectRef loc);
97  static DiagnosticBuilder Error(ObjectRef loc);
98  static DiagnosticBuilder Warning(ObjectRef loc);
99  static DiagnosticBuilder Note(ObjectRef loc);
100  static DiagnosticBuilder Help(ObjectRef loc);
101  // variants uses object ptr.
102  static DiagnosticBuilder Bug(const Object* loc);
103  static DiagnosticBuilder Error(const Object* loc);
104  static DiagnosticBuilder Warning(const Object* loc);
105  static DiagnosticBuilder Note(const Object* loc);
106  static DiagnosticBuilder Help(const Object* loc);
107  // variants with error kind
108  static DiagnosticBuilder Error(std::string error_kind, Span span);
109  static DiagnosticBuilder Error(std::string error_kind, ObjectRef loc);
110  static DiagnosticBuilder Error(std::string error_kind, const Object* loc);
111 
113 };
114 
119  public:
122 
125 
128 
132  ObjectRef loc;
133 
135  std::string error_kind{"InternalError"};
136 
137  template <typename T>
138  DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*)
139  stream_ << val;
140  return *this;
141  }
142 
144 
146  : level(builder.level),
147  source_name(builder.source_name),
148  span(builder.span),
149  error_kind(builder.error_kind) {}
150 
152 
154 
156  DiagnosticBuilder& WithErrorKind(std::string kind) {
157  error_kind = std::move(kind);
158  return *this;
159  }
160 
161  operator Diagnostic() {
162  return Diagnostic(this->level, this->span, this->stream_.str(), this->error_kind);
163  }
164 
165  private:
166  std::stringstream stream_;
167  friend class Diagnostic;
168 };
169 
173 class DiagnosticContext;
174 
184 class DiagnosticRendererNode : public Object {
185  public:
186  ffi::TypedFunction<void(DiagnosticContext ctx)> renderer;
187 
188  static void RegisterReflection() {
189  namespace refl = tvm::ffi::reflection;
190  refl::ObjectDef<DiagnosticRendererNode>().def_ro("renderer", &DiagnosticRendererNode::renderer);
191  }
193 };
194 
195 class DiagnosticRenderer : public ObjectRef {
196  public:
197  TVM_DLL DiagnosticRenderer(ffi::TypedFunction<void(DiagnosticContext ctx)> render);
199  : DiagnosticRenderer(ffi::TypedFunction<void(DiagnosticContext ctx)>()) {}
200 
201  void Render(const DiagnosticContext& ctx);
202 
204  TVM_FFI_ICHECK(get() != nullptr);
205  return static_cast<DiagnosticRendererNode*>(get_mutable());
206  }
207 
210 };
211 
212 class DiagnosticContextNode : public Object {
213  public:
216 
218  ffi::Array<Diagnostic> diagnostics;
219 
222 
223  static void RegisterReflection() {
224  namespace refl = tvm::ffi::reflection;
225  refl::ObjectDef<DiagnosticContextNode>()
226  .def_ro("module", &DiagnosticContextNode::module)
227  .def_ro("diagnostics", &DiagnosticContextNode::diagnostics);
228  }
229 
230  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
232 };
233 
234 class DiagnosticContext : public ObjectRef {
235  public:
236  TVM_DLL DiagnosticContext(const IRModule& module, const DiagnosticRenderer& renderer);
237  TVM_DLL static DiagnosticContext Default(const IRModule& source_map);
238 
242  void Emit(const Diagnostic& diagnostic);
243 
251  void EmitFatal(const Diagnostic& diagnostic);
252 
254  void Render();
255 
257  TVM_FFI_ICHECK(get() != nullptr);
258  return static_cast<DiagnosticContextNode*>(get_mutable());
259  }
260 
263 };
264 
265 DiagnosticRenderer TerminalRenderer(std::ostream& ostream);
266 
267 } // namespace tvm
268 #endif // TVM_IR_DIAGNOSTIC_H_
A wrapper around std::stringstream to build a diagnostic.
Definition: diagnostic.h:118
DiagnosticBuilder(DiagnosticLevel level, ObjectRef loc)
Definition: diagnostic.h:153
friend class Diagnostic
Definition: diagnostic.h:167
DiagnosticBuilder(const DiagnosticBuilder &builder)
Definition: diagnostic.h:145
Span span
The span of the diagnostic.
Definition: diagnostic.h:127
DiagnosticBuilder(DiagnosticLevel level, Span span)
Definition: diagnostic.h:151
SourceName source_name
The source name.
Definition: diagnostic.h:124
DiagnosticBuilder & operator<<(const T &val)
Definition: diagnostic.h:138
DiagnosticLevel level
The level.
Definition: diagnostic.h:121
DiagnosticBuilder & WithErrorKind(std::string kind)
Set the error kind for this diagnostic.
Definition: diagnostic.h:156
DiagnosticBuilder()
Definition: diagnostic.h:143
std::string error_kind
The error kind (e.g. "TypeError", "ValueError").
Definition: diagnostic.h:135
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:132
Definition: diagnostic.h:212
ffi::Array< Diagnostic > diagnostics
The set of diagnostics to report.
Definition: diagnostic.h:218
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: diagnostic.h:230
static void RegisterReflection()
Definition: diagnostic.h:223
IRModule module
The Module to report against.
Definition: diagnostic.h:215
DiagnosticRenderer renderer
The renderer set for the context.
Definition: diagnostic.h:221
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("DiagnosticContext", DiagnosticContextNode, Object)
Definition: diagnostic.h:234
void Render()
Render the errors and raise a DiagnosticError exception.
void Emit(const Diagnostic &diagnostic)
Emit a diagnostic.
void EmitFatal(const Diagnostic &diagnostic)
Emit a diagnostic and then immediately attempt to render all errors.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DiagnosticContext, ObjectRef, DiagnosticContextNode)
DiagnosticContextNode * operator->()
Definition: diagnostic.h:256
DiagnosticContext(const IRModule &module, const DiagnosticRenderer &renderer)
static DiagnosticContext Default(const IRModule &source_map)
A compiler diagnostic message.
Definition: diagnostic.h:52
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: diagnostic.h:80
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:65
ffi::String error_kind
The error kind when the diagnostic is used as an error (e.g. "TypeError").
Definition: diagnostic.h:69
ffi::String message
The diagnostic message.
Definition: diagnostic.h:67
DiagnosticLevel level
The level.
Definition: diagnostic.h:55
static void RegisterReflection()
Definition: diagnostic.h:71
Span span
The span at which to report an error.
Definition: diagnostic.h:57
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("Diagnostic", DiagnosticNode, Object)
Display diagnostics in a given display format.
Definition: diagnostic.h:184
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("DiagnosticRenderer", DiagnosticRendererNode, Object)
ffi::TypedFunction< void(DiagnosticContext ctx)> renderer
Definition: diagnostic.h:186
static void RegisterReflection()
Definition: diagnostic.h:188
Definition: diagnostic.h:195
DiagnosticRenderer(ffi::TypedFunction< void(DiagnosticContext ctx)> render)
void Render(const DiagnosticContext &ctx)
DiagnosticRendererNode * operator->()
Definition: diagnostic.h:203
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DiagnosticRenderer, ObjectRef, DiagnosticRendererNode)
DiagnosticRenderer()
Definition: diagnostic.h:198
Definition: diagnostic.h:84
static DiagnosticBuilder Error(const Object *loc)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Diagnostic, ObjectRef, DiagnosticNode)
static DiagnosticBuilder Error(std::string error_kind, const Object *loc)
static DiagnosticBuilder Bug(ObjectRef loc)
Diagnostic(DiagnosticLevel level, Span span, const std::string &message)
static DiagnosticBuilder Warning(Span span)
static DiagnosticBuilder Note(ObjectRef loc)
static DiagnosticBuilder Warning(const Object *loc)
static DiagnosticBuilder Bug(Span span)
static DiagnosticBuilder Help(Span span)
static DiagnosticBuilder Note(const Object *loc)
static DiagnosticBuilder Bug(const Object *loc)
static DiagnosticBuilder Help(ObjectRef loc)
static DiagnosticBuilder Warning(ObjectRef loc)
static DiagnosticBuilder Error(Span span)
static DiagnosticBuilder Help(const Object *loc)
static DiagnosticBuilder Error(std::string error_kind, Span span)
Diagnostic(DiagnosticLevel level, Span span, const std::string &message, const std::string &error_kind)
static DiagnosticBuilder Error(std::string error_kind, ObjectRef loc)
static DiagnosticBuilder Error(ObjectRef loc)
static DiagnosticBuilder Note(Span span)
Managed reference class to IRModuleNode.
Definition: module.h:257
The source name of a file span.
Definition: source_map.h:64
Definition: source_map.h:111
IRModule that holds the functions and type definitions.
Definition: repr_printer.h:91
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
DiagnosticRenderer TerminalRenderer(std::ostream &ostream)
DiagnosticLevel
The diagnostic level, controls the printing of the message.
Definition: diagnostic.h:38