Implemented RAII Type to set current program phase (#2886)

Within `explorer/common/trace_stream.h`, implemented RAII Type `SetProgramPhase`.
This RAII type simplifies the process by automatically setting the desired program phase upon construction and restoring the previous phase upon destruction.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Prabhat Sachdeva
2023-06-13 16:19:13 -07:00
committed by GitHub
co-authored by Richard Smith
parent 202d3f5993
commit aa8cdd526f
5 changed files with 84 additions and 15 deletions
@@ -8,6 +8,7 @@
#include "common/check.h"
#include "common/error.h"
#include "explorer/common/trace_stream.h"
#include "explorer/interpreter/exec_program.h"
#include "explorer/interpreter/stack_space.h"
#include "explorer/syntax/parse.h"
@@ -24,14 +25,13 @@ static auto PrintTimingOnExit(TraceStream* trace_stream, const char* label,
auto duration = end - *cursor;
*cursor = end;
auto exit_scope_function = llvm::make_scope_exit([=]() {
trace_stream->set_current_phase(ProgramPhase::Timing);
SetProgramPhase set_program_phase(*trace_stream, ProgramPhase::Timing);
if (trace_stream->is_enabled()) {
*trace_stream << "Time elapsed in " << label << ": "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
duration)
.count()
<< "ms\n";
trace_stream->set_current_phase(ProgramPhase::Unknown);
}
});
return exit_scope_function;
@@ -67,7 +67,6 @@ static auto ParseAndExecuteHelper(std::function<ErrorOr<AST>(Arena*)> parse,
}
// Run the program.
trace_stream->set_current_phase(ProgramPhase::Execution);
ErrorOr<int> exec_result =
ExecProgram(&arena, *analyze_result, trace_stream, print_stream);
auto print_exec_time =
@@ -76,14 +75,12 @@ static auto ParseAndExecuteHelper(std::function<ErrorOr<AST>(Arena*)> parse,
if (!exec_result.ok()) {
return ErrorBuilder() << "RUNTIME ERROR: " << exec_result.error();
}
trace_stream->set_current_phase(ProgramPhase::Unknown);
auto print_trace_timing_heading = llvm::make_scope_exit([=]() {
trace_stream->set_current_phase(ProgramPhase::Timing);
SetProgramPhase set_prog_phase(*trace_stream, ProgramPhase::Timing);
if (trace_stream->is_enabled()) {
*trace_stream << "********** printing timing **********\n";
}
trace_stream->set_current_phase(ProgramPhase::Unknown);
});
return exec_result;