Get rid of exceptions in llvmc.
[oota-llvm.git] / include / llvm / CompilerDriver / CompilationGraph.h
index f3c31ab22c890290ac454654246af3a746fa9b2d..9f2e50e30412890c8154196ac8eaa1458781ad5b 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
-#define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
+#ifndef LLVM_INCLUDE_COMPILER_DRIVER_COMPILATION_GRAPH_H
+#define LLVM_INCLUDE_COMPILER_DRIVER_COMPILATION_GRAPH_H
 
 #include "llvm/CompilerDriver/Tool.h"
 
 #include "llvm/ADT/GraphTraits.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
-#include "llvm/ADT/iterator.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSet.h"
@@ -37,14 +36,14 @@ namespace llvmc {
   public:
 
     /// GetLanguage -  Find the language name corresponding to a given file.
-    const std::string& GetLanguage(const llvm::sys::Path&) const;
+    const std::string* GetLanguage(const llvm::sys::Path&) const;
   };
 
   /// Edge - Represents an edge of the compilation graph.
   class Edge : public llvm::RefCountedBaseVPTR<Edge> {
   public:
     Edge(const std::string& T) : ToolName_(T) {}
-    virtual ~Edge() {};
+    virtual ~Edge() {}
 
     const std::string& ToolName() const { return ToolName_; }
     virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0;
@@ -83,8 +82,7 @@ namespace llvmc {
 
     /// AddEdge - Add an outward edge. Takes ownership of the provided
     /// Edge object.
-    void AddEdge(Edge* E)
-    { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
+    void AddEdge(Edge* E);
 
     // Inward edge counter. Used to implement topological sort.
     void IncrInEdges() { ++InEdges; }
@@ -124,6 +122,9 @@ namespace llvmc {
 
   public:
 
+    typedef nodes_map_type::iterator nodes_iterator;
+    typedef nodes_map_type::const_iterator const_nodes_iterator;
+
     CompilationGraph();
 
     /// insertNode - Insert a new node into the graph. Takes
@@ -132,16 +133,21 @@ namespace llvmc {
 
     /// insertEdge - Insert a new edge into the graph. Takes ownership
     /// of the Edge object.
-    void insertEdge(const std::string& A, Edge* E);
+    int insertEdge(const std::string& A, Edge* E);
 
     /// Build - Build target(s) from the input file set. Command-line
     /// options are passed implicitly as global variables.
     int Build(llvm::sys::Path const& TempDir, const LanguageMap& LangMap);
 
+    /// Check - Check the compilation graph for common errors like
+    /// cycles, input/output language mismatch and multiple default
+    /// edges. Prints error messages and in case it finds any errors.
+    int Check();
+
     /// getNode - Return a reference to the node correponding to the
     /// given tool name. Throws std::runtime_error.
-    Node& getNode(const std::string& ToolName);
-    const Node& getNode(const std::string& ToolName) const;
+    Node* getNode(const std::string& ToolName);
+    const Node* getNode(const std::string& ToolName) const;
 
     /// viewGraph - This function is meant for use from the debugger.
     /// You can just say 'call G->viewGraph()' and a ghostview window
@@ -150,8 +156,8 @@ namespace llvmc {
     /// in your path.
     void viewGraph();
 
-    /// writeGraph - Write a compilation-graph.dot file.
-    void writeGraph();
+    /// writeGraph - Write Graphviz .dot source file to the current direcotry.
+    int writeGraph(const std::string& OutputFilename);
 
     // GraphTraits support.
     friend NodesIterator GraphBegin(CompilationGraph*);
@@ -163,31 +169,44 @@ namespace llvmc {
     /// getToolsVector - Return a reference to the list of tool names
     /// corresponding to the given language name. Throws
     /// std::runtime_error.
-    const tools_vector_type& getToolsVector(const std::string& LangName) const;
+    const tools_vector_type* getToolsVector(const std::string& LangName) const;
 
     /// PassThroughGraph - Pass the input file through the toolchain
     /// starting at StartNode.
-    void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
-                           const InputLanguagesSet& InLangs,
-                           const llvm::sys::Path& TempDir,
-                           const LanguageMap& LangMap) const;
+    int PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
+                          const InputLanguagesSet& InLangs,
+                          const llvm::sys::Path& TempDir,
+                          const LanguageMap& LangMap) const;
 
-    /// FindToolChain - Find head of the toolchain corresponding to the given file.
+    /// FindToolChain - Find head of the toolchain corresponding to
+    /// the given file.
     const Node* FindToolChain(const llvm::sys::Path& In,
                               const std::string* ForceLanguage,
                               InputLanguagesSet& InLangs,
                               const LanguageMap& LangMap) const;
 
     /// BuildInitial - Traverse the initial parts of the toolchains.
-    void BuildInitial(InputLanguagesSet& InLangs,
-                      const llvm::sys::Path& TempDir,
-                      const LanguageMap& LangMap);
+    int BuildInitial(InputLanguagesSet& InLangs,
+                     const llvm::sys::Path& TempDir,
+                     const LanguageMap& LangMap);
 
     /// TopologicalSort - Sort the nodes in topological order.
-    void TopologicalSort(std::vector<const Node*>& Out);
+    int TopologicalSort(std::vector<const Node*>& Out);
     /// TopologicalSortFilterJoinNodes - Call TopologicalSort and
     /// filter the resulting list to include only Join nodes.
-    void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
+    int TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
+
+    // Functions used to implement Check().
+
+    /// CheckLanguageNames - Check that output/input language names
+    /// match for all nodes.
+    int CheckLanguageNames() const;
+    /// CheckMultipleDefaultEdges - check that there are no multiple
+    /// default default edges.
+    int CheckMultipleDefaultEdges() const;
+    /// CheckCycles - Check that there are no cycles in the graph.
+    int CheckCycles();
+
   };
 
   // GraphTraits support code.
@@ -195,8 +214,8 @@ namespace llvmc {
   /// NodesIterator - Auxiliary class needed to implement GraphTraits
   /// support. Can be generalised to something like value_iterator
   /// for map-like containers.
-  class NodesIterator : public llvm::StringMap<Node>::iterator {
-    typedef llvm::StringMap<Node>::iterator super;
+  class NodesIterator : public CompilationGraph::nodes_iterator {
+    typedef CompilationGraph::nodes_iterator super;
     typedef NodesIterator ThisType;
     typedef Node* pointer;
     typedef Node& reference;
@@ -222,7 +241,8 @@ namespace llvmc {
 
 
   /// NodeChildIterator - Another auxiliary class needed by GraphTraits.
-  class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
+  class NodeChildIterator : public
+               std::iterator<std::bidirectional_iterator_tag, Node, ptrdiff_t> {
     typedef NodeChildIterator ThisType;
     typedef Node::container_type::iterator iterator;
 
@@ -241,16 +261,19 @@ namespace llvmc {
       return *this;
     }
 
-    inline bool operator==(const ThisType& I) const
-    { return EdgeIter == I.EdgeIter; }
-    inline bool operator!=(const ThisType& I) const
-    { return EdgeIter != I.EdgeIter; }
+    inline bool operator==(const ThisType& I) const {
+      assert(OwningGraph == I.OwningGraph);
+      return EdgeIter == I.EdgeIter;
+    }
+    inline bool operator!=(const ThisType& I) const {
+      return !this->operator==(I);
+    }
 
     inline pointer operator*() const {
-      return &OwningGraph->getNode((*EdgeIter)->ToolName());
+      return OwningGraph->getNode((*EdgeIter)->ToolName());
     }
     inline pointer operator->() const {
-      return &OwningGraph->getNode((*EdgeIter)->ToolName());
+      return this->operator*();
     }
 
     ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
@@ -278,7 +301,7 @@ namespace llvm {
     typedef llvmc::NodeChildIterator ChildIteratorType;
 
     static NodeType* getEntryNode(GraphType* G) {
-      return &G->getNode("root");
+      return G->getNode("root");
     }
 
     static ChildIteratorType child_begin(NodeType* N) {
@@ -299,4 +322,4 @@ namespace llvm {
 
 }
 
-#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
+#endif // LLVM_INCLUDE_COMPILER_DRIVER_COMPILATION_GRAPH_H