From ba9cc1f78908e6756afdc8515389ec0bef5eb74d Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 18 Feb 2004 16:53:59 +0000 Subject: [PATCH] Give extract -o and -f options, just like every other tool! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11580 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/extract/extract.cpp | 32 +++++++++++++++++++++++++---- tools/llvm-extract/llvm-extract.cpp | 32 +++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/tools/extract/extract.cpp b/tools/extract/extract.cpp index 72412b8b098..9c03f85bf57 100644 --- a/tools/extract/extract.cpp +++ b/tools/extract/extract.cpp @@ -20,7 +20,7 @@ #include "llvm/Target/TargetData.h" #include "Support/CommandLine.h" #include - +#include using namespace llvm; // InputFilename - The filename to read from. @@ -28,6 +28,12 @@ static cl::opt InputFilename(cl::Positional, cl::desc(""), cl::init("-"), cl::value_desc("filename")); +static cl::opt +OutputFilename("o", cl::desc("Specify output filename"), + cl::value_desc("filename"), cl::init("-")); + +static cl::opt +Force("f", cl::desc("Overwrite output files")); // ExtractFunc - The function to extract from the module... defaults to main. static cl::opt @@ -51,8 +57,8 @@ int main(int argc, char **argv) { return 1; } - // In addition to just parsing the input from GCC, we also want to spiff it up - // a little bit. Do this now. + // 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("extract", M.get())); // Use correct TargetData @@ -60,8 +66,26 @@ int main(int argc, char **argv) { Passes.add(createGlobalDCEPass()); // Delete unreachable globals Passes.add(createFunctionResolvingPass()); // Delete prototypes Passes.add(createDeadTypeEliminationPass()); // Remove dead types... - Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file... + std::ostream *Out = 0; + + if (OutputFilename != "-") { // Not stdout? + if (!Force && std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + std::cerr << argv[0] << ": error opening '" << OutputFilename + << "': file exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); + } else { // Specified stdout + Out = &std::cout; + } + + Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file... Passes.run(*M.get()); + + if (Out != &std::cout) + delete Out; return 0; } diff --git a/tools/llvm-extract/llvm-extract.cpp b/tools/llvm-extract/llvm-extract.cpp index 72412b8b098..9c03f85bf57 100644 --- a/tools/llvm-extract/llvm-extract.cpp +++ b/tools/llvm-extract/llvm-extract.cpp @@ -20,7 +20,7 @@ #include "llvm/Target/TargetData.h" #include "Support/CommandLine.h" #include - +#include using namespace llvm; // InputFilename - The filename to read from. @@ -28,6 +28,12 @@ static cl::opt InputFilename(cl::Positional, cl::desc(""), cl::init("-"), cl::value_desc("filename")); +static cl::opt +OutputFilename("o", cl::desc("Specify output filename"), + cl::value_desc("filename"), cl::init("-")); + +static cl::opt +Force("f", cl::desc("Overwrite output files")); // ExtractFunc - The function to extract from the module... defaults to main. static cl::opt @@ -51,8 +57,8 @@ int main(int argc, char **argv) { return 1; } - // In addition to just parsing the input from GCC, we also want to spiff it up - // a little bit. Do this now. + // 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("extract", M.get())); // Use correct TargetData @@ -60,8 +66,26 @@ int main(int argc, char **argv) { Passes.add(createGlobalDCEPass()); // Delete unreachable globals Passes.add(createFunctionResolvingPass()); // Delete prototypes Passes.add(createDeadTypeEliminationPass()); // Remove dead types... - Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file... + std::ostream *Out = 0; + + if (OutputFilename != "-") { // Not stdout? + if (!Force && std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + std::cerr << argv[0] << ": error opening '" << OutputFilename + << "': file exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); + } else { // Specified stdout + Out = &std::cout; + } + + Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file... Passes.run(*M.get()); + + if (Out != &std::cout) + delete Out; return 0; }