Yume
errors.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cxxabi.h>
4#include <exception>
5#include <llvm/Support/PrettyStackTrace.h>
6#include <llvm/Support/raw_ostream.h>
7#include <string>
8#include <string_view>
9#include <typeinfo>
10
11namespace yume {
12namespace ast {
13class AST;
14}
15struct Token;
16
17inline auto what(const std::exception_ptr& eptr = std::current_exception()) -> std::string_view {
18 if (!eptr)
19 throw std::bad_exception();
20
21 try {
22 std::rethrow_exception(eptr);
23 } catch (const std::exception& e) {
24 return e.what();
25 } catch (const std::string& e) {
26 return e;
27 } catch (const char* e) {
28 return e;
29 } catch (...) {
30 return "[none available]";
31 }
32}
33
34// `free`ing pointers is really not important when the application has already crashed
35inline auto current_exception_name() -> const char* {
36 int status{};
37 return abi::__cxa_demangle(abi::__cxa_current_exception_type()->name(), nullptr, nullptr, &status);
38}
39
40// Performance is really not important when the application has already crashed
41inline void print_exception() {
42 auto exception = std::current_exception();
43 if (exception != nullptr) {
44 using enum llvm::raw_fd_ostream::Colors;
45 llvm::errs().changeColor(RED, true) << "Uncaught exception resulted in termination!\n";
46 llvm::errs().changeColor(RED) << " Ultimately caught ";
47 llvm::errs().resetColor() << current_exception_name() << "\n";
48 llvm::errs().changeColor(RED) << " Additional info: ";
49 llvm::errs().changeColor(YELLOW) << what(exception) << "\n\n";
50 llvm::errs().resetColor();
51 }
52}
53
54void backtrace(void* /*unused*/);
55
56struct ASTStackTrace : public llvm::PrettyStackTraceEntry {
57 std::string message;
58
59 ASTStackTrace(std::string message);
60 ASTStackTrace(std::string message, const ast::AST& ast);
61
62 void print(llvm::raw_ostream& stream) const override { stream << message << "\n"; };
63};
64
65struct ParserStackTrace : public llvm::PrettyStackTraceEntry {
66 std::string message;
67
69 ParserStackTrace(std::string message, const Token& token);
70
71 void print(llvm::raw_ostream& stream) const override { stream << message << "\n"; };
72};
73} // namespace yume
All nodes in the AST tree of the program inherit from this class.
Definition: ast.hpp:224
Definition: ast.cpp:8
auto current_exception_name() -> const char *
Definition: errors.hpp:35
void backtrace(void *)
Definition: errors.cpp:284
void print_exception()
Definition: errors.hpp:41
auto what(const std::exception_ptr &eptr=std::current_exception()) -> std::string_view
Definition: errors.hpp:17
void print(llvm::raw_ostream &stream) const override
Definition: errors.hpp:62
ASTStackTrace(std::string message, const ast::AST &ast)
std::string message
Definition: errors.hpp:57
ASTStackTrace(std::string message)
ParserStackTrace(std::string message, const Token &token)
std::string message
Definition: errors.hpp:66
ParserStackTrace(std::string message)
void print(llvm::raw_ostream &stream) const override
Definition: errors.hpp:71
A categorized token in source code, created by the tokenizer. These tokens are consumed by the lexer.
Definition: token.hpp:80