From 49af7f2db5b6795c4f2289a7993bda2ed95b2b8b Mon Sep 17 00:00:00 2001 From: caheckman <48068198+caheckman@users.noreply.github.com> Date: Thu, 29 Feb 2024 01:25:08 +0000 Subject: [PATCH] GP-4376 Add setPackedOutput to Emit --- .../Decompiler/src/decompile/cpp/ifacedecomp.cc | 2 ++ .../Decompiler/src/decompile/cpp/marshal.hh | 2 +- .../Decompiler/src/decompile/cpp/prettyprint.cc | 14 +++++++++++--- .../Decompiler/src/decompile/cpp/prettyprint.hh | 17 ++++++++++++++++- .../src/decompile/cpp/printlanguage.cc | 8 ++++---- .../src/decompile/cpp/printlanguage.hh | 3 ++- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc index ac94d261c5..0237adb820 100644 --- a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc +++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc @@ -964,7 +964,9 @@ void IfcPrintCXml::execute(istream &s) dcp->conf->print->setOutputStream(status->fileoptr); dcp->conf->print->setMarkup(true); + dcp->conf->print->setPackedOutput(false); dcp->conf->print->docFunction(dcp->fd); + *status->fileoptr << endl; dcp->conf->print->setMarkup(false); } diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh index 6750a239d9..1fca0ed368 100644 --- a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh +++ b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh @@ -432,7 +432,7 @@ class XmlEncode : public Encoder { tag_stop = 2 ///< No tag is currently being written }; static const char spaces[]; ///< Array of ' ' characters for emitting indents - static const int4 MAX_SPACES; + static const int4 MAX_SPACES; ///< Maximum number of leading spaces when indenting XML ostream &outStream; ///< The stream receiving the encoded data int4 tagStatus; ///< Stage of writing an element tag int4 depth; ///< Depth of open elements diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/prettyprint.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/prettyprint.cc index 459ee5c341..ad8a6ded10 100644 --- a/Ghidra/Features/Decompiler/src/decompile/cpp/prettyprint.cc +++ b/Ghidra/Features/Decompiler/src/decompile/cpp/prettyprint.cc @@ -329,6 +329,17 @@ void EmitMarkup::setOutputStream(ostream *t) encoder = new PackedEncode(*s); } +void EmitMarkup::setPackedOutput(bool val) + +{ + if (encoder == (Encoder *)0) return; + delete encoder; + if (val) + encoder = new PackedEncode(*s); + else + encoder = new XmlEncode(*s); +} + int4 TokenSplit::countbase = 0; /// Emit markup or content corresponding to \b this token on a low-level emitter. @@ -1199,9 +1210,6 @@ void EmitPrettyPrint::flush(void) lowlevel->flush(); } -/// This method toggles the low-level emitter between EmitMarkup and EmitNoMarkup depending -/// on whether markup is desired. -/// \param val is \b true if markup is desired void EmitPrettyPrint::setMarkup(bool val) { diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/prettyprint.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/prettyprint.hh index 4b96b8cace..da60367503 100644 --- a/Ghidra/Features/Decompiler/src/decompile/cpp/prettyprint.hh +++ b/Ghidra/Features/Decompiler/src/decompile/cpp/prettyprint.hh @@ -349,6 +349,19 @@ public: virtual void clear(void) { parenlevel = 0; indentlevel=0; pendPrint=(PendPrint *)0; } virtual void setOutputStream(ostream *t)=0; ///< Set the output stream for the emitter virtual ostream *getOutputStream(void) const=0; ///< Get the current output stream + + /// \brief Toggle whether \b this emits mark-up or not + + /// If the emitter supports it, \b true turns on mark-up, \b false turns it off. Otherwise there is no effect. + /// \param val is \b true if markup is desired + virtual void setMarkup(bool val) {} + + /// \brief Toggle whether \b this emitter produces packed output + /// + /// If the emitter supports it, \b true selects packed output and \b false selects unpacked XML output. + /// Otherwise the method has no effect. + /// \param val is \b true for packed or \b false for unpacked + virtual void setPackedOutput(bool val) {} virtual void spaces(int4 num,int4 bump=0); /// \brief Start a new indent level @@ -522,6 +535,7 @@ public: virtual void closeParen(const string &paren,int4 id); virtual void setOutputStream(ostream *t); virtual ostream *getOutputStream(void) const { return s; } + virtual void setPackedOutput(bool val); virtual bool emitsMarkup(void) const { return true; } }; @@ -1085,6 +1099,7 @@ public: virtual void clear(void); virtual void setOutputStream(ostream *t) { lowlevel->setOutputStream(t); } virtual ostream *getOutputStream(void) const { return lowlevel->getOutputStream(); } + virtual void setPackedOutput(bool val) { lowlevel->setPackedOutput(val); } virtual void spaces(int4 num,int4 bump=0); virtual int4 startIndent(void); virtual void stopIndent(int4 id); @@ -1096,7 +1111,7 @@ public: virtual void setCommentFill(const string &fill) { commentfill = fill; } virtual bool emitsMarkup(void) const { return lowlevel->emitsMarkup(); } virtual void resetDefaults(void); - void setMarkup(bool val); ///< Toggle whether the low-level emitter emits markup or not + virtual void setMarkup(bool val); }; /// \brief Helper class for sending cancelable print commands to an ExitXml diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc index d29bd4e2a8..cee8630e70 100644 --- a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc +++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc @@ -648,12 +648,12 @@ void PrintLanguage::emitLineComment(int4 indent,const Comment *comm) comm->setEmitted(true); } -/// Tell the emitter whether to emit just the raw tokens or if additional mark-up should be provided. -/// \param val is \b true for additional mark-up -void PrintLanguage::setMarkup(bool val) +/// Select packed or unpacked (XML) output, if the emitter supports it. +/// \param val is \b true for packed or \b false for unpacked +void PrintLanguage::setPackedOutput(bool val) { - ((EmitPrettyPrint *)emit)->setMarkup(val); + emit->setPackedOutput(val); } /// Emitting formal code structuring can be turned off, causing all control-flow diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.hh index 07e3bda04e..7cdb7069cf 100644 --- a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.hh +++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.hh @@ -454,7 +454,8 @@ public: uint4 getHeaderComment(void) const { return head_comment_type; } ///< Get the type of comments suitable for a function header void setHeaderComment(uint4 val) { head_comment_type = val; } ///< Set the type of comments suitable for a function header bool emitsMarkup(void) const { return emit->emitsMarkup(); } ///< Does the low-level emitter, emit markup - void setMarkup(bool val); ///< Set whether the low-level emitter, emits markup + void setMarkup(bool val) { emit->setMarkup(val); } ///< Turn on/off mark-up in emitted output + void setPackedOutput(bool val); ///< Turn on/off packed output void setFlat(bool val); ///< Set whether nesting code structure should be emitted virtual void initializeFromArchitecture(void)=0; ///< Initialize architecture specific aspects of printer