mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-02 16:05:49 +01:00
Improved trace output selection using program phase filtering (#2851)
By implementing these improvements, users will have the ability to choose specific parts of the trace output. Currently, when executing a file using the explorer with the --trace_file=- or --trace_file=filename.txt flag, the resulting output is an extensive and verbose log containing all the information. In this PR, I have introduced the `ProgramPhase` enum class, which have distinct phases encountered during the compilation of a program in the explorer. Each member of this enum class corresponds to a specific phase, signifying the relevant information to be included in the trace output. The phases covered by the `ProgramPhase` enum class are as follows: 1. Printing the source program 2. Name resolution 3. Control flow resolution 4. Type checking 5. Unformed variable resolution 6. Printing declarations 7. Printing the timings 8. Printing whole output. These phases can be selected by passing the following compiler flags along with `--trace_file=-`. `-trace_source_program`, `-trace_name_resolution`, `-trace_control_flow_resolution`, `-trace_type_checking`, `-trace_unformed_variables_resolution`, `-trace_declarations`, `-trace_execution`, `-trace_timing` and `-trace_all`. If none of these flags is passed only execution trace will be added to the output. Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Richard Smith
parent
9eb040d1c0
commit
04d9a7b533
@@ -7,8 +7,10 @@
|
||||
#include <variant>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "common/error.h"
|
||||
#include "common/ostream.h"
|
||||
#include "explorer/common/arena.h"
|
||||
#include "explorer/common/trace_stream.h"
|
||||
#include "explorer/interpreter/interpreter.h"
|
||||
#include "explorer/interpreter/resolve_control_flow.h"
|
||||
#include "explorer/interpreter/resolve_names.h"
|
||||
@@ -21,6 +23,7 @@ namespace Carbon {
|
||||
auto AnalyzeProgram(Nonnull<Arena*> arena, AST ast,
|
||||
Nonnull<TraceStream*> trace_stream,
|
||||
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<AST> {
|
||||
trace_stream->set_current_phase(ProgramPhase::SourceProgram);
|
||||
if (trace_stream->is_enabled()) {
|
||||
*trace_stream << "********** source program **********\n";
|
||||
for (int i = ast.num_prelude_declarations;
|
||||
@@ -28,33 +31,39 @@ auto AnalyzeProgram(Nonnull<Arena*> arena, AST ast,
|
||||
*trace_stream << *ast.declarations[i];
|
||||
}
|
||||
}
|
||||
|
||||
SourceLocation source_loc("<Main()>", 0);
|
||||
ast.main_call = arena->New<CallExpression>(
|
||||
source_loc, arena->New<IdentifierExpression>(source_loc, "Main"),
|
||||
arena->New<TupleLiteral>(source_loc));
|
||||
// Although name resolution is currently done once, generic programming
|
||||
// (particularly templates) may require more passes.
|
||||
trace_stream->set_current_phase(ProgramPhase::NameResolution);
|
||||
if (trace_stream->is_enabled()) {
|
||||
*trace_stream << "********** resolving names **********\n";
|
||||
}
|
||||
CARBON_RETURN_IF_ERROR(ResolveNames(ast));
|
||||
|
||||
trace_stream->set_current_phase(ProgramPhase::ControlFlowResolution);
|
||||
if (trace_stream->is_enabled()) {
|
||||
*trace_stream << "********** resolving control flow **********\n";
|
||||
}
|
||||
CARBON_RETURN_IF_ERROR(ResolveControlFlow(ast));
|
||||
|
||||
trace_stream->set_current_phase(ProgramPhase::TypeChecking);
|
||||
if (trace_stream->is_enabled()) {
|
||||
*trace_stream << "********** type checking **********\n";
|
||||
}
|
||||
CARBON_RETURN_IF_ERROR(
|
||||
TypeChecker(arena, trace_stream, print_stream).TypeCheck(ast));
|
||||
|
||||
trace_stream->set_current_phase(ProgramPhase::UnformedVariableResolution);
|
||||
if (trace_stream->is_enabled()) {
|
||||
*trace_stream << "********** resolving unformed variables **********\n";
|
||||
}
|
||||
CARBON_RETURN_IF_ERROR(ResolveUnformed(ast));
|
||||
|
||||
trace_stream->set_current_phase(ProgramPhase::Declarations);
|
||||
if (trace_stream->is_enabled()) {
|
||||
*trace_stream << "********** printing declarations **********\n";
|
||||
for (int i = ast.num_prelude_declarations;
|
||||
@@ -62,16 +71,25 @@ auto AnalyzeProgram(Nonnull<Arena*> arena, AST ast,
|
||||
*trace_stream << *ast.declarations[i];
|
||||
}
|
||||
}
|
||||
trace_stream->set_current_phase(ProgramPhase::Unknown);
|
||||
return ast;
|
||||
}
|
||||
|
||||
auto ExecProgram(Nonnull<Arena*> arena, AST ast,
|
||||
Nonnull<TraceStream*> trace_stream,
|
||||
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<int> {
|
||||
trace_stream->set_current_phase(ProgramPhase::Execution);
|
||||
if (trace_stream->is_enabled()) {
|
||||
*trace_stream << "********** starting execution **********\n";
|
||||
}
|
||||
return InterpProgram(ast, arena, trace_stream, print_stream);
|
||||
CARBON_ASSIGN_OR_RETURN(
|
||||
auto interpreter_result,
|
||||
InterpProgram(ast, arena, trace_stream, print_stream));
|
||||
if (trace_stream->is_enabled()) {
|
||||
*trace_stream << "interpreter result: " << interpreter_result << "\n";
|
||||
}
|
||||
trace_stream->set_current_phase(ProgramPhase::Unknown);
|
||||
return interpreter_result;
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
Reference in New Issue
Block a user