| author | |
| committer | |
| log | f07f09a373639ae8f1e46fb7beef239f7f85f57d |
| tree | e42bf01cc130ff8e5a46e2bd3f3cffb72b43bbb7 |
| parent | 056c4e2c988c0a2ff6f1be8fe18a0a056d848271 |
| parent | 9e11f67f0d921ed1280dc39e991163053be8a9ee |
| signature |
6 files changed, 95 insertions(+), 12 deletions(-)
src/ast_render.cpp+16-9| ... | @@ -319,6 +319,9 @@ static bool is_digit(uint8_t c) { | ... | @@ -319,6 +319,9 @@ static bool is_digit(uint8_t c) { |
| 319 | } | 319 | } |
| 320 | 320 | ||
| 321 | static bool is_printable(uint8_t c) { | 321 | static bool is_printable(uint8_t c) { |
| 322 | if (c == 0) { | ||
| 323 | return false; | ||
| 324 | } | ||
| 322 | static const uint8_t printables[] = | 325 | static const uint8_t printables[] = |
| 323 | " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.~`!@#$%^&*()_-+=\\{}[];'\"?/<>,:"; | 326 | " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.~`!@#$%^&*()_-+=\\{}[];'\"?/<>,:"; |
| 324 | for (size_t i = 0; i < array_length(printables); i += 1) { | 327 | for (size_t i = 0; i < array_length(printables); i += 1) { |
| ... | @@ -337,20 +340,12 @@ static void string_literal_escape(Buf *source, Buf *dest) { | ... | @@ -337,20 +340,12 @@ static void string_literal_escape(Buf *source, Buf *dest) { |
| 337 | buf_append_str(dest, "\\\""); | 340 | buf_append_str(dest, "\\\""); |
| 338 | } else if (c == '\\') { | 341 | } else if (c == '\\') { |
| 339 | buf_append_str(dest, "\\\\"); | 342 | buf_append_str(dest, "\\\\"); |
| 340 | } else if (c == '\a') { | ||
| 341 | buf_append_str(dest, "\\a"); | ||
| 342 | } else if (c == '\b') { | ||
| 343 | buf_append_str(dest, "\\b"); | ||
| 344 | } else if (c == '\f') { | ||
| 345 | buf_append_str(dest, "\\f"); | ||
| 346 | } else if (c == '\n') { | 343 | } else if (c == '\n') { |
| 347 | buf_append_str(dest, "\\n"); | 344 | buf_append_str(dest, "\\n"); |
| 348 | } else if (c == '\r') { | 345 | } else if (c == '\r') { |
| 349 | buf_append_str(dest, "\\r"); | 346 | buf_append_str(dest, "\\r"); |
| 350 | } else if (c == '\t') { | 347 | } else if (c == '\t') { |
| 351 | buf_append_str(dest, "\\t"); | 348 | buf_append_str(dest, "\\t"); |
| 352 | } else if (c == '\v') { | ||
| 353 | buf_append_str(dest, "\\v"); | ||
| 354 | } else if (is_printable(c)) { | 349 | } else if (is_printable(c)) { |
| 355 | buf_append_char(dest, c); | 350 | buf_append_char(dest, c); |
| 356 | } else { | 351 | } else { |
| ... | @@ -630,7 +625,19 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { | ... | @@ -630,7 +625,19 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { |
| 630 | case NodeTypeCharLiteral: | 625 | case NodeTypeCharLiteral: |
| 631 | { | 626 | { |
| 632 | uint8_t c = node->data.char_literal.value; | 627 | uint8_t c = node->data.char_literal.value; |
| 633 | if (is_printable(c)) { | 628 | if (c == '\'') { |
| 629 | fprintf(ar->f, "'\\''"); | ||
| 630 | } else if (c == '\"') { | ||
| 631 | fprintf(ar->f, "'\\\"'"); | ||
| 632 | } else if (c == '\\') { | ||
| 633 | fprintf(ar->f, "'\\\\'"); | ||
| 634 | } else if (c == '\n') { | ||
| 635 | fprintf(ar->f, "'\\n'"); | ||
| 636 | } else if (c == '\r') { | ||
| 637 | fprintf(ar->f, "'\\r'"); | ||
| 638 | } else if (c == '\t') { | ||
| 639 | fprintf(ar->f, "'\\t'"); | ||
| 640 | } else if (is_printable(c)) { | ||
| 634 | fprintf(ar->f, "'%c'", c); | 641 | fprintf(ar->f, "'%c'", c); |
| 635 | } else { | 642 | } else { |
| 636 | fprintf(ar->f, "'\\x%02x'", (int)c); | 643 | fprintf(ar->f, "'\\x%02x'", (int)c); |
src/codegen.cpp+6-2| ... | @@ -4988,7 +4988,8 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab | ... | @@ -4988,7 +4988,8 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab |
| 4988 | LLVMValueRef vector = ir_llvm_value(g, instruction->vector); | 4988 | LLVMValueRef vector = ir_llvm_value(g, instruction->vector); |
| 4989 | LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, result_loc, | 4989 | LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, result_loc, |
| 4990 | LLVMPointerType(get_llvm_type(g, instruction->vector->value.type), 0), ""); | 4990 | LLVMPointerType(get_llvm_type(g, instruction->vector->value.type), 0), ""); |
| 4991 | gen_store_untyped(g, vector, casted_ptr, get_ptr_align(g, instruction->result_loc->value.type), false); | 4991 | uint32_t alignment = get_ptr_align(g, instruction->result_loc->value.type); |
| 4992 | gen_store_untyped(g, vector, casted_ptr, alignment, false); | ||
| 4992 | return result_loc; | 4993 | return result_loc; |
| 4993 | } | 4994 | } |
| 4994 | 4995 | ||
| ... | @@ -5001,7 +5002,10 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executab | ... | @@ -5001,7 +5002,10 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executab |
| 5001 | LLVMValueRef array_ptr = ir_llvm_value(g, instruction->array); | 5002 | LLVMValueRef array_ptr = ir_llvm_value(g, instruction->array); |
| 5002 | LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr, | 5003 | LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr, |
| 5003 | LLVMPointerType(get_llvm_type(g, vector_type), 0), ""); | 5004 | LLVMPointerType(get_llvm_type(g, vector_type), 0), ""); |
| 5004 | return gen_load_untyped(g, casted_ptr, 0, false, ""); | 5005 | ZigType *array_type = instruction->array->value.type; |
| 5006 | assert(array_type->id == ZigTypeIdArray); | ||
| 5007 | uint32_t alignment = get_abi_alignment(g, array_type->data.array.child_type); | ||
| 5008 | return gen_load_untyped(g, casted_ptr, alignment, false, ""); | ||
| 5005 | } | 5009 | } |
| 5006 | 5010 | ||
| 5007 | static LLVMValueRef ir_render_assert_zero(CodeGen *g, IrExecutable *executable, | 5011 | static LLVMValueRef ir_render_assert_zero(CodeGen *g, IrExecutable *executable, |
std/zig/parser_test.zig+26| ... | @@ -2267,6 +2267,32 @@ test "zig fmt: file ends with struct field" { | ... | @@ -2267,6 +2267,32 @@ test "zig fmt: file ends with struct field" { |
| 2267 | ); | 2267 | ); |
| 2268 | } | 2268 | } |
| 2269 | 2269 | ||
| 2270 | test "zig fmt: comments at several places in struct init" { | ||
| 2271 | try testTransform( | ||
| 2272 | \\var bar = Bar{ | ||
| 2273 | \\ .x = 10, // test | ||
| 2274 | \\ .y = "test" | ||
| 2275 | \\ // test | ||
| 2276 | \\}; | ||
| 2277 | \\ | ||
| 2278 | , | ||
| 2279 | \\var bar = Bar{ | ||
| 2280 | \\ .x = 10, // test | ||
| 2281 | \\ .y = "test", // test | ||
| 2282 | \\}; | ||
| 2283 | \\ | ||
| 2284 | ); | ||
| 2285 | |||
| 2286 | try testCanonical( | ||
| 2287 | \\var bar = Bar{ // test | ||
| 2288 | \\ .x = 10, // test | ||
| 2289 | \\ .y = "test", | ||
| 2290 | \\ // test | ||
| 2291 | \\}; | ||
| 2292 | \\ | ||
| 2293 | ); | ||
| 2294 | } | ||
| 2295 | |||
| 2270 | const std = @import("std"); | 2296 | const std = @import("std"); |
| 2271 | const mem = std.mem; | 2297 | const mem = std.mem; |
| 2272 | const warn = std.debug.warn; | 2298 | const warn = std.debug.warn; |
std/zig/render.zig+7-1| ... | @@ -2020,7 +2020,13 @@ fn renderTokenOffset( | ... | @@ -2020,7 +2020,13 @@ fn renderTokenOffset( |
| 2020 | 2020 | ||
| 2021 | const after_comment_token = tree.tokens.at(token_index + offset); | 2021 | const after_comment_token = tree.tokens.at(token_index + offset); |
| 2022 | const next_line_indent = switch (after_comment_token.id) { | 2022 | const next_line_indent = switch (after_comment_token.id) { |
| 2023 | Token.Id.RParen, Token.Id.RBrace, Token.Id.RBracket => indent - indent_delta, | 2023 | Token.Id.RParen, Token.Id.RBrace, Token.Id.RBracket => blk: { |
| 2024 | if (indent > indent_delta) { | ||
| 2025 | break :blk indent - indent_delta; | ||
| 2026 | } else { | ||
| 2027 | break :blk 0; | ||
| 2028 | } | ||
| 2029 | }, | ||
| 2024 | else => indent, | 2030 | else => indent, |
| 2025 | }; | 2031 | }; |
| 2026 | try stream.writeByteNTimes(' ', next_line_indent); | 2032 | try stream.writeByteNTimes(' ', next_line_indent); |
test/stage1/behavior/vector.zig+6| ... | @@ -74,3 +74,9 @@ test "implicit cast vector to array" { | ... | @@ -74,3 +74,9 @@ test "implicit cast vector to array" { |
| 74 | S.doTheTest(); | 74 | S.doTheTest(); |
| 75 | comptime S.doTheTest(); | 75 | comptime S.doTheTest(); |
| 76 | } | 76 | } |
| 77 | |||
| 78 | test "array to vector" { | ||
| 79 | var foo: f32 = 3.14; | ||
| 80 | var arr = [4]f32{ foo, 1.5, 0.0, 0.0 }; | ||
| 81 | var vec: @Vector(4, f32) = arr; | ||
| 82 | } |
test/translate_c.zig+34| ... | @@ -1780,6 +1780,40 @@ pub fn addCases(cases: *tests.TranslateCContext) void { | ... | @@ -1780,6 +1780,40 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1780 | \\} | 1780 | \\} |
| 1781 | ); | 1781 | ); |
| 1782 | 1782 | ||
| 1783 | cases.addC("escape sequences", | ||
| 1784 | \\const char *escapes() { | ||
| 1785 | \\char a = '\'', | ||
| 1786 | \\ b = '\\', | ||
| 1787 | \\ c = '\a', | ||
| 1788 | \\ d = '\b', | ||
| 1789 | \\ e = '\f', | ||
| 1790 | \\ f = '\n', | ||
| 1791 | \\ g = '\r', | ||
| 1792 | \\ h = '\t', | ||
| 1793 | \\ i = '\v', | ||
| 1794 | \\ j = '\0', | ||
| 1795 | \\ k = '\"'; | ||
| 1796 | \\ return "\'\\\a\b\f\n\r\t\v\0\""; | ||
| 1797 | \\} | ||
| 1798 | \\ | ||
| 1799 | , | ||
| 1800 | \\pub export fn escapes() [*c]const u8 { | ||
| 1801 | \\ var a: u8 = u8('\''); | ||
| 1802 | \\ var b: u8 = u8('\\'); | ||
| 1803 | \\ var c: u8 = u8('\x07'); | ||
| 1804 | \\ var d: u8 = u8('\x08'); | ||
| 1805 | \\ var e: u8 = u8('\x0c'); | ||
| 1806 | \\ var f: u8 = u8('\n'); | ||
| 1807 | \\ var g: u8 = u8('\r'); | ||
| 1808 | \\ var h: u8 = u8('\t'); | ||
| 1809 | \\ var i: u8 = u8('\x0b'); | ||
| 1810 | \\ var j: u8 = u8('\x00'); | ||
| 1811 | \\ var k: u8 = u8('\"'); | ||
| 1812 | \\ return c"\'\\\x07\x08\x0c\n\r\t\x0b\x00\""; | ||
| 1813 | \\} | ||
| 1814 | \\ | ||
| 1815 | ); | ||
| 1816 | |||
| 1783 | /////////////// Cases for only stage1 because stage2 behavior is better //////////////// | 1817 | /////////////// Cases for only stage1 because stage2 behavior is better //////////////// |
| 1784 | cases.addC("Parameterless function prototypes", | 1818 | cases.addC("Parameterless function prototypes", |
| 1785 | \\void foo() {} | 1819 | \\void foo() {} |