llvm.org GIT mirror llvm / ec08046
Fix llvm-extract's "writing bitcode to a terminal" warning, which wasn't 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 Dan Gohman 10 years ago
12 changed file(s) with 78 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
4444 Specify the output filename. If filename is "-" (the default), then
4545 B sends its output to standard output.
4646
47 =item B<-S>
48
49 Write output in LLVM intermediate language (instead of bitcode).
50
4751 =back
4852
4953 =head1 EXIT STATUS
239239 /// outputting colored text, or before program exit.
240240 virtual raw_ostream &resetColor() { return *this; }
241241
242 /// This function determines if this stream is connected to a "tty" or
243 /// "console" window. That is, the output would be displayed to the user
244 /// rather than being put on a pipe or stored in a file.
245 virtual bool is_displayed() const { return false; }
246
242247 //===--------------------------------------------------------------------===//
243248 // Subclass Interface
244249 //===--------------------------------------------------------------------===//
369374 virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
370375 bool bg=false);
371376 virtual raw_ostream &resetColor();
377
378 virtual bool is_displayed() const;
372379 };
373380
374381 /// raw_stdout_ostream - This is a stream that always prints to stdout.
9393 /// the user rather than being put on a pipe or stored in a file.
9494 static bool StandardErrIsDisplayed();
9595
96 /// This function determines if the given file descriptor is connected to
97 /// a "tty" or "console" window. That is, the output would be displayed to
98 /// the user rather than being put on a pipe or stored in a file.
99 static bool FileDescriptorIsDisplayed(int fd);
100
96101 /// This function determines the number of columns in the window
97102 /// if standard output is connected to a "tty" or "console"
98103 /// window. If standard output is not connected to a tty or
1919
2020 bool llvm::CheckBitcodeOutputToConsole(raw_ostream &stream_to_check,
2121 bool print_warning) {
22 if (&stream_to_check == &outs() &&
23 sys::Process::StandardOutIsDisplayed()) {
22 if (stream_to_check.is_displayed()) {
2423 if (print_warning) {
2524 errs() << "WARNING: You're attempting to print out a bitcode file.\n"
2625 << "This is inadvisable as it may cause display problems. If\n"
453453 return *this;
454454 }
455455
456 bool raw_fd_ostream::is_displayed() const {
457 return sys::Process::FileDescriptorIsDisplayed(FD);
458 }
459
456460 //===----------------------------------------------------------------------===//
457461 // raw_stdout/err_ostream
458462 //===----------------------------------------------------------------------===//
178178 }
179179
180180 bool Process::StandardInIsUserInput() {
181 return FileDescriptorIsDisplayed(STDIN_FILENO);
182 }
183
184 bool Process::StandardOutIsDisplayed() {
185 return FileDescriptorIsDisplayed(STDOUT_FILENO);
186 }
187
188 bool Process::StandardErrIsDisplayed() {
189 return FileDescriptorIsDisplayed(STDERR_FILENO);
190 }
191
192 bool Process::FileDescriptorIsDisplayed(int fd) {
181193 #if HAVE_ISATTY
182 return isatty(0);
183 #else
184 // If we don't have isatty, just return false.
185 return false;
186 #endif
187 }
188
189 bool Process::StandardOutIsDisplayed() {
190 #if HAVE_ISATTY
191 return isatty(1);
192 #else
193 // If we don't have isatty, just return false.
194 return false;
195 #endif
196 }
197
198 bool Process::StandardErrIsDisplayed() {
199 #if HAVE_ISATTY
200 return isatty(2);
194 return isatty(fd);
201195 #else
202196 // If we don't have isatty, just return false.
203197 return false;
119119 }
120120
121121 bool Process::StandardInIsUserInput() {
122 return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR;
122 return FileDescriptorIsDisplayed(0);
123123 }
124124
125125 bool Process::StandardOutIsDisplayed() {
126 return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR;
126 return FileDescriptorIsDisplayed(1);
127127 }
128128
129129 bool Process::StandardErrIsDisplayed() {
130 return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR;
130 return FileDescriptorIsDisplayed(2);
131 }
132
133 bool Process::FileDescriptorIsDisplayed(int fd) {
134 return GetFileType((HANDLE)_get_osfhandle(fd)) == FILE_TYPE_CHAR;
131135 }
132136
133137 unsigned Process::StandardOutColumns() {
9393 OutputFilename += ".bc";
9494 }
9595 }
96
96
97 // Make sure that the Out file gets unlinked from the disk if we get a
98 // SIGINT.
99 if (OutputFilename != "-")
100 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
101
97102 std::string ErrorInfo;
98103 std::auto_ptr Out
99104 (new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
102107 errs() << ErrorInfo << '\n';
103108 return 1;
104109 }
105
106
107 // Make sure that the Out file gets unlinked from the disk if we get a
108 // SIGINT.
109 if (OutputFilename != "-")
110 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
111110
112111 if (!DisableOutput)
113112 if (Force || !CheckBitcodeOutputToConsole(*Out, true))
8888 OutputFilename = IFN+".ll";
8989 }
9090 }
91
91
92 // Make sure that the Out file gets unlinked from the disk if we get a
93 // SIGINT.
94 if (OutputFilename != "-")
95 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
96
9297 std::string ErrorInfo;
9398 std::auto_ptr
9499 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
97102 errs() << ErrorInfo << '\n';
98103 return 1;
99104 }
100
101 // Make sure that the Out file gets unlinked from the disk if we get a
102 // SIGINT.
103 if (OutputFilename != "-")
104 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
105105
106106 // All that llvm-dis does is write the assembly to a file.
107107 if (!DontPrint) {
99 LEVEL = ../..
1010
1111 TOOLNAME = llvm-extract
12 LINK_COMPONENTS := ipo bitreader bitwriter
12 LINK_COMPONENTS := ipo bitreader bitwriter asmparser
1313
1414 # This tool has no plugins, optimize startup time.
1515 TOOL_NO_EXPORTS = 1
1414 #include "llvm/LLVMContext.h"
1515 #include "llvm/Module.h"
1616 #include "llvm/PassManager.h"
17 #include "llvm/Assembly/PrintModulePass.h"
1718 #include "llvm/Bitcode/ReaderWriter.h"
1819 #include "llvm/Transforms/IPO.h"
1920 #include "llvm/Target/TargetData.h"
2021 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/IRReader.h"
2123 #include "llvm/Support/ManagedStatic.h"
2224 #include "llvm/Support/MemoryBuffer.h"
2325 #include "llvm/Support/PrettyStackTrace.h"
5658 ExtractGlobal("glob", cl::desc("Specify global to extract"), cl::init(""),
5759 cl::value_desc("global"));
5860
61 static cl::opt
62 OutputAssembly("S",
63 cl::desc("Write output as LLVM assembly"), cl::Hidden);
64
5965 int main(int argc, char **argv) {
6066 // Print a stack trace if we signal out.
6167 sys::PrintStackTraceOnErrorSignal();
6571 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
6672 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
6773
74 SMDiagnostic Err;
6875 std::auto_ptr M;
69
70 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename);
71 if (Buffer == 0) {
72 errs() << argv[0] << ": Error reading file '" + InputFilename + "'\n";
73 return 1;
74 } else {
75 M.reset(ParseBitcodeFile(Buffer, Context));
76 }
77 delete Buffer;
78
76 M.reset(ParseIRFile(InputFilename, Err, Context));
77
7978 if (M.get() == 0) {
80 errs() << argv[0] << ": bitcode didn't read correctly.\n";
79 Err.Print(argv[0], errs());
8180 return 1;
8281 }
8382
110109 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
111110 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
112111
112 // Make sure that the Output file gets unlinked from the disk if we get a
113 // SIGINT
114 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
115
113116 std::string ErrorInfo;
114 std::auto_ptr
115 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
116 raw_fd_ostream::F_Binary));
117 raw_fd_ostream Out(OutputFilename.c_str(), ErrorInfo,
118 raw_fd_ostream::F_Binary);
117119 if (!ErrorInfo.empty()) {
118120 errs() << ErrorInfo << '\n';
119121 return 1;
120122 }
121123
122 if (Force || !CheckBitcodeOutputToConsole(*Out, true))
123 Passes.add(createBitcodeWriterPass(*Out));
124 if (OutputAssembly)
125 Passes.add(createPrintModulePass(&Out));
126 else if (Force || !CheckBitcodeOutputToConsole(Out, true))
127 Passes.add(createBitcodeWriterPass(Out));
124128
125129 Passes.run(*M.get());
126130
361361 // FIXME: outs() is not binary!
362362 raw_ostream *Out = &outs(); // Default to printing to stdout...
363363 if (OutputFilename != "-") {
364 // Make sure that the Output file gets unlinked from the disk if we get a
365 // SIGINT
366 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
367
364368 std::string ErrorInfo;
365369 Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
366370 raw_fd_ostream::F_Binary);
369373 delete Out;
370374 return 1;
371375 }
372
373 // Make sure that the Output file gets unlinked from the disk if we get a
374 // SIGINT
375 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
376376 }
377377
378378 // If the output is set to be emitted to standard out, and standard out is a