Fix llvm-extract's "writing bitcode to a terminal" warning, which wasn't
authorDan Gohman <gohman@apple.com>
Fri, 11 Sep 2009 20:46:33 +0000 (20:46 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 11 Sep 2009 20:46:33 +0000 (20:46 +0000)
working. To support this, add an is_displayed() function to raw_ostream,
and generalize Process::StandardOutIsDisplayed and friends in order to
support it.

Also, call RemoveFileOnSignal before creating a file instead of after, so
that the file isn't left behind if the program is interrupted between when
the file is created and RemoveFileOnSignal is called.

While here, add a -S to llvm-extract and port it to IRReader so that it
supports assembly input.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81568 91177308-0d34-0410-b5e6-96231b3b80d8

12 files changed:
docs/CommandGuide/llvm-extract.pod
include/llvm/Support/raw_ostream.h
include/llvm/System/Process.h
lib/Support/SystemUtils.cpp
lib/Support/raw_ostream.cpp
lib/System/Unix/Process.inc
lib/System/Win32/Process.inc
tools/llvm-as/llvm-as.cpp
tools/llvm-dis/llvm-dis.cpp
tools/llvm-extract/Makefile
tools/llvm-extract/llvm-extract.cpp
tools/opt/opt.cpp

index c3bc019c6b8f169d0263992455c87e37d374d5e0..b62e8ae312bf482541b778f1e5b657f5f3e8ce6e 100644 (file)
@@ -45,6 +45,10 @@ Print a summary of command line options.
 Specify the output filename.  If filename is "-" (the default), then
 B<llvm-extract> sends its output to standard output.
 
+=item B<-S>
+
+Write output in LLVM intermediate language (instead of bitcode).
+
 =back
 
 =head1 EXIT STATUS
index a01d4cdb040baaf9a7eb4b6f836258726a2462a5..dbad40ca91c19873cc1b3413977f5c0642c624a9 100644 (file)
@@ -240,6 +240,11 @@ public:
   /// outputting colored text, or before program exit.
   virtual raw_ostream &resetColor() { return *this; }
 
+  /// This function determines if this stream is connected to a "tty" or
+  /// "console" window. That is, the output would be displayed to the user
+  /// rather than being put on a pipe or stored in a file.
+  virtual bool is_displayed() const { return false; }
+
   //===--------------------------------------------------------------------===//
   // Subclass Interface
   //===--------------------------------------------------------------------===//
@@ -370,6 +375,8 @@ public:
   virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
                                    bool bg=false);
   virtual raw_ostream &resetColor();
+
+  virtual bool is_displayed() const;
 };
 
 /// raw_stdout_ostream - This is a stream that always prints to stdout.
index 11dbf759a6c4f8cffe2213c1d15c9d7f60c63530..010499acd4bfeff228b7cf81d4fd834d738a1239 100644 (file)
@@ -94,6 +94,11 @@ namespace sys {
       /// the user rather than being put on a pipe or stored in a file.
       static bool StandardErrIsDisplayed();
 
+      /// This function determines if the given file descriptor is connected to
+      /// a "tty" or "console" window. That is, the output would be displayed to
+      /// the user rather than being put on a pipe or stored in a file.
+      static bool FileDescriptorIsDisplayed(int fd);
+
       /// This function determines the number of columns in the window
       /// if standard output is connected to a "tty" or "console"
       /// window. If standard output is not connected to a tty or
index 3d3649eaebabfe4403c2bd148d2f596d58a96f2b..299032f1871562b6f35fef059c2b8d5c638fa8a4 100644 (file)
@@ -20,8 +20,7 @@ using namespace llvm;
 
 bool llvm::CheckBitcodeOutputToConsole(raw_ostream &stream_to_check,
                                        bool print_warning) {
-  if (&stream_to_check == &outs() &&
-      sys::Process::StandardOutIsDisplayed()) {
+  if (stream_to_check.is_displayed()) {
     if (print_warning) {
       errs() << "WARNING: You're attempting to print out a bitcode file.\n"
              << "This is inadvisable as it may cause display problems. If\n"
index a229efde6ea1eb956a19b73b617aa7701e05d461..2cb3771287296ee04129746bd70b13563e020468 100644 (file)
@@ -454,6 +454,10 @@ raw_ostream &raw_fd_ostream::resetColor() {
   return *this;
 }
 
+bool raw_fd_ostream::is_displayed() const {
+  return sys::Process::FileDescriptorIsDisplayed(FD);
+}
+
 //===----------------------------------------------------------------------===//
 //  raw_stdout/err_ostream
 //===----------------------------------------------------------------------===//
index 774783f8ad0ecf82f673ec0b696270be8c686e96..d7155852bf2aa5de55ffc2b82c80b222d82252b1 100644 (file)
@@ -179,26 +179,20 @@ void Process::PreventCoreFiles() {
 }
 
 bool Process::StandardInIsUserInput() {
-#if HAVE_ISATTY
-  return isatty(0);
-#else
-  // If we don't have isatty, just return false.
-  return false;
-#endif
+  return FileDescriptorIsDisplayed(STDIN_FILENO);
 }
 
 bool Process::StandardOutIsDisplayed() {
-#if HAVE_ISATTY
-  return isatty(1);
-#else
-  // If we don't have isatty, just return false.
-  return false;
-#endif
+  return FileDescriptorIsDisplayed(STDOUT_FILENO);
 }
 
 bool Process::StandardErrIsDisplayed() {
+  return FileDescriptorIsDisplayed(STDERR_FILENO);
+}
+
+bool Process::FileDescriptorIsDisplayed(int fd) {
 #if HAVE_ISATTY
-  return isatty(2);
+  return isatty(fd);
 #else
   // If we don't have isatty, just return false.
   return false;
index cfbe33c85a2f33e5e5df3005b2766e56ed8b5b7c..feb0806116e46bbb1b545449227d436fd81f6aa9 100644 (file)
@@ -120,15 +120,19 @@ void Process::PreventCoreFiles() {
 }
 
 bool Process::StandardInIsUserInput() {
-  return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR;
+  return FileDescriptorIsDisplayed(0);
 }
 
 bool Process::StandardOutIsDisplayed() {
-  return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR;
+  return FileDescriptorIsDisplayed(1);
 }
 
 bool Process::StandardErrIsDisplayed() {
-  return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR;
+  return FileDescriptorIsDisplayed(2);
+}
+
+bool Process::FileDescriptorIsDisplayed(int fd) {
+  return GetFileType((HANDLE)_get_osfhandle(fd)) == FILE_TYPE_CHAR;
 }
 
 unsigned Process::StandardOutColumns() {
index 9027cfc3cf200780c3409a7e95325f5a679da51c..d510297aa35c1ab25229187a6465dd5e25d93a96 100644 (file)
@@ -94,7 +94,12 @@ int main(int argc, char **argv) {
       OutputFilename += ".bc";
     }
   }
-  
+
+  // Make sure that the Out file gets unlinked from the disk if we get a
+  // SIGINT.
+  if (OutputFilename != "-")
+    sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+
   std::string ErrorInfo;
   std::auto_ptr<raw_ostream> Out
   (new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
@@ -103,12 +108,6 @@ int main(int argc, char **argv) {
     errs() << ErrorInfo << '\n';
     return 1;
   }
-  
-  
-  // Make sure that the Out file gets unlinked from the disk if we get a
-  // SIGINT.
-  if (OutputFilename != "-")
-    sys::RemoveFileOnSignal(sys::Path(OutputFilename));
 
   if (!DisableOutput)
     if (Force || !CheckBitcodeOutputToConsole(*Out, true))
index 89073c08843665b07dd8179509394abdbfa6415b..43bde53599cb77764213a0578626e1ea885fe74d 100644 (file)
@@ -89,7 +89,12 @@ int main(int argc, char **argv) {
         OutputFilename = IFN+".ll";
     }
   }
-  
+
+  // Make sure that the Out file gets unlinked from the disk if we get a
+  // SIGINT.
+  if (OutputFilename != "-")
+    sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+
   std::string ErrorInfo;
   std::auto_ptr<raw_fd_ostream> 
   Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
@@ -99,11 +104,6 @@ int main(int argc, char **argv) {
     return 1;
   }
 
-  // Make sure that the Out file gets unlinked from the disk if we get a
-  // SIGINT.
-  if (OutputFilename != "-")
-    sys::RemoveFileOnSignal(sys::Path(OutputFilename));
-
   // All that llvm-dis does is write the assembly to a file.
   if (!DontPrint) {
     PassManager Passes;
index 2ef88415c6a6f2597565c43a44d10573932ff718..5672aa3299a2426f37ccf142f48fc935a50c9363 100644 (file)
@@ -10,7 +10,7 @@
 LEVEL = ../..
 
 TOOLNAME = llvm-extract
-LINK_COMPONENTS := ipo bitreader bitwriter
+LINK_COMPONENTS := ipo bitreader bitwriter asmparser
 
 # This tool has no plugins, optimize startup time.
 TOOL_NO_EXPORTS = 1
index 543d01f9103b22d86a10669c8a2ce9d4369f93b0..517244f55ba4bf4723582abbe2c7ef3651bc7df6 100644 (file)
 #include "llvm/LLVMContext.h"
 #include "llvm/Module.h"
 #include "llvm/PassManager.h"
+#include "llvm/Assembly/PrintModulePass.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/IRReader.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
@@ -57,6 +59,10 @@ static cl::opt<std::string>
 ExtractGlobal("glob", cl::desc("Specify global to extract"), cl::init(""),
               cl::value_desc("global"));
 
+static cl::opt<bool>
+OutputAssembly("S",
+               cl::desc("Write output as LLVM assembly"), cl::Hidden);
+
 int main(int argc, char **argv) {
   // Print a stack trace if we signal out.
   sys::PrintStackTraceOnErrorSignal();
@@ -66,19 +72,12 @@ int main(int argc, char **argv) {
   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
   cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
 
+  SMDiagnostic Err;
   std::auto_ptr<Module> M;
-  
-  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename);
-  if (Buffer == 0) {
-    errs() << argv[0] << ": Error reading file '" + InputFilename + "'\n";
-    return 1;
-  } else {
-    M.reset(ParseBitcodeFile(Buffer, Context));
-  }
-  delete Buffer;
-  
+  M.reset(ParseIRFile(InputFilename, Err, Context));
+
   if (M.get() == 0) {
-    errs() << argv[0] << ": bitcode didn't read correctly.\n";
+    Err.Print(argv[0], errs());
     return 1;
   }
 
@@ -111,17 +110,22 @@ int main(int argc, char **argv) {
   Passes.add(createDeadTypeEliminationPass());   // Remove dead types...
   Passes.add(createStripDeadPrototypesPass());   // Remove dead func decls
 
+  // Make sure that the Output file gets unlinked from the disk if we get a
+  // SIGINT
+  sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+
   std::string ErrorInfo;
-  std::auto_ptr<raw_fd_ostream>
-  Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
-                         raw_fd_ostream::F_Binary));
+  raw_fd_ostream Out(OutputFilename.c_str(), ErrorInfo,
+                     raw_fd_ostream::F_Binary);
   if (!ErrorInfo.empty()) {
     errs() << ErrorInfo << '\n';
     return 1;
   }
 
-  if (Force || !CheckBitcodeOutputToConsole(*Out, true))
-    Passes.add(createBitcodeWriterPass(*Out));
+  if (OutputAssembly)
+    Passes.add(createPrintModulePass(&Out));
+  else if (Force || !CheckBitcodeOutputToConsole(Out, true))
+    Passes.add(createBitcodeWriterPass(Out));
 
   Passes.run(*M.get());
 
index e4700667122452db1b38b7a669b52ce881ddf829..fe0e03649ddc911f4fcd4742ec62cf17c1ebc743 100644 (file)
@@ -362,6 +362,10 @@ int main(int argc, char **argv) {
     // FIXME: outs() is not binary!
     raw_ostream *Out = &outs();  // Default to printing to stdout...
     if (OutputFilename != "-") {
+      // Make sure that the Output file gets unlinked from the disk if we get a
+      // SIGINT
+      sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+
       std::string ErrorInfo;
       Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
                                raw_fd_ostream::F_Binary);
@@ -370,10 +374,6 @@ int main(int argc, char **argv) {
         delete Out;
         return 1;
       }
-
-      // Make sure that the Output file gets unlinked from the disk if we get a
-      // SIGINT
-      sys::RemoveFileOnSignal(sys::Path(OutputFilename));
     }
 
     // If the output is set to be emitted to standard out, and standard out is a