fix handling of escaped chars in json backend and frontend

This commit is contained in:
N. Engelhardt 2022-02-18 17:13:09 +01:00
parent 1586000048
commit 8fd1b06249
5 changed files with 63 additions and 5 deletions

View File

@ -915,7 +915,7 @@ clean:
rm -rf tests/simple/*.out tests/simple/*.log rm -rf tests/simple/*.out tests/simple/*.log
rm -rf tests/memories/*.out tests/memories/*.log tests/memories/*.dmp rm -rf tests/memories/*.out tests/memories/*.log tests/memories/*.dmp
rm -rf tests/sat/*.log tests/techmap/*.log tests/various/*.log rm -rf tests/sat/*.log tests/techmap/*.log tests/various/*.log
rm -rf tests/bram/temp tests/fsm/temp tests/realmath/temp tests/share/temp tests/smv/temp rm -rf tests/bram/temp tests/fsm/temp tests/realmath/temp tests/share/temp tests/smv/temp tests/various/temp
rm -rf vloghtb/Makefile vloghtb/refdat vloghtb/rtl vloghtb/scripts vloghtb/spec vloghtb/check_yosys vloghtb/vloghammer_tb.tar.bz2 vloghtb/temp vloghtb/log_test_* rm -rf vloghtb/Makefile vloghtb/refdat vloghtb/rtl vloghtb/scripts vloghtb/spec vloghtb/check_yosys vloghtb/vloghammer_tb.tar.bz2 vloghtb/temp vloghtb/log_test_*
rm -f tests/svinterfaces/*.log_stdout tests/svinterfaces/*.log_stderr tests/svinterfaces/dut_result.txt tests/svinterfaces/reference_result.txt tests/svinterfaces/a.out tests/svinterfaces/*_syn.v tests/svinterfaces/*.diff rm -f tests/svinterfaces/*.log_stdout tests/svinterfaces/*.log_stderr tests/svinterfaces/dut_result.txt tests/svinterfaces/reference_result.txt tests/svinterfaces/a.out tests/svinterfaces/*_syn.v tests/svinterfaces/*.diff
rm -f tests/tools/cmp_tbdata rm -f tests/tools/cmp_tbdata

View File

@ -52,8 +52,23 @@ struct JsonWriter
string newstr = "\""; string newstr = "\"";
for (char c : str) { for (char c : str) {
if (c == '\\') if (c == '\\')
newstr += "\\\\";
else if (c == '"')
newstr += "\\\"";
else if (c == '\b')
newstr += "\\b";
else if (c == '\f')
newstr += "\\f";
else if (c == '\n')
newstr += "\\n";
else if (c == '\r')
newstr += "\\r";
else if (c == '\t')
newstr += "\\t";
else if (c < 0x20)
newstr += stringf("\\u%04X", c);
else
newstr += c; newstr += c;
newstr += c;
} }
return newstr + "\""; return newstr + "\"";
} }

View File

@ -60,10 +60,38 @@ struct JsonNode
break; break;
if (ch == '\\') { if (ch == '\\') {
int ch = f.get(); ch = f.get();
if (ch == EOF) switch (ch) {
log_error("Unexpected EOF in JSON string.\n"); case EOF: log_error("Unexpected EOF in JSON string.\n"); break;
case '"':
case '/':
case '\\': break;
case 'b': ch = '\b'; break;
case 'f': ch = '\f'; break;
case 'n': ch = '\n'; break;
case 'r': ch = '\r'; break;
case 't': ch = '\t'; break;
case 'u':
int val = 0;
for (int i = 0; i < 4; i++) {
ch = f.get();
val <<= 4;
if (ch >= '0' && '9' >= ch) {
val += ch - '0';
} else if (ch >= 'A' && 'F' >= ch) {
val += 10 + ch - 'A';
} else if (ch >= 'a' && 'f' >= ch) {
val += 10 + ch - 'a';
} else
log_error("Unexpected non-digit character in \\uXXXX sequence: %c.\n", ch);
}
if (val < 128)
ch = val;
else
log_error("Unsupported \\uXXXX sequence in JSON string: %04X.\n", val);
break;
}
} }
data_string += ch; data_string += ch;

View File

@ -5,3 +5,4 @@
/run-test.mk /run-test.mk
/plugin.so /plugin.so
/plugin.so.dSYM /plugin.so.dSYM
/temp

View File

@ -0,0 +1,14 @@
! mkdir -p temp
read_verilog <<EOT
(* src = "\042 \057 \134 \010 \014 \012 \015 \011 \025 \033" *)
module foo;
endmodule
EOT
write_json temp/test_escapes.json
design -reset
read_json temp/test_escapes.json
write_json temp/test_escapes.json
design -reset
read_json temp/test_escapes.json
write_rtlil temp/test_escapes.json.il
! grep -F 'attribute \src "\" / \\ \010 \014 \n \015 \t \025 \033"' temp/test_escapes.json.il