Commit cb6bc5bdb5

foobles <buzdav2@gmail.com>
2020-05-26 18:55:31
Add caller location tracking for asserts (ir_assert, src_assert, ir_assert_gen) (#5393)
1 parent 57b78ff
src/analyze.cpp
@@ -9418,15 +9418,16 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {
     return type->llvm_di_type;
 }
 
-void src_assert(bool ok, AstNode *source_node) {
+void src_assert_impl(bool ok, AstNode *source_node, char const *file, unsigned int line) {
     if (ok) return;
     if (source_node == nullptr) {
-        fprintf(stderr, "when analyzing (unknown source location): ");
+        fprintf(stderr, "when analyzing (unknown source location) ");
     } else {
-        fprintf(stderr, "when analyzing %s:%u:%u: ",
+        fprintf(stderr, "when analyzing %s:%u:%u ",
             buf_ptr(source_node->owner->data.structure.root_struct->path),
             (unsigned)source_node->line + 1, (unsigned)source_node->column + 1);
     }
+    fprintf(stderr, "in compiler source at %s:%u: ", file, line);
     const char *msg = "assertion failed. This is a bug in the Zig compiler.";
     stage2_panic(msg, strlen(msg));
 }
src/analyze.hpp
@@ -260,7 +260,7 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);
 void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_path, bool translate_c,
         FileExt source_kind);
 
-void src_assert(bool ok, AstNode *source_node);
+void src_assert_impl(bool ok, AstNode *source_node, const char *file, unsigned int line);
 bool is_container(ZigType *type_entry);
 ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
         Buf *type_name, UndefAllowed undef);
@@ -290,4 +290,7 @@ bool type_has_optional_repr(ZigType *ty);
 bool is_opt_err_set(ZigType *ty);
 bool type_is_numeric(ZigType *ty);
 const char *float_op_to_name(BuiltinFnId op);
+
+#define src_assert(OK, SOURCE_NODE) src_assert_impl((OK), (SOURCE_NODE), __FILE__, __LINE__)
+
 #endif
src/codegen.cpp
@@ -870,11 +870,13 @@ static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, ZigType *type
     }
 }
 
-static void ir_assert(bool ok, IrInstGen *source_instruction) {
+static void ir_assert_impl(bool ok, IrInstGen *source_instruction, const char *file, unsigned int line) {
     if (ok) return;
-    src_assert(ok, source_instruction->base.source_node);
+    src_assert_impl(ok, source_instruction->base.source_node, file, line);
 }
 
+#define ir_assert(OK, SOURCE_INSTRUCTION) ir_assert_impl((OK), (SOURCE_INSTRUCTION), __FILE__, __LINE__)
+
 static bool ir_want_fast_math(CodeGen *g, IrInstGen *instruction) {
     // TODO memoize
     Scope *scope = instruction->base.scope;
src/ir.cpp
@@ -224,8 +224,8 @@ static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutableSrc *exec, As
 static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
     IrInst* source_instr, IrInstGen *container_ptr, IrInst *container_ptr_src,
     ZigType *container_type, bool initializing);
-static void ir_assert(bool ok, IrInst* source_instruction);
-static void ir_assert_gen(bool ok, IrInstGen *source_instruction);
+static void ir_assert_impl(bool ok, IrInst* source_instruction, const char *file, unsigned int line);
+static void ir_assert_gen_impl(bool ok, IrInstGen *source_instruction, const char *file, unsigned int line);
 static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *var);
 static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstGen *op);
 static IrInstSrc *ir_lval_wrap(IrBuilderSrc *irb, Scope *scope, IrInstSrc *value, LVal lval, ResultLoc *result_loc);
@@ -286,6 +286,9 @@ static IrInstGen *ir_analyze_struct_value_field_value(IrAnalyze *ira, IrInst* so
 static bool value_cmp_numeric_val_any(ZigValue *left, Cmp predicate, ZigValue *right);
 static bool value_cmp_numeric_val_all(ZigValue *left, Cmp predicate, ZigValue *right);
 
+#define ir_assert(OK, SOURCE_INSTRUCTION) ir_assert_impl((OK), (SOURCE_INSTRUCTION), __FILE__, __LINE__)
+#define ir_assert_gen(OK, SOURCE_INSTRUCTION) ir_assert_gen_impl((OK), (SOURCE_INSTRUCTION), __FILE__, __LINE__)
+
 static void destroy_instruction_src(IrInstSrc *inst) {
     switch (inst->id) {
         case IrInstSrcIdInvalid:
@@ -10316,14 +10319,14 @@ static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInst *source_instruction, Buf *m
     return ir_add_error_node(ira, source_instruction->source_node, msg);
 }
 
-static void ir_assert(bool ok, IrInst *source_instruction) {
+static void ir_assert_impl(bool ok, IrInst *source_instruction, char const *file, unsigned int line) {
     if (ok) return;
-    src_assert(ok, source_instruction->source_node);
+    src_assert_impl(ok, source_instruction->source_node, file, line);
 }
 
-static void ir_assert_gen(bool ok, IrInstGen *source_instruction) {
+static void ir_assert_gen_impl(bool ok, IrInstGen *source_instruction, char const *file, unsigned int line) {
     if (ok) return;
-    src_assert(ok, source_instruction->base.source_node);
+    src_assert_impl(ok, source_instruction->base.source_node, file, line);
 }
 
 // This function takes a comptime ptr and makes the child const value conform to the type