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 {
7979 Escape,
8080 Hex,
8181 Octal,
82 HexZero,
83 OctalZero,
8482 } = .Start;
8583 var i: usize = 0;
8684 var count: u8 = 0;
......@@ -92,20 +90,15 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {
9290 'n', 'r', 't', '\\', '\'', '\"' => {
9391 bytes[i] = c;
9492 },
95 '0' => {
96 state = .OctalZero;
97 bytes[i] = 'x';
98 },
99 '1'...'7' => {
93 '0'...'7' => {
10094 count += 1;
101 num *= 8;
10295 num += c - '0';
10396 state = .Octal;
10497 bytes[i] = 'x';
10598 },
10699 'x' => {
107 state = .HexZero;
108 bytes[i] = c;
100 state = .Hex;
101 bytes[i] = 'x';
109102 },
110103 'a' => {
111104 bytes[i] = 'x';
......@@ -159,83 +152,37 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {
159152 bytes[i] = c;
160153 i += 1;
161154 },
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 },
184155 .Hex => {
185156 switch (c) {
186157 '0'...'9' => {
187 count += 1;
188 num *= 16;
158 num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed;
189159 num += c - '0';
190 if (count < 2)
191 continue;
192160 },
193161 'a'...'f' => {
194 count += 1;
195 num *= 16;
162 num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed;
196163 num += c - 'a' + 10;
197 if (count < 2)
198 continue;
199164 },
200165 'A'...'F' => {
201 count += 1;
202 num *= 16;
166 num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed;
203167 num += c - 'A' + 10;
204 if (count < 2)
205 continue;
206168 },
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,
213169 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;
215176 bytes[i] = c;
216177 i += 1;
217178 },
218179 }
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;
233180 },
234181 .Octal => {
235182 switch (c) {
236183 '0'...'7' => {
237184 count += 1;
238 num *= 8;
185 num = std.math.mul(u8, num, 8) catch return error.TokenizingFailed;
239186 num += c - '0';
240187 if (count < 3)
241188 continue;
......@@ -243,15 +190,7 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken {
243190 else => {},
244191 }
245192 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2});
246 switch (c) {
247 '\\' => state = .Escape,
248 '0'...'7' => state = .Start,
249 else => {
250 state = .Start;
251 bytes[i] = c;
252 i += 1;
253 },
254 }
193 state = .Start;
255194 count = 0;
256195 num = 0;
257196 },
......@@ -799,12 +738,12 @@ test "escape sequences" {
799738 })).bytes, "\\x77"));
800739 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{
801740 .id = .StrLit,
802 .bytes = "\\00245",
803 })).bytes, "\\xa5"));
741 .bytes = "\\24500",
742 })).bytes, "\\xa500"));
804743 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{
805744 .id = .StrLit,
806 .bytes = "\\x0077abc",
807 })).bytes, "\\x77abc"));
745 .bytes = "\\x0077 abc",
746 })).bytes, "\\x77 abc"));
808747 expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{
809748 .id = .StrLit,
810749 .bytes = "\\045abc",
src-self-hosted/translate_c.zig+4-6
......@@ -340,11 +340,10 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void {
340340}
341341
342342fn 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))
344345 return; // Avoid processing this decl twice
345346 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);
348347 const fn_decl_loc = ZigClangFunctionDecl_getLocation(fn_decl);
349348 const has_body = ZigClangFunctionDecl_hasBody(fn_decl);
350349 const storage_class = ZigClangFunctionDecl_getStorageClass(fn_decl);
......@@ -436,7 +435,8 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
436435}
437436
438437fn 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))
440440 return; // Avoid processing this decl twice
441441 const rp = makeRestorePoint(c);
442442 const visib_tok = try appendToken(c, .Keyword_pub, "pub");
......@@ -447,12 +447,10 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
447447 try appendToken(c, .Keyword_threadlocal, "threadlocal");
448448
449449 const scope = &c.global_scope.base;
450 const var_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, var_decl)));
451450
452451 // TODO https://github.com/ziglang/zig/issues/3756
453452 // TODO https://github.com/ziglang/zig/issues/1802
454453 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);
456454 const var_decl_loc = ZigClangVarDecl_getLocation(var_decl);
457455
458456 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 {
21672167 , &[_][]const u8{
21682168 \\pub const FOO = "aoeu\x13 derp";
21692169 ,
2170 \\pub const FOO2 = "aoeu\x9c derp";
2170 \\pub const FOO2 = "aoeu\x134 derp";
21712171 ,
21722172 \\pub const FOO_CHAR = '\x3f';
21732173 });