From 855699099fe3b65fff4ab68976638390fec4464c Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Tue, 9 Jul 2019 17:22:10 +0200 Subject: [PATCH] Use numbers instead of pointers to define node ids in dot file --- src/language/ASTDotPrinter.cpp | 21 ++++++++++++++++----- src/language/ASTDotPrinter.hpp | 5 +++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/language/ASTDotPrinter.cpp b/src/language/ASTDotPrinter.cpp index 270438e16..7339d6230 100644 --- a/src/language/ASTDotPrinter.cpp +++ b/src/language/ASTDotPrinter.cpp @@ -7,20 +7,21 @@ void ASTDotPrinter::_print(std::ostream& os, const ASTNode& node) const { if (node.is_root()) { - os << " x" << &node << " [ label=\"root \\n" << dataTypeName(node.m_data_type) << "\" ]\n"; + os << " x" << m_node_number_map.at(&node) << " [ label=\"root \\n" << dataTypeName(node.m_data_type) << "\" ]\n"; } else { if (node.has_content()) { - os << " x" << &node << " [ label=\"" << node.name() << "\\n" + os << " x" << m_node_number_map.at(&node) << " [ label=\"" << node.name() << "\\n" << node.string_view() << "\\n" << dataTypeName(node.m_data_type) << "\" ]\n"; } else { - os << " x" << &node << " [ label=\"" << node.name() << "\\n" << dataTypeName(node.m_data_type) << "\" ]\n"; + os << " x" << m_node_number_map.at(&node) << " [ label=\"" << node.name() << "\\n" + << dataTypeName(node.m_data_type) << "\" ]\n"; } } if (!node.children.empty()) { - os << " x" << &node << " -> { "; + os << " x" << m_node_number_map.at(&node) << " -> { "; for (auto& child : node.children) { - os << "x" << child.get() << ((child == node.children.back()) ? " }\n" : ", "); + os << "x" << m_node_number_map.at(child.get()) << ((child == node.children.back()) ? " }\n" : ", "); } for (auto& child : node.children) { this->_print(os, *child); @@ -37,7 +38,17 @@ operator<<(std::ostream& os, const ASTDotPrinter& ast_printer) return os; } +void +ASTDotPrinter::_buildNodeNumberMap(const ASTNode& node) +{ + m_node_number_map[&node] = m_node_number_map.size(); + for (auto& child : node.children) { + this->_buildNodeNumberMap(*child); + } +} + ASTDotPrinter::ASTDotPrinter(const ASTNode& node) : m_node{node} { Assert(node.is_root()); + this->_buildNodeNumberMap(node); } diff --git a/src/language/ASTDotPrinter.hpp b/src/language/ASTDotPrinter.hpp index 522f59b17..6dabb0ce1 100644 --- a/src/language/ASTDotPrinter.hpp +++ b/src/language/ASTDotPrinter.hpp @@ -2,14 +2,19 @@ #define AST_DOT_PRINTER_HPP #include <ASTNode.hpp> +#include <map> class ASTDotPrinter { private: const ASTNode& m_node; + std::map<const ASTNode*, uint32_t> m_node_number_map; + void _print(std::ostream& os, const ASTNode& node) const; + void _buildNodeNumberMap(const ASTNode& node); + public: friend std::ostream& operator<<(std::ostream& os, const ASTDotPrinter& ast_printer); -- GitLab