Caffe2 - C++ API
A deep learning, cross platform ML framework
error_report.h
1 #pragma once
2 
3 #include <torch/csrc/jit/script/tree.h>
4 
5 namespace torch {
6 namespace jit {
7 namespace script {
8 
9 struct ErrorReport : public std::exception {
10  ErrorReport(const ErrorReport& e)
11  : ss(e.ss.str()), context(e.context), the_message(e.the_message) {}
12 
13  ErrorReport() : context(nullptr) {}
14  explicit ErrorReport(const SourceRange& r)
15  : context(std::make_shared<SourceRange>(r)) {}
16  explicit ErrorReport(std::shared_ptr<SourceLocation> loc)
17  : context(std::move(loc)) {}
18  explicit ErrorReport(const TreeRef& tree) : ErrorReport(tree->range()) {}
19  explicit ErrorReport(const Token& tok) : ErrorReport(tok.range) {}
20  const char* what() const noexcept override {
21  std::stringstream msg;
22  msg << "\n" << ss.str();
23  if (context != nullptr) {
24  msg << ":\n";
25  context->highlight(msg);
26  } else {
27  msg << ".\n";
28  }
29  the_message = msg.str();
30  return the_message.c_str();
31  }
32 
33  private:
34  template <typename T>
35  friend const ErrorReport& operator<<(const ErrorReport& e, const T& t);
36 
37  mutable std::stringstream ss;
38  std::shared_ptr<SourceLocation> context;
39  mutable std::string the_message;
40 };
41 
42 template <typename T>
43 const ErrorReport& operator<<(const ErrorReport& e, const T& t) {
44  e.ss << t;
45  return e;
46 }
47 
48 #define JIT_SCRIPT_ASSERT(ctx, cond) \
49  if (!(cond)) { \
50  throw ::torch::jit::script::ErrorReport(ctx) \
51  << __FILE__ << ":" << __LINE__ << ": assertion failed: " << #cond; \
52  }
53 } // namespace script
54 } // namespace jit
55 } // namespace torch
Definition: jit_type.h:17