diff --git a/Changes b/Changes index 22f60b336..57319487c 100644 --- a/Changes +++ b/Changes @@ -20,6 +20,8 @@ The contributors that suggested a given feature are shown in []. Thanks! **** Fix real parameter assignment, bug1427. [Todd Strader] +**** Fix invalid XML output due to special chars, bug1444. [Kanad Kanhere] + * Verilator 4.014 2019-05-08 diff --git a/src/V3EmitXml.cpp b/src/V3EmitXml.cpp index 79f7d86cf..79d2718b6 100644 --- a/src/V3EmitXml.cpp +++ b/src/V3EmitXml.cpp @@ -59,7 +59,7 @@ class EmitXmlFileVisitor : public AstNVisitor { // Don't use to quote a filename for #include - #include doesn't \ escape. // Duplicate in V3File - here so we can print to string putsNoTracking("\""); - putsNoTracking(V3OutFormatter::quoteNameControls(str)); + putsNoTracking(V3OutFormatter::quoteNameControls(str, V3OutFormatter::LA_XML)); putsNoTracking("\""); } diff --git a/src/V3File.cpp b/src/V3File.cpp index 116a7d0b4..ea2c89d81 100644 --- a/src/V3File.cpp +++ b/src/V3File.cpp @@ -847,26 +847,49 @@ void V3OutFormatter::putcNoTracking(char chr) { putcOutput(chr); } -string V3OutFormatter::quoteNameControls(const string& namein, V3OutFormatter::Language) { - // Encode control chars into C style escapes +string V3OutFormatter::quoteNameControls(const string& namein, V3OutFormatter::Language lang) { + // Encode control chars into output-appropriate escapes // Reverse is V3Parse::deQuote string out; - for (string::const_iterator pos=namein.begin(); pos!=namein.end(); ++pos) { - if (pos[0]=='\\' || pos[0]=='"') { - out += string("\\")+pos[0]; - } else if (pos[0]=='\n') { - out += "\\n"; - } else if (pos[0]=='\r') { - out += "\\r"; - } else if (pos[0]=='\t') { - out += "\\t"; - } else if (isprint(pos[0])) { - out += pos[0]; - } else { - // This will also cover \a etc - // Can't use %03o as messes up when signed - char octal[10]; sprintf(octal, "\\%o%o%o", (pos[0]>>6)&3, (pos[0]>>3)&7, pos[0]&7); - out += octal; + if (lang==LA_XML) { + // Encode chars into XML string + for (string::const_iterator pos=namein.begin(); pos!=namein.end(); ++pos) { + if (pos[0]=='"') { + out += string("""); + } else if (pos[0]=='\'') { + out += string("'"); + } else if (pos[0]=='<') { + out += string("<"); + } else if (pos[0]=='>') { + out += string(">"); + } else if (pos[0]=='&') { + out += string("&"); + } else if (isprint(pos[0])) { + out += pos[0]; + } else { + char decimal[10]; sprintf(decimal, "&#%u;", (unsigned char)pos[0]); + out += decimal; + } + } + } else { + // Encode control chars into C style escapes + for (string::const_iterator pos=namein.begin(); pos!=namein.end(); ++pos) { + if (pos[0]=='\\' || pos[0]=='"') { + out += string("\\")+pos[0]; + } else if (pos[0]=='\n') { + out += "\\n"; + } else if (pos[0]=='\r') { + out += "\\r"; + } else if (pos[0]=='\t') { + out += "\\t"; + } else if (isprint(pos[0])) { + out += pos[0]; + } else { + // This will also cover \a etc + // Can't use %03o as messes up when signed + char octal[10]; sprintf(octal, "\\%o%o%o", (pos[0]>>6)&3, (pos[0]>>3)&7, pos[0]&7); + out += octal; + } } } return out; diff --git a/test_regress/t/t_xml_tag.out b/test_regress/t/t_xml_tag.out index 80376b7db..22911aa85 100644 --- a/test_regress/t/t_xml_tag.out +++ b/test_regress/t/t_xml_tag.out @@ -31,6 +31,24 @@ + + + + + + + + + + + + + + + + + + @@ -43,23 +61,24 @@ - - - - + + + + - - - - + + + + - - + + - + + diff --git a/test_regress/t/t_xml_tag.v b/test_regress/t/t_xml_tag.v index f5772107d..1126c8005 100644 --- a/test_regress/t/t_xml_tag.v +++ b/test_regress/t/t_xml_tag.v @@ -31,4 +31,13 @@ module m wire [31:0] dotted = itop.value; + function f(input string m); + $display("%s", m); + endfunction + + initial begin + // Contains all 256 characters except 0 (null character) + f("\x01\x02\x03\x04\x05\x06\a\x08\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"); + end + endmodule