1 //===-- llvm/Support/DOTGraphTraits.h - Customize .dot output ---*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines a template class that can be used to customize dot output 10 // graphs generated by the GraphWriter.h file. The default implementation of 11 // this file will produce a simple, but not very polished graph. By 12 // specializing this template, lots of customization opportunities are possible. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_SUPPORT_DOTGRAPHTRAITS_H 17 #define LLVM_SUPPORT_DOTGRAPHTRAITS_H 18 19 #include <string> 20 21 namespace llvm { 22 23 /// DefaultDOTGraphTraits - This class provides the default implementations of 24 /// all of the DOTGraphTraits methods. If a specialization does not need to 25 /// override all methods here it should inherit so that it can get the default 26 /// implementations. 27 /// 28 struct DefaultDOTGraphTraits { 29 private: 30 bool IsSimple; 31 32 protected: isSimpleDefaultDOTGraphTraits33 bool isSimple() { 34 return IsSimple; 35 } 36 37 public: IsSimpleDefaultDOTGraphTraits38 explicit DefaultDOTGraphTraits(bool simple=false) : IsSimple (simple) {} 39 40 /// getGraphName - Return the label for the graph as a whole. Printed at the 41 /// top of the graph. 42 /// 43 template<typename GraphType> getGraphNameDefaultDOTGraphTraits44 static std::string getGraphName(const GraphType &) { return ""; } 45 46 /// getGraphProperties - Return any custom properties that should be included 47 /// in the top level graph structure for dot. 48 /// 49 template<typename GraphType> getGraphPropertiesDefaultDOTGraphTraits50 static std::string getGraphProperties(const GraphType &) { 51 return ""; 52 } 53 54 /// renderGraphFromBottomUp - If this function returns true, the graph is 55 /// emitted bottom-up instead of top-down. This requires graphviz 2.0 to work 56 /// though. renderGraphFromBottomUpDefaultDOTGraphTraits57 static bool renderGraphFromBottomUp() { 58 return false; 59 } 60 61 /// isNodeHidden - If the function returns true, the given node is not 62 /// displayed in the graph. 63 template <typename GraphType> isNodeHiddenDefaultDOTGraphTraits64 static bool isNodeHidden(const void *, const GraphType &) { 65 return false; 66 } 67 68 /// getNodeLabel - Given a node and a pointer to the top level graph, return 69 /// the label to print in the node. 70 template<typename GraphType> getNodeLabelDefaultDOTGraphTraits71 std::string getNodeLabel(const void *, const GraphType &) { 72 return ""; 73 } 74 75 // getNodeIdentifierLabel - Returns a string representing the 76 // address or other unique identifier of the node. (Only used if 77 // non-empty.) 78 template <typename GraphType> getNodeIdentifierLabelDefaultDOTGraphTraits79 static std::string getNodeIdentifierLabel(const void *, const GraphType &) { 80 return ""; 81 } 82 83 template<typename GraphType> getNodeDescriptionDefaultDOTGraphTraits84 static std::string getNodeDescription(const void *, const GraphType &) { 85 return ""; 86 } 87 88 /// If you want to specify custom node attributes, this is the place to do so 89 /// 90 template<typename GraphType> getNodeAttributesDefaultDOTGraphTraits91 static std::string getNodeAttributes(const void *, 92 const GraphType &) { 93 return ""; 94 } 95 96 /// If you want to override the dot attributes printed for a particular edge, 97 /// override this method. 98 template<typename EdgeIter, typename GraphType> getEdgeAttributesDefaultDOTGraphTraits99 static std::string getEdgeAttributes(const void *, EdgeIter, 100 const GraphType &) { 101 return ""; 102 } 103 104 /// getEdgeSourceLabel - If you want to label the edge source itself, 105 /// implement this method. 106 template<typename EdgeIter> getEdgeSourceLabelDefaultDOTGraphTraits107 static std::string getEdgeSourceLabel(const void *, EdgeIter) { 108 return ""; 109 } 110 111 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge 112 /// should actually target another edge source, not a node. If this method is 113 /// implemented, getEdgeTarget should be implemented. 114 template<typename EdgeIter> edgeTargetsEdgeSourceDefaultDOTGraphTraits115 static bool edgeTargetsEdgeSource(const void *, EdgeIter) { 116 return false; 117 } 118 119 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is 120 /// called to determine which outgoing edge of Node is the target of this 121 /// edge. 122 template<typename EdgeIter> getEdgeTargetDefaultDOTGraphTraits123 static EdgeIter getEdgeTarget(const void *, EdgeIter I) { 124 return I; 125 } 126 127 /// hasEdgeDestLabels - If this function returns true, the graph is able 128 /// to provide labels for edge destinations. hasEdgeDestLabelsDefaultDOTGraphTraits129 static bool hasEdgeDestLabels() { 130 return false; 131 } 132 133 /// numEdgeDestLabels - If hasEdgeDestLabels, this function returns the 134 /// number of incoming edge labels the given node has. numEdgeDestLabelsDefaultDOTGraphTraits135 static unsigned numEdgeDestLabels(const void *) { 136 return 0; 137 } 138 139 /// getEdgeDestLabel - If hasEdgeDestLabels, this function returns the 140 /// incoming edge label with the given index in the given node. getEdgeDestLabelDefaultDOTGraphTraits141 static std::string getEdgeDestLabel(const void *, unsigned) { 142 return ""; 143 } 144 145 /// addCustomGraphFeatures - If a graph is made up of more than just 146 /// straight-forward nodes and edges, this is the place to put all of the 147 /// custom stuff necessary. The GraphWriter object, instantiated with your 148 /// GraphType is passed in as an argument. You may call arbitrary methods on 149 /// it to add things to the output graph. 150 /// 151 template<typename GraphType, typename GraphWriter> addCustomGraphFeaturesDefaultDOTGraphTraits152 static void addCustomGraphFeatures(const GraphType &, GraphWriter &) {} 153 }; 154 155 156 /// DOTGraphTraits - Template class that can be specialized to customize how 157 /// graphs are converted to 'dot' graphs. When specializing, you may inherit 158 /// from DefaultDOTGraphTraits if you don't need to override everything. 159 /// 160 template <typename Ty> 161 struct DOTGraphTraits : public DefaultDOTGraphTraits { DefaultDOTGraphTraitsDOTGraphTraits162 DOTGraphTraits (bool simple=false) : DefaultDOTGraphTraits (simple) {} 163 }; 164 165 } // End llvm namespace 166 167 #endif 168