From 1af5ab6d4e791b1beb5ce30b57b7c35cf96d8a25 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 14 Feb 2024 00:19:16 -0500 Subject: Add #line directives for source code mapping --- compile.c | 9 +++++++-- nextlang.c | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/compile.c b/compile.c index 4611305e..6da984ea 100644 --- a/compile.c +++ b/compile.c @@ -19,13 +19,18 @@ CORD compile_type(env_t *env, type_ast_t *t) CORD compile_statement(env_t *env, ast_t *ast) { + CORD stmt; switch (ast->tag) { case If: case For: case While: case FunctionDef: case Return: case StructDef: case EnumDef: case Declare: case Assign: case UpdateAssign: case DocTest: - return compile(env, ast); + stmt = compile(env, ast); + break; default: - return CORD_asprintf("(void)%r;", compile(env, ast)); + stmt = CORD_asprintf("(void)%r;", compile(env, ast)); + break; } + int64_t line = get_line_number(ast->file, ast->start); + return stmt ? CORD_asprintf("#line %ld\n%r", line, stmt) : stmt; } CORD compile(env_t *env, ast_t *ast) diff --git a/nextlang.c b/nextlang.c index 7a0f4e3a..98e54ebb 100644 --- a/nextlang.c +++ b/nextlang.c @@ -55,6 +55,7 @@ int main(int argc, char *argv[]) } CORD program = CORD_asprintf( + "#line 0 \"%s\"\n" // file "// Generated code:\n" "%r\n" // imports "%r\n" // typedefs @@ -74,6 +75,7 @@ int main(int argc, char *argv[]) "__load();\n" "return 0;\n" "}\n", + f->filename, env.imports, env.typedefs, env.types, env.staticdefs, env.funcs, env.main); -- cgit v1.2.3