FreeTensor
Loading...
Searching...
No Matches
except.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_EXCEPT_H
2#define FREE_TENSOR_EXCEPT_H
3
4#include <source_location>
5#include <sstream>
6#include <stdexcept>
7#include <string>
8
9namespace freetensor {
10
12 std::ostringstream os_;
13
14 public:
15 template <typename T> MessageBuilder &operator<<(const T &obj) {
16 os_ << obj;
17 return *this;
18 }
19
20 operator std::string() const { return os_.str(); }
21};
22
23#define FT_MSG MessageBuilder()
24
25class Error : public std::runtime_error {
26 public:
27 // NOTE: `source_location` is intended to have a small size and can be
28 // copied efficiently.
29 Error(const std::string &msg,
30 std::source_location loc = std::source_location::current())
31 : std::runtime_error(FT_MSG << loc.file_name() << ":" << loc.line()
32 << ": " << msg) {}
33};
34
35class StmtNode;
36class ScheduleLogItem;
37template <class T> class Ref;
38typedef Ref<StmtNode> Stmt;
39
40class InvalidSchedule : public Error {
41 public:
42 InvalidSchedule(const std::string &msg,
43 std::source_location loc = std::source_location::current())
44 : Error(msg, loc) {}
45 InvalidSchedule(const Stmt &ast, const std::string &msg,
46 std::source_location loc = std::source_location::current());
47 InvalidSchedule(const Ref<ScheduleLogItem> &log, const Stmt &ast,
48 const std::string &msg,
49 std::source_location loc = std::source_location::current());
50};
51
52class InvalidAutoGrad : public Error {
53 public:
54 InvalidAutoGrad(const std::string &msg,
55 std::source_location loc = std::source_location::current())
56 : Error(msg, loc) {}
57};
58
63class DriverError : public Error {
64 public:
65 DriverError(const std::string &msg,
66 std::source_location loc = std::source_location::current())
67 : Error(msg, loc) {}
68};
69
73class InvalidIO : public Error {
74 public:
75 InvalidIO(const std::string &msg,
76 std::source_location loc = std::source_location::current())
77 : Error(msg, loc) {}
78};
79
83class InvalidProgram : public Error {
84 public:
85 InvalidProgram(const std::string &msg,
86 std::source_location loc = std::source_location::current())
87 : Error(msg, loc) {}
88};
89
90class SymbolNotFound : public Error {
91 public:
92 SymbolNotFound(const std::string &msg,
93 std::source_location loc = std::source_location::current())
94 : Error(msg, loc) {}
95};
96
98 public:
100 const std::string &msg,
101 std::source_location loc = std::source_location::current())
102 : InvalidProgram(msg, loc) {}
103};
104
105class ParserError : public Error {
106 public:
107 ParserError(const std::string &msg,
108 std::source_location loc = std::source_location::current())
109 : Error(msg, loc) {}
110};
111
113 public:
115 const std::string &msg,
116 std::source_location loc = std::source_location::current())
117 : Error(msg, loc) {}
118};
119
134class InterruptExcept : public Error {
135 public:
137};
138
139void reportWarning(const std::string &msg);
140
141#define ERROR(msg) \
142 do { \
143 throw ::freetensor::Error(msg); \
144 } while (0)
145
146#define WARNING(msg) \
147 do { \
148 reportWarning(FT_MSG << "[WARNING] " __FILE__ ":" << __LINE__ << ": " \
149 << std::string(msg)); \
150 } while (0)
151
152#define ASSERT(expr) \
153 do { \
154 if (!(expr)) \
155 ERROR("Assertion false: " #expr); \
156 } while (0)
157
158} // namespace freetensor
159
160#endif // FREE_TENSOR_EXCEPT_H
Definition: except.h:97
AssertAlwaysFalse(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:99
Definition: except.h:63
DriverError(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:65
Definition: except.h:25
Error(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:29
Definition: except.h:134
InterruptExcept()
Definition: except.cc:33
Definition: except.h:52
InvalidAutoGrad(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:54
Definition: except.h:73
InvalidIO(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:75
Definition: except.h:83
InvalidProgram(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:85
Definition: except.h:40
InvalidSchedule(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:42
Definition: except.h:11
MessageBuilder & operator<<(const T &obj)
Definition: except.h:15
Definition: except.h:105
ParserError(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:107
Definition: except.h:90
SymbolNotFound(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:92
Definition: except.h:112
UnexpectedQueryResult(const std::string &msg, std::source_location loc=std::source_location::current())
Definition: except.h:114
#define FT_MSG
Definition: except.h:23
Definition: allocator.h:9
void reportWarning(const std::string &msg)
Definition: except.cc:39
Ref< StmtNode > Stmt
Definition: ast.h:152
STL namespace.