23 #ifndef LLVM_SUPPORT_GRAPHWRITER_H
24 #define LLVM_SUPPORT_GRAPHWRITER_H
35 #include <type_traits>
50 namespace GraphProgram {
65 template<
typename GraphType>
70 using DOTTraits = DOTGraphTraits<GraphType>;
71 using GTraits = GraphTraits<GraphType>;
73 using node_iterator =
typename GTraits::nodes_iterator;
74 using child_iterator =
typename GTraits::ChildIteratorType;
77 static_assert(std::is_pointer<NodeRef>::value,
78 "FIXME: Currently GraphWriter requires the NodeRef type to be "
79 "a pointer.\nThe pointer usage should be moved to "
80 "DOTGraphTraits, and removed from GraphWriter itself.");
84 bool getEdgeSourceLabels(raw_ostream &O, NodeRef Node) {
85 child_iterator EI = GTraits::child_begin(Node);
86 child_iterator EE = GTraits::child_end(Node);
87 bool hasEdgeSourceLabels =
false;
89 for (
unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
95 hasEdgeSourceLabels =
true;
103 if (EI != EE && hasEdgeSourceLabels)
104 O <<
"|<s64>truncated...";
106 return hasEdgeSourceLabels;
133 else if (!GraphName.empty())
136 O <<
"digraph unnamed {\n";
139 O <<
"\trankdir=\"BT\";\n";
143 else if (!GraphName.empty())
156 for (
const auto Node : nodes<GraphType>(G))
168 O <<
"\tNode" <<
static_cast<const void*
>(Node) <<
" [shape=record,";
169 if (!NodeAttributes.empty()) O << NodeAttributes <<
",";
181 if (!NodeDesc.empty())
185 std::string edgeSourceLabels;
187 bool hasEdgeSourceLabels = getEdgeSourceLabels(EdgeSourceLabels, Node);
189 if (hasEdgeSourceLabels) {
192 O <<
"{" << EdgeSourceLabels.
str() <<
"}";
206 if (!NodeDesc.empty())
214 for (; i != e && i != 64; ++i) {
216 O <<
"<d" << i <<
">"
221 O <<
"|<d64>truncated...";
228 child_iterator EI = GTraits::child_begin(Node);
229 child_iterator EE = GTraits::child_end(Node);
230 for (
unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
233 for (; EI != EE; ++EI)
238 void writeEdge(NodeRef Node,
unsigned edgeidx, child_iterator EI) {
239 if (NodeRef TargetNode = *EI) {
246 (
unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
247 DestPort =
static_cast<int>(
Offset);
253 emitEdge(static_cast<const void*>(Node), edgeidx,
254 static_cast<const void*>(TargetNode), DestPort,
261 const std::string &Label,
unsigned NumEdgeSources = 0,
262 const std::vector<std::string> *EdgeSourceLabels =
nullptr) {
263 O <<
"\tNode" << ID <<
"[ ";
267 if (NumEdgeSources) O <<
"{";
269 if (NumEdgeSources) {
272 for (
unsigned i = 0; i != NumEdgeSources; ++i) {
274 O <<
"<s" << i <<
">";
283 void emitEdge(
const void *SrcNodeID,
int SrcNodePort,
284 const void *DestNodeID,
int DestNodePort,
285 const std::string &
Attrs) {
286 if (SrcNodePort > 64)
return;
287 if (DestNodePort > 64) DestNodePort = 64;
289 O <<
"\tNode" << SrcNodeID;
290 if (SrcNodePort >= 0)
291 O <<
":s" << SrcNodePort;
292 O <<
" -> Node" << DestNodeID;
294 O <<
":d" << DestNodePort;
297 O <<
"[" << Attrs <<
"]";
308 template<
typename GraphType>
310 bool ShortNames =
false,
311 const Twine &Title =
"") {
316 W.writeGraph(Title.str());
323 template <
typename GraphType>
325 bool ShortNames =
false,
const Twine &Title =
"") {
328 std::string
N = Name.
str();
329 N = N.substr(0, std::min<std::size_t>(N.size(), 140));
334 errs() <<
"error opening file '" << Filename <<
"' for writing!\n";
339 errs() <<
" done. \n";
347 template<
typename GraphType>
349 bool ShortNames =
false,
const Twine &Title =
"",
353 if (Filename.empty())
361 #endif // LLVM_SUPPORT_GRAPHWRITER_H
static std::string getNodeDescription(const void *, const GraphType &)
static EdgeIter getEdgeTarget(const void *, EdgeIter I)
getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is called to determine which outgo...
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static void addCustomGraphFeatures(const GraphType &, GraphWriter &)
addCustomGraphFeatures - If a graph is made up of more than just straight-forward nodes and edges...
static std::string getNodeIdentifierLabel(const void *, const GraphType &)
void emitSimpleNode(const void *ID, const std::string &Attr, const std::string &Label, unsigned NumEdgeSources=0, const std::vector< std::string > *EdgeSourceLabels=nullptr)
emitSimpleNode - Outputs a simple (non-record) node
static std::string getEdgeAttributes(const void *, EdgeIter, const GraphType &)
If you want to override the dot attributes printed for a particular edge, override this method...
raw_ostream & getOStream()
getOStream - Get the raw output stream into the graph file.
std::string str() const
Return the twine contents as a std::string.
static std::string getEdgeSourceLabel(const void *, EdgeIter)
getEdgeSourceLabel - If you want to label the edge source itself, implement this method.
static bool hasEdgeDestLabels()
hasEdgeDestLabels - If this function returns true, the graph is able to provide labels for edge desti...
static unsigned numEdgeDestLabels(const void *)
numEdgeDestLabels - If hasEdgeDestLabels, this function returns the number of incoming edge labels th...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static bool edgeTargetsEdgeSource(const void *, EdgeIter)
edgeTargetsEdgeSource - This method returns true if this outgoing edge should actually target another...
GraphWriter(raw_ostream &o, const GraphType &g, bool SN)
std::string getNodeLabel(const void *, const GraphType &)
getNodeLabel - Given a node and a pointer to the top level graph, return the label to print in the no...
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
typename GraphType::UnknownGraphTypeError NodeRef
std::string EscapeString(const std::string &Label)
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
bool isNodeHidden(NodeRef Node)
void writeNode(NodeRef Node)
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
std::string createGraphFilename(const Twine &Name, int &FD)
static std::string getEdgeDestLabel(const void *, unsigned)
getEdgeDestLabel - If hasEdgeDestLabels, this function returns the incoming edge label with the given...
static std::string getGraphProperties(const GraphType &)
getGraphProperties - Return any custom properties that should be included in the top level graph stru...
void writeHeader(const std::string &Title)
StringRef getColorString(unsigned NodeNumber)
Get a color string for this node number.
static std::string getNodeAttributes(const void *, const GraphType &)
If you want to specify custom node attributes, this is the place to do so.
static bool isNodeHidden(const void *)
isNodeHidden - If the function returns true, the given node is not displayed in the graph...
void writeEdge(NodeRef Node, unsigned edgeidx, child_iterator EI)
A raw_ostream that writes to a file descriptor.
bool DisplayGraph(StringRef Filename, bool wait=true, GraphProgram::Name program=GraphProgram::DOT)
A raw_ostream that writes to an std::string.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
void writeGraph(const std::string &Title="")
static bool renderGraphFromBottomUp()
renderGraphFromBottomUp - If this function returns true, the graph is emitted bottom-up instead of to...
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
void emitEdge(const void *SrcNodeID, int SrcNodePort, const void *DestNodeID, int DestNodePort, const std::string &Attrs)
emitEdge - Output an edge from a simple node into the graph...