Files
nudt-compiler-cpp/src/utils/Log.cpp
2026-03-13 21:37:37 +08:00

67 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 格式化错误并统一打印异常与帮助信息。
#include "utils/Log.h"
#include <ostream>
#include <string>
namespace {
bool IsCLIError(const std::string_view msg) {
return HasErrorPrefix(msg, "cli");
}
} // namespace
void LogInfo(const std::string_view msg, std::ostream& os) {
os << "[info] " << msg << "\n";
}
void LogError(const std::string_view msg, std::ostream& os) {
os << "[error] " << msg << "\n";
}
std::string FormatError(const std::string_view stage,
const std::string_view msg) {
return "[" + std::string(stage) + "] " + std::string(msg);
}
std::string FormatErrorAt(const std::string_view stage, const std::size_t line,
const std::size_t column,
const std::string_view msg) {
return "[" + std::string(stage) + "] @" + std::to_string(line) + ":" +
std::to_string(column) + " - " + std::string(msg);
}
bool HasErrorPrefix(const std::string_view msg, const std::string_view stage) {
const std::string prefix = "[" + std::string(stage) + "]";
return msg.rfind(prefix, 0) == 0;
}
void PrintException(std::ostream& os, const std::exception& ex) {
LogError(ex.what(), os);
if (IsCLIError(ex.what())) {
os << "\n";
PrintHelp(os);
}
}
void PrintHelp(std::ostream& os) {
os << "SysY Compiler\n"
<< "\n"
<< "用法:\n"
<< " compiler [--help] [--emit-parse-tree] [--emit-ir] [--emit-asm] <input.sy>\n"
<< "\n"
<< "选项:\n"
<< " -h, --help 打印帮助信息并退出\n"
<< " --emit-parse-tree 仅在显式模式下启用语法树输出\n"
<< " --emit-ir 仅在显式模式下启用 IR 输出\n"
<< " --emit-asm 仅在显式模式下启用 AArch64 汇编输出\n"
<< "\n"
<< "说明:\n"
<< " - 默认输出 IR\n"
<< " - 若使用 --emit-parse-tree/--emit-ir/--emit-asm则仅输出显式选择的阶段\n"
<< " - 可使用重定向写入文件:\n"
<< " compiler --emit-asm test/test_case/functional/simple_add.sy > out.s\n";
}