authorgravatar for 94326797+riverbl@users.noreply.github.comriverbl <94326797+riverbl@users.noreply.github.com> 2022-01-12 11:07:12+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-12 11:59:30-07:00
logbb8eef8d2403fd5b84ca5fd956f381da1c5cd9b0
tree250ee029a9cab47a567ac6c68bbce95bcaec318f
parent511990c83ba7f27ddf62a2067fa9d773aff26aa2

translate-c: Fix macro define of float constant using scientific notation

Fixes compiler attempting to use null value when translating macro define of float constant using scientific notation with no decimal point

2 files changed, 16 insertions(+), 10 deletions(-)

src/translate_c.zig+12-10
...@@ -5602,16 +5602,18 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {...@@ -5602,16 +5602,18 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
5602 },5602 },
5603 .FloatLiteral => |suffix| {5603 .FloatLiteral => |suffix| {
5604 if (suffix != .none) lit_bytes = lit_bytes[0 .. lit_bytes.len - 1];5604 if (suffix != .none) lit_bytes = lit_bytes[0 .. lit_bytes.len - 1];
5605 const dot_index = mem.indexOfScalar(u8, lit_bytes, '.').?;5605
5606 if (dot_index == 0) {5606 if (mem.indexOfScalar(u8, lit_bytes, '.')) |dot_index| {
5607 lit_bytes = try std.fmt.allocPrint(c.arena, "0{s}", .{lit_bytes});5607 if (dot_index == 0) {
5608 } else if (dot_index + 1 == lit_bytes.len or !std.ascii.isDigit(lit_bytes[dot_index + 1])) {5608 lit_bytes = try std.fmt.allocPrint(c.arena, "0{s}", .{lit_bytes});
5609 // If the literal lacks a digit after the `.`, we need to5609 } else if (dot_index + 1 == lit_bytes.len or !std.ascii.isDigit(lit_bytes[dot_index + 1])) {
5610 // add one since `1.` or `1.e10` would be invalid syntax in Zig.5610 // If the literal lacks a digit after the `.`, we need to
5611 lit_bytes = try std.fmt.allocPrint(c.arena, "{s}0{s}", .{5611 // add one since `1.` or `1.e10` would be invalid syntax in Zig.
5612 lit_bytes[0 .. dot_index + 1],5612 lit_bytes = try std.fmt.allocPrint(c.arena, "{s}0{s}", .{
5613 lit_bytes[dot_index + 1 ..],5613 lit_bytes[0 .. dot_index + 1],
5614 });5614 lit_bytes[dot_index + 1 ..],
5615 });
5616 }
5615 }5617 }
56165618
5617 if (suffix == .none)5619 if (suffix == .none)
test/translate_c.zig+4
...@@ -1135,11 +1135,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1135,11 +1135,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1135 \\#define bar 16.e-2l1135 \\#define bar 16.e-2l
1136 \\#define FOO 0.123451136 \\#define FOO 0.12345
1137 \\#define BAR .123451137 \\#define BAR .12345
1138 \\#define baz 1e1
1139 \\#define BAZ 42e-3f
1138 , &[_][]const u8{1140 , &[_][]const u8{
1139 "pub const foo = @as(f32, 3.14);",1141 "pub const foo = @as(f32, 3.14);",
1140 "pub const bar = @as(c_longdouble, 16.0e-2);",1142 "pub const bar = @as(c_longdouble, 16.0e-2);",
1141 "pub const FOO = 0.12345;",1143 "pub const FOO = 0.12345;",
1142 "pub const BAR = 0.12345;",1144 "pub const BAR = 0.12345;",
1145 "pub const baz = 1e1;",
1146 "pub const BAZ = @as(f32, 42e-3);",
1143 });1147 });
11441148
1145 cases.add("comments",1149 cases.add("comments",