Tensor Comprehensions
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
error_report.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include "tc/lang/tree.h"
19 
20 namespace lang {
21 
22 struct ErrorReport : public std::exception {
24  : ss(e.ss.str()), context(e.context), the_message(e.the_message) {}
25 
26  ErrorReport(TreeRef context) : context(context->range()) {}
27  ErrorReport(SourceRange range) : context(std::move(range)) {}
28  virtual const char* what() const noexcept override {
29  std::stringstream msg;
30  msg << "\n" << ss.str() << ":\n";
31  context.highlight(msg);
32  the_message = msg.str();
33  return the_message.c_str();
34  }
35 
36  private:
37  template <typename T>
38  friend const ErrorReport& operator<<(const ErrorReport& e, const T& t);
39 
40  mutable std::stringstream ss;
42  mutable std::string the_message;
43 };
44 
45 inline void warn(const ErrorReport& err) {
46  std::cerr << err.what();
47 }
48 
49 template <typename T>
50 const ErrorReport& operator<<(const ErrorReport& e, const T& t) {
51  e.ss << t;
52  return e;
53 }
54 
55 #define TC_ASSERT(ctx, cond) \
56  if (!(cond)) { \
57  throw ::lang::ErrorReport(ctx) \
58  << __FILE__ << ":" << __LINE__ << ": assertion failed: " << #cond; \
59  }
60 } // namespace lang
friend const ErrorReport & operator<<(const ErrorReport &e, const T &t)
Definition: error_report.h:50
void highlight(std::ostream &out) const
Definition: lexer.h:315
const ErrorReport & operator<<(const ErrorReport &e, const T &t)
Definition: error_report.h:50
ErrorReport(TreeRef context)
Definition: error_report.h:26
void warn(const ErrorReport &err)
Definition: error_report.h:45
virtual const char * what() const noexceptoverride
Definition: error_report.h:28
SourceRange context
Definition: error_report.h:41
Definition: lexer.h:303
std::stringstream ss
Definition: error_report.h:40
ErrorReport(SourceRange range)
Definition: error_report.h:27
std::string the_message
Definition: error_report.h:42
ErrorReport(const ErrorReport &e)
Definition: error_report.h:23
Definition: error_report.h:22
std::shared_ptr< Tree > TreeRef
Definition: tree.h:44