Browse Source

Improve error bounds for `check_comparison`

gingerBill 2 years ago
parent
commit
6a6d7701f9
3 changed files with 14 additions and 8 deletions
  1. 3 3
      src/check_expr.cpp
  2. 4 4
      src/check_stmt.cpp
  3. 7 1
      src/error.cpp

+ 3 - 3
src/check_expr.cpp

@@ -2397,7 +2397,7 @@ gb_internal void add_comparison_procedures_for_fields(CheckerContext *c, Type *t
 }
 }
 
 
 
 
-gb_internal void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) {
+gb_internal void check_comparison(CheckerContext *c, Ast *node, Operand *x, Operand *y, TokenKind op) {
 	if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
 	if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
 		bool comp = are_types_identical(x->type, y->type);
 		bool comp = are_types_identical(x->type, y->type);
 		switch (op) {
 		switch (op) {
@@ -2485,7 +2485,7 @@ gb_internal void check_comparison(CheckerContext *c, Operand *x, Operand *y, Tok
 	}
 	}
 
 
 	if (err_str != nullptr) {
 	if (err_str != nullptr) {
-		error(x->expr, "Cannot compare expression, %s", err_str);
+		error(node, "Cannot compare expression, %s", err_str);
 		x->type = t_untyped_bool;
 		x->type = t_untyped_bool;
 	} else {
 	} else {
 		if (x->mode == Addressing_Constant &&
 		if (x->mode == Addressing_Constant &&
@@ -3498,7 +3498,7 @@ gb_internal void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Typ
 
 
 
 
 	if (token_is_comparison(op.kind)) {
 	if (token_is_comparison(op.kind)) {
-		check_comparison(c, x, y, op.kind);
+		check_comparison(c, node, x, y, op.kind);
 		return;
 		return;
 	}
 	}
 
 

+ 4 - 4
src/check_stmt.cpp

@@ -978,19 +978,19 @@ gb_internal void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags
 
 
 				Operand a = lhs;
 				Operand a = lhs;
 				Operand b = rhs;
 				Operand b = rhs;
-				check_comparison(ctx, &a, &x, Token_LtEq);
+				check_comparison(ctx, expr, &a, &x, Token_LtEq);
 				if (a.mode == Addressing_Invalid) {
 				if (a.mode == Addressing_Invalid) {
 					continue;
 					continue;
 				}
 				}
 
 
-				check_comparison(ctx, &b, &x, upper_op);
+				check_comparison(ctx, expr, &b, &x, upper_op);
 				if (b.mode == Addressing_Invalid) {
 				if (b.mode == Addressing_Invalid) {
 					continue;
 					continue;
 				}
 				}
 
 
 				Operand a1 = lhs;
 				Operand a1 = lhs;
 				Operand b1 = rhs;
 				Operand b1 = rhs;
-				check_comparison(ctx, &a1, &b1, Token_LtEq);
+				check_comparison(ctx, expr, &a1, &b1, Token_LtEq);
 
 
 				add_to_seen_map(ctx, &seen, upper_op, x, lhs, rhs);
 				add_to_seen_map(ctx, &seen, upper_op, x, lhs, rhs);
 
 
@@ -1029,7 +1029,7 @@ gb_internal void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags
 
 
 					// NOTE(bill): the ordering here matters
 					// NOTE(bill): the ordering here matters
 					Operand z = y;
 					Operand z = y;
-					check_comparison(ctx, &z, &x, Token_CmpEq);
+					check_comparison(ctx, expr, &z, &x, Token_CmpEq);
 					if (z.mode == Addressing_Invalid) {
 					if (z.mode == Addressing_Invalid) {
 						continue;
 						continue;
 					}
 					}

+ 7 - 1
src/error.cpp

@@ -264,7 +264,7 @@ gb_internal bool show_error_on_line(TokenPos const &pos, TokenPos end) {
 			ELLIPSIS_PADDING = 8 // `...  ...`
 			ELLIPSIS_PADDING = 8 // `...  ...`
 		};
 		};
 
 
-		error_out("\t");
+		error_out("\n\t");
 
 
 		terminal_set_colours(TerminalStyle_Bold, TerminalColour_White);
 		terminal_set_colours(TerminalStyle_Bold, TerminalColour_White);
 
 
@@ -345,6 +345,9 @@ gb_internal void error_va(TokenPos const &pos, TokenPos end, char const *fmt, va
 	} else if (global_error_collector.prev != pos) {
 	} else if (global_error_collector.prev != pos) {
 		global_error_collector.prev = pos;
 		global_error_collector.prev = pos;
 		error_out_pos(pos);
 		error_out_pos(pos);
+		if (has_ansi_terminal_colours()) {
+			error_out_coloured("Error: ", TerminalStyle_Normal, TerminalColour_Red);
+		}
 		error_out_va(fmt, va);
 		error_out_va(fmt, va);
 		error_out("\n");
 		error_out("\n");
 		show_error_on_line(pos, end);
 		show_error_on_line(pos, end);
@@ -395,6 +398,9 @@ gb_internal void error_no_newline_va(TokenPos const &pos, char const *fmt, va_li
 	} else if (global_error_collector.prev != pos) {
 	} else if (global_error_collector.prev != pos) {
 		global_error_collector.prev = pos;
 		global_error_collector.prev = pos;
 		error_out_pos(pos);
 		error_out_pos(pos);
+		if (has_ansi_terminal_colours()) {
+			error_out_coloured("Error: ", TerminalStyle_Normal, TerminalColour_Red);
+		}
 		error_out_va(fmt, va);
 		error_out_va(fmt, va);
 	}
 	}
 	mutex_unlock(&global_error_collector.mutex);
 	mutex_unlock(&global_error_collector.mutex);