From 6c33c4c56fd8d4dd9c126c23795ae58626de9264 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com> Date: Sun, 14 Jul 2024 10:58:45 +0300 Subject: [PATCH] Type: print _BitInt size in diagnostics --- src/aro/Diagnostics/messages.def | 13 +++++++++---- src/aro/Parser.zig | 11 ----------- src/aro/Type.zig | 24 +++++++++--------------- test/cases/_BitInt change size.c | 5 +++++ test/cases/_BitInt.c | 4 ++-- test/cases/array of invalid.c | 4 ++-- test/cases/constexpr.c | 2 +- 7 files changed, 28 insertions(+), 35 deletions(-) create mode 100644 test/cases/_BitInt change size.c diff --git a/src/aro/Diagnostics/messages.def b/src/aro/Diagnostics/messages.def index 13467ba9..119e72a2 100644 --- a/src/aro/Diagnostics/messages.def +++ b/src/aro/Diagnostics/messages.def @@ -2058,17 +2058,22 @@ bit_int .suppress_version = .c23 unsigned_bit_int_too_small - .msg = "{s} must have a bit size of at least 1" + .msg = "{s}unsigned _BitInt must have a bit size of at least 1" .extra = .str .kind = .@"error" signed_bit_int_too_small - .msg = "{s} must have a bit size of at least 2" + .msg = "{s}signed _BitInt must have a bit size of at least 2" .extra = .str .kind = .@"error" -bit_int_too_big - .msg = "{s} of bit sizes greater than " ++ std.fmt.comptimePrint("{d}", .{Properties.max_bits}) ++ " not supported" +unsigned_bit_int_too_big + .msg = "{s}unsigned _BitInt of bit sizes greater than " ++ std.fmt.comptimePrint("{d}", .{Properties.max_bits}) ++ " not supported" + .extra = .str + .kind = .@"error" + +signed_bit_int_too_big + .msg = "{s}signed _BitInt of bit sizes greater than " ++ std.fmt.comptimePrint("{d}", .{Properties.max_bits}) ++ " not supported" .extra = .str .kind = .@"error" diff --git a/src/aro/Parser.zig b/src/aro/Parser.zig index dbc9dad4..270d0a33 100644 --- a/src/aro/Parser.zig +++ b/src/aro/Parser.zig @@ -8505,17 +8505,6 @@ fn bitInt(p: *Parser, base: u8, buf: []const u8, suffix: NumberSuffix, tok_i: To // value of the constant is positive or was specified in hexadecimal or octal notation. const sign_bits = @intFromBool(suffix.isSignedInteger()); const bits_needed = count + sign_bits; - if (bits_needed > Compilation.bit_int_max_bits) { - const specifier: Type.Builder.Specifier = switch (suffix) { - .WB => .{ .bit_int = 0 }, - .UWB => .{ .ubit_int = 0 }, - .IWB => .{ .complex_bit_int = 0 }, - .IUWB => .{ .complex_ubit_int = 0 }, - else => unreachable, - }; - try p.errStr(.bit_int_too_big, tok_i, specifier.str(p.comp.langopts).?); - return error.ParsingFailed; - } break :blk @intCast(bits_needed); }; diff --git a/src/aro/Type.zig b/src/aro/Type.zig index 92804fc8..f48560c0 100644 --- a/src/aro/Type.zig +++ b/src/aro/Type.zig @@ -1616,9 +1616,6 @@ pub const Builder = struct { .int128 => "__int128", .sint128 => "signed __int128", .uint128 => "unsigned __int128", - .bit_int => "_BitInt", - .sbit_int => "signed _BitInt", - .ubit_int => "unsigned _BitInt", .complex_char => "_Complex char", .complex_schar => "_Complex signed char", .complex_uchar => "_Complex unsigned char", @@ -1648,9 +1645,6 @@ pub const Builder = struct { .complex_int128 => "_Complex __int128", .complex_sint128 => "_Complex signed __int128", .complex_uint128 => "_Complex unsigned __int128", - .complex_bit_int => "_Complex _BitInt", - .complex_sbit_int => "_Complex signed _BitInt", - .complex_ubit_int => "_Complex unsigned _BitInt", .fp16 => "__fp16", .float16 => "_Float16", @@ -1759,19 +1753,20 @@ pub const Builder = struct { .complex_uint128 => ty.specifier = .complex_uint128, .bit_int, .sbit_int, .ubit_int, .complex_bit_int, .complex_ubit_int, .complex_sbit_int => |bits| { const unsigned = b.specifier == .ubit_int or b.specifier == .complex_ubit_int; + const complex_str = if (b.complex_tok != null) "_Complex " else ""; if (unsigned) { if (bits < 1) { - try p.errStr(.unsigned_bit_int_too_small, b.bit_int_tok.?, b.specifier.str(p.comp.langopts).?); + try p.errStr(.unsigned_bit_int_too_small, b.bit_int_tok.?, complex_str); return Type.invalid; } } else { if (bits < 2) { - try p.errStr(.signed_bit_int_too_small, b.bit_int_tok.?, b.specifier.str(p.comp.langopts).?); + try p.errStr(.signed_bit_int_too_small, b.bit_int_tok.?, complex_str); return Type.invalid; } } if (bits > Compilation.bit_int_max_bits) { - try p.errStr(.bit_int_too_big, b.bit_int_tok.?, b.specifier.str(p.comp.langopts).?); + try p.errStr(if (unsigned) .unsigned_bit_int_too_big else .signed_bit_int_too_big, b.bit_int_tok.?, complex_str); return Type.invalid; } ty.specifier = if (b.complex_tok != null) .complex_bit_int else .bit_int; @@ -2485,6 +2480,8 @@ fn printPrologue(ty: Type, mapper: StringInterner.TypeMapper, langopts: LangOpts _ = try elem_ty.printPrologue(mapper, langopts, w); try w.writeAll("' values)"); }, + .bit_int => try w.print("{s} _BitInt({d})", .{ @tagName(ty.data.int.signedness), ty.data.int.bits }), + .complex_bit_int => try w.print("_Complex {s} _BitInt({d})", .{ @tagName(ty.data.int.signedness), ty.data.int.bits }), else => try w.writeAll(Builder.fromType(ty).str(langopts).?), } return true; @@ -2643,12 +2640,9 @@ pub fn dump(ty: Type, mapper: StringInterner.TypeMapper, langopts: LangOpts, w: try ty.data.attributed.base.dump(mapper, langopts, w); try w.writeAll(")"); }, - else => { - try w.writeAll(Builder.fromType(ty).str(langopts).?); - if (ty.specifier == .bit_int or ty.specifier == .complex_bit_int) { - try w.print("({d})", .{ty.data.int.bits}); - } - }, + .bit_int => try w.print("{s} _BitInt({d})", .{ @tagName(ty.data.int.signedness), ty.data.int.bits }), + .complex_bit_int => try w.print("_Complex {s} _BitInt({d})", .{ @tagName(ty.data.int.signedness), ty.data.int.bits }), + else => try w.writeAll(Builder.fromType(ty).str(langopts).?), } } diff --git a/test/cases/_BitInt change size.c b/test/cases/_BitInt change size.c new file mode 100644 index 00000000..b926169e --- /dev/null +++ b/test/cases/_BitInt change size.c @@ -0,0 +1,5 @@ +_BitInt(10) x = 1.2; +_Complex unsigned _BitInt(10) y = 1.2; + +#define EXPECTED_ERRORS "_BitInt change size.c:1:17: warning: implicit conversion from 'double' to 'signed _BitInt(10)' changes value from 1.2 to 1 [-Wfloat-conversion]"\ + "_BitInt change size.c:2:35: warning: implicit conversion from 'double' to '_Complex unsigned _BitInt(10)' changes value from 1.2 to 1 [-Wfloat-conversion]" diff --git a/test/cases/_BitInt.c b/test/cases/_BitInt.c index f815a73d..c45d42e8 100644 --- a/test/cases/_BitInt.c +++ b/test/cases/_BitInt.c @@ -29,12 +29,12 @@ enum E: _BitInt(512) { _Static_assert(sizeof(_BitInt(65535)) == 8192, ""); #define EXPECTED_ERRORS "_BitInt.c:3:1: warning: '_BitInt' in C17 and earlier is a Clang extension' [-Wbit-int-extension]" \ - "_BitInt.c:13:1: error: _BitInt of bit sizes greater than 65535 not supported" \ + "_BitInt.c:13:1: error: signed _BitInt of bit sizes greater than 65535 not supported" \ "_BitInt.c:14:8: error: signed _BitInt must have a bit size of at least 2" \ "_BitInt.c:15:10: error: unsigned _BitInt must have a bit size of at least 1" \ "_BitInt.c:17:16: warning: '_BitInt' suffix for literals is a C23 extension [-Wc23-extensions]" \ "_BitInt.c:18:25: warning: '_BitInt' suffix for literals is a C23 extension [-Wc23-extensions]" \ "_BitInt.c:18:25: warning: '_BitInt' suffix for literals is a C23 extension [-Wc23-extensions]" \ "_BitInt.c:22:10: error: expected ';', found 'a character literal'" \ - "_BitInt.c:27:5: error: enumerator value is not representable in the underlying type '_BitInt'" \ + "_BitInt.c:27:5: error: enumerator value is not representable in the underlying type 'signed _BitInt(512)'" \ diff --git a/test/cases/array of invalid.c b/test/cases/array of invalid.c index 4133baa5..ea4a77d6 100644 --- a/test/cases/array of invalid.c +++ b/test/cases/array of invalid.c @@ -1,6 +1,6 @@ _BitInt(0) a[]; _BitInt(0) b[10]; -#define EXPECTED_ERRORS "array of invalid.c:1:1: error: _BitInt must have a bit size of at least 2" \ - "array of invalid.c:2:1: error: _BitInt must have a bit size of at least 2" \ +#define EXPECTED_ERRORS "array of invalid.c:1:1: error: signed _BitInt must have a bit size of at least 2" \ + "array of invalid.c:2:1: error: signed _BitInt must have a bit size of at least 2" \ diff --git a/test/cases/constexpr.c b/test/cases/constexpr.c index 56f7b8ef..d47b9f31 100644 --- a/test/cases/constexpr.c +++ b/test/cases/constexpr.c @@ -26,5 +26,5 @@ static_assert(!const_bool_false); #define EXPECTED_ERRORS "constexpr.c:5:14: error: cannot combine with previous 'thread_local' specifier" \ "constexpr.c:7:19: error: invalid storage class on function parameter" \ "constexpr.c:7:1: error: illegal storage class on function" \ - "constexpr.c:13:28: warning: implicit conversion from 'int' to '_BitInt' changes non-zero value from 4 to 0 [-Wconstant-conversion]" \ + "constexpr.c:13:28: warning: implicit conversion from 'int' to 'signed _BitInt(2)' changes non-zero value from 4 to 0 [-Wconstant-conversion]" \