authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-12-20 18:51:44+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-12-20 18:51:44+02:00
log9437d99ae25c635239cdb113457053862b3339f8
tree94fa2212db7c381b5b58577c9754d3363a3d0caa
parente0046b737eddffe0d6881d202608769fe720ff94
signature Commit is signed but in an unrecognized format.

translate-c-2 final small fixes


3 files changed, 23 insertions(+), 86 deletions(-)

src-self-hosted/c_tokenizer.zig+18-79
...@@ -79,8 +79,6 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {...@@ -79,8 +79,6 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {
79 Escape,79 Escape,
80 Hex,80 Hex,
81 Octal,81 Octal,
82 HexZero,
83 OctalZero,
84 } = .Start;82 } = .Start;
85 var i: usize = 0;83 var i: usize = 0;
86 var count: u8 = 0;84 var count: u8 = 0;
...@@ -92,20 +90,15 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {...@@ -92,20 +90,15 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {
92 'n', 'r', 't', '\\', '\'', '\"' => {90 'n', 'r', 't', '\\', '\'', '\"' => {
93 bytes[i] = c;91 bytes[i] = c;
94 },92 },
95 '0' => {93 '0'...'7' => {
96 state = .OctalZero;
97 bytes[i] = 'x';
98 },
99 '1'...'7' => {
100 count += 1;94 count += 1;
101 num *= 8;
102 num += c - '0';95 num += c - '0';
103 state = .Octal;96 state = .Octal;
104 bytes[i] = 'x';97 bytes[i] = 'x';
105 },98 },
106 'x' => {99 'x' => {
107 state = .HexZero;100 state = .Hex;
108 bytes[i] = c;101 bytes[i] = 'x';
109 },102 },
110 'a' => {103 'a' => {
111 bytes[i] = 'x';104 bytes[i] = 'x';
...@@ -159,83 +152,37 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {...@@ -159,83 +152,37 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {
159 bytes[i] = c;152 bytes[i] = c;
160 i += 1;153 i += 1;
161 },154 },
162 .HexZero => {
163 switch (c) {
164 '0' => { continue; },
165 '1'...'9' => {
166 count += 1;
167 num *= 16;
168 num += c - '0';
169 },
170 'a'...'f' => {
171 count += 1;
172 num *= 16;
173 num += c - 'a' + 10;
174 },
175 'A'...'F' => {
176 count += 1;
177 num *= 16;
178 num += c - 'A' + 10;
179 },
180 else => {},
181 }
182 state = .Hex;
183 },
184 .Hex => {155 .Hex => {
185 switch (c) {156 switch (c) {
186 '0'...'9' => {157 '0'...'9' => {
187 count += 1;158 num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed;
188 num *= 16;
189 num += c - '0';159 num += c - '0';
190 if (count < 2)
191 continue;
192 },160 },
193 'a'...'f' => {161 'a'...'f' => {
194 count += 1;162 num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed;
195 num *= 16;
196 num += c - 'a' + 10;163 num += c - 'a' + 10;
197 if (count < 2)
198 continue;
199 },164 },
200 'A'...'F' => {165 'A'...'F' => {
201 count += 1;166 num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed;
202 num *= 16;
203 num += c - 'A' + 10;167 num += c - 'A' + 10;
204 if (count < 2)
205 continue;
206 },168 },
207 else => {},
208 }
209 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2});
210 switch (c) {
211 '\\' => state = .Escape,
212 '0'...'9', 'a'...'f','A'...'F' => state = .Start,
213 else => {169 else => {
214 state = .Start;170 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2});
171 num = 0;
172 if (c == '\\')
173 state = .Escape
174 else
175 state = .Start;
215 bytes[i] = c;176 bytes[i] = c;
216 i += 1;177 i += 1;
217 },178 },
218 }179 }
219 count = 0;
220 num = 0;
221 },
222 .OctalZero => {
223 switch (c) {
224 '0' => { continue; },
225 '1'...'7' => {
226 count += 1;
227 num *= 8;
228 num += c - '0';
229 },
230 else => {},
231 }
232 state = .Octal;
233 },180 },
234 .Octal => {181 .Octal => {
235 switch (c) {182 switch (c) {
236 '0'...'7' => {183 '0'...'7' => {
237 count += 1;184 count += 1;
238 num *= 8;185 num = std.math.mul(u8, num, 8) catch return error.TokenizingFailed;
239 num += c - '0';186 num += c - '0';
240 if (count < 3)187 if (count < 3)
241 continue;188 continue;
...@@ -243,15 +190,7 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {...@@ -243,15 +190,7 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {
243 else => {},190 else => {},
244 }191 }
245 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2});192 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2});
246 switch (c) {193 state = .Start;
247 '\\' => state = .Escape,
248 '0'...'7' => state = .Start,
249 else => {
250 state = .Start;
251 bytes[i] = c;
252 i += 1;
253 },
254 }
255 count = 0;194 count = 0;
256 num = 0;195 num = 0;
257 },196 },
...@@ -799,12 +738,12 @@ test "escape sequences" {...@@ -799,12 +738,12 @@ test "escape sequences" {
799 })).bytes, "\\x77"));738 })).bytes, "\\x77"));
800 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{739 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{
801 .id = .StrLit,740 .id = .StrLit,
802 .bytes = "\\00245",741 .bytes = "\\24500",
803 })).bytes, "\\xa5"));742 })).bytes, "\\xa500"));
804 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{743 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{
805 .id = .StrLit,744 .id = .StrLit,
806 .bytes = "\\x0077abc",745 .bytes = "\\x0077 abc",
807 })).bytes, "\\x77abc"));746 })).bytes, "\\x77 abc"));
808 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{747 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{
809 .id = .StrLit,748 .id = .StrLit,
810 .bytes = "\\045abc",749 .bytes = "\\045abc",
src-self-hosted/translate_c.zig+4-6
...@@ -340,11 +340,10 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void {...@@ -340,11 +340,10 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void {
340}340}
341341
342fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {342fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
343 if (c.decl_table.contains(@ptrToInt(ZigClangFunctionDecl_getCanonicalDecl(fn_decl))))343 const fn_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl)));
344 if (c.global_scope.sym_table.contains(fn_name))
344 return; // Avoid processing this decl twice345 return; // Avoid processing this decl twice
345 const rp = makeRestorePoint(c);346 const rp = makeRestorePoint(c);
346 const fn_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl)));
347 _ = try c.decl_table.put(@ptrToInt(fn_decl), fn_name);
348 const fn_decl_loc = ZigClangFunctionDecl_getLocation(fn_decl);347 const fn_decl_loc = ZigClangFunctionDecl_getLocation(fn_decl);
349 const has_body = ZigClangFunctionDecl_hasBody(fn_decl);348 const has_body = ZigClangFunctionDecl_hasBody(fn_decl);
350 const storage_class = ZigClangFunctionDecl_getStorageClass(fn_decl);349 const storage_class = ZigClangFunctionDecl_getStorageClass(fn_decl);
...@@ -436,7 +435,8 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {...@@ -436,7 +435,8 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
436}435}
437436
438fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {437fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
439 if (c.decl_table.contains(@ptrToInt(ZigClangVarDecl_getCanonicalDecl(var_decl))))438 const var_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, var_decl)));
439 if (c.global_scope.sym_table.contains(var_name))
440 return; // Avoid processing this decl twice440 return; // Avoid processing this decl twice
441 const rp = makeRestorePoint(c);441 const rp = makeRestorePoint(c);
442 const visib_tok = try appendToken(c, .Keyword_pub, "pub");442 const visib_tok = try appendToken(c, .Keyword_pub, "pub");
...@@ -447,12 +447,10 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {...@@ -447,12 +447,10 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
447 try appendToken(c, .Keyword_threadlocal, "threadlocal");447 try appendToken(c, .Keyword_threadlocal, "threadlocal");
448448
449 const scope = &c.global_scope.base;449 const scope = &c.global_scope.base;
450 const var_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, var_decl)));
451450
452 // TODO https://github.com/ziglang/zig/issues/3756451 // TODO https://github.com/ziglang/zig/issues/3756
453 // TODO https://github.com/ziglang/zig/issues/1802452 // TODO https://github.com/ziglang/zig/issues/1802
454 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.a(), "_{}", .{var_name}) else var_name;453 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.a(), "_{}", .{var_name}) else var_name;
455 _ = try c.decl_table.put(@ptrToInt(var_decl), checked_name);
456 const var_decl_loc = ZigClangVarDecl_getLocation(var_decl);454 const var_decl_loc = ZigClangVarDecl_getLocation(var_decl);
457455
458 const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl);456 const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl);
test/translate_c.zig+1-1
...@@ -2167,7 +2167,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2167,7 +2167,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2167 , &[_][]const u8{2167 , &[_][]const u8{
2168 \\pub const FOO = "aoeu\x13 derp";2168 \\pub const FOO = "aoeu\x13 derp";
2169 ,2169 ,
2170 \\pub const FOO2 = "aoeu\x9c derp";2170 \\pub const FOO2 = "aoeu\x134 derp";
2171 ,2171 ,
2172 \\pub const FOO_CHAR = '\x3f';2172 \\pub const FOO_CHAR = '\x3f';
2173 });2173 });