[llvm-commits] [parallel] CVS: llvm/tools/extract/extract.cpp

Misha Brukman brukman at cs.uiuc.edu
Mon Mar 1 20:16:47 PST 2004


Changes in directory llvm/tools/extract:

extract.cpp updated: 1.19 -> 1.19.4.1

---
Log message:

Merge from trunk

---
Diffs of the changes:  (+30 -4)

Index: llvm/tools/extract/extract.cpp
diff -u llvm/tools/extract/extract.cpp:1.19 llvm/tools/extract/extract.cpp:1.19.4.1
--- llvm/tools/extract/extract.cpp:1.19	Tue Nov 11 16:41:34 2003
+++ llvm/tools/extract/extract.cpp	Mon Mar  1 17:59:18 2004
@@ -19,8 +19,9 @@
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Target/TargetData.h"
 #include "Support/CommandLine.h"
+#include "Support/Signals.h"
 #include <memory>
-
+#include <fstream>
 using namespace llvm;
 
 // InputFilename - The filename to read from.
@@ -28,6 +29,12 @@
 InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
               cl::init("-"), cl::value_desc("filename"));
               
+static cl::opt<std::string>
+OutputFilename("o", cl::desc("Specify output filename"), 
+               cl::value_desc("filename"), cl::init("-"));
+
+static cl::opt<bool>
+Force("f", cl::desc("Overwrite output files"));
 
 // ExtractFunc - The function to extract from the module... defaults to main.
 static cl::opt<std::string>
@@ -36,6 +43,7 @@
 
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
+  PrintStackTraceOnErrorSignal();
 
   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
   if (M.get() == 0) {
@@ -51,8 +59,8 @@
     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 +68,26 @@
   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;
 }





More information about the llvm-commits mailing list