error messages

This commit is contained in:
Jorijn van der Graaf 2025-09-09 23:04:05 +02:00
commit 23fa8b98b0
9 changed files with 199 additions and 90 deletions

View file

@ -25,36 +25,65 @@ module;
#include <array>
#include <string>
#include <cstdio>
#include <string_view>
#include <regex>
export module Crafter.Build:Bounce;
export void BounceCommand(const std::string& cmd) {
// std::array<char, 128> buffer;
// std::string result;
namespace Crafter::Build {
export std::string RunCommand(const std::string_view cmd) {
std::array<char, 128> buffer;
std::string result;
// std::string with = cmd + " 2>&1";
// // Open pipe to file
// FILE* pipe = popen(with.c_str(), "r");
// if (!pipe) throw std::runtime_error("popen() failed!");
std::string with = std::string(cmd) + " 2>&1";
// Open pipe to file
FILE* pipe = popen(with.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
// // Read till end of process:
// while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
// result += buffer.data();
// }
// Read till end of process:
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
// // Close pipe
// auto returnCode = pclose(pipe);
// if (returnCode != 0) {
// // Optional: handle non-zero exit status
// }
// Close pipe
pclose(pipe);
return result;
}
export void RunCommandIgnore(const std::string_view cmd) {
std::string with = std::string(cmd) + " > /dev/null 2>&1";
FILE* pipe = popen(with.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
pclose(pipe);
}
// std::cout << result;
system(cmd.c_str());
}
export struct ClangError {
std::string filename;
uint32_t line_number;
std::string error_message;
std::string code;
};
export void BounceCommandIgnore(const std::string& cmd) {
std::string with = cmd + " > /dev/null 2>&1";
FILE* pipe = popen(with.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
pclose(pipe);
export std::vector<ClangError> RunClang(const std::string_view cmd) {
std::string result = RunCommand(cmd);
std::vector<ClangError> errors;
std::regex error_regex(R"((/[^:]+\.cpp):(\d+):\d+: error: (.*)\n\s*[0-9| ]*\s*(.*))");
std::smatch match;
while (std::regex_search(result, match, error_regex)) {
ClangError error;
error.filename = match[1].str();
error.line_number = std::stoi(match[2].str());
error.error_message = match[3].str();
error.code = match[4].str();
errors.push_back(error);
result = match.suffix().str();
}
if(result != "" && errors.size() == 0) {
throw std::runtime_error(result);
}
return errors;
}
}