Skip to content

Commit

Permalink
Merge branch 'improve-errmsgs' into 'main'
Browse files Browse the repository at this point in the history
Improve error messages

See merge request repositories/verilator-infrastructure-bugpoint!27
  • Loading branch information
sgizler committed Oct 9, 2024
2 parents 51cbafd + fd61fb9 commit 0a58dbf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
8 changes: 4 additions & 4 deletions source/SvBugpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ void minimize() {
} while (committed);

} else {
std::cerr << "sv-bugpoint: failed to load " << paths.input << " file "
<< treeOrErr.error().second << "\n";
PRINTF_ERR("failed to load '%s' file: %s\n", paths.input.c_str(),
std::string(treeOrErr.error().second).c_str());
exit(1);
}
}
Expand All @@ -109,8 +109,8 @@ void initOutDir(bool force) {
void dryRun() {
auto info = AttemptStats("-", "dryRun");
if (!test(info)) {
std::cerr << "sv-bugpoint: '" << paths.checkScript << " " << paths.tmpOutput
<< "' exited with non-zero on dry run with unmodified input.\n";
PRINTF_ERR("'%s %s' exited with non-zero on dry run with unmodified input\n",
paths.checkScript.c_str(), paths.tmpOutput.c_str());
exit(1);
}
}
Expand Down
22 changes: 12 additions & 10 deletions source/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
#include <slang/ast/ASTVisitor.h>
#include <slang/syntax/SyntaxPrinter.h>
#include <slang/syntax/SyntaxVisitor.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstring>

#ifdef __GLIBCXX__
#include <cxxabi.h>
std::string tryDemangle(const char* mangled) {
int rc;
char* out = abi::__cxa_demangle(mangled, NULL, NULL, &rc);
if (rc != 0) {
std::cerr << "demangling error!\n";
PRINTF_ERR("demangling error\n");
exit(1);
}
std::string outStr = out;
Expand Down Expand Up @@ -138,8 +141,8 @@ void dumpTrees() {

printAst(compilation.getRoot(), astDumpFile);
} else {
std::cerr << "sv-bugpoint: failed to load " << paths.input << " file "
<< treeOrErr.error().second << "\n";
PRINTF_ERR("failed to load '%s' file: %s\n", paths.input.c_str(),
std::string(treeOrErr.error().second).c_str());
exit(1);
}
}
Expand All @@ -164,8 +167,8 @@ void copyFile(const std::string& from, const std::string& to) {
try {
std::filesystem::copy(from, to, std::filesystem::copy_options::overwrite_existing);
} catch (const std::filesystem::filesystem_error& err) {
std::cerr << "sv-bugpoint: failed to copy " << from << "to" << to << ": "
<< err.code().message() << "\n";
PRINTF_ERR("failed to copy '%s' to '%s': %s\n", from.c_str(), to.c_str(),
err.code().message().c_str());
exit(1);
}
}
Expand All @@ -174,8 +177,8 @@ void mkdir(const std::string& path) {
try {
std::filesystem::create_directory(path);
} catch (const std::filesystem::filesystem_error& err) {
std::cerr << "sv-bugpoint: failed to make directory " << path << ": "
<< err.code().message() << "\n";
PRINTF_ERR("failed to make directory '%s': %s\n", path.c_str(),
err.code().message().c_str());
exit(1);
}
}
Expand Down Expand Up @@ -231,13 +234,12 @@ bool test(AttemptStats& stats) {
stats.begin();
pid_t pid = fork();
if (pid == -1) {
perror("fork failed");
PRINTF_ERR("fork failed: %s\n", strerror(errno));
exit(1);
} else if (pid == 0) { // we are inside child
const char* const argv[] = {paths.checkScript.c_str(), paths.tmpOutput.c_str(), NULL};
if (execv(argv[0], const_cast<char* const*>(argv))) { // replace child with prog
std::string err = "sv-bugpoint: failed to lanuch " + paths.checkScript;
perror(err.c_str());
PRINTF_ERR("failed to launch '%s': %s\n", paths.checkScript.c_str(), strerror(errno));
kill(getppid(), SIGINT); // terminate parent
exit(1);
}
Expand Down
8 changes: 8 additions & 0 deletions source/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ bool test(AttemptStats& stats);
bool test(std::shared_ptr<SyntaxTree>& tree, AttemptStats& info);

std::string prefixLines(const std::string& str, const std::string& linePrefix);

// NOTE: doing it as variadic func rather than macro would prevent
// compiler from issuing warnings about incorrect format string
#define PRINTF_ERR(...) \
do { \
fprintf(stderr, "sv-bugpoint: "); \
fprintf(stderr, __VA_ARGS__); \
} while (0)

0 comments on commit 0a58dbf

Please sign in to comment.