improve scaninc error messages

This commit is contained in:
Henny022p 2021-11-30 06:44:30 +01:00
parent b6314d9269
commit 1c932ffea8
6 changed files with 32 additions and 59 deletions

View File

@ -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,7 +156,7 @@ 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();

View File

@ -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();

View File

@ -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++;
}

View File

@ -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;

View File

@ -25,40 +25,13 @@
#include <cstdlib>
#include <fmt/format.h>
#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<typename...T>
inline void fatal_error(std::string_view format, T...args){
fmt::print(stderr, format, args...);
exit(EXIT_FAILURE);
}
#define SCANINC_MAX_PATH 255

View File

@ -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;