Trace output filtering based on file context (#2916)

To filter based on file contexts, you can use `-trace_file_context=...`
flag along with `--trace_file=...` flag. These are the following options
you can pass to the `-trace_file_context=...` flag (you can also pass
them as list by separating them with comma).
1. `main`: Include trace output for file containing the main function.
2. `prelude`: Include trace output for prelude.
3. `import`: Include trace output for imports.
4. `include`: Include trace output for all.

If the flag isn't used or non of the options are passed, by default only
main file context is traced.

File contexts are distinguished based upon the source location by
passing the source location to `TraceStream::is_enabled(..)` as
arguments. If no arguments are passed, file context is
`FileContext::Unknown` and for now it will add the trace from unknown
file context.

Note: `import` option currently doesn't work as imports aren't supported
yet.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Prabhat Sachdeva
2023-07-07 22:17:34 +00:00
committed by GitHub
co-authored by Richard Smith
parent 037196f69f
commit c0d18a62eb
14 changed files with 291 additions and 1213 deletions
+6 -3
View File
@@ -24,6 +24,7 @@ auto AnalyzeProgram(Nonnull<Arena*> arena, AST ast,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<AST> {
SetProgramPhase set_prog_phase(*trace_stream, ProgramPhase::SourceProgram);
SetFileContext set_file_ctx(*trace_stream, std::nullopt);
if (trace_stream->is_enabled()) {
*trace_stream << "********** source program **********\n";
@@ -68,9 +69,11 @@ auto AnalyzeProgram(Nonnull<Arena*> arena, AST ast,
set_prog_phase.update_phase(ProgramPhase::Declarations);
if (trace_stream->is_enabled()) {
*trace_stream << "********** printing declarations **********\n";
for (int i = ast.num_prelude_declarations;
i < static_cast<int>(ast.declarations.size()); ++i) {
*trace_stream << *ast.declarations[i];
for (auto& declaration : ast.declarations) {
set_file_ctx.update_source_loc(declaration->source_loc());
if (trace_stream->is_enabled()) {
*trace_stream << *declaration;
}
}
}
return ast;