diff options
| author | Ian Simonson <ian.simonson@protonmail.com> | 2020-04-30 08:38:36 +1000 |
|---|---|---|
| committer | Ian Simonson <ian.simonson@protonmail.com> | 2020-04-30 12:48:27 +1000 |
| commit | e6fa0beb335ad8ecff005fef924c2a39ed748c56 (patch) | |
| tree | 51d8dbed193e84d0c0e5843be5acf35ce141b359 /test | |
| parent | a08675723cd39e3001327aa52b8af4b1c7bb0f32 (diff) | |
| download | zig-e6fa0beb335ad8ecff005fef924c2a39ed748c56.tar.gz zig-e6fa0beb335ad8ecff005fef924c2a39ed748c56.zip | |
Translate-C convert bools to int in complex expressions
Pre-requisite for having a test case for #5062
In complex C statements which are outside of macros,
it is valid C to perform e.g. a bitor between an
integer and a boolean `5 | (8 == 9)`
Currently this results in a zig error after translating
as `c_int | bool` is invalid Zig.
Detects if a sub-expression of a numeric operator is
boolean and if so converts it to int
Diffstat (limited to 'test')
| -rw-r--r-- | test/run_translated_c.zig | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig index 629a850836..ec6a11b9b4 100644 --- a/test/run_translated_c.zig +++ b/test/run_translated_c.zig @@ -195,4 +195,52 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { \\ return 0; \\} , ""); + + cases.add("case boolean expression converted to int", + \\#include <stdlib.h> + \\int main(int argc, char **argv) { + \\ int value = 1 + 2 * 3 + 4 * 5 + 6 << 7 | 8 == 9; + \\ if (value != 4224) abort(); + \\ return 0; + \\} + , ""); + + cases.add("case boolean expression on left converted to int", + \\#include <stdlib.h> + \\int main(int argc, char **argv) { + \\ int value = 8 == 9 | 1 + 2 * 3 + 4 * 5 + 6 << 7; + \\ if (value != 4224) abort(); + \\ return 0; + \\} + , ""); + + cases.add("case boolean and operator+ converts bool to int", + \\#include <stdlib.h> + \\int main(int argc, char **argv) { + \\ int value = (8 == 9) + 3; + \\ int value2 = 3 + (8 == 9); + \\ if (value != value2) abort(); + \\ return 0; + \\} + , ""); + + cases.add("case boolean and operator<", + \\#include <stdlib.h> + \\int main(int argc, char **argv) { + \\ int value = (8 == 9) < 3; + \\ if (value == 0) abort(); + \\ return 0; + \\} + , ""); + + cases.add("case boolean and operator*", + \\#include <stdlib.h> + \\int main(int argc, char **argv) { + \\ int value = (8 == 9) * 3; + \\ int value2 = 3 * (9 == 9); + \\ if (value != 0) abort(); + \\ if (value2 == 0) abort(); + \\ return 0; + \\} + , ""); } |
