From af8661405b908c0abfc191501a8ad1a59a54e86a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 19 Jul 2019 16:56:44 -0400 Subject: fix usingnamespace It used to be that usingnamespace was only allowed at top level. This made it OK to put the state inside the AST node data structure. However, now usingnamespace can occur inside any aggregate data structure, and therefore the state must be in the TopLevelDeclaration rather than in the AST node. There were two other problems with the usingnamespace implementation: * It was passing the wrong destination ScopeDecl, so it could cause an incorrect error such as "import of file outside package path". * When doing `usingnamespace` on a file that already had `pub usingnamespace` in it would "steal" the usingnamespace, causing incorrect "use of undeclared identifier" errors in the target file. closes #2632 closes #2580 --- src/ast_render.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/ast_render.cpp') diff --git a/src/ast_render.cpp b/src/ast_render.cpp index 154803f884..fe131ab65f 100644 --- a/src/ast_render.cpp +++ b/src/ast_render.cpp @@ -193,8 +193,8 @@ static const char *node_type_str(NodeType node_type) { return "Symbol"; case NodeTypePrefixOpExpr: return "PrefixOpExpr"; - case NodeTypeUse: - return "Use"; + case NodeTypeUsingNamespace: + return "UsingNamespace"; case NodeTypeBoolLiteral: return "BoolLiteral"; case NodeTypeNullLiteral: @@ -791,7 +791,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { AstNode *decls_node = node->data.container_decl.decls.at(decl_i); render_node_grouped(ar, decls_node); - if (decls_node->type == NodeTypeUse || + if (decls_node->type == NodeTypeUsingNamespace || decls_node->type == NodeTypeVariableDeclaration || decls_node->type == NodeTypeFnProto) { @@ -1170,7 +1170,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { case NodeTypeParamDecl: case NodeTypeTestDecl: case NodeTypeStructField: - case NodeTypeUse: + case NodeTypeUsingNamespace: zig_panic("TODO more ast rendering"); } } -- cgit v1.2.3 From 57aa8997bd0b651d56a22efee46598fff167253d Mon Sep 17 00:00:00 2001 From: Vexu <15308111+Vexu@users.noreply.github.com> Date: Sun, 21 Jul 2019 14:35:45 +0300 Subject: fix escape sequence rendering --- src/ast_render.cpp | 25 ++++++++++++++++--------- test/translate_c.zig | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) (limited to 'src/ast_render.cpp') diff --git a/src/ast_render.cpp b/src/ast_render.cpp index fe131ab65f..af134d29b5 100644 --- a/src/ast_render.cpp +++ b/src/ast_render.cpp @@ -319,6 +319,9 @@ static bool is_digit(uint8_t c) { } static bool is_printable(uint8_t c) { + if (c == 0) { + return false; + } static const uint8_t printables[] = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.~`!@#$%^&*()_-+=\\{}[];'\"?/<>,:"; for (size_t i = 0; i < array_length(printables); i += 1) { @@ -337,20 +340,12 @@ static void string_literal_escape(Buf *source, Buf *dest) { buf_append_str(dest, "\\\""); } else if (c == '\\') { buf_append_str(dest, "\\\\"); - } else if (c == '\a') { - buf_append_str(dest, "\\a"); - } else if (c == '\b') { - buf_append_str(dest, "\\b"); - } else if (c == '\f') { - buf_append_str(dest, "\\f"); } else if (c == '\n') { buf_append_str(dest, "\\n"); } else if (c == '\r') { buf_append_str(dest, "\\r"); } else if (c == '\t') { buf_append_str(dest, "\\t"); - } else if (c == '\v') { - buf_append_str(dest, "\\v"); } else if (is_printable(c)) { buf_append_char(dest, c); } else { @@ -630,7 +625,19 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { case NodeTypeCharLiteral: { uint8_t c = node->data.char_literal.value; - if (is_printable(c)) { + if (c == '\'') { + fprintf(ar->f, "'\\''"); + } else if (c == '\"') { + fprintf(ar->f, "'\\\"'"); + } else if (c == '\\') { + fprintf(ar->f, "'\\\\'"); + } else if (c == '\n') { + fprintf(ar->f, "'\\n'"); + } else if (c == '\r') { + fprintf(ar->f, "'\\r'"); + } else if (c == '\t') { + fprintf(ar->f, "'\\t'"); + } else if (is_printable(c)) { fprintf(ar->f, "'%c'", c); } else { fprintf(ar->f, "'\\x%02x'", (int)c); diff --git a/test/translate_c.zig b/test/translate_c.zig index d2a5b72b2b..672075e3b6 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1780,6 +1780,40 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\} ); + cases.addC("escape sequences", + \\const char *escapes() { + \\char a = '\'', + \\ b = '\\', + \\ c = '\a', + \\ d = '\b', + \\ e = '\f', + \\ f = '\n', + \\ g = '\r', + \\ h = '\t', + \\ i = '\v', + \\ j = '\0', + \\ k = '\"'; + \\ return "\'\\\a\b\f\n\r\t\v\0\""; + \\} + \\ + , + \\pub export fn escapes() [*c]const u8 { + \\ var a: u8 = u8('\''); + \\ var b: u8 = u8('\\'); + \\ var c: u8 = u8('\x07'); + \\ var d: u8 = u8('\x08'); + \\ var e: u8 = u8('\x0c'); + \\ var f: u8 = u8('\n'); + \\ var g: u8 = u8('\r'); + \\ var h: u8 = u8('\t'); + \\ var i: u8 = u8('\x0b'); + \\ var j: u8 = u8('\x00'); + \\ var k: u8 = u8('\"'); + \\ return c"\'\\\x07\x08\x0c\n\r\t\x0b\x00\""; + \\} + \\ + ); + /////////////// Cases for only stage1 because stage2 behavior is better //////////////// cases.addC("Parameterless function prototypes", \\void foo() {} -- cgit v1.2.3