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/ir/module.h>
30 
31 #include <sstream>
32 #include <string>
33 
34 namespace tvm {
35 
37 
39 enum class DiagnosticLevel : int {
40  kBug = 10,
41  kError = 20,
42  kWarning = 30,
43  kNote = 40,
44  kHelp = 50,
45 };
46 
47 class DiagnosticBuilder;
48 
50 class Diagnostic;
51 
53 class DiagnosticNode : public Object {
54  public:
69 
70  // override attr visitor
72  v->Visit("level", &level);
73  v->Visit("span", &span);
74  v->Visit("message", &message);
75  }
76 
77  bool SEqualReduce(const DiagnosticNode* other, SEqualReducer equal) const {
78  return equal(this->level, other->level) && equal(this->span, other->span) &&
79  equal(this->message, other->message);
80  }
81 
82  static constexpr const char* _type_key = "Diagnostic";
84 };
85 
86 class Diagnostic : public ObjectRef {
87  public:
88  TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message);
89 
90  static DiagnosticBuilder Bug(Span span);
93  static DiagnosticBuilder Note(Span span);
94  static DiagnosticBuilder Help(Span span);
95  // variants uses object location
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 
109 };
110 
115  public:
118 
121 
124 
129 
130  template <typename T>
131  DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*)
132  stream_ << val;
133  return *this;
134  }
135 
137 
139  : level(builder.level), source_name(builder.source_name), span(builder.span) {}
140 
142 
144 
145  operator Diagnostic() { return Diagnostic(this->level, this->span, this->stream_.str()); }
146 
147  private:
148  std::stringstream stream_;
149  friend class Diagnostic;
150 };
151 
155 class DiagnosticContext;
156 
167  public:
169 
170  // override attr visitor
172 
173  static constexpr const char* _type_key = "DiagnosticRenderer";
175 };
176 
178  public:
182 
183  void Render(const DiagnosticContext& ctx);
184 
186  ICHECK(get() != nullptr);
187  return static_cast<DiagnosticRendererNode*>(get_mutable());
188  }
189 
191 };
192 
194  public:
197 
200 
203 
205  v->Visit("module", &module);
206  v->Visit("diagnostics", &diagnostics);
207  }
208 
210  return equal(module, other->module) && equal(diagnostics, other->diagnostics);
211  }
212 
213  static constexpr const char* _type_key = "DiagnosticContext";
215 };
216 
217 class DiagnosticContext : public ObjectRef {
218  public:
219  TVM_DLL DiagnosticContext(const IRModule& module, const DiagnosticRenderer& renderer);
220  TVM_DLL static DiagnosticContext Default(const IRModule& source_map);
221 
225  void Emit(const Diagnostic& diagnostic);
226 
234  void EmitFatal(const Diagnostic& diagnostic);
235 
237  void Render();
238 
240  ICHECK(get() != nullptr);
241  return static_cast<DiagnosticContextNode*>(get_mutable());
242  }
243 
245 };
246 
247 DiagnosticRenderer TerminalRenderer(std::ostream& ostream);
248 
249 } // namespace tvm
250 #endif // TVM_IR_DIAGNOSTIC_H_
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
A wrapper around std::stringstream to build a diagnostic.
Definition: diagnostic.h:114
DiagnosticBuilder(DiagnosticLevel level, ObjectRef loc)
Definition: diagnostic.h:143
friend class Diagnostic
Definition: diagnostic.h:149
DiagnosticBuilder(const DiagnosticBuilder &builder)
Definition: diagnostic.h:138
Span span
The span of the diagnostic.
Definition: diagnostic.h:123
DiagnosticBuilder(DiagnosticLevel level, Span span)
Definition: diagnostic.h:141
SourceName source_name
The source name.
Definition: diagnostic.h:120
DiagnosticBuilder & operator<<(const T &val)
Definition: diagnostic.h:131
DiagnosticLevel level
The level.
Definition: diagnostic.h:117
DiagnosticBuilder()
Definition: diagnostic.h:136
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:128
Definition: diagnostic.h:193
static constexpr const char * _type_key
Definition: diagnostic.h:213
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticContextNode, Object)
bool SEqualReduce(const DiagnosticContextNode *other, SEqualReducer equal) const
Definition: diagnostic.h:209
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:204
Array< Diagnostic > diagnostics
The set of diagnostics to report.
Definition: diagnostic.h:199
IRModule module
The Module to report against.
Definition: diagnostic.h:196
DiagnosticRenderer renderer
The renderer set for the context.
Definition: diagnostic.h:202
Definition: diagnostic.h:217
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.
DiagnosticContextNode * operator->()
Definition: diagnostic.h:239
DiagnosticContext(const IRModule &module, const DiagnosticRenderer &renderer)
static DiagnosticContext Default(const IRModule &source_map)
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DiagnosticContext, ObjectRef, DiagnosticContextNode)
A compiler diagnostic message.
Definition: diagnostic.h:53
bool SEqualReduce(const DiagnosticNode *other, SEqualReducer equal) const
Definition: diagnostic.h:77
static constexpr const char * _type_key
Definition: diagnostic.h:82
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:66
String message
The diagnostic message.
Definition: diagnostic.h:68
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticNode, Object)
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:71
DiagnosticLevel level
The level.
Definition: diagnostic.h:56
Span span
The span at which to report an error.
Definition: diagnostic.h:58
Display diagnostics in a given display format.
Definition: diagnostic.h:166
static constexpr const char * _type_key
Definition: diagnostic.h:173
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticRendererNode, Object)
TypedPackedFunc< void(DiagnosticContext ctx)> renderer
Definition: diagnostic.h:168
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:171
Definition: diagnostic.h:177
DiagnosticRenderer(TypedPackedFunc< void(DiagnosticContext ctx)> render)
void Render(const DiagnosticContext &ctx)
DiagnosticRendererNode * operator->()
Definition: diagnostic.h:185
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DiagnosticRenderer, ObjectRef, DiagnosticRendererNode)
DiagnosticRenderer()
Definition: diagnostic.h:180
Definition: diagnostic.h:86
static DiagnosticBuilder Error(const Object *loc)
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Diagnostic, ObjectRef, DiagnosticNode)
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(ObjectRef loc)
static DiagnosticBuilder Note(Span span)
Managed reference class to IRModuleNode.
Definition: module.h:366
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:137
The source name of a file span.
Definition: source_map.h:67
Definition: source_map.h:120
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Base class of all object reference.
Definition: object.h:519
const Object * get() const
Definition: object.h:554
Object * get_mutable() const
Definition: object.h:607
base class of all object containers.
Definition: object.h:171
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:63
IRModule that holds the functions and type definitions.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
DiagnosticRenderer TerminalRenderer(std::ostream &ostream)
DiagnosticLevel
The diagnostic level, controls the printing of the message.
Definition: diagnostic.h:39