translate-c: convert tabs to \t in object-like macro string literals

Closes #12549
This commit is contained in:
Evan Haas 2022-09-05 21:32:20 -07:00 committed by Veikka Tuominen
parent e2bb92b2e2
commit e283a40d17
3 changed files with 21 additions and 1 deletions

View File

@ -5799,7 +5799,7 @@ fn zigifyEscapeSequences(ctx: *Context, m: *MacroCtx) ![]const u8 {
}
}
for (source) |c| {
if (c == '\\') {
if (c == '\\' or c == '\t') {
break;
}
} else return source;
@ -5876,6 +5876,13 @@ fn zigifyEscapeSequences(ctx: *Context, m: *MacroCtx) ![]const u8 {
state = .Start;
},
.Start => {
if (c == '\t') {
bytes[i] = '\\';
i += 1;
bytes[i] = 't';
i += 1;
continue;
}
if (c == '\\') {
state = .Escape;
}

View File

@ -50,3 +50,5 @@ typedef _Bool uintptr_t;
#define CAST_TO_UINTPTR(X) (uintptr_t)(X)
#define LARGE_INT 18446744073709550592
#define EMBEDDED_TAB "hello "

View File

@ -2,6 +2,7 @@ const builtin = @import("builtin");
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings;
const h = @cImport(@cInclude("behavior/translate_c_macros.h"));
@ -123,3 +124,13 @@ test "large integer macro" {
try expectEqual(@as(c_ulonglong, 18446744073709550592), h.LARGE_INT);
}
test "string literal macro with embedded tab character" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
try expectEqualStrings("hello\t", h.EMBEDDED_TAB);
}