diff --git a/include/serene/context.h b/include/serene/context.h index 2e3a7e1..e42302b 100644 --- a/include/serene/context.h +++ b/include/serene/context.h @@ -32,11 +32,12 @@ #include #include #include -#include #include #include #include +#include + namespace serene { namespace reader { @@ -153,6 +154,9 @@ private: /// process to store the state SERENE_EXPORT std::unique_ptr makeSereneContext(); -}; // namespace serene +/// Terminates the serene compiler process in a thread safe manner +SERENE_EXPORT void terminate(SereneContext &ctx, int exitCode); + +} // namespace serene #endif diff --git a/src/libserene/context.cpp b/src/libserene/context.cpp index 620fa2e..93fa6f7 100644 --- a/src/libserene/context.cpp +++ b/src/libserene/context.cpp @@ -24,6 +24,7 @@ #include "serene/slir/generatable.h" #include + #include namespace serene { @@ -100,6 +101,15 @@ NSPtr SereneContext::readNamespace(std::string name, return sourceManager.readNamespace(*this, std::move(name), loc); } +void terminate(SereneContext &ctx, int exitCode) { + UNUSED(ctx); + // TODO: Since we are running in a single thread for now using exit is fine + // but we need to adjust and change it to a thread safe termination + // process later on. + // NOLINTNEXTLINE(concurrency-mt-unsafe) + std::exit(exitCode); +} + std::unique_ptr makeSereneContext() { return std::make_unique(); }; diff --git a/src/libserene/diagnostics.cpp b/src/libserene/diagnostics.cpp index 84bed4d..06e43f8 100644 --- a/src/libserene/diagnostics.cpp +++ b/src/libserene/diagnostics.cpp @@ -28,6 +28,7 @@ #include #include #include + #include namespace serene { @@ -57,7 +58,7 @@ void Diagnostic::writeColorByType(llvm::raw_ostream &os, llvm::StringRef str) { }; std::string Diagnostic::getPrefix(llvm::StringRef prefix) { - if (prefix != "") { + if (!prefix.empty()) { return prefix.str(); } @@ -104,8 +105,8 @@ void Diagnostic::print(llvm::raw_ostream &os, llvm::StringRef prefix) { s << "\n\n"; - auto &srcBuf = ctx.sourceManager.getBufferInfo(loc.start.ns); - const char *line = srcBuf.getPointerForLineNumber(loc.start.line); + const auto &srcBuf = ctx.sourceManager.getBufferInfo(loc.start.ns); + const char *line = srcBuf.getPointerForLineNumber(loc.start.line); while (*line != '\n' && line != srcBuf.buffer->getBufferEnd()) { s << *line; @@ -121,25 +122,25 @@ void Diagnostic::print(llvm::raw_ostream &os, llvm::StringRef prefix) { s << '\n'; s << "["; - if (err) { + if (err != nullptr) { writeColorByType(os, err->getErrId()); } s << "] "; writeColorByType(os, getPrefix(prefix)); s << ": "; - if (err) { + if (err != nullptr) { s << err->description << '\n'; } - if (message != "") { + if (!message.empty()) { s.changeColor(llvm::raw_ostream::Colors::YELLOW); s << "With message"; s.resetColor(); s << ": " << message << "\n"; } - if (err) { + if (err != nullptr) { s << "For more information checkout"; s.changeColor(llvm::raw_ostream::Colors::CYAN); s << " `serenec --explain "; @@ -151,6 +152,7 @@ DiagnosticEngine::DiagnosticEngine(SereneContext &ctx) : ctx(ctx), diagEngine(ctx.mlirContext.getDiagEngine()){}; void DiagnosticEngine::print(llvm::raw_ostream &os, Diagnostic &d) { + UNUSED(ctx); UNUSED(os); UNUSED(d); }; @@ -165,7 +167,7 @@ Diagnostic DiagnosticEngine::toDiagnostic(reader::LocationRange loc, void DiagnosticEngine::enqueueError(llvm::StringRef msg) { llvm::errs() << llvm::formatv("FIX ME (better emit error): {0}\n", msg); - exit(1); + terminate(ctx, 1); }; void DiagnosticEngine::emitSyntaxError(reader::LocationRange loc, @@ -174,7 +176,7 @@ void DiagnosticEngine::emitSyntaxError(reader::LocationRange loc, Diagnostic diag(ctx, loc, &e, msg); diag.print(llvm::errs(), "SyntaxError"); - exit(1); + terminate(ctx, 1); }; void DiagnosticEngine::panic(llvm::StringRef msg) { @@ -194,7 +196,7 @@ void DiagnosticEngine::panic(llvm::StringRef msg) { s << msg << "\n"; // TODO: Use a proper error code - std::exit(1); + terminate(ctx, 1); }; std::unique_ptr makeDiagnosticEngine(SereneContext &ctx) { diff --git a/src/libserene/passes/slir_lowering.cpp b/src/libserene/passes/slir_lowering.cpp index 91660f7..aff410a 100644 --- a/src/libserene/passes/slir_lowering.cpp +++ b/src/libserene/passes/slir_lowering.cpp @@ -91,7 +91,7 @@ FnOpLowering::matchAndRewrite(serene::slir::FnOp op, llvm::SmallVector arg_types; - for (auto &arg : args) { + for (const auto &arg : args) { auto attr = std::get<1>(arg).dyn_cast(); if (!attr) { diff --git a/src/libserene/reader/reader.cpp b/src/libserene/reader/reader.cpp index d5c2a56..7185044 100644 --- a/src/libserene/reader/reader.cpp +++ b/src/libserene/reader/reader.cpp @@ -167,7 +167,7 @@ exprs::Node Reader::readNumber(bool neg) { if (isdigit(*c) == 0) { ctx.diagEngine->emitSyntaxError(loc, errors::InvalidDigitForNumber); - exit(1); + terminate(ctx, 1); } for (;;) { @@ -179,7 +179,7 @@ exprs::Node Reader::readNumber(bool neg) { if (*c == '.' && floatNum) { loc = LocationRange(getCurrentLocation()); ctx.diagEngine->emitSyntaxError(loc, errors::TwoFloatPoints); - exit(1); + terminate(ctx, 1); } if (*c == '.') { @@ -196,7 +196,7 @@ exprs::Node Reader::readNumber(bool neg) { advance(); loc.start = getCurrentLocation(); ctx.diagEngine->emitSyntaxError(loc, errors::InvalidDigitForNumber); - exit(1); + terminate(ctx, 1); } loc.end = getCurrentLocation(); @@ -222,7 +222,7 @@ exprs::Node Reader::readSymbol() { ctx.diagEngine->emitSyntaxError(loc, errors::InvalidCharacterForSymbol, msg); - exit(1); + terminate(ctx, 1); } if (*c == '-') { @@ -280,7 +280,7 @@ exprs::Node Reader::readList() { list->location.end = getCurrentLocation(); ctx.diagEngine->emitSyntaxError(list->location, errors::EOFWhileScaningAList); - exit(1); + terminate(ctx, 1); } switch (*c) {