瀏覽代碼

Remove transmute suggestion with `-vet-cast` when transmuting native <-> endian-specific types

gingerBill 7 月之前
父節點
當前提交
f80bea5b11
共有 2 個文件被更改,包括 23 次插入1 次删除
  1. 2 1
      src/check_expr.cpp
  2. 21 0
      src/types.cpp

+ 2 - 1
src/check_expr.cpp

@@ -3649,7 +3649,8 @@ gb_internal bool check_transmute(CheckerContext *c, Ast *node, Operand *o, Type
 				gb_string_free(oper_str);
 				gb_string_free(to_type);
 			} else if (is_type_integer(src_t) && is_type_integer(dst_t) &&
-			           types_have_same_internal_endian(src_t, dst_t)) {
+			           types_have_same_internal_endian(src_t, dst_t) &&
+			           type_endian_kind_of(src_t) == type_endian_kind_of(dst_t)) {
 				gbString oper_type = type_to_string(src_t);
 				gbString to_type   = type_to_string(dst_t);
 				error(o->expr, "Use of 'transmute' where 'cast' would be preferred since both are integers of the same endianness, from '%s' to '%s'", oper_type, to_type);

+ 21 - 0
src/types.cpp

@@ -1801,6 +1801,27 @@ gb_internal bool is_type_union_maybe_pointer_original_alignment(Type *t) {
 }
 
 
+enum TypeEndianKind {
+	TypeEndian_Platform,
+	TypeEndian_Little,
+	TypeEndian_Big,
+};
+
+gb_internal TypeEndianKind type_endian_kind_of(Type *t) {
+	t = core_type(t);
+	if (t->kind == Type_Basic) {
+		if (t->Basic.flags & BasicFlag_EndianLittle) {
+			return TypeEndian_Little;
+		}
+		if (t->Basic.flags & BasicFlag_EndianBig) {
+			return TypeEndian_Big;
+		}
+	} else if (t->kind == Type_BitSet) {
+		return type_endian_kind_of(bit_set_to_int(t));
+	}
+	return TypeEndian_Platform;
+}
+
 
 gb_internal bool is_type_endian_big(Type *t) {
 	t = core_type(t);