diff options
| author | Evan Haas <evan@lagerdata.com> | 2021-03-18 23:13:59 -0700 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2021-03-22 10:48:08 +0200 |
| commit | dce612ac2be5aa8a1aa0ee8dd670d7e875624216 (patch) | |
| tree | 5d551dbee5d5cb9f5270f40ea62057961440fc3f /test | |
| parent | 187af14599a083f728f79c4a57ceed30fd01f85d (diff) | |
| download | zig-dce612ac2be5aa8a1aa0ee8dd670d7e875624216.tar.gz zig-dce612ac2be5aa8a1aa0ee8dd670d7e875624216.zip | |
translate-c: Ensure assignments are within a block when necessary
Ensures that if an assignment statement is the sole statement within a
C if statement, for loop, do loop, or do while loop, then when translated
it resides within a block, even though it does not in the original C.
Fixes the following invalid translation:
`if (1) if (1) 2;` -> `if (true) if (true) _ = @as(c_int, 2);`
To this:
```zig
if (true) if (true) {
_ = @as(c_int, 2);
};
```
Fixes #8159
Diffstat (limited to 'test')
| -rw-r--r-- | test/run_translated_c.zig | 14 | ||||
| -rw-r--r-- | test/translate_c.zig | 16 |
2 files changed, 26 insertions, 4 deletions
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig index 44c3956a12..9af9583c27 100644 --- a/test/run_translated_c.zig +++ b/test/run_translated_c.zig @@ -1244,4 +1244,18 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { \\ return 0; \\} , ""); + + cases.add("convert single-statement bodies into blocks for if/else/for/while. issue #8159", + \\#include <stdlib.h> + \\int foo() { return 1; } + \\int main(void) { + \\ int i = 0; + \\ if (i == 0) if (i == 0) if (i != 0) i = 1; + \\ if (i != 0) i = 1; else if (i == 0) if (i == 0) i += 1; + \\ for (; i < 10;) for (; i < 10;) i++; + \\ while (i == 100) while (i == 100) foo(); + \\ if (0) do do "string"; while(1); while(1); + \\ return 0; + \\} + , ""); } diff --git a/test/translate_c.zig b/test/translate_c.zig index 2d5d838033..214aba5112 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1934,7 +1934,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() c_int { \\ var a: c_int = 5; - \\ while (true) a = 2; + \\ while (true) { + \\ a = 2; + \\ } \\ while (true) { \\ var a_1: c_int = 4; \\ a_1 = 9; @@ -1947,7 +1949,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ var a_1: c_int = 2; \\ a_1 = 12; \\ } - \\ while (true) a = 7; + \\ while (true) { + \\ a = 7; + \\ } \\ return 0; \\} }); @@ -2008,7 +2012,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\} , &[_][]const u8{ \\pub export fn bar() c_int { - \\ if ((if (true) @as(c_int, 5) else if (true) @as(c_int, 4) else @as(c_int, 6)) != 0) _ = @as(c_int, 2); + \\ if ((if (true) @as(c_int, 5) else if (true) @as(c_int, 4) else @as(c_int, 6)) != 0) { + \\ _ = @as(c_int, 2); + \\ } \\ return if (true) @as(c_int, 5) else if (true) @as(c_int, 4) else @as(c_int, 6); \\} }); @@ -2389,7 +2395,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub const yes = [*c]u8; \\pub export fn foo() void { \\ var a: yes = undefined; - \\ if (a != null) _ = @as(c_int, 2); + \\ if (a != null) { + \\ _ = @as(c_int, 2); + \\ } \\} }); |
