| ... | ... | @@ -207,23 +207,30 @@ fn zigifyEscapeSequences(ctx: *Context, loc: ZigClangSourceLocation, name: []con |
| 207 | 207 | } |
| 208 | 208 | }, |
| 209 | 209 | .Octal => { |
| 210 | | switch (c) { |
| 211 | | '0'...'7' => { |
| 212 | | count += 1; |
| 213 | | num = std.math.mul(u8, num, 8) catch { |
| 214 | | try failDecl(ctx, loc, name, "macro tokenizing failed: octal literal overflowed", .{}); |
| 215 | | return error.TokenizingFailed; |
| 216 | | }; |
| 217 | | num += c - '0'; |
| 218 | | if (count < 3) |
| 219 | | continue; |
| 220 | | }, |
| 221 | | else => {}, |
| 210 | const accept_digit = switch (c) { |
| 211 | // The maximum length of a octal literal is 3 digits |
| 212 | '0'...'7' => count < 3, |
| 213 | else => false, |
| 214 | }; |
| 215 | |
| 216 | if (accept_digit) { |
| 217 | count += 1; |
| 218 | num = std.math.mul(u8, num, 8) catch { |
| 219 | try failDecl(ctx, loc, name, "macro tokenizing failed: octal literal overflowed", .{}); |
| 220 | return error.TokenizingFailed; |
| 221 | }; |
| 222 | num += c - '0'; |
| 223 | } else { |
| 224 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); |
| 225 | num = 0; |
| 226 | count = 0; |
| 227 | if (c == '\\') |
| 228 | state = .Escape |
| 229 | else |
| 230 | state = .Start; |
| 231 | bytes[i] = c; |
| 232 | i += 1; |
| 222 | 233 | } |
| 223 | | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); |
| 224 | | state = .Start; |
| 225 | | count = 0; |
| 226 | | num = 0; |
| 227 | 234 | }, |
| 228 | 235 | } |
| 229 | 236 | } |
| ... | ... | @@ -940,4 +947,21 @@ test "escape sequences" { |
| 940 | 947 | .id = .StrLit, |
| 941 | 948 | .bytes = "\\045abc", |
| 942 | 949 | })).bytes, "\\x25abc")); |
| 950 | |
| 951 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(undefined, undefined, undefined, a, .{ |
| 952 | .id = .CharLit, |
| 953 | .bytes = "\\0", |
| 954 | })).bytes, "\\x00")); |
| 955 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(undefined, undefined, undefined, a, .{ |
| 956 | .id = .CharLit, |
| 957 | .bytes = "\\00", |
| 958 | })).bytes, "\\x00")); |
| 959 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(undefined, undefined, undefined, a, .{ |
| 960 | .id = .CharLit, |
| 961 | .bytes = "\\000\\001", |
| 962 | })).bytes, "\\x00\\x01")); |
| 963 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(undefined, undefined, undefined, a, .{ |
| 964 | .id = .CharLit, |
| 965 | .bytes = "\\000abc", |
| 966 | })).bytes, "\\x00abc")); |
| 943 | 967 | } |