Commit dd144bbb authored by mstarzinger's avatar mstarzinger Committed by Commit bot

[turbofan] Fix JSON escapes in --trace-turbo files.

This makes the character escaping in the graph visualizer less generic
but correct against the JSON spec. The spec has the following definition
for valid characters within a double quoted string:

char ::
  any-Unicode-character-except-"-or-\-or-control-character
  \"
  \\
  \/
  \b
  \f
  \n
  \r
  \t
  \u four-hex-digits

R=bmeurer@chromium.org

Review-Url: https://codereview.chromium.org/2421313002
Cr-Commit-Position: refs/heads/master@{#40364}
parent d293bf54
......@@ -81,33 +81,28 @@ static const char* SafeMnemonic(Node* node) {
return node == nullptr ? "null" : node->op()->mnemonic();
}
#define DEAD_COLOR "#999999"
class Escaped {
class JSONEscaped {
public:
explicit Escaped(const std::ostringstream& os,
const char* escaped_chars = "<>|{}\\")
: str_(os.str()), escaped_chars_(escaped_chars) {}
friend std::ostream& operator<<(std::ostream& os, const Escaped& e) {
for (std::string::const_iterator i = e.str_.begin(); i != e.str_.end();
++i) {
if (e.needs_escape(*i)) os << "\\";
os << *i;
}
explicit JSONEscaped(const std::ostringstream& os) : str_(os.str()) {}
friend std::ostream& operator<<(std::ostream& os, const JSONEscaped& e) {
for (char c : e.str_) PipeCharacter(os, c);
return os;
}
private:
bool needs_escape(char ch) const {
for (size_t i = 0; i < strlen(escaped_chars_); ++i) {
if (ch == escaped_chars_[i]) return true;
}
return false;
static std::ostream& PipeCharacter(std::ostream& os, char c) {
if (c == '"') return os << "\\\"";
if (c == '\\') return os << "\\\\";
if (c == '\b') return os << "\\b";
if (c == '\f') return os << "\\f";
if (c == '\n') return os << "\\n";
if (c == '\r') return os << "\\r";
if (c == '\t') return os << "\\t";
return os << c;
}
const std::string str_;
const char* const escaped_chars_;
};
class JSONGraphNodeWriter {
......@@ -135,11 +130,11 @@ class JSONGraphNodeWriter {
node->op()->PrintTo(label, Operator::PrintVerbosity::kSilent);
node->op()->PrintTo(title, Operator::PrintVerbosity::kVerbose);
node->op()->PrintPropsTo(properties);
os_ << "{\"id\":" << SafeId(node) << ",\"label\":\""
<< Escaped(label, "\"\\") << "\""
<< ",\"title\":\"" << Escaped(title, "\"\\") << "\""
os_ << "{\"id\":" << SafeId(node) << ",\"label\":\"" << JSONEscaped(label)
<< "\""
<< ",\"title\":\"" << JSONEscaped(title) << "\""
<< ",\"live\": " << (live_.IsLive(node) ? "true" : "false")
<< ",\"properties\":\"" << Escaped(properties, "\"\\") << "\"";
<< ",\"properties\":\"" << JSONEscaped(properties) << "\"";
IrOpcode::Value opcode = node->opcode();
if (IrOpcode::IsPhiOpcode(opcode)) {
os_ << ",\"rankInputs\":[0," << NodeProperties::FirstControlIndex(node)
......@@ -171,7 +166,7 @@ class JSONGraphNodeWriter {
Type* type = NodeProperties::GetType(node);
std::ostringstream type_out;
type->PrintTo(type_out);
os_ << ",\"type\":\"" << Escaped(type_out, "\"\\") << "\"";
os_ << ",\"type\":\"" << JSONEscaped(type_out) << "\"";
}
os_ << "}";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment