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 #include <tvm/parser/source_map.h>
31 
32 #include <sstream>
33 #include <string>
34 
35 namespace tvm {
36 
39 
41 enum class DiagnosticLevel : int {
42  kBug = 10,
43  kError = 20,
44  kWarning = 30,
45  kNote = 40,
46  kHelp = 50,
47 };
48 
49 class DiagnosticBuilder;
50 
52 class Diagnostic;
53 
55 class DiagnosticNode : public Object {
56  public:
63 
64  // override attr visitor
66  v->Visit("level", &level);
67  v->Visit("span", &span);
68  v->Visit("message", &message);
69  }
70 
71  bool SEqualReduce(const DiagnosticNode* other, SEqualReducer equal) const {
72  return equal(this->level, other->level) && equal(this->span, other->span) &&
73  equal(this->message, other->message);
74  }
75 
76  static constexpr const char* _type_key = "Diagnostic";
78 };
79 
80 class Diagnostic : public ObjectRef {
81  public:
82  TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message);
83 
84  static DiagnosticBuilder Bug(Span span);
85  static DiagnosticBuilder Error(Span span);
86  static DiagnosticBuilder Warning(Span span);
87  static DiagnosticBuilder Note(Span span);
88  static DiagnosticBuilder Help(Span span);
89 
91 };
92 
97  public:
100 
103 
106 
107  template <typename T>
108  DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*)
109  stream_ << val;
110  return *this;
111  }
112 
113  DiagnosticBuilder() : level(DiagnosticLevel::kError), source_name(), span(Span()) {}
114 
116  : level(builder.level), source_name(builder.source_name), span(builder.span) {}
117 
118  DiagnosticBuilder(DiagnosticLevel level, Span span) : level(level), span(span) {}
119 
120  operator Diagnostic() { return Diagnostic(this->level, this->span, this->stream_.str()); }
121 
122  private:
123  std::stringstream stream_;
124  friend class Diagnostic;
125 };
126 
130 class DiagnosticContext;
131 
142  public:
144 
145  // override attr visitor
147 
148  static constexpr const char* _type_key = "DiagnosticRenderer";
150 };
151 
153  public:
154  TVM_DLL DiagnosticRenderer(TypedPackedFunc<void(DiagnosticContext ctx)> render);
156  : DiagnosticRenderer(TypedPackedFunc<void(DiagnosticContext ctx)>()) {}
157 
158  void Render(const DiagnosticContext& ctx);
159 
161  ICHECK(get() != nullptr);
162  return static_cast<DiagnosticRendererNode*>(get_mutable());
163  }
164 
166 };
167 
169  public:
172 
175 
178 
180  v->Visit("module", &module);
181  v->Visit("diagnostics", &diagnostics);
182  }
183 
185  return equal(module, other->module) && equal(diagnostics, other->diagnostics);
186  }
187 
188  static constexpr const char* _type_key = "DiagnosticContext";
190 };
191 
192 class DiagnosticContext : public ObjectRef {
193  public:
194  TVM_DLL DiagnosticContext(const IRModule& module, const DiagnosticRenderer& renderer);
195  TVM_DLL static DiagnosticContext Default(const IRModule& source_map);
196 
200  void Emit(const Diagnostic& diagnostic);
201 
209  void EmitFatal(const Diagnostic& diagnostic);
210 
212  void Render();
213 
215  ICHECK(get() != nullptr);
216  return static_cast<DiagnosticContextNode*>(get_mutable());
217  }
218 
220 };
221 
222 DiagnosticRenderer TerminalRenderer(std::ostream& ostream);
223 
224 } // namespace tvm
225 #endif // TVM_IR_DIAGNOSTIC_H_
Display diagnostics in a given display format.
Definition: diagnostic.h:141
A compiler diagnostic message.
Definition: diagnostic.h:55
DiagnosticLevel level
The level.
Definition: diagnostic.h:58
Span span
The span at which to report an error.
Definition: diagnostic.h:60
Definition: diagnostic.h:80
DiagnosticContextNode * operator->()
Definition: diagnostic.h:214
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:146
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:124
IRModule that holds the functions and type definitions.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
SourceName source_name
The source name.
Definition: diagnostic.h:102
DiagnosticBuilder()
Definition: diagnostic.h:113
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
DiagnosticLevel level
The level.
Definition: diagnostic.h:99
Definition: diagnostic.h:168
A map from source names to source code.
DiagnosticBuilder(DiagnosticLevel level, Span span)
Definition: diagnostic.h:118
base class of all object containers.
Definition: object.h:167
DiagnosticRendererNode * operator->()
Definition: diagnostic.h:160
Definition: source_map.h:97
Definition: diagnostic.h:152
DiagnosticBuilder & operator<<(const T &val)
Definition: diagnostic.h:108
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
Definition: span.h:115
A wrapper around std::stringstream to build a diagnostic.
Definition: diagnostic.h:96
String message
The diagnostic message.
Definition: diagnostic.h:62
The source name of a file span.
Definition: span.h:62
IRModule module
The Module to report against.
Definition: diagnostic.h:171
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
DiagnosticRenderer TerminalRenderer(std::ostream &ostream)
DiagnosticLevel
The diagnostic level, controls the printing of the message.
Definition: diagnostic.h:41
Definition: diagnostic.h:192
Reference to string objects.
Definition: string.h:97
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
Array< Diagnostic > diagnostics
The set of diagnostics to report.
Definition: diagnostic.h:174
Base class of all object reference.
Definition: object.h:511
DiagnosticRenderer renderer
The renderer set for the context.
Definition: diagnostic.h:177
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
Managed reference class to IRModuleNode.
Definition: module.h:352
Span span
The span of the diagnostic.
Definition: diagnostic.h:105
DiagnosticBuilder(const DiagnosticBuilder &builder)
Definition: diagnostic.h:115
bool SEqualReduce(const DiagnosticContextNode *other, SEqualReducer equal) const
Definition: diagnostic.h:184
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:179
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:65
bool SEqualReduce(const DiagnosticNode *other, SEqualReducer equal) const
Definition: diagnostic.h:71
DiagnosticRenderer()
Definition: diagnostic.h:155
TypedPackedFunc< void(DiagnosticContext ctx)> renderer
Definition: diagnostic.h:143
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728