2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "DotSerializer.hpp"
8 #include <boost/assert.hpp>
17 std::string Indent(int numSpaces)
20 for (int i = 0; i < numSpaces; i++)
29 HtmlFont::HtmlFont(std::ostream& stream, int fontSize, const char *color, const char *face)
32 GetStream() << "<FONT";
36 GetStream() << " POINT-SIZE=" << "\"" << fontSize << "\"";
39 if (color && std::strlen(color) != 0)
41 GetStream() << " COLOR=\"" << color << "\" ";
44 if (face && std::strlen(face) != 0)
46 GetStream() << " FACE=\"" << face << "\" ";
53 HtmlFont::HtmlFont(std::ostream& stream)
54 : HtmlFont(stream, -1, nullptr, nullptr)
59 GetStream() << "</FONT>";
63 DotAttributeSet::DotAttributeSet(std::ostream& stream)
69 DotAttributeSet::~DotAttributeSet()
72 for (auto&& attrib : m_Attributes)
79 GetStream() << attrib;
86 DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, const std::stringstream& value)
89 ss << name <<"=" << value.str();
90 m_Attributes.push_back(ss.str());
94 DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, int value)
97 ss << name <<"=" << value;
98 m_Attributes.push_back(ss.str());
102 DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, const std::string& value)
104 std::stringstream ss;
105 ss << name <<"=\"" << value << "\"";
106 m_Attributes.push_back(ss.str());
110 DotEdge::DotEdge(std::ostream& stream, unsigned int fromNodeId, unsigned int toNodeId)
113 std::stringstream ss;
114 ss << Indent(4) << fromNodeId << " -> " << toNodeId << " ";
115 GetStream() << ss.str();
117 m_Attributes = std::make_unique<DotAttributeSet>(stream);
122 m_Attributes.reset(nullptr);
123 GetStream() << ";" << std::endl;
127 NodeContent::NodeContent(std::ostream& stream)
132 NodeContent & NodeContent::SetName(const std::string & name)
138 NodeContent & NodeContent::AddContent(const std::string & content)
140 m_Contents.push_back(content);
144 NodeContent::~NodeContent()
146 std::stringstream ss;
147 ss << "label=\"{" << m_Name;
148 if (!m_Contents.empty())
152 for (auto & content : m_Contents)
162 // Coverity fix: std::stringstream::str() may throw an exception of type std::length_error.
165 catch (const std::exception&) { } // Swallow any exception.
170 DotNode::DotNode(std::ostream& stream, unsigned int nodeId, const char* label)
173 std::stringstream ss;
174 ss << Indent(4) << nodeId;
176 GetStream() << ss.str() << " ";
178 m_Contents = std::make_unique<NodeContent>(stream);
179 m_Attributes = std::make_unique<DotAttributeSet>(stream);
181 if (std::strlen(label) != 0)
183 m_Contents->SetName(label);
187 m_Contents->SetName("<noname>");
193 m_Contents.reset(nullptr);
194 m_Attributes.reset(nullptr);
195 GetStream() << ";" << std::endl;
199 DotDefaults::DotDefaults(std::ostream& stream, const char* type)
202 std::stringstream ss;
203 ss << Indent(4) << type;
205 GetStream() << ss.str() << " ";
206 m_Attributes = std::make_unique<DotAttributeSet>(stream);
209 DotDefaults::~DotDefaults()
211 m_Attributes.reset(nullptr);
212 GetStream() << ";" << std::endl;
215 DotGraph::DotGraph(std::ostream& stream, const char* name)
218 GetStream() << "digraph " << name << " {" << std::endl;
221 DotGraph::~DotGraph()
223 GetStream() << "}" << std::endl;