From a499d20e8d032909a4af42915a118e4c0cde92cd Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Wed, 10 Feb 2010 23:58:53 +0000 Subject: Add support to llvm-extract for extracting multiple functions and/or multiple global variables at a time. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@95825 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/llvm-extract/llvm-extract.cpp | 52 +++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 23 deletions(-) (limited to 'tools/llvm-extract') diff --git a/tools/llvm-extract/llvm-extract.cpp b/tools/llvm-extract/llvm-extract.cpp index 517244f55b..231634c84b 100644 --- a/tools/llvm-extract/llvm-extract.cpp +++ b/tools/llvm-extract/llvm-extract.cpp @@ -49,15 +49,15 @@ static cl::opt Relink("relink", cl::desc("Turn external linkage for callees of function to delete")); -// ExtractFunc - The function to extract from the module... -static cl::opt -ExtractFunc("func", cl::desc("Specify function to extract"), cl::init(""), - cl::value_desc("function")); +// ExtractFuncs - The functions to extract from the module... +static cl::list +ExtractFuncs("func", cl::desc("Specify function to extract"), + cl::ZeroOrMore, cl::value_desc("function")); -// ExtractGlobal - The global to extract from the module... -static cl::opt -ExtractGlobal("glob", cl::desc("Specify global to extract"), cl::init(""), - cl::value_desc("global")); +// ExtractGlobals - The globals to extract from the module... +static cl::list +ExtractGlobals("glob", cl::desc("Specify global to extract"), + cl::ZeroOrMore, cl::value_desc("global")); static cl::opt OutputAssembly("S", @@ -81,28 +81,34 @@ int main(int argc, char **argv) { return 1; } - // Figure out which function we should extract - GlobalVariable *G = !ExtractGlobal.empty() ? - M.get()->getNamedGlobal(ExtractGlobal) : 0; - - // Figure out which function we should extract - if (ExtractFunc.empty() && ExtractGlobal.empty()) ExtractFunc = "main"; - Function *F = M.get()->getFunction(ExtractFunc); + std::vector GVs; + + // Figure out which globals we should extract. + for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) { + GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]); + if (!GV) { + errs() << argv[0] << ": program doesn't contain global named '" + << ExtractGlobals[i] << "'!\n"; + return 1; + } + GVs.push_back(GV); + } - if (F == 0 && G == 0) { - errs() << argv[0] << ": program doesn't contain function named '" - << ExtractFunc << "' or a global named '" << ExtractGlobal << "'!\n"; - return 1; + // Figure out which functions we should extract. + for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) { + GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]); + if (!GV) { + errs() << argv[0] << ": program doesn't contain function named '" + << ExtractFuncs[i] << "'!\n"; + return 1; + } + GVs.push_back(GV); } // In addition to deleting all other functions, we also want to spiff it // up a little bit. Do this now. PassManager Passes; Passes.add(new TargetData(M.get())); // Use correct TargetData - // Either isolate the function or delete it from the Module - std::vector GVs; - if (F) GVs.push_back(F); - if (G) GVs.push_back(G); Passes.add(createGVExtractionPass(GVs, DeleteFn, Relink)); if (!DeleteFn) -- cgit v1.2.3