From 1c932ffea86160bd5f71f7e5523b6d215dad4543 Mon Sep 17 00:00:00 2001 From: Henny022p Date: Tue, 30 Nov 2021 06:44:30 +0100 Subject: [PATCH] improve scaninc error messages --- tools/src/scaninc/asm_file.cpp | 18 +++++++------- tools/src/scaninc/asm_file.h | 4 ++-- tools/src/scaninc/c_file.cpp | 18 +++++++------- tools/src/scaninc/scaninc.cpp | 8 +++---- tools/src/scaninc/scaninc.h | 39 +++++-------------------------- tools/src/scaninc/source_file.cpp | 4 ++-- 6 files changed, 32 insertions(+), 59 deletions(-) diff --git a/tools/src/scaninc/asm_file.cpp b/tools/src/scaninc/asm_file.cpp index 37aeb933..f53777bc 100644 --- a/tools/src/scaninc/asm_file.cpp +++ b/tools/src/scaninc/asm_file.cpp @@ -29,7 +29,7 @@ AsmFile::AsmFile(std::string path) { FILE* fp = std::fopen(path.c_str(), "rb"); if (fp == NULL) - FATAL_ERROR("Failed to open \"%s\" for reading.\n", path.c_str()); + fatal_error("Failed to open \"%s\" for reading.\n", path.c_str()); std::fseek(fp, 0, SEEK_END); @@ -40,7 +40,7 @@ AsmFile::AsmFile(std::string path) { std::rewind(fp); if (std::fread(m_buffer, m_size, 1, fp) != 1) - FATAL_ERROR("Failed to read \"%s\".\n", path.c_str()); + fatal_error("Failed to read \"%s\".\n", path.c_str()); std::fclose(fp); @@ -104,22 +104,22 @@ std::string AsmFile::ReadPath() { break; if (c == -1) - FATAL_INPUT_ERROR("unexpected EOF in include string\n"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected EOF in include string\n")); if (c == 0) - FATAL_INPUT_ERROR("unexpected NUL character in include string\n"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected NUL character in include string\n")); if (c == '\n') - FATAL_INPUT_ERROR("unexpected end of line character in include string\n"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected end of line character in include string\n")); // Don't bother allowing any escape sequences. if (c == '\\') - FATAL_INPUT_ERROR("unexpected escape in include string\n"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected escape in include string\n")); length++; if (length > SCANINC_MAX_PATH) - FATAL_INPUT_ERROR("path is too long"); + fatal_error(INPUT_ERROR_MESSAGE("path is too long")); } return std::string(m_buffer + startPos, length); @@ -156,10 +156,10 @@ void AsmFile::SkipString() { break; if (c == -1) - FATAL_INPUT_ERROR("unexpected EOF in string\n"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected EOF in string\n")); if (c == '\\') { - c = GetChar(); + c = GetChar(); } } } diff --git a/tools/src/scaninc/asm_file.h b/tools/src/scaninc/asm_file.h index 4ee0fddf..79a87a63 100644 --- a/tools/src/scaninc/asm_file.h +++ b/tools/src/scaninc/asm_file.h @@ -50,7 +50,7 @@ class AsmFile { m_lineNum++; return '\n'; } else { - FATAL_INPUT_ERROR("CR line endings are not supported\n"); + fatal_error(INPUT_ERROR_MESSAGE("CR line endings are not supported\n")); } } @@ -89,7 +89,7 @@ class AsmFile { SkipTabsAndSpaces(); if (GetChar() != '"') - FATAL_INPUT_ERROR("no path after \".%s\" directive\n", directiveName.c_str()); + fatal_error(INPUT_ERROR_MESSAGE("no path after \".%s\" directive\n"), directiveName.c_str()); path = ReadPath(); diff --git a/tools/src/scaninc/c_file.cpp b/tools/src/scaninc/c_file.cpp index fca672e8..a8ff921f 100644 --- a/tools/src/scaninc/c_file.cpp +++ b/tools/src/scaninc/c_file.cpp @@ -26,7 +26,7 @@ CFile::CFile(std::string path) { FILE* fp = std::fopen(path.c_str(), "rb"); if (fp == NULL) - FATAL_ERROR("Failed to open \"%s\" for reading.\n", path.c_str()); + fatal_error("Failed to open \"%s\" for reading.\n", path.c_str()); std::fseek(fp, 0, SEEK_END); @@ -38,7 +38,7 @@ CFile::CFile(std::string path) { std::rewind(fp); if (std::fread(m_buffer, m_size, 1, fp) != 1) - FATAL_ERROR("Failed to read \"%s\".\n", path.c_str()); + fatal_error("Failed to read \"%s\".\n", path.c_str()); std::fclose(fp); @@ -82,7 +82,7 @@ void CFile::FindIncbins() { else if (c == '\'') stringChar = '\''; else if (c == 0) - FATAL_INPUT_ERROR("unexpected null character"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected null character")); } } } @@ -224,7 +224,7 @@ void CFile::CheckIncbin() { } if (m_buffer[m_pos] != ')') - FATAL_INPUT_ERROR("expected ')'"); + fatal_error(INPUT_ERROR_MESSAGE("expected ')'")); m_pos++; } @@ -234,7 +234,7 @@ std::string CFile::ReadPath() { if (m_buffer[m_pos] == '<') { return std::string(); } - FATAL_INPUT_ERROR("expected '\"' or '<'"); + fatal_error(INPUT_ERROR_MESSAGE("expected '\"' or '<'")); } m_pos++; @@ -244,16 +244,16 @@ std::string CFile::ReadPath() { while (m_buffer[m_pos] != '"') { if (m_buffer[m_pos] == 0) { if (m_pos >= m_size) - FATAL_INPUT_ERROR("unexpected EOF in path string"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected EOF in path string")); else - FATAL_INPUT_ERROR("unexpected null character in path string"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected null character in path string")); } if (m_buffer[m_pos] == '\r' || m_buffer[m_pos] == '\n') - FATAL_INPUT_ERROR("unexpected end of line character in path string"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected end of line character in path string")); if (m_buffer[m_pos] == '\\') - FATAL_INPUT_ERROR("unexpected escape in path string"); + fatal_error(INPUT_ERROR_MESSAGE("unexpected escape in path string")); m_pos++; } diff --git a/tools/src/scaninc/scaninc.cpp b/tools/src/scaninc/scaninc.cpp index e77982c8..d1487422 100644 --- a/tools/src/scaninc/scaninc.cpp +++ b/tools/src/scaninc/scaninc.cpp @@ -62,14 +62,14 @@ int main(int argc, char** argv) { } includeDirs.push_back(includeDir); } else { - FATAL_ERROR(USAGE); + fatal_error(USAGE); } argc--; argv++; } if (argc != 1) { - FATAL_ERROR(USAGE); + fatal_error(USAGE); } std::string initialPath(argv[0]); @@ -92,8 +92,8 @@ int main(int argc, char** argv) { } } } - for (auto include : file.GetIncludes()) { - for (auto includeDir : includeDirs) { + for (const auto &include : file.GetIncludes()) { + for (const auto &includeDir : includeDirs) { std::string path(includeDir + include); if (CanOpenFile(path)) { bool inserted = dependencies.insert(path).second; diff --git a/tools/src/scaninc/scaninc.h b/tools/src/scaninc/scaninc.h index d48fb7a7..61f76137 100644 --- a/tools/src/scaninc/scaninc.h +++ b/tools/src/scaninc/scaninc.h @@ -25,40 +25,13 @@ #include #include -#ifdef _MSC_VER +#define INPUT_ERROR_MESSAGE(format) "{}:{} " format, m_path.c_str(), m_lineNum -#define FATAL_INPUT_ERROR(format, ...) \ - do { \ - fprintf(stderr, "%s:%d " format, m_path.c_str(), m_lineNum, __VA_ARGS__); \ - exit(1); \ - } while (0) - -#define FATAL_ERROR(format, ...) \ - do { \ - fprintf(stderr, format, __VA_ARGS__); \ - exit(1); \ - } while (0) - -#else - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" - -#define FATAL_INPUT_ERROR(format, ...) \ - do { \ - fmt::print(stderr, "%s:%d " format, m_path.c_str(), m_lineNum, ##__VA_ARGS__); \ - exit(1); \ - } while (0) - -#define FATAL_ERROR(format, ...) \ - do { \ - fmt::print(stderr, format, ##__VA_ARGS__); \ - exit(1); \ - } while (0) - -#pragma GCC diagnostic pop - -#endif // _MSC_VER +template +inline void fatal_error(std::string_view format, T...args){ + fmt::print(stderr, format, args...); + exit(EXIT_FAILURE); +} #define SCANINC_MAX_PATH 255 diff --git a/tools/src/scaninc/source_file.cpp b/tools/src/scaninc/source_file.cpp index 12daa0c6..0cefc9f1 100644 --- a/tools/src/scaninc/source_file.cpp +++ b/tools/src/scaninc/source_file.cpp @@ -25,7 +25,7 @@ SourceFileType GetFileType(std::string& path) { std::size_t pos = path.find_last_of('.'); if (pos == std::string::npos) - FATAL_ERROR("no file extension in path \"%s\"\n", path.c_str()); + fatal_error("no file extension in path \"%s\"\n", path.c_str()); std::string extension = path.substr(pos + 1); @@ -38,7 +38,7 @@ SourceFileType GetFileType(std::string& path) { else if (extension == "inc") return SourceFileType::Inc; else - FATAL_ERROR("Unrecognized extension \"%s\"\n", extension.c_str()); + fatal_error("Unrecognized extension \"%s\"\n", extension.c_str()); // Unreachable return SourceFileType::Cpp;