[applied] ir: Add a debug_comp_stack debugging function

Message ID 871qovfjn9.fsf@seketeli.org
State New
Headers
Series [applied] ir: Add a debug_comp_stack debugging function |

Commit Message

Dodji Seketeli Dec. 19, 2022, 4:22 p.m. UTC
  Hello,

When debugging type comparison, it can useful to see what the
comparison stack is.  This patch adds the a debug_comp_stack to dump
the comparison stack as a string.

	* include/abg-fwd.h (debug_comp_stack): Declare function.
	* src/abg-ir.cc (debug_comp_vec, print_comp_stack): Define static
	functions.
	(debug_comp_stack): Define new function.

Signed-off-by: Dodji Seketeli <dodji@seketeli.org>
---
 include/abg-fwd.h |  3 +++
 src/abg-ir.cc     | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
  

Patch

diff --git a/include/abg-fwd.h b/include/abg-fwd.h
index d5fcce7b..8a4383c2 100644
--- a/include/abg-fwd.h
+++ b/include/abg-fwd.h
@@ -1056,6 +1056,9 @@  debug(const decl_base* artifact);
 bool
 debug_equals(const type_or_decl_base *l, const type_or_decl_base *r);
 
+void
+debug_comp_stack(const environment& env);
+
 bool
 odr_is_relevant(const type_or_decl_base&);
 
diff --git a/src/abg-ir.cc b/src/abg-ir.cc
index 85b5cbb3..2719954e 100644
--- a/src/abg-ir.cc
+++ b/src/abg-ir.cc
@@ -9533,6 +9533,52 @@  debug_equals(const type_or_decl_base *l, const type_or_decl_base *r)
   return (*l == *r);
 }
 
+/// Emit a trace of a comparison operand stack.
+///
+/// @param vect the operand stack to emit the trace for.
+///
+/// @param o the output stream to emit the trace to.
+static void
+debug_comp_vec(const vector<const type_base*>& vect, std::ostringstream& o)
+{
+  for (auto t : vect)
+    {
+      o << "|" << t->get_pretty_representation()
+	<< "@" << std::hex << t << std::dec;
+    }
+  if (!vect.empty())
+    o << "|";
+}
+
+/// Construct a trace of the two comparison operand stacks.
+///
+/// @param the environment in which the comparison operand stacks are.
+///
+/// @return a string representing the trace.
+static string
+print_comp_stack(const environment& env)
+{
+  std::ostringstream o;
+  o << "left-operands: ";
+  debug_comp_vec(env.priv_->left_type_comp_operands_, o);
+  o << "\n" << "right-operands: ";
+  debug_comp_vec(env.priv_->right_type_comp_operands_, o);
+  o << "\n";
+  return o.str();
+}
+
+/// Emit a trace of the two comparison operands stack on the standard
+/// error stream.
+///
+/// @param env the environment the comparison operands stack belong
+/// to.
+void
+debug_comp_stack(const environment& env)
+{
+  std::cerr << print_comp_stack(env);
+  std::cerr << std::endl;
+}
+
 /// By looking at the language of the TU a given ABI artifact belongs
 /// to, test if the ONE Definition Rule should apply.
 ///