Get rid of GlobalLanguageMap. Global state is evil.

llvm-svn: 56462
This commit is contained in:
Mikhail Glushenkov 2008-09-22 20:47:46 +00:00
parent 632d982a7f
commit 8a2bdc74b2
7 changed files with 58 additions and 44 deletions

View File

@ -24,11 +24,6 @@
using namespace llvm; using namespace llvm;
using namespace llvmc; using namespace llvmc;
namespace llvmc {
extern LanguageMap GlobalLanguageMap;
extern const std::string& GetLanguage(const sys::Path& File);
}
extern cl::opt<std::string> OutputFilename; extern cl::opt<std::string> OutputFilename;
// The auto-generated file // The auto-generated file

View File

@ -20,12 +20,12 @@
namespace llvmc { namespace llvmc {
typedef llvm::StringMap<std::string> LanguageMap; class LanguageMap;
class CompilationGraph; class CompilationGraph;
/// PopulateLanguageMap - The auto-generated function that fills in /// PopulateLanguageMap - The auto-generated function that fills in
/// the language map (map from file extensions to language names). /// the language map (map from file extensions to language names).
void PopulateLanguageMap(); void PopulateLanguageMap(LanguageMap& langMap);
/// PopulateCompilationGraph - The auto-generated function that /// PopulateCompilationGraph - The auto-generated function that
/// populates the compilation graph with nodes and edges. /// populates the compilation graph with nodes and edges.
void PopulateCompilationGraph(CompilationGraph& tools); void PopulateCompilationGraph(CompilationGraph& tools);

View File

@ -33,13 +33,10 @@ extern cl::opt<std::string> OutputFilename;
extern cl::list<std::string> Languages; extern cl::list<std::string> Languages;
namespace llvmc { 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& LanguageMap::GetLanguage(const sys::Path& File) const {
const std::string& GetLanguage(const sys::Path& File) { LanguageMap::const_iterator Lang = this->find(File.getSuffix());
LanguageMap::const_iterator Lang = GlobalLanguageMap.find(File.getSuffix()); if (Lang == this->end())
if (Lang == GlobalLanguageMap.end())
throw std::runtime_error("Unknown suffix: " + File.getSuffix()); throw std::runtime_error("Unknown suffix: " + File.getSuffix());
return Lang->second; return Lang->second;
} }
@ -165,7 +162,8 @@ namespace {
void CompilationGraph::PassThroughGraph (const sys::Path& InFile, void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
const Node* StartNode, const Node* StartNode,
const InputLanguagesSet& InLangs, const InputLanguagesSet& InLangs,
const sys::Path& TempDir) const { const sys::Path& TempDir,
const LanguageMap& LangMap) const {
bool Last = false; bool Last = false;
sys::Path In = InFile; sys::Path In = InFile;
const Node* CurNode = StartNode; const Node* CurNode = StartNode;
@ -196,7 +194,7 @@ void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix()); 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); throw error_code(ret);
if (Last) if (Last)
@ -212,12 +210,12 @@ void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
// Find the head of the toolchain corresponding to the given file. // Find the head of the toolchain corresponding to the given file.
// Also, insert an input language into InLangs. // Also, insert an input language into InLangs.
const Node* CompilationGraph:: const Node* CompilationGraph::
FindToolChain(const sys::Path& In, const std::string* forceLanguage, FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
InputLanguagesSet& InLangs) const { InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
// Determine the input language. // Determine the input language.
const std::string& InLanguage = const std::string& InLanguage =
forceLanguage ? *forceLanguage : GetLanguage(In); ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In);
// Add the current input language to the input language set. // Add the current input language to the input language set.
InLangs.insert(InLanguage); 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). // Traverses initial portions of the toolchains (up to the first Join node).
// This function is also responsible for handling the -x option. // This function is also responsible for handling the -x option.
void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs, void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
const sys::Path& TempDir) { const sys::Path& TempDir,
const LanguageMap& LangMap) {
// This is related to -x option handling. // This is related to -x option handling.
cl::list<std::string>::const_iterator xIter = Languages.begin(), cl::list<std::string>::const_iterator xIter = Languages.begin(),
xBegin = xIter, xEnd = Languages.end(); xBegin = xIter, xEnd = Languages.end();
@ -283,9 +282,9 @@ void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
} }
// Find the toolchain corresponding to this file. // 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. // 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<const Node*>& Out) {
std::back_inserter(Out), NotJoinNode); std::back_inserter(Out), NotJoinNode);
} }
int CompilationGraph::Build (const sys::Path& TempDir) { int CompilationGraph::Build (const sys::Path& TempDir,
const LanguageMap& LangMap) {
InputLanguagesSet InLangs; InputLanguagesSet InLangs;
// Traverse initial parts of the toolchains and fill in InLangs. // Traverse initial parts of the toolchains and fill in InLangs.
BuildInitial(InLangs, TempDir); BuildInitial(InLangs, TempDir, LangMap);
std::vector<const Node*> JTV; std::vector<const Node*> JTV;
TopologicalSortFilterJoinNodes(JTV); TopologicalSortFilterJoinNodes(JTV);
@ -362,14 +362,14 @@ int CompilationGraph::Build (const sys::Path& TempDir) {
Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix()); 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); throw error_code(ret);
if (!IsLast) { if (!IsLast) {
const Node* NextNode = const Node* NextNode =
&getNode(ChooseEdge(CurNode->OutEdges, InLangs, &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
CurNode->Name())->ToolName()); CurNode->Name())->ToolName());
PassThroughGraph(Out, NextNode, InLangs, TempDir); PassThroughGraph(Out, NextNode, InLangs, TempDir, LangMap);
} }
} }

View File

@ -32,6 +32,14 @@ namespace llvmc {
typedef llvm::StringSet<> InputLanguagesSet; 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. /// Edge - Represents an edge of the compilation graph.
class Edge : public llvm::RefCountedBaseVPTR<Edge> { class Edge : public llvm::RefCountedBaseVPTR<Edge> {
public: public:
@ -128,7 +136,7 @@ namespace llvmc {
/// Build - Build target(s) from the input file set. Command-line /// Build - Build target(s) from the input file set. Command-line
/// options are passed implicitly as global variables. /// 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 /// getNode - Return a reference to the node correponding to the
/// given tool name. Throws std::runtime_error. /// given tool name. Throws std::runtime_error.
@ -161,16 +169,19 @@ namespace llvmc {
/// starting at StartNode. /// starting at StartNode.
void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode, void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
const InputLanguagesSet& InLangs, 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. /// FindToolChain - Find head of the toolchain corresponding to the given file.
const Node* FindToolChain(const llvm::sys::Path& In, const Node* FindToolChain(const llvm::sys::Path& In,
const std::string* forceLanguage, const std::string* ForceLanguage,
InputLanguagesSet& InLangs) const; InputLanguagesSet& InLangs,
const LanguageMap& LangMap) const;
/// BuildInitial - Traverse the initial parts of the toolchains. /// BuildInitial - Traverse the initial parts of the toolchains.
void BuildInitial(InputLanguagesSet& InLangs, void BuildInitial(InputLanguagesSet& InLangs,
const llvm::sys::Path& TempDir); const llvm::sys::Path& TempDir,
const LanguageMap& LangMap);
/// TopologicalSort - Sort the nodes in topological order. /// TopologicalSort - Sort the nodes in topological order.
void TopologicalSort(std::vector<const Node*>& Out); void TopologicalSort(std::vector<const Node*>& Out);

View File

@ -36,11 +36,13 @@ namespace llvmc {
virtual Action GenerateAction (const PathVector& inFiles, virtual Action GenerateAction (const PathVector& inFiles,
const llvm::sys::Path& outFile, 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, virtual Action GenerateAction (const llvm::sys::Path& inFile,
const llvm::sys::Path& outFile, 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* Name() const = 0;
virtual const char** InputLanguages() const = 0; virtual const char** InputLanguages() const = 0;
@ -59,8 +61,9 @@ namespace llvmc {
bool JoinListEmpty() const { return JoinList_.empty(); } bool JoinListEmpty() const { return JoinList_.empty(); }
Action GenerateAction(const llvm::sys::Path& outFile, Action GenerateAction(const llvm::sys::Path& outFile,
const InputLanguagesSet& InLangs) const { const InputLanguagesSet& InLangs,
return GenerateAction(JoinList_, outFile, InLangs); const LanguageMap& LangMap) const {
return GenerateAction(JoinList_, outFile, InLangs, LangMap);
} }
// We shouldn't shadow base class's version of GenerateAction. // We shouldn't shadow base class's version of GenerateAction.
using Tool::GenerateAction; using Tool::GenerateAction;

View File

@ -55,14 +55,14 @@ cl::opt<bool> SaveTemps("save-temps",
namespace { namespace {
/// BuildTargets - A small wrapper for CompilationGraph::Build. /// BuildTargets - A small wrapper for CompilationGraph::Build.
int BuildTargets(CompilationGraph& graph) { int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
int ret; int ret;
const sys::Path& tempDir = SaveTemps const sys::Path& tempDir = SaveTemps
? sys::Path("") ? sys::Path("")
: sys::Path(sys::Path::GetTemporaryDirectory()); : sys::Path(sys::Path::GetTemporaryDirectory());
try { try {
ret = graph.Build(tempDir); ret = graph.Build(tempDir, langMap);
} }
catch(...) { catch(...) {
tempDir.eraseFromDisk(true); tempDir.eraseFromDisk(true);
@ -77,10 +77,13 @@ namespace {
int main(int argc, char** argv) { int main(int argc, char** argv) {
try { try {
LanguageMap langMap;
CompilationGraph graph; CompilationGraph graph;
cl::ParseCommandLineOptions cl::ParseCommandLineOptions
(argc, argv, "LLVM Compiler Driver (Work In Progress)", true); (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
PopulateLanguageMap(langMap);
PopulateCompilationGraph(graph); PopulateCompilationGraph(graph);
if (WriteGraph) { if (WriteGraph) {
@ -98,7 +101,7 @@ int main(int argc, char** argv) {
throw std::runtime_error("no input files"); throw std::runtime_error("no input files");
} }
return BuildTargets(graph); return BuildTargets(graph, langMap);
} }
catch(llvmc::error_code& ec) { catch(llvmc::error_code& ec) {
return ec.code(); return ec.code();

View File

@ -826,8 +826,9 @@ bool EmitCaseTest1Arg(const std::string& TestName,
O << "InLangs.count(\"" << OptName << "\") != 0"; O << "InLangs.count(\"" << OptName << "\") != 0";
return true; return true;
} else if (TestName == "in_language") { } else if (TestName == "in_language") {
// TODO: remove this restriction
// Works only for cmd_line! // Works only for cmd_line!
O << "GetLanguage(inFile) == \"" << OptName << '\"'; O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
return true; return true;
} else if (TestName == "not_empty") { } else if (TestName == "not_empty") {
if (OptName == "o") { if (OptName == "o") {
@ -1205,7 +1206,8 @@ void EmitGenerateActionMethod (const ToolProperties& P,
O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n"; O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
O << Indent2 << "const sys::Path& outFile,\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" << Indent1 << "{\n"
<< Indent2 << "const char* cmd;\n" << Indent2 << "const char* cmd;\n"
<< Indent2 << "std::vector<std::string> vec;\n"; << Indent2 << "std::vector<std::string> vec;\n";
@ -1245,7 +1247,8 @@ void EmitGenerateActionMethods (const ToolProperties& P,
if (!P.isJoin()) if (!P.isJoin())
O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n" O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
<< Indent2 << "const llvm::sys::Path& outFile,\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" << Indent1 << "{\n"
<< Indent2 << "throw std::runtime_error(\"" << P.Name << Indent2 << "throw std::runtime_error(\"" << P.Name
<< " is not a Join tool!\");\n" << " 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!"); throw std::string("Error in the language map definition!");
// Generate code // Generate code
O << "void llvmc::PopulateLanguageMap() {\n"; O << "void llvmc::PopulateLanguageMap(LanguageMap& langMap) {\n";
for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) { for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i); Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
@ -1460,7 +1463,7 @@ void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes"); const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
for (unsigned i = 0; i < Suffixes->size(); ++i) for (unsigned i = 0; i < Suffixes->size(); ++i)
O << Indent1 << "GlobalLanguageMap[\"" O << Indent1 << "langMap[\""
<< InitPtrToString(Suffixes->getElement(i)) << InitPtrToString(Suffixes->getElement(i))
<< "\"] = \"" << Lang << "\";\n"; << "\"] = \"" << Lang << "\";\n";
} }
@ -1588,8 +1591,7 @@ void EmitPopulateCompilationGraph (Record* CompilationGraph,
ListInit* edges = CompilationGraph->getValueAsListInit("edges"); ListInit* edges = CompilationGraph->getValueAsListInit("edges");
// Generate code // Generate code
O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n" O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n";
<< Indent1 << "PopulateLanguageMap();\n\n";
// Insert vertices // Insert vertices