authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-17 11:32:11+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-18 22:46:00+02:00
logac7217e1f5ff15a6fc8248b9c1c651b318f472ad
treecfff232bb0186eef06ebed715635d9a215445034
parent7c6eb4161912f1c66acbdd341339e6cca8c410d8

translate-c: preserve zero fractional part in float literals


3 files changed, 21 insertions(+), 10 deletions(-)

src/translate_c.zig+4-4
......@@ -3248,11 +3248,11 @@ fn transFloatingLiteral(c: *Context, scope: *Scope, stmt: *const clang.FloatingL
32483248 var dbl = stmt.getValueAsApproximateDouble();
32493249 const is_negative = dbl < 0;
32503250 if (is_negative) dbl = -dbl;
3251 const str = try std.fmt.allocPrint(c.arena, "{d}", .{dbl});
3252 var node = if (dbl == std.math.floor(dbl))
3253 try Tag.integer_literal.create(c.arena, str)
3251 const str = if (dbl == std.math.floor(dbl))
3252 try std.fmt.allocPrint(c.arena, "{d}.0", .{dbl})
32543253 else
3255 try Tag.float_literal.create(c.arena, str);
3254 try std.fmt.allocPrint(c.arena, "{d}", .{dbl});
3255 var node = try Tag.float_literal.create(c.arena, str);
32563256 if (is_negative) node = try Tag.negate.create(c.arena, node);
32573257 return maybeSuppressResult(c, scope, used, node);
32583258}
test/run_translated_c.zig+11
......@@ -3,6 +3,17 @@ const tests = @import("tests.zig");
33const nl = std.cstr.line_sep;
44
55pub fn addCases(cases: *tests.RunTranslatedCContext) void {
6 cases.add("division of floating literals",
7 \\#define _NO_CRT_STDIO_INLINE 1
8 \\#include <stdio.h>
9 \\#define PI 3.14159265358979323846f
10 \\#define DEG2RAD (PI/180.0f)
11 \\int main(void) {
12 \\ printf("DEG2RAD is: %f\n", DEG2RAD);
13 \\ return 0;
14 \\}
15 , "DEG2RAD is: 0.017453" ++ nl);
16
617 cases.add("use global scope for record/enum/typedef type transalation if needed",
718 \\void bar(void);
819 \\void baz(void);
test/translate_c.zig+6-6
......@@ -2428,7 +2428,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24282428 \\ b: c_int,
24292429 \\};
24302430 \\pub extern var a: struct_Foo;
2431 \\pub export var b: f32 = 2;
2431 \\pub export var b: f32 = 2.0;
24322432 \\pub export fn foo() void {
24332433 \\ var c: [*c]struct_Foo = undefined;
24342434 \\ _ = a.b;
......@@ -2992,17 +2992,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29922992 \\pub extern fn fn_bool(x: bool) void;
29932993 \\pub extern fn fn_ptr(x: ?*c_void) void;
29942994 \\pub export fn call() void {
2995 \\ fn_int(@floatToInt(c_int, 3));
2996 \\ fn_int(@floatToInt(c_int, 3));
2997 \\ fn_int(@floatToInt(c_int, 3));
2995 \\ fn_int(@floatToInt(c_int, 3.0));
2996 \\ fn_int(@floatToInt(c_int, 3.0));
2997 \\ fn_int(@floatToInt(c_int, 3.0));
29982998 \\ fn_int(@as(c_int, 1094861636));
29992999 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
30003000 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
30013001 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
30023002 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
30033003 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3004 \\ fn_f32(3);
3005 \\ fn_f64(3);
3004 \\ fn_f32(3.0);
3005 \\ fn_f64(3.0);
30063006 \\ fn_bool(@as(c_int, 123) != 0);
30073007 \\ fn_bool(@as(c_int, 0) != 0);
30083008 \\ fn_bool(@ptrToInt(fn_int) != 0);