From 11a353a20614010029ee11bd30be4414ec36e315 Mon Sep 17 00:00:00 2001 From: Mikhail Glushenkov Date: Mon, 22 Sep 2008 20:47:46 +0000 Subject: [PATCH] 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 --- tools/llvmc2/AutoGenerated.cpp | 5 --- tools/llvmc2/AutoGenerated.h | 4 +-- tools/llvmc2/CompilationGraph.cpp | 36 ++++++++++---------- tools/llvmc2/CompilationGraph.h | 21 +++++++++--- tools/llvmc2/Tool.h | 11 +++--- tools/llvmc2/llvmc.cpp | 9 +++-- utils/TableGen/LLVMCConfigurationEmitter.cpp | 16 +++++---- 7 files changed, 58 insertions(+), 44 deletions(-) diff --git a/tools/llvmc2/AutoGenerated.cpp b/tools/llvmc2/AutoGenerated.cpp index 30aeb8f75ea..4cc85f92d08 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 OutputFilename; // The auto-generated file diff --git a/tools/llvmc2/AutoGenerated.h b/tools/llvmc2/AutoGenerated.h index 5ace7d2a943..0de7208f7f5 100644 --- a/tools/llvmc2/AutoGenerated.h +++ b/tools/llvmc2/AutoGenerated.h @@ -20,12 +20,12 @@ namespace llvmc { - typedef llvm::StringMap 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 acf391a2906..01953957045 100644 --- a/tools/llvmc2/CompilationGraph.cpp +++ b/tools/llvmc2/CompilationGraph.cpp @@ -33,13 +33,10 @@ extern cl::opt OutputFilename; extern cl::list 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 @@ namespace { 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 @@ void CompilationGraph::PassThroughGraph (const sys::Path& InFile, 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 @@ void CompilationGraph::PassThroughGraph (const sys::Path& InFile, // 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 @@ FindToolChain(const sys::Path& In, const std::string* forceLanguage, // 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::const_iterator xIter = Languages.begin(), xBegin = xIter, xEnd = Languages.end(); @@ -283,9 +282,9 @@ void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs, } // 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 @@ TopologicalSortFilterJoinNodes(std::vector& Out) { 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 JTV; TopologicalSortFilterJoinNodes(JTV); @@ -362,14 +362,14 @@ int CompilationGraph::Build (const sys::Path& TempDir) { 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 4324c38ab85..71ae2272eb8 100644 --- a/tools/llvmc2/CompilationGraph.h +++ b/tools/llvmc2/CompilationGraph.h @@ -32,6 +32,14 @@ namespace llvmc { typedef llvm::StringSet<> InputLanguagesSet; + /// LanguageMap - Maps from extensions to language names. + class LanguageMap : public llvm::StringMap { + 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 { public: @@ -128,7 +136,7 @@ namespace llvmc { /// 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 @@ namespace llvmc { /// 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& Out); diff --git a/tools/llvmc2/Tool.h b/tools/llvmc2/Tool.h index 93fa5dfb43a..f4fe0c4ed50 100644 --- a/tools/llvmc2/Tool.h +++ b/tools/llvmc2/Tool.h @@ -36,11 +36,13 @@ namespace llvmc { 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 @@ namespace llvmc { 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 e073845d09e..08a59890eb6 100644 --- a/tools/llvmc2/llvmc.cpp +++ b/tools/llvmc2/llvmc.cpp @@ -55,14 +55,14 @@ cl::opt SaveTemps("save-temps", 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 @@ namespace { 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 @@ int main(int argc, char** argv) { throw std::runtime_error("no input files"); } - return BuildTargets(graph); + return BuildTargets(graph, langMap); } catch(llvmc::error_code& ec) { return ec.code(); diff --git a/utils/TableGen/LLVMCConfigurationEmitter.cpp b/utils/TableGen/LLVMCConfigurationEmitter.cpp index bc742998bd8..133415bf73b 100644 --- a/utils/TableGen/LLVMCConfigurationEmitter.cpp +++ b/utils/TableGen/LLVMCConfigurationEmitter.cpp @@ -826,8 +826,9 @@ bool EmitCaseTest1Arg(const std::string& TestName, O << "InLangs.count(\"" << OptName << "\") != 0"; return true; } else if (TestName == "in_language") { + // TODO: remove this restriction // Works only for cmd_line! - O << "GetLanguage(inFile) == \"" << OptName << '\"'; + O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"'; return true; } else if (TestName == "not_empty") { if (OptName == "o") { @@ -1205,7 +1206,8 @@ void EmitGenerateActionMethod (const ToolProperties& P, O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n"; O << Indent2 << "const sys::Path& outFile,\n" - << Indent2 << "const InputLanguagesSet& InLangs) const\n" + << Indent2 << "const InputLanguagesSet& InLangs,\n" + << Indent2 << "const LanguageMap& LangMap) const\n" << Indent1 << "{\n" << Indent2 << "const char* cmd;\n" << Indent2 << "std::vector vec;\n"; @@ -1245,7 +1247,8 @@ void EmitGenerateActionMethods (const ToolProperties& P, if (!P.isJoin()) O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n" << Indent2 << "const llvm::sys::Path& outFile,\n" - << Indent2 << "const InputLanguagesSet& InLangs) const\n" + << Indent2 << "const InputLanguagesSet& InLangs,\n" + << Indent2 << "const LanguageMap& LangMap) const\n" << Indent1 << "{\n" << Indent2 << "throw std::runtime_error(\"" << P.Name << " is not a Join tool!\");\n" @@ -1451,7 +1454,7 @@ void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O) throw std::string("Error in the language map definition!"); // Generate code - O << "void llvmc::PopulateLanguageMap() {\n"; + O << "void llvmc::PopulateLanguageMap(LanguageMap& langMap) {\n"; for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) { Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i); @@ -1460,7 +1463,7 @@ void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O) const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes"); for (unsigned i = 0; i < Suffixes->size(); ++i) - O << Indent1 << "GlobalLanguageMap[\"" + O << Indent1 << "langMap[\"" << InitPtrToString(Suffixes->getElement(i)) << "\"] = \"" << Lang << "\";\n"; } @@ -1588,8 +1591,7 @@ void EmitPopulateCompilationGraph (Record* CompilationGraph, ListInit* edges = CompilationGraph->getValueAsListInit("edges"); // Generate code - O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n" - << Indent1 << "PopulateLanguageMap();\n\n"; + O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n"; // Insert vertices -- 2.34.1