Skip to content

Commit

Permalink
Fixed a bug in the error system that was causing some errors to cause…
Browse files Browse the repository at this point in the history
… crashes.
  • Loading branch information
Goubermouche committed Feb 2, 2024
1 parent bcfba29 commit 04dcaa3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
32 changes: 25 additions & 7 deletions source/compiler/compiler/diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace sigma {
* \brief Predeclared error codes
*/
enum class code : u32 {
TEST_ERROR,

// filesystem (1000 - 1999)
EXPECTED_FILE = 1000,
FILE_DOES_NOT_EXIST,
Expand Down Expand Up @@ -57,16 +59,21 @@ namespace sigma {
*/
template<typename... arguments>
static auto emit(code code, handle<token_location> location, arguments&&... args) -> utility::error {
const std::string str = std::format(
const std::string message = std::vformat(
m_errors.find(code)->second,
std::make_format_args(std::forward<arguments>(args)...)
);

const std::string error_message = std::format(
"{}:{}:{}: error C{}: {}",
location->file->get_filename(),
location->line_index + 1,
location->char_index + 1,
location->line_index + 1,
location->char_index + 1,
static_cast<u32>(code),
m_errors.find(code)->second
message
);

return utility::error(str, std::forward<arguments>(args)...);
return { error_message };
}

/**
Expand All @@ -78,11 +85,22 @@ namespace sigma {
*/
template<typename... arguments>
static auto emit(code code, arguments&&... args) -> utility::error {
const std::string str = std::format("error C{}: {}", static_cast<u32>(code), m_errors.find(code)->second);
return utility::error(str, std::forward<arguments>(args)...);
const std::string message = std::vformat(
m_errors.find(code)->second,
std::make_format_args(std::forward<arguments>(args)...)
);

const std::string error_message = std::format(
"error C{}: {}",
static_cast<u32>(code), message
);

return { error_message };
}
private:
const static inline std::unordered_map<code, std::string> m_errors = {
{ code::TEST_ERROR, "test {}" },

// filesystem
{ code::EXPECTED_FILE, "expected a file but got a directory ('{}')" },
{ code::FILE_DOES_NOT_EXIST, "specified file does not exist ('{}')" },
Expand Down
4 changes: 1 addition & 3 deletions source/compiler/test/main.s
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// - more info related to numerical errors (hex etc)
// - add namespaces to messages, whenever applicable (ie. x::y::test)
// - BUGS:
// - the parser returns an empty error in some cases
// - set crashes with more than 4(?) parameters
// - TESTS:
// - add more test cases
Expand All @@ -20,6 +19,5 @@
// - implicit returns for non-void functions should not be a thing

i32 main() {
printf("%d\n", true);
ret 0;
a
}
6 changes: 1 addition & 5 deletions source/utility/diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ namespace utility {

class error {
public:
template<typename... arguments>
error(const std::string& fmt, arguments&&... args) {
auto formatted_args = std::make_format_args(std::forward<arguments>(args)...);
m_message = std::vformat(fmt, formatted_args);
}
error(const std::string& message) : m_message(message) {}

auto get_message() const -> const std::string& {
return m_message;
Expand Down
2 changes: 1 addition & 1 deletion source/utility/filesystem/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace utility {
std::ifstream file(path.get_path());

if (!file) {
return error("cannot open file '{}'", path);
return error(std::format("cannot open file '{}'", path));
}

return std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
Expand Down

0 comments on commit 04dcaa3

Please sign in to comment.