Get rid of GlobalLanguageMap. Global state is evil.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@56462 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/llvmc2/AutoGenerated.cpp b/tools/llvmc2/AutoGenerated.cpp
index 30aeb8f..4cc85f9 100644
--- a/tools/llvmc2/AutoGenerated.cpp
+++ b/tools/llvmc2/AutoGenerated.cpp
@@ -24,11 +24,6 @@
 using namespace llvm;
 using namespace llvmc;
 
-namespace llvmc {
-  extern LanguageMap GlobalLanguageMap;
-  extern const std::string& GetLanguage(const sys::Path& File);
-}
-
 extern cl::opt<std::string> OutputFilename;
 
 // The auto-generated file
diff --git a/tools/llvmc2/AutoGenerated.h b/tools/llvmc2/AutoGenerated.h
index 5ace7d2..0de7208 100644
--- a/tools/llvmc2/AutoGenerated.h
+++ b/tools/llvmc2/AutoGenerated.h
@@ -20,12 +20,12 @@
 
 namespace llvmc {
 
-  typedef llvm::StringMap<std::string> LanguageMap;
+  class LanguageMap;
   class CompilationGraph;
 
   /// PopulateLanguageMap - The auto-generated function that fills in
   /// the language map (map from file extensions to language names).
-  void PopulateLanguageMap();
+  void PopulateLanguageMap(LanguageMap& langMap);
   /// PopulateCompilationGraph - The auto-generated function that
   /// populates the compilation graph with nodes and edges.
   void PopulateCompilationGraph(CompilationGraph& tools);
diff --git a/tools/llvmc2/CompilationGraph.cpp b/tools/llvmc2/CompilationGraph.cpp
index acf391a..0195395 100644
--- a/tools/llvmc2/CompilationGraph.cpp
+++ b/tools/llvmc2/CompilationGraph.cpp
@@ -33,13 +33,10 @@
 extern cl::list<std::string> Languages;
 
 namespace llvmc {
-  /// ExtsToLangs - Map from file extensions to language names.
-  LanguageMap GlobalLanguageMap;
 
-  /// GetLanguage -  Find the language name corresponding to the given file.
-  const std::string& GetLanguage(const sys::Path& File) {
-    LanguageMap::const_iterator Lang = GlobalLanguageMap.find(File.getSuffix());
-    if (Lang == GlobalLanguageMap.end())
+  const std::string& LanguageMap::GetLanguage(const sys::Path& File) const {
+    LanguageMap::const_iterator Lang = this->find(File.getSuffix());
+    if (Lang == this->end())
       throw std::runtime_error("Unknown suffix: " + File.getSuffix());
     return Lang->second;
   }
@@ -165,7 +162,8 @@
 void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
                                          const Node* StartNode,
                                          const InputLanguagesSet& InLangs,
-                                         const sys::Path& TempDir) const {
+                                         const sys::Path& TempDir,
+                                         const LanguageMap& LangMap) const {
   bool Last = false;
   sys::Path In = InFile;
   const Node* CurNode = StartNode;
@@ -196,7 +194,7 @@
       Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
     }
 
-    if (int ret = CurTool->GenerateAction(In, Out, InLangs).Execute())
+    if (int ret = CurTool->GenerateAction(In, Out, InLangs, LangMap).Execute())
       throw error_code(ret);
 
     if (Last)
@@ -212,12 +210,12 @@
 // Find the head of the toolchain corresponding to the given file.
 // Also, insert an input language into InLangs.
 const Node* CompilationGraph::
-FindToolChain(const sys::Path& In, const std::string* forceLanguage,
-              InputLanguagesSet& InLangs) const {
+FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
+              InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
 
   // Determine the input language.
   const std::string& InLanguage =
-    forceLanguage ? *forceLanguage : GetLanguage(In);
+    ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In);
 
   // Add the current input language to the input language set.
   InLangs.insert(InLanguage);
@@ -234,7 +232,8 @@
 // Traverses initial portions of the toolchains (up to the first Join node).
 // This function is also responsible for handling the -x option.
 void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
-                                     const sys::Path& TempDir) {
+                                     const sys::Path& TempDir,
+                                     const LanguageMap& LangMap) {
   // This is related to -x option handling.
   cl::list<std::string>::const_iterator xIter = Languages.begin(),
     xBegin = xIter, xEnd = Languages.end();
@@ -283,9 +282,9 @@
     }
 
     // Find the toolchain corresponding to this file.
-    const Node* N = FindToolChain(In, xLanguage, InLangs);
+    const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
     // Pass file through the chain starting at head.
-    PassThroughGraph(In, N, InLangs, TempDir);
+    PassThroughGraph(In, N, InLangs, TempDir, LangMap);
   }
 }
 
@@ -324,12 +323,13 @@
                       std::back_inserter(Out), NotJoinNode);
 }
 
-int CompilationGraph::Build (const sys::Path& TempDir) {
+int CompilationGraph::Build (const sys::Path& TempDir,
+                             const LanguageMap& LangMap) {
 
   InputLanguagesSet InLangs;
 
   // Traverse initial parts of the toolchains and fill in InLangs.
-  BuildInitial(InLangs, TempDir);
+  BuildInitial(InLangs, TempDir, LangMap);
 
   std::vector<const Node*> JTV;
   TopologicalSortFilterJoinNodes(JTV);
@@ -362,14 +362,14 @@
       Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
     }
 
-    if (int ret = JT->GenerateAction(Out, InLangs).Execute())
+    if (int ret = JT->GenerateAction(Out, InLangs, LangMap).Execute())
       throw error_code(ret);
 
     if (!IsLast) {
       const Node* NextNode =
         &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
                             CurNode->Name())->ToolName());
-      PassThroughGraph(Out, NextNode, InLangs, TempDir);
+      PassThroughGraph(Out, NextNode, InLangs, TempDir, LangMap);
     }
   }
 
diff --git a/tools/llvmc2/CompilationGraph.h b/tools/llvmc2/CompilationGraph.h
index 4324c38..71ae227 100644
--- a/tools/llvmc2/CompilationGraph.h
+++ b/tools/llvmc2/CompilationGraph.h
@@ -32,6 +32,14 @@
 
   typedef llvm::StringSet<> InputLanguagesSet;
 
+  /// LanguageMap - Maps from extensions to language names.
+  class LanguageMap : public llvm::StringMap<std::string> {
+  public:
+
+    /// GetLanguage -  Find the language name corresponding to a given file.
+    const std::string& GetLanguage(const llvm::sys::Path&) const;
+  };
+
   /// Edge - Represents an edge of the compilation graph.
   class Edge : public llvm::RefCountedBaseVPTR<Edge> {
   public:
@@ -128,7 +136,7 @@
 
     /// 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);
+    int Build(llvm::sys::Path const& TempDir, const LanguageMap& LangMap);
 
     /// getNode - Return a reference to the node correponding to the
     /// given tool name. Throws std::runtime_error.
@@ -161,16 +169,19 @@
     /// starting at StartNode.
     void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
                            const InputLanguagesSet& InLangs,
-                           const llvm::sys::Path& TempDir) const;
+                           const llvm::sys::Path& TempDir,
+                           const LanguageMap& LangMap) const;
 
     /// 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;
+                              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 llvm::sys::Path& TempDir,
+                      const LanguageMap& LangMap);
 
     /// TopologicalSort - Sort the nodes in topological order.
     void TopologicalSort(std::vector<const Node*>& Out);
diff --git a/tools/llvmc2/Tool.h b/tools/llvmc2/Tool.h
index 93fa5df..f4fe0c4 100644
--- a/tools/llvmc2/Tool.h
+++ b/tools/llvmc2/Tool.h
@@ -36,11 +36,13 @@
 
     virtual Action GenerateAction (const PathVector& inFiles,
                                    const llvm::sys::Path& outFile,
-                                   const InputLanguagesSet& InLangs) const = 0;
+                                   const InputLanguagesSet& InLangs,
+                                   const LanguageMap& LangMap) const = 0;
 
     virtual Action GenerateAction (const llvm::sys::Path& inFile,
                                    const llvm::sys::Path& outFile,
-                                   const InputLanguagesSet& InLangs) const = 0;
+                                   const InputLanguagesSet& InLangs,
+                                   const LanguageMap& LangMap) const = 0;
 
     virtual const char*  Name() const = 0;
     virtual const char** InputLanguages() const = 0;
@@ -59,8 +61,9 @@
     bool JoinListEmpty() const { return JoinList_.empty(); }
 
     Action GenerateAction(const llvm::sys::Path& outFile,
-                          const InputLanguagesSet& InLangs) const {
-      return GenerateAction(JoinList_, outFile, InLangs);
+                          const InputLanguagesSet& InLangs,
+                          const LanguageMap& LangMap) const {
+      return GenerateAction(JoinList_, outFile, InLangs, LangMap);
     }
     // We shouldn't shadow base class's version of GenerateAction.
     using Tool::GenerateAction;
diff --git a/tools/llvmc2/llvmc.cpp b/tools/llvmc2/llvmc.cpp
index e073845..08a5989 100644
--- a/tools/llvmc2/llvmc.cpp
+++ b/tools/llvmc2/llvmc.cpp
@@ -55,14 +55,14 @@
 
 namespace {
   /// BuildTargets - A small wrapper for CompilationGraph::Build.
-  int BuildTargets(CompilationGraph& graph) {
+  int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
     int ret;
     const sys::Path& tempDir = SaveTemps
       ? sys::Path("")
       : sys::Path(sys::Path::GetTemporaryDirectory());
 
     try {
-      ret = graph.Build(tempDir);
+      ret = graph.Build(tempDir, langMap);
     }
     catch(...) {
       tempDir.eraseFromDisk(true);
@@ -77,10 +77,13 @@
 
 int main(int argc, char** argv) {
   try {
+    LanguageMap langMap;
     CompilationGraph graph;
 
     cl::ParseCommandLineOptions
       (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
+
+    PopulateLanguageMap(langMap);
     PopulateCompilationGraph(graph);
 
     if (WriteGraph) {
@@ -98,7 +101,7 @@
       throw std::runtime_error("no input files");
     }
 
-    return BuildTargets(graph);
+    return BuildTargets(graph, langMap);
   }
   catch(llvmc::error_code& ec) {
     return ec.code();