authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-06 09:33:12-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-12 10:41:09-05:00
log4023ed56d4fd7b627aed73428b5fa4be4b7c05ad
treef9af2b1886a98e91d72c5c67ccf1138dac26180d
parent8dd078b99a31f49194c07af93b0f555f2c7260dc

Convert translate-c to fmtstream


1 files changed, 23 insertions(+), 28 deletions(-)

src-self-hosted/translate_c.zig+23-28
...@@ -89,7 +89,7 @@ const Scope = struct {...@@ -89,7 +89,7 @@ const Scope = struct {
89 var proposed_name = name;89 var proposed_name = name;
90 while (scope.contains(proposed_name)) {90 while (scope.contains(proposed_name)) {
91 scope.mangle_count += 1;91 scope.mangle_count += 1;
92 proposed_name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ name, scope.mangle_count });92 proposed_name = try std.fmtstream.allocPrint(c.a(), "{}_{}", .{ name, scope.mangle_count });
93 }93 }
94 try scope.variables.push(.{ .name = name, .alias = proposed_name });94 try scope.variables.push(.{ .name = name, .alias = proposed_name });
95 return proposed_name;95 return proposed_name;
...@@ -246,7 +246,7 @@ pub const Context = struct {...@@ -246,7 +246,7 @@ pub const Context = struct {
246246
247 const line = ZigClangSourceManager_getSpellingLineNumber(c.source_manager, spelling_loc);247 const line = ZigClangSourceManager_getSpellingLineNumber(c.source_manager, spelling_loc);
248 const column = ZigClangSourceManager_getSpellingColumnNumber(c.source_manager, spelling_loc);248 const column = ZigClangSourceManager_getSpellingColumnNumber(c.source_manager, spelling_loc);
249 return std.fmt.allocPrint(c.a(), "{}:{}:{}", .{ filename, line, column });249 return std.fmtstream.allocPrint(c.a(), "{}:{}:{}", .{ filename, line, column });
250 }250 }
251};251};
252252
...@@ -516,7 +516,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {...@@ -516,7 +516,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
516516
517 const arg_name = blk: {517 const arg_name = blk: {
518 const param_prefix = if (is_const) "" else "arg_";518 const param_prefix = if (is_const) "" else "arg_";
519 const bare_arg_name = try std.fmt.allocPrint(c.a(), "{}{}", .{ param_prefix, mangled_param_name });519 const bare_arg_name = try std.fmtstream.allocPrint(c.a(), "{}{}", .{ param_prefix, mangled_param_name });
520 break :blk try block_scope.makeMangledName(c, bare_arg_name);520 break :blk try block_scope.makeMangledName(c, bare_arg_name);
521 };521 };
522522
...@@ -560,7 +560,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {...@@ -560,7 +560,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
560560
561 // TODO https://github.com/ziglang/zig/issues/3756561 // TODO https://github.com/ziglang/zig/issues/3756
562 // TODO https://github.com/ziglang/zig/issues/1802562 // TODO https://github.com/ziglang/zig/issues/1802
563 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{var_name, c.getMangle()}) else var_name;563 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmtstream.allocPrint(c.a(), "{}_{}", .{var_name, c.getMangle()}) else var_name;
564 const var_decl_loc = ZigClangVarDecl_getLocation(var_decl);564 const var_decl_loc = ZigClangVarDecl_getLocation(var_decl);
565565
566 const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl);566 const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl);
...@@ -620,7 +620,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {...@@ -620,7 +620,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
620 _ = try appendToken(rp.c, .LParen, "(");620 _ = try appendToken(rp.c, .LParen, "(");
621 const expr = try transCreateNodeStringLiteral(621 const expr = try transCreateNodeStringLiteral(
622 rp.c,622 rp.c,
623 try std.fmt.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),623 try std.fmtstream.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),
624 );624 );
625 _ = try appendToken(rp.c, .RParen, ")");625 _ = try appendToken(rp.c, .RParen, ")");
626626
...@@ -677,7 +677,7 @@ fn transTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl, top_l...@@ -677,7 +677,7 @@ fn transTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl, top_l
677677
678 // TODO https://github.com/ziglang/zig/issues/3756678 // TODO https://github.com/ziglang/zig/issues/3756
679 // TODO https://github.com/ziglang/zig/issues/1802679 // TODO https://github.com/ziglang/zig/issues/1802
680 const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{typedef_name, c.getMangle()}) else typedef_name;680 const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmtstream.allocPrint(c.a(), "{}_{}", .{typedef_name, c.getMangle()}) else typedef_name;
681681
682 if (mem.eql(u8, checked_name, "uint8_t"))682 if (mem.eql(u8, checked_name, "uint8_t"))
683 return transTypeDefAsBuiltin(c, typedef_decl, "u8")683 return transTypeDefAsBuiltin(c, typedef_decl, "u8")
...@@ -738,7 +738,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*...@@ -738,7 +738,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
738 // Record declarations such as `struct {...} x` have no name but they're not738 // Record declarations such as `struct {...} x` have no name but they're not
739 // anonymous hence here isAnonymousStructOrUnion is not needed739 // anonymous hence here isAnonymousStructOrUnion is not needed
740 if (bare_name.len == 0) {740 if (bare_name.len == 0) {
741 bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});741 bare_name = try std.fmtstream.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
742 is_unnamed = true;742 is_unnamed = true;
743 }743 }
744744
...@@ -755,7 +755,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*...@@ -755,7 +755,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
755 return null;755 return null;
756 }756 }
757757
758 const name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ container_kind_name, bare_name });758 const name = try std.fmtstream.allocPrint(c.a(), "{}_{}", .{ container_kind_name, bare_name });
759 _ = try c.decl_table.put(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)), name);759 _ = try c.decl_table.put(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)), name);
760760
761 const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name);761 const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name);
...@@ -812,7 +812,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*...@@ -812,7 +812,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
812 var is_anon = false;812 var is_anon = false;
813 var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl)));813 var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl)));
814 if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) {814 if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) {
815 raw_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});815 raw_name = try std.fmtstream.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
816 is_anon = true;816 is_anon = true;
817 }817 }
818 const field_name = try appendIdentifier(c, raw_name);818 const field_name = try appendIdentifier(c, raw_name);
...@@ -882,11 +882,11 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No...@@ -882,11 +882,11 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
882 var bare_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, enum_decl)));882 var bare_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, enum_decl)));
883 var is_unnamed = false;883 var is_unnamed = false;
884 if (bare_name.len == 0) {884 if (bare_name.len == 0) {
885 bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});885 bare_name = try std.fmtstream.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
886 is_unnamed = true;886 is_unnamed = true;
887 }887 }
888888
889 const name = try std.fmt.allocPrint(c.a(), "enum_{}", .{bare_name});889 const name = try std.fmtstream.allocPrint(c.a(), "enum_{}", .{bare_name});
890 _ = try c.decl_table.put(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)), name);890 _ = try c.decl_table.put(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)), name);
891 const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name);891 const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name);
892 node.eq_token = try appendToken(c, .Equal, "=");892 node.eq_token = try appendToken(c, .Equal, "=");
...@@ -1754,9 +1754,9 @@ fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 {...@@ -1754,9 +1754,9 @@ fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 {
1754 // Handle the remaining escapes Zig doesn't support by turning them1754 // Handle the remaining escapes Zig doesn't support by turning them
1755 // into their respective hex representation1755 // into their respective hex representation
1756 if (std.ascii.isCntrl(c))1756 if (std.ascii.isCntrl(c))
1757 return std.fmt.bufPrint(char_buf[0..], "\\x{x:0<2}", .{c}) catch unreachable1757 return std.fmtstream.bufPrint(char_buf[0..], "\\x{x:0<2}", .{c}) catch unreachable
1758 else1758 else
1759 return std.fmt.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable;1759 return std.fmtstream.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable;
1760 },1760 },
1761 };1761 };
1762}1762}
...@@ -2436,7 +2436,7 @@ fn transCase(...@@ -2436,7 +2436,7 @@ fn transCase(
2436) TransError!*ast.Node {2436) TransError!*ast.Node {
2437 const block_scope = scope.findBlockScope(rp.c) catch unreachable;2437 const block_scope = scope.findBlockScope(rp.c) catch unreachable;
2438 const switch_scope = scope.getSwitch();2438 const switch_scope = scope.getSwitch();
2439 const label = try std.fmt.allocPrint(rp.c.a(), "__case_{}", .{switch_scope.cases.len - @boolToInt(switch_scope.has_default)});2439 const label = try std.fmtstream.allocPrint(rp.c.a(), "__case_{}", .{switch_scope.cases.len - @boolToInt(switch_scope.has_default)});
2440 _ = try appendToken(rp.c, .Semicolon, ";");2440 _ = try appendToken(rp.c, .Semicolon, ";");
24412441
2442 const expr = if (ZigClangCaseStmt_getRHS(stmt)) |rhs| blk: {2442 const expr = if (ZigClangCaseStmt_getRHS(stmt)) |rhs| blk: {
...@@ -4607,7 +4607,7 @@ fn finishTransFnProto(...@@ -4607,7 +4607,7 @@ fn finishTransFnProto(
4607 _ = try appendToken(rp.c, .LParen, "(");4607 _ = try appendToken(rp.c, .LParen, "(");
4608 const expr = try transCreateNodeStringLiteral(4608 const expr = try transCreateNodeStringLiteral(
4609 rp.c,4609 rp.c,
4610 try std.fmt.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),4610 try std.fmtstream.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),
4611 );4611 );
4612 _ = try appendToken(rp.c, .RParen, ")");4612 _ = try appendToken(rp.c, .RParen, ")");
46134613
...@@ -4752,15 +4752,10 @@ fn appendToken(c: *Context, token_id: Token.Id, bytes: []const u8) !ast.TokenInd...@@ -4752,15 +4752,10 @@ fn appendToken(c: *Context, token_id: Token.Id, bytes: []const u8) !ast.TokenInd
4752}4752}
47534753
4754fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8, args: var) !ast.TokenIndex {4754fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8, args: var) !ast.TokenIndex {
4755 const S = struct {
4756 fn callback(context: *Context, bytes: []const u8) error{OutOfMemory}!void {
4757 return context.source_buffer.append(bytes);
4758 }
4759 };
4760 const start_index = c.source_buffer.len();4755 const start_index = c.source_buffer.len();
4761 errdefer c.source_buffer.shrink(start_index);4756 errdefer c.source_buffer.shrink(start_index);
47624757
4763 try std.fmt.format(c, error{OutOfMemory}, S.callback, format, args);4758 try c.source_buffer.print(format, args);
4764 const end_index = c.source_buffer.len();4759 const end_index = c.source_buffer.len();
4765 const token_index = c.tree.tokens.len;4760 const token_index = c.tree.tokens.len;
4766 const new_token = try c.tree.tokens.addOne();4761 const new_token = try c.tree.tokens.addOne();
...@@ -4871,7 +4866,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {...@@ -4871,7 +4866,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {
4871 const name = try c.str(raw_name);4866 const name = try c.str(raw_name);
4872 // TODO https://github.com/ziglang/zig/issues/37564867 // TODO https://github.com/ziglang/zig/issues/3756
4873 // TODO https://github.com/ziglang/zig/issues/18024868 // TODO https://github.com/ziglang/zig/issues/1802
4874 const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{name, c.getMangle()}) else name;4869 const mangled_name = if (isZigPrimitiveType(name)) try std.fmtstream.allocPrint(c.a(), "{}_{}", .{name, c.getMangle()}) else name;
4875 if (scope.containsNow(mangled_name)) {4870 if (scope.containsNow(mangled_name)) {
4876 continue;4871 continue;
4877 }4872 }
...@@ -5156,11 +5151,11 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl...@@ -5156,11 +5151,11 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
5156 switch (lit_bytes[1]) {5151 switch (lit_bytes[1]) {
5157 '0'...'7' => {5152 '0'...'7' => {
5158 // Octal5153 // Octal
5159 lit_bytes = try std.fmt.allocPrint(c.a(), "0o{}", .{lit_bytes});5154 lit_bytes = try std.fmtstream.allocPrint(c.a(), "0o{}", .{lit_bytes});
5160 },5155 },
5161 'X' => {5156 'X' => {
5162 // Hexadecimal with capital X, valid in C but not in Zig5157 // Hexadecimal with capital X, valid in C but not in Zig
5163 lit_bytes = try std.fmt.allocPrint(c.a(), "0x{}", .{lit_bytes[2..]});5158 lit_bytes = try std.fmtstream.allocPrint(c.a(), "0x{}", .{lit_bytes[2..]});
5164 },5159 },
5165 else => {},5160 else => {},
5166 }5161 }
...@@ -5191,7 +5186,7 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl...@@ -5191,7 +5186,7 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
5191 return &cast_node.base;5186 return &cast_node.base;
5192 } else if (tok.id == .FloatLiteral) {5187 } else if (tok.id == .FloatLiteral) {
5193 if (lit_bytes[0] == '.')5188 if (lit_bytes[0] == '.')
5194 lit_bytes = try std.fmt.allocPrint(c.a(), "0{}", .{lit_bytes});5189 lit_bytes = try std.fmtstream.allocPrint(c.a(), "0{}", .{lit_bytes});
5195 if (tok.id.FloatLiteral == .None) {5190 if (tok.id.FloatLiteral == .None) {
5196 return transCreateNodeFloat(c, lit_bytes);5191 return transCreateNodeFloat(c, lit_bytes);
5197 }5192 }
...@@ -5324,7 +5319,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const...@@ -5324,7 +5319,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const
5324 num += c - 'A' + 10;5319 num += c - 'A' + 10;
5325 },5320 },
5326 else => {5321 else => {
5327 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 });5322 i += std.fmtstream.formatIntBuf(bytes[i..], num, 16, false, std.fmtstream.FormatOptions{ .fill = '0', .width = 2 });
5328 num = 0;5323 num = 0;
5329 if (c == '\\')5324 if (c == '\\')
5330 state = .Escape5325 state = .Escape
...@@ -5350,7 +5345,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const...@@ -5350,7 +5345,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const
5350 };5345 };
5351 num += c - '0';5346 num += c - '0';
5352 } else {5347 } else {
5353 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 });5348 i += std.fmtstream.formatIntBuf(bytes[i..], num, 16, false, std.fmtstream.FormatOptions{ .fill = '0', .width = 2 });
5354 num = 0;5349 num = 0;
5355 count = 0;5350 count = 0;
5356 if (c == '\\')5351 if (c == '\\')
...@@ -5364,7 +5359,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const...@@ -5364,7 +5359,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const
5364 }5359 }
5365 }5360 }
5366 if (state == .Hex or state == .Octal)5361 if (state == .Hex or state == .Octal)
5367 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 });5362 i += std.fmtstream.formatIntBuf(bytes[i..], num, 16, false, std.fmtstream.FormatOptions{ .fill = '0', .width = 2 });
5368 return bytes[0..i];5363 return bytes[0..i];
5369}5364}
53705365