1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v.pref: support v -trace-calls run file.v (#15450)

This commit is contained in:
Larpon
2022-08-18 10:45:37 +02:00
committed by GitHub
parent d3307fd2e3
commit af8bd10761
7 changed files with 124 additions and 1 deletions

View File

@@ -226,6 +226,7 @@ pub fn (mut t Transformer) stmt(mut node ast.Stmt) ast.Stmt {
}
}
ast.FnDecl {
t.fn_decl(mut node)
t.index.indent(true)
for mut stmt in node.stmts {
stmt = t.stmt(mut stmt)
@@ -1034,3 +1035,51 @@ pub fn (mut t Transformer) sql_expr(mut node ast.SqlExpr) ast.Expr {
}
return node
}
// fn_decl mutates `node`.
// if `pref.trace_calls` is true ast Nodes for `eprintln(...)` is prepended to the `FnDecl`'s
// stmts list to let the gen backend generate the target specific code for the print.
pub fn (mut t Transformer) fn_decl(mut node ast.FnDecl) {
if t.pref.trace_calls {
// Skip `C.fn()` and all of builtin
// builtin could probably be traced also but would need
// special cases for, at least, println/eprintln
if node.no_body || node.is_builtin {
return
}
call_expr := t.gen_trace_print_call_expr(node)
expr_stmt := ast.ExprStmt{
expr: call_expr
}
node.stmts.prepend(expr_stmt)
}
}
// gen_trace_print_expr_stmt generates an ast.CallExpr representation of a
// `eprint(...)` V code statement.
fn (t Transformer) gen_trace_print_call_expr(node ast.FnDecl) ast.CallExpr {
print_str := '> trace ' + node.stringify(t.table, node.mod, map[string]string{})
call_arg := ast.CallArg{
expr: ast.StringLiteral{
val: print_str
}
typ: ast.string_type_idx
}
args := [call_arg]
fn_name := 'eprintln'
call_expr := ast.CallExpr{
name: fn_name
args: args
mod: node.mod
pos: node.pos
language: node.language
scope: node.scope
comments: [ast.Comment{
text: 'fn $node.short_name trace call'
}]
}
return call_expr
}