authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-01-15 19:16:18-08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-01 10:34:23+02:00
log45d220cac6e9dbb130b48554915233672c240b7b
tree771dbd6a3fb71e04e049bb721cdd952118659877
parent9550db33cbb7dadd555842ef6d7214660e2b00d6

translate-c: add <assert.h> support

Implement __builtin_expect so C code that uses assert() can be translated.

2 files changed, 29 insertions(+), 0 deletions(-)

lib/std/c/builtins.zig+6
......@@ -182,3 +182,9 @@ pub fn __builtin_memcpy(
182182 @memcpy(dst_cast, src_cast, len);
183183 return dst;
184184}
185
186/// The return value of __builtin_expect is `expr`. `c` is the expected value
187/// of `expr` and is used as a hint to the compiler in C. Here it is unused.
188pub fn __builtin_expect(expr: c_long, c: c_long) callconv(.Inline) c_long {
189 return expr;
190}
test/run_translated_c.zig+23
......@@ -1106,4 +1106,27 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
11061106 \\ return 0;
11071107 \\}
11081108 , "");
1109
1110 cases.add("handle assert.h",
1111 \\#include <assert.h>
1112 \\int main() {
1113 \\ int x = 1;
1114 \\ int *xp = &x;
1115 \\ assert(1);
1116 \\ assert(x != 0);
1117 \\ assert(xp);
1118 \\ assert(*xp);
1119 \\ return 0;
1120 \\}
1121 , "");
1122
1123 cases.add("NDEBUG disables assert",
1124 \\#define NDEBUG
1125 \\#include <assert.h>
1126 \\int main() {
1127 \\ assert(0);
1128 \\ assert(NULL);
1129 \\ return 0;
1130 \\}
1131 , "");
11091132}