Commit 113f0c9450

Andrew Kelley <superjoe30@gmail.com>
2015-12-08 07:09:48
add local variables to debug info
1 parent 9c9ea93
src/codegen.cpp
@@ -528,7 +528,15 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
                     return nullptr;
                 } else {
                     add_debug_source_node(g, node);
-                    return LLVMBuildStore(g->builder, value, variable->value_ref);
+                    variable->value_ref = LLVMBuildAlloca(g->builder,
+                            variable->type->type_ref, buf_ptr(&variable->name));
+                    LLVMValueRef store_instr = LLVMBuildStore(g->builder, value, variable->value_ref);
+
+                    LLVMZigDILocation *debug_loc = LLVMZigGetDebugLoc(node->line + 1, node->column + 1,
+                            g->cur_block_context->di_scope);
+                    LLVMZigInsertDeclare(g->dbuilder, variable->value_ref, variable->di_loc_var,
+                            debug_loc, store_instr);
+                    return nullptr;
                 }
             }
         case NodeTypeCastExpr:
@@ -577,6 +585,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
                 if (variable->type == g->builtin_types.entry_void) {
                     return nullptr;
                 } else if (variable->is_ptr) {
+                    add_debug_source_node(g, node);
                     return LLVMBuildLoad(g->builder, variable->value_ref, "");
                 } else {
                     return variable->value_ref;
@@ -770,9 +779,6 @@ static void do_code_gen(CodeGen *g) {
                 } else {
                     tag = LLVMZigTag_DW_auto_variable();
                     arg_no = 0;
-
-                    add_debug_source_node(g, var->decl_node);
-                    var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
                 }
 
                 var->di_loc_var = LLVMZigCreateLocalVariable(g->dbuilder, tag,
src/zig_llvm.cpp
@@ -304,6 +304,24 @@ void LLVMZigRestoreInsertPoint(LLVMBuilderRef builder, LLVMZigInsertionPoint *ip
     unwrap(builder)->restoreIP(*ip);
 }
 
+
+LLVMValueRef LLVMZigInsertDeclare(LLVMZigDIBuilder *dibuilder, LLVMValueRef storage,
+        LLVMZigDILocalVariable *var_info, LLVMZigDILocation *debug_loc, LLVMValueRef insert_before_instr)
+{
+    Instruction *result = reinterpret_cast<DIBuilder*>(dibuilder)->insertDeclare(
+            unwrap(storage),
+            reinterpret_cast<DILocalVariable *>(var_info),
+            reinterpret_cast<DIBuilder*>(dibuilder)->createExpression(),
+            reinterpret_cast<DILocation*>(debug_loc),
+            static_cast<Instruction*>(unwrap(insert_before_instr)));
+    return wrap(result);
+}
+
+LLVMZigDILocation *LLVMZigGetDebugLoc(unsigned line, unsigned col, LLVMZigDIScope *scope) {
+    DebugLoc debug_loc = DebugLoc::get(line, col, reinterpret_cast<DIScope*>(scope), nullptr);
+    return reinterpret_cast<LLVMZigDILocation*>(debug_loc.get());
+}
+
 //------------------------------------
 
 enum FloatAbi {
src/zig_llvm.hpp
@@ -23,6 +23,7 @@ struct LLVMZigDILexicalBlock;
 struct LLVMZigDISubprogram;
 struct LLVMZigDISubroutineType;
 struct LLVMZigDILocalVariable;
+struct LLVMZigDILocation;
 struct LLVMZigInsertionPoint;
 
 void LLVMZigInitializeLoopStrengthReducePass(LLVMPassRegistryRef R);
@@ -91,6 +92,10 @@ void LLVMZigDIBuilderFinalize(LLVMZigDIBuilder *dibuilder);
 LLVMZigInsertionPoint *LLVMZigSaveInsertPoint(LLVMBuilderRef builder);
 void LLVMZigRestoreInsertPoint(LLVMBuilderRef builder, LLVMZigInsertionPoint *point);
 
+LLVMValueRef LLVMZigInsertDeclare(LLVMZigDIBuilder *dibuilder, LLVMValueRef storage,
+        LLVMZigDILocalVariable *var_info, LLVMZigDILocation *debug_loc, LLVMValueRef insert_before_instr);
+LLVMZigDILocation *LLVMZigGetDebugLoc(unsigned line, unsigned col, LLVMZigDIScope *scope);
+
 
 /*
  * This stuff is not LLVM API but it depends on the LLVM C++ API so we put it here.