authorgravatar for buzdav2@gmail.comfoobles <buzdav2@gmail.com> 2020-05-26 11:55:31-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-26 12:55:31-04:00
logcb6bc5bdb5a3ebc95b0ee5fa414c4a2d4449e172
treeb1db09ec9d0e5be585ccd93da6431c965a3cbb94
parent57b78fff7366c134bfb56084ec353006a7cb39fc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Add caller location tracking for asserts (ir_assert, src_assert, ir_assert_gen) (#5393)


4 files changed, 21 insertions(+), 12 deletions(-)

src/analyze.cpp+4-3
...@@ -9418,15 +9418,16 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {...@@ -9418,15 +9418,16 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {
9418 return type->llvm_di_type;9418 return type->llvm_di_type;
9419}9419}
94209420
9421void src_assert(bool ok, AstNode *source_node) {9421void src_assert_impl(bool ok, AstNode *source_node, char const *file, unsigned int line) {
9422 if (ok) return;9422 if (ok) return;
9423 if (source_node == nullptr) {9423 if (source_node == nullptr) {
9424 fprintf(stderr, "when analyzing (unknown source location): ");9424 fprintf(stderr, "when analyzing (unknown source location) ");
9425 } else {9425 } else {
9426 fprintf(stderr, "when analyzing %s:%u:%u: ",9426 fprintf(stderr, "when analyzing %s:%u:%u ",
9427 buf_ptr(source_node->owner->data.structure.root_struct->path),9427 buf_ptr(source_node->owner->data.structure.root_struct->path),
9428 (unsigned)source_node->line + 1, (unsigned)source_node->column + 1);9428 (unsigned)source_node->line + 1, (unsigned)source_node->column + 1);
9429 }9429 }
9430 fprintf(stderr, "in compiler source at %s:%u: ", file, line);
9430 const char *msg = "assertion failed. This is a bug in the Zig compiler.";9431 const char *msg = "assertion failed. This is a bug in the Zig compiler.";
9431 stage2_panic(msg, strlen(msg));9432 stage2_panic(msg, strlen(msg));
9432}9433}
src/analyze.hpp+4-1
...@@ -260,7 +260,7 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);...@@ -260,7 +260,7 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);
260void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_path, bool translate_c,260void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_path, bool translate_c,
261 FileExt source_kind);261 FileExt source_kind);
262262
263void src_assert(bool ok, AstNode *source_node);263void src_assert_impl(bool ok, AstNode *source_node, const char *file, unsigned int line);
264bool is_container(ZigType *type_entry);264bool is_container(ZigType *type_entry);
265ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,265ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
266 Buf *type_name, UndefAllowed undef);266 Buf *type_name, UndefAllowed undef);
...@@ -290,4 +290,7 @@ bool type_has_optional_repr(ZigType *ty);...@@ -290,4 +290,7 @@ bool type_has_optional_repr(ZigType *ty);
290bool is_opt_err_set(ZigType *ty);290bool is_opt_err_set(ZigType *ty);
291bool type_is_numeric(ZigType *ty);291bool type_is_numeric(ZigType *ty);
292const char *float_op_to_name(BuiltinFnId op);292const char *float_op_to_name(BuiltinFnId op);
293
294#define src_assert(OK, SOURCE_NODE) src_assert_impl((OK), (SOURCE_NODE), __FILE__, __LINE__)
295
293#endif296#endif
src/codegen.cpp+4-2
...@@ -870,11 +870,13 @@ static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, ZigType *type...@@ -870,11 +870,13 @@ static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, ZigType *type
870 }870 }
871}871}
872872
873static void ir_assert(bool ok, IrInstGen *source_instruction) {873static void ir_assert_impl(bool ok, IrInstGen *source_instruction, const char *file, unsigned int line) {
874 if (ok) return;874 if (ok) return;
875 src_assert(ok, source_instruction->base.source_node);875 src_assert_impl(ok, source_instruction->base.source_node, file, line);
876}876}
877877
878#define ir_assert(OK, SOURCE_INSTRUCTION) ir_assert_impl((OK), (SOURCE_INSTRUCTION), __FILE__, __LINE__)
879
878static bool ir_want_fast_math(CodeGen *g, IrInstGen *instruction) {880static bool ir_want_fast_math(CodeGen *g, IrInstGen *instruction) {
879 // TODO memoize881 // TODO memoize
880 Scope *scope = instruction->base.scope;882 Scope *scope = instruction->base.scope;
src/ir.cpp+9-6
...@@ -224,8 +224,8 @@ static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutableSrc *exec, As...@@ -224,8 +224,8 @@ static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutableSrc *exec, As
224static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,224static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
225 IrInst* source_instr, IrInstGen *container_ptr, IrInst *container_ptr_src,225 IrInst* source_instr, IrInstGen *container_ptr, IrInst *container_ptr_src,
226 ZigType *container_type, bool initializing);226 ZigType *container_type, bool initializing);
227static void ir_assert(bool ok, IrInst* source_instruction);227static void ir_assert_impl(bool ok, IrInst* source_instruction, const char *file, unsigned int line);
228static void ir_assert_gen(bool ok, IrInstGen *source_instruction);228static void ir_assert_gen_impl(bool ok, IrInstGen *source_instruction, const char *file, unsigned int line);
229static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *var);229static IrInstGen *ir_get_var_ptr(IrAnalyze *ira, IrInst *source_instr, ZigVar *var);
230static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstGen *op);230static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstGen *op);
231static IrInstSrc *ir_lval_wrap(IrBuilderSrc *irb, Scope *scope, IrInstSrc *value, LVal lval, ResultLoc *result_loc);231static 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...@@ -286,6 +286,9 @@ static IrInstGen *ir_analyze_struct_value_field_value(IrAnalyze *ira, IrInst* so
286static bool value_cmp_numeric_val_any(ZigValue *left, Cmp predicate, ZigValue *right);286static bool value_cmp_numeric_val_any(ZigValue *left, Cmp predicate, ZigValue *right);
287static bool value_cmp_numeric_val_all(ZigValue *left, Cmp predicate, ZigValue *right);287static bool value_cmp_numeric_val_all(ZigValue *left, Cmp predicate, ZigValue *right);
288288
289#define ir_assert(OK, SOURCE_INSTRUCTION) ir_assert_impl((OK), (SOURCE_INSTRUCTION), __FILE__, __LINE__)
290#define ir_assert_gen(OK, SOURCE_INSTRUCTION) ir_assert_gen_impl((OK), (SOURCE_INSTRUCTION), __FILE__, __LINE__)
291
289static void destroy_instruction_src(IrInstSrc *inst) {292static void destroy_instruction_src(IrInstSrc *inst) {
290 switch (inst->id) {293 switch (inst->id) {
291 case IrInstSrcIdInvalid:294 case IrInstSrcIdInvalid:
...@@ -10316,14 +10319,14 @@ static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInst *source_instruction, Buf *m...@@ -10316,14 +10319,14 @@ static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInst *source_instruction, Buf *m
10316 return ir_add_error_node(ira, source_instruction->source_node, msg);10319 return ir_add_error_node(ira, source_instruction->source_node, msg);
10317}10320}
1031810321
10319static void ir_assert(bool ok, IrInst *source_instruction) {10322static void ir_assert_impl(bool ok, IrInst *source_instruction, char const *file, unsigned int line) {
10320 if (ok) return;10323 if (ok) return;
10321 src_assert(ok, source_instruction->source_node);10324 src_assert_impl(ok, source_instruction->source_node, file, line);
10322}10325}
1032310326
10324static void ir_assert_gen(bool ok, IrInstGen *source_instruction) {10327static void ir_assert_gen_impl(bool ok, IrInstGen *source_instruction, char const *file, unsigned int line) {
10325 if (ok) return;10328 if (ok) return;
10326 src_assert(ok, source_instruction->base.source_node);10329 src_assert_impl(ok, source_instruction->base.source_node, file, line);
10327}10330}
1032810331
10329// This function takes a comptime ptr and makes the child const value conform to the type10332// This function takes a comptime ptr and makes the child const value conform to the type