From 179294a91a5306f67da0485a8c0a1525932056b5 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 2 Nov 2022 08:15:33 +0100 Subject: [PATCH] Error: Add a throw_error variant to report compiler bugs :) --- src/AST/MulNode.cpp | 9 ++++++--- src/AST/SumNode.cpp | 9 ++++++--- src/Error.cpp | 17 +++++++++++++++++ src/Error.h | 2 ++ src/Location.h | 2 ++ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/AST/MulNode.cpp b/src/AST/MulNode.cpp index 40dcb83..edafa4f 100644 --- a/src/AST/MulNode.cpp +++ b/src/AST/MulNode.cpp @@ -1,10 +1,14 @@ #include "MulNode.h" #include "../IRBuilder.h" +#include "../Error.h" MulNode::MulNode(std::shared_ptr left, std::shared_ptr right, char op) : BinaryOpNode(left, right), op(op) { - assert(op == '*' || op == '/'); + if(op != '*' && op != '/') + { + Error::throw_error(CURRENT_LOCATION, "BUG: MulNode expects op to be either * or /"); + } } MulNode::~MulNode() @@ -20,7 +24,6 @@ llvm::Value* MulNode::codegen(IRBuilder* generator) case '/': return generator->getBuilder()->CreateSDiv(left->codegen(generator), right->codegen(generator)); default: - assert(0 && "unreachable"); - return nullptr; + __builtin_unreachable(); } } \ No newline at end of file diff --git a/src/AST/SumNode.cpp b/src/AST/SumNode.cpp index 19143c5..ba824f1 100644 --- a/src/AST/SumNode.cpp +++ b/src/AST/SumNode.cpp @@ -1,10 +1,14 @@ #include "SumNode.h" #include "../IRBuilder.h" +#include "../Error.h" SumNode::SumNode(std::shared_ptr left, std::shared_ptr right, char op) : BinaryOpNode(left, right), op(op) { - assert(op == '+' || op == '-'); + if(op != '+' && op != '-') + { + Error::throw_error(CURRENT_LOCATION, "BUG: SumNode expects op to be either + or -"); + } } SumNode::~SumNode() @@ -20,7 +24,6 @@ llvm::Value* SumNode::codegen(IRBuilder* generator) case '-': return generator->getBuilder()->CreateSub(left->codegen(generator), right->codegen(generator)); default: - assert(0 && "unreachable"); - return nullptr; + __builtin_unreachable(); } } \ No newline at end of file diff --git a/src/Error.cpp b/src/Error.cpp index e190f09..2b63f52 100644 --- a/src/Error.cpp +++ b/src/Error.cpp @@ -72,6 +72,23 @@ void Error::show_import_lines(const Location& loc, void (*import_line_printer)(c exit(1); } +[[noreturn]] void Error::throw_error(const Location& loc, const std::string& details) +{ + std::cerr << "\033[1;1m"; + std::cerr << loc.str(); + + std::cerr << ": "; + + std::cerr << "\033[31;49m"; + std::cerr << "error: "; + + std::cerr << "\033[0;0m"; + std::cerr << details; + std::cerr << std::endl; + + exit(1); +} + [[noreturn]] void Error::throw_error_without_location(const std::string& details) { std::cerr << "\033[1;1m"; diff --git a/src/Error.h b/src/Error.h index 3cc3775..8b2ce34 100644 --- a/src/Error.h +++ b/src/Error.h @@ -7,6 +7,8 @@ void show_import_line(const Location& loc, std::ostream& output_stream); [[noreturn]] void throw_error(const Location& loc, const std::string line_text, const std::string& details); +[[noreturn]] void throw_error(const Location& loc, const std::string& details); + [[noreturn]] void throw_error_without_location(const std::string& details); void throw_warning(const Location& loc, const std::string line_text, const std::string& details); diff --git a/src/Location.h b/src/Location.h index 7138a76..320043d 100644 --- a/src/Location.h +++ b/src/Location.h @@ -36,3 +36,5 @@ struct Location /* Returns a copy of the original Location. */ static Location copy(const Location& other); }; + +#define CURRENT_LOCATION (Location {__LINE__, 0, __FILE__}) \ No newline at end of file