LLVM API Documentation

DOTGraphTraits.h
Go to the documentation of this file.
00001 //===-- llvm/Support/DotGraphTraits.h - Customize .dot output ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a template class that can be used to customize dot output
00011 // graphs generated by the GraphWriter.h file.  The default implementation of
00012 // this file will produce a simple, but not very polished graph.  By
00013 // specializing this template, lots of customization opportunities are possible.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_SUPPORT_DOTGRAPHTRAITS_H
00018 #define LLVM_SUPPORT_DOTGRAPHTRAITS_H
00019 
00020 #include <string>
00021 
00022 namespace llvm {
00023 
00024 /// DefaultDOTGraphTraits - This class provides the default implementations of
00025 /// all of the DOTGraphTraits methods.  If a specialization does not need to
00026 /// override all methods here it should inherit so that it can get the default
00027 /// implementations.
00028 ///
00029 struct DefaultDOTGraphTraits {
00030 private:
00031   bool IsSimple;
00032 
00033 protected:
00034   bool isSimple() {
00035     return IsSimple;
00036   }
00037 
00038 public:
00039   explicit DefaultDOTGraphTraits(bool simple=false) : IsSimple (simple) {}
00040 
00041   /// getGraphName - Return the label for the graph as a whole.  Printed at the
00042   /// top of the graph.
00043   ///
00044   template<typename GraphType>
00045   static std::string getGraphName(const GraphType &) { return ""; }
00046 
00047   /// getGraphProperties - Return any custom properties that should be included
00048   /// in the top level graph structure for dot.
00049   ///
00050   template<typename GraphType>
00051   static std::string getGraphProperties(const GraphType &) {
00052     return "";
00053   }
00054 
00055   /// renderGraphFromBottomUp - If this function returns true, the graph is
00056   /// emitted bottom-up instead of top-down.  This requires graphviz 2.0 to work
00057   /// though.
00058   static bool renderGraphFromBottomUp() {
00059     return false;
00060   }
00061 
00062   /// isNodeHidden - If the function returns true, the given node is not
00063   /// displayed in the graph.
00064   static bool isNodeHidden(const void *) {
00065     return false;
00066   }
00067 
00068   /// getNodeLabel - Given a node and a pointer to the top level graph, return
00069   /// the label to print in the node.
00070   template<typename GraphType>
00071   std::string getNodeLabel(const void *, const GraphType &) {
00072     return "";
00073   }
00074 
00075   /// hasNodeAddressLabel - If this method returns true, the address of the node
00076   /// is added to the label of the node.
00077   template<typename GraphType>
00078   static bool hasNodeAddressLabel(const void *, const GraphType &) {
00079     return false;
00080   }
00081 
00082   template<typename GraphType>
00083   static std::string getNodeDescription(const void *, const GraphType &) {
00084     return "";
00085   }
00086 
00087   /// If you want to specify custom node attributes, this is the place to do so
00088   ///
00089   template<typename GraphType>
00090   static std::string getNodeAttributes(const void *,
00091                                        const GraphType &) {
00092     return "";
00093   }
00094 
00095   /// If you want to override the dot attributes printed for a particular edge,
00096   /// override this method.
00097   template<typename EdgeIter, typename GraphType>
00098   static std::string getEdgeAttributes(const void *, EdgeIter,
00099                                        const GraphType &) {
00100     return "";
00101   }
00102 
00103   /// getEdgeSourceLabel - If you want to label the edge source itself,
00104   /// implement this method.
00105   template<typename EdgeIter>
00106   static std::string getEdgeSourceLabel(const void *, EdgeIter) {
00107     return "";
00108   }
00109 
00110   /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
00111   /// should actually target another edge source, not a node.  If this method is
00112   /// implemented, getEdgeTarget should be implemented.
00113   template<typename EdgeIter>
00114   static bool edgeTargetsEdgeSource(const void *, EdgeIter) {
00115     return false;
00116   }
00117 
00118   /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
00119   /// called to determine which outgoing edge of Node is the target of this
00120   /// edge.
00121   template<typename EdgeIter>
00122   static EdgeIter getEdgeTarget(const void *, EdgeIter I) {
00123     return I;
00124   }
00125 
00126   /// hasEdgeDestLabels - If this function returns true, the graph is able
00127   /// to provide labels for edge destinations.
00128   static bool hasEdgeDestLabels() {
00129     return false;
00130   }
00131 
00132   /// numEdgeDestLabels - If hasEdgeDestLabels, this function returns the
00133   /// number of incoming edge labels the given node has.
00134   static unsigned numEdgeDestLabels(const void *) {
00135     return 0;
00136   }
00137 
00138   /// getEdgeDestLabel - If hasEdgeDestLabels, this function returns the
00139   /// incoming edge label with the given index in the given node.
00140   static std::string getEdgeDestLabel(const void *, unsigned) {
00141     return "";
00142   }
00143 
00144   /// addCustomGraphFeatures - If a graph is made up of more than just
00145   /// straight-forward nodes and edges, this is the place to put all of the
00146   /// custom stuff necessary.  The GraphWriter object, instantiated with your
00147   /// GraphType is passed in as an argument.  You may call arbitrary methods on
00148   /// it to add things to the output graph.
00149   ///
00150   template<typename GraphType, typename GraphWriter>
00151   static void addCustomGraphFeatures(const GraphType &, GraphWriter &) {}
00152 };
00153 
00154 
00155 /// DOTGraphTraits - Template class that can be specialized to customize how
00156 /// graphs are converted to 'dot' graphs.  When specializing, you may inherit
00157 /// from DefaultDOTGraphTraits if you don't need to override everything.
00158 ///
00159 template <typename Ty>
00160 struct DOTGraphTraits : public DefaultDOTGraphTraits {
00161   DOTGraphTraits (bool simple=false) : DefaultDOTGraphTraits (simple) {}
00162 };
00163 
00164 } // End llvm namespace
00165 
00166 #endif