From d9c9da536f0df0efa4dfd95e1797c0edc1950589 Mon Sep 17 00:00:00 2001 From: Jason Molenda Date: Wed, 20 Jul 2016 03:49:02 +0000 Subject: [PATCH] Add a default-value bool flag pretty_print to the StructuredData Dump methods. They will dump pretty-print (indentation, extra whitepsace) by default. I'll make a change to ProcessGDBRemote soon so it stops sending JSON strings to debugserver pretty-printed; it's unnecessary extra bytes being sent between the two. llvm-svn: 276079 --- lldb/include/lldb/Core/StructuredData.h | 20 +++--- lldb/source/Core/StructuredData.cpp | 76 +++++++++++++++------- .../ScriptInterpreter/Python/PythonDataObjects.cpp | 2 +- .../ScriptInterpreter/Python/PythonDataObjects.h | 2 +- 4 files changed, 63 insertions(+), 37 deletions(-) diff --git a/lldb/include/lldb/Core/StructuredData.h b/lldb/include/lldb/Core/StructuredData.h index 52f3452..28fb80f 100644 --- a/lldb/include/lldb/Core/StructuredData.h +++ b/lldb/include/lldb/Core/StructuredData.h @@ -193,10 +193,10 @@ public: ObjectSP GetObjectForDotSeparatedPath (llvm::StringRef path); - void DumpToStdout() const; + void DumpToStdout(bool pretty_print = true) const; virtual void - Dump (Stream &s) const = 0; + Dump (Stream &s, bool pretty_print = true) const = 0; private: Type m_type; @@ -357,7 +357,7 @@ public: m_items.push_back(item); } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; protected: typedef std::vector collection; @@ -387,7 +387,7 @@ public: return m_value; } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; protected: uint64_t m_value; @@ -416,7 +416,7 @@ public: return m_value; } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; protected: double m_value; @@ -445,7 +445,7 @@ public: return m_value; } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; protected: bool m_value; @@ -486,7 +486,7 @@ public: return m_value; } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; protected: std::string m_value; @@ -697,7 +697,7 @@ public: AddItem (key, ObjectSP (new Boolean(value))); } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; protected: typedef std::map collection; @@ -720,7 +720,7 @@ public: return false; } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; }; class Generic : public Object @@ -750,7 +750,7 @@ public: return m_object != nullptr; } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; private: void *m_object; diff --git a/lldb/source/Core/StructuredData.cpp b/lldb/source/Core/StructuredData.cpp index efc104f..000dacb 100644 --- a/lldb/source/Core/StructuredData.cpp +++ b/lldb/source/Core/StructuredData.cpp @@ -203,50 +203,64 @@ StructuredData::Object::GetObjectForDotSeparatedPath (llvm::StringRef path) } void -StructuredData::Object::DumpToStdout() const +StructuredData::Object::DumpToStdout(bool pretty_print) const { StreamString stream; - Dump(stream); + Dump(stream, pretty_print); printf("%s\n", stream.GetString().c_str()); } void -StructuredData::Array::Dump(Stream &s) const +StructuredData::Array::Dump(Stream &s, bool pretty_print) const { bool first = true; - s << "[\n"; - s.IndentMore(); + s << "["; + if (pretty_print) + { + s << "\n"; + s.IndentMore(); + } for (const auto &item_sp : m_items) { if (first) + { first = false; + } else - s << ",\n"; + { + s << ","; + if (pretty_print) + s << "\n"; + } + if (pretty_print) + s.Indent(); + item_sp->Dump(s, pretty_print); + } + if (pretty_print) + { + s.IndentLess(); + s.EOL(); s.Indent(); - item_sp->Dump(s); } - s.IndentLess(); - s.EOL(); - s.Indent(); s << "]"; } void -StructuredData::Integer::Dump (Stream &s) const +StructuredData::Integer::Dump (Stream &s, bool pretty_print) const { s.Printf ("%" PRIu64, m_value); } void -StructuredData::Float::Dump (Stream &s) const +StructuredData::Float::Dump (Stream &s, bool pretty_print) const { s.Printf ("%lg", m_value); } void -StructuredData::Boolean::Dump (Stream &s) const +StructuredData::Boolean::Dump (Stream &s, bool pretty_print) const { if (m_value == true) s.PutCString ("true"); @@ -256,7 +270,7 @@ StructuredData::Boolean::Dump (Stream &s) const void -StructuredData::String::Dump (Stream &s) const +StructuredData::String::Dump (Stream &s, bool pretty_print) const { std::string quoted; const size_t strsize = m_value.size(); @@ -271,35 +285,47 @@ StructuredData::String::Dump (Stream &s) const } void -StructuredData::Dictionary::Dump (Stream &s) const +StructuredData::Dictionary::Dump (Stream &s, bool pretty_print) const { bool first = true; - s << "{\n"; - s.IndentMore(); + s << "{"; + if (pretty_print) + { + s << "{\n"; + s.IndentMore(); + } for (const auto &pair : m_dict) { if (first) first = false; else - s << ",\n"; - s.Indent(); + { + s << ","; + if (pretty_print) + s << "\n"; + } + if (pretty_print) + s.Indent(); s << "\"" << pair.first.AsCString() << "\" : "; - pair.second->Dump(s); + pair.second->Dump(s, pretty_print); + } + if (pretty_print) + { + s.IndentLess(); + s.EOL(); + s.Indent(); } - s.IndentLess(); - s.EOL(); - s.Indent(); s << "}"; } void -StructuredData::Null::Dump (Stream &s) const +StructuredData::Null::Dump (Stream &s, bool pretty_print) const { s << "null"; } void -StructuredData::Generic::Dump(Stream &s) const +StructuredData::Generic::Dump(Stream &s, bool pretty_print) const { s << "0x" << m_object; } diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp index 1fdf4c7..b460ab5 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp @@ -32,7 +32,7 @@ using namespace lldb_private; using namespace lldb; void -StructuredPythonObject::Dump(Stream &s) const +StructuredPythonObject::Dump(Stream &s, bool pretty_print) const { s << "Python Obj: 0x" << GetValue(); } diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h index 78245a9..fd77a34 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -60,7 +60,7 @@ public: return GetValue() && GetValue() != Py_None; } - void Dump(Stream &s) const override; + void Dump(Stream &s, bool pretty_print = true) const override; private: DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject); -- 2.7.4