llvm.org GIT mirror llvm / a1bdced
Add a Force option to raw_fd_ostream to specify whether opening an existing file is considered an error. Convert several tools to use raw_fd_ostream instead of std::ostream, and to use this new option instead of doing a manual check. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75801 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
12 changed file(s) with 120 addition(s) and 150 deletion(s). Raw diff Collapse all Expand all
250250 /// stream will use stdout instead.
251251 /// \param Binary - The file should be opened in binary mode on
252252 /// platforms that support this distinction.
253 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
253 /// \param Force - Don't consider the case where the file already
254 /// exists to be an error.
255 raw_fd_ostream(const char *Filename, bool Binary, bool Force,
256 std::string &ErrorInfo);
254257
255258 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
256259 /// ShouldClose is true, this closes the file when the stream is destroyed.
245245 /// occurs, information about the error is put into ErrorInfo, and the
246246 /// stream should be immediately destroyed; the string will be empty
247247 /// if no error occurred.
248 raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
248 raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
249249 std::string &ErrorInfo) : pos(0) {
250250 ErrorInfo.clear();
251251
265265 if (Binary)
266266 Flags |= O_BINARY;
267267 #endif
268 if (!Force)
269 Flags |= O_EXCL;
268270 FD = open(Filename, Flags, 0664);
269271 if (FD < 0) {
270272 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
1919 #include "llvm/Support/FileUtilities.h"
2020 #include
2121 #include
22 #include
2322 using namespace llvm;
2423
2524 namespace {
157156 LLIArgs.push_back(Args[i].c_str());
158157 LLIArgs.push_back(0);
159158
160 std::cout << "" << std::flush;
159 outs() << ""; outs().flush();
161160 DEBUG(errs() << "\nAbout to run:\t";
162161 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
163162 errs() << " " << LLIArgs[i];
311310 LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode
312311 LLCArgs.push_back (0);
313312
314 std::cout << "" << std::flush;
313 outs() << ""; outs().flush();
315314 DEBUG(errs() << "\nAbout to run:\t";
316315 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
317316 errs() << " " << LLCArgs[i];
428427 JITArgs.push_back(Args[i].c_str());
429428 JITArgs.push_back(0);
430429
431 std::cout << "" << std::flush;
430 outs() << ""; outs().flush();
432431 DEBUG(errs() << "\nAbout to run:\t";
433432 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
434433 errs() << " " << JITArgs[i];
477476 LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode
478477 LLCArgs.push_back (0);
479478
480 std::cout << "" << std::flush;
479 outs() << ""; outs().flush();
481480 DEBUG(errs() << "\nAbout to run:\t";
482481 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
483482 errs() << " " << LLCArgs[i];
620619 #endif
621620 GCCArgs.push_back(0); // NULL terminator
622621
623 std::cout << "" << std::flush;
622 outs() << ""; outs().flush();
624623 DEBUG(errs() << "\nAbout to run:\t";
625624 for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
626625 errs() << " " << GCCArgs[i];
664663 ProgramArgs.push_back(0); // NULL terminator
665664
666665 // Now that we have a binary, run it!
667 std::cout << "" << std::flush;
666 outs() << ""; outs().flush();
668667 DEBUG(errs() << "\nAbout to run:\t";
669668 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
670669 errs() << " " << ProgramArgs[i];
679678 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
680679 Timeout, MemoryLimit);
681680 } else {
682 std::cout << "" << std::flush;
681 outs() << ""; outs().flush();
683682 int RemoteClientStatus = RunProgramWithTimeout(sys::Path(RemoteClientPath),
684683 &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
685684 sys::Path(OutputFile), Timeout, MemoryLimit);
755754
756755
757756
758 std::cout << "" << std::flush;
757 outs() << ""; outs().flush();
759758 DEBUG(errs() << "\nAbout to run:\t";
760759 for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
761760 errs() << " " << GCCArgs[i];
361361 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
362362 return LDPS_ERR;
363363 }
364 raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(), true,
364 raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(),
365 /*Binary=*/true,
366 /*Force=*/true,
365367 ErrMsg);
366368 if (!ErrMsg.empty()) {
367369 delete objFile;
4040 #include "llvm/Config/config.h"
4141 #include "llvm/LinkAllVMCore.h"
4242 #include "llvm/Target/TargetSelect.h"
43 #include
44 #include
4543 #include
4644 using namespace llvm;
4745
132130 if (OutputFilename == "-")
133131 return &fouts();
134132
135 // Specified an output filename?
136 if (!Force && std::ifstream(OutputFilename.c_str())) {
137 // If force is not specified, make sure not to overwrite a file!
138 errs() << ProgName << ": error opening '" << OutputFilename
139 << "': file exists!\n"
140 << "Use -f command line argument to force output\n";
141 return 0;
142 }
143133 // Make sure that the Out file gets unlinked from the disk if we get a
144134 // SIGINT
145135 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
146136
147137 std::string error;
148138 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
149 true, error);
139 /*Binary=*/true, Force, error);
140 if (!error.empty()) {
141 errs() << error << '\n';
142 if (!Force)
143 errs() << "Use -f command line argument to force output\n";
144 delete FDOut;
145 return 0;
146 }
150147 formatted_raw_ostream *Out =
151148 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
152 if (!error.empty()) {
153 errs() << error << '\n';
154 delete Out;
155 return 0;
156 }
157149
158150 return Out;
159151 }
188180 break;
189181 }
190182
191 if (!Force && std::ifstream(OutputFilename.c_str())) {
192 // If force is not specified, make sure not to overwrite a file!
193 errs() << ProgName << ": error opening '" << OutputFilename
194 << "': file exists!\n"
195 << "Use -f command line argument to force output\n";
196 return 0;
197 }
198
199183 // Make sure that the Out file gets unlinked from the disk if we get a
200184 // SIGINT
201185 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
202186
203187 std::string error;
204188 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
205 Binary, error);
189 Binary, Force, error);
190 if (!error.empty()) {
191 errs() << error << '\n';
192 if (!Force)
193 errs() << "Use -f command line argument to force output\n";
194 delete FDOut;
195 return 0;
196 }
197
206198 formatted_raw_ostream *Out =
207199 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
208 if (!error.empty()) {
209 errs() << error << '\n';
210 delete Out;
211 return 0;
212 }
213200
214201 return Out;
215202 }
2727 #include "llvm/Support/SystemUtils.h"
2828 #include "llvm/Support/raw_ostream.h"
2929 #include "llvm/System/Signals.h"
30 #include
31 #include
3230 #include
3331 using namespace llvm;
3432
6159 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
6260
6361 int exitCode = 0;
64 std::ostream *Out = 0;
62 raw_ostream *Out = 0;
6563 try {
6664 // Parse the file now...
6765 SMDiagnostic Err;
8583
8684 if (OutputFilename != "") { // Specified an output filename?
8785 if (OutputFilename != "-") { // Not stdout?
88 if (!Force && std::ifstream(OutputFilename.c_str())) {
89 // If force is not specified, make sure not to overwrite a file!
90 cerr << argv[0] << ": error opening '" << OutputFilename
91 << "': file exists!\n"
92 << "Use -f command line argument to force output\n";
86 std::string ErrorInfo;
87 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
88 Force, ErrorInfo);
89 if (!ErrorInfo.empty()) {
90 errs() << ErrorInfo << '\n';
91 if (!Force)
92 errs() << "Use -f command line argument to force output\n";
93 delete Out;
9394 return 1;
9495 }
95 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
96 std::ios::trunc | std::ios::binary);
9796 } else { // Specified stdout
98 // FIXME: cout is not binary!
99 Out = &std::cout;
97 // FIXME: outs() is not binary!
98 Out = &outs();
10099 }
101100 } else {
102101 if (InputFilename == "-") {
103102 OutputFilename = "-";
104 Out = &std::cout;
103 Out = &outs();
105104 } else {
106105 std::string IFN = InputFilename;
107106 int Len = IFN.length();
113112 }
114113 OutputFilename += ".bc";
115114
116 if (!Force && std::ifstream(OutputFilename.c_str())) {
117 // If force is not specified, make sure not to overwrite a file!
118 cerr << argv[0] << ": error opening '" << OutputFilename
119 << "': file exists!\n"
120 << "Use -f command line argument to force output\n";
115 std::string ErrorInfo;
116 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
117 Force, ErrorInfo);
118 if (!ErrorInfo.empty()) {
119 errs() << ErrorInfo << '\n';
120 if (!Force)
121 errs() << "Use -f command line argument to force output\n";
122 delete Out;
121123 return 1;
122124 }
123
124 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
125 std::ios::trunc | std::ios::binary);
126125 // Make sure that the Out file gets unlinked from the disk if we get a
127126 // SIGINT
128127 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
129128 }
130 }
131
132 if (!Out->good()) {
133 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
134 return 1;
135129 }
136130
137131 if (!DisableOutput)
145139 exitCode = 1;
146140 }
147141
148 if (Out != &std::cout) delete Out;
142 if (Out != &outs()) delete Out;
149143 return exitCode;
150144 }
151145
2727 #include "llvm/Support/Streams.h"
2828 #include "llvm/Support/raw_ostream.h"
2929 #include "llvm/System/Signals.h"
30 #include
31 #include
3230 #include
3331 using namespace llvm;
3432
5553 try {
5654 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
5755
58 std::ostream *Out = &std::cout; // Default to printing to stdout.
56 raw_ostream *Out = &outs(); // Default to printing to stdout.
5957 std::string ErrorMessage;
6058
6159 std::auto_ptr M;
7977 // Just use stdout. We won't actually print anything on it.
8078 } else if (OutputFilename != "") { // Specified an output filename?
8179 if (OutputFilename != "-") { // Not stdout?
82 if (!Force && std::ifstream(OutputFilename.c_str())) {
83 // If force is not specified, make sure not to overwrite a file!
84 cerr << argv[0] << ": error opening '" << OutputFilename
85 << "': file exists! Sending to standard output.\n";
86 } else {
87 Out = new std::ofstream(OutputFilename.c_str());
80 std::string ErrorInfo;
81 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
82 Force, ErrorInfo);
83 if (!ErrorInfo.empty()) {
84 errs() << ErrorInfo << '\n';
85 if (!Force)
86 errs() << "Use -f command line argument to force output\n";
87 delete Out;
88 return 1;
8889 }
8990 }
9091 } else {
100101 OutputFilename = IFN+".ll";
101102 }
102103
103 if (!Force && std::ifstream(OutputFilename.c_str())) {
104 // If force is not specified, make sure not to overwrite a file!
105 cerr << argv[0] << ": error opening '" << OutputFilename
106 << "': file exists! Sending to standard output.\n";
107 } else {
108 Out = new std::ofstream(OutputFilename.c_str());
104 std::string ErrorInfo;
105 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
106 Force, ErrorInfo);
107 if (!ErrorInfo.empty()) {
108 errs() << ErrorInfo << '\n';
109 if (!Force)
110 errs() << "Use -f command line argument to force output\n";
111 delete Out;
112 return 1;
113 }
109114
110 // Make sure that the Out file gets unlinked from the disk if we get a
111 // SIGINT
112 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
113 }
115 // Make sure that the Out file gets unlinked from the disk if we get a
116 // SIGINT
117 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
114118 }
115 }
116
117 if (!Out->good()) {
118 cerr << argv[0] << ": error opening " << OutputFilename
119 << ": sending to stdout instead!\n";
120 Out = &std::cout;
121119 }
122120
123121 // All that llvm-dis does is write the assembly to a file.
124122 if (!DontPrint) {
125123 PassManager Passes;
126 raw_os_ostream L(*Out);
127 Passes.add(createPrintModulePass(&L));
124 Passes.add(createPrintModulePass(Out));
128125 Passes.run(*M.get());
129126 }
130127
131 if (Out != &std::cout) {
132 ((std::ofstream*)Out)->close();
128 if (Out != &outs())
133129 delete Out;
134 }
135130 return 0;
136131 } catch (const std::string& msg) {
137132 cerr << argv[0] << ": " << msg << "\n";
2121 #include "llvm/Support/ManagedStatic.h"
2222 #include "llvm/Support/MemoryBuffer.h"
2323 #include "llvm/Support/PrettyStackTrace.h"
24 #include "llvm/Support/raw_ostream.h"
2425 #include "llvm/System/Signals.h"
25 #include
2626 #include
27 #include
2827 using namespace llvm;
2928
3029 // InputFilename - The filename to read from.
110109 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
111110 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
112111
113 std::ostream *Out = 0;
112 raw_ostream *Out = 0;
114113
115114 if (OutputFilename != "-") { // Not stdout?
116 if (!Force && std::ifstream(OutputFilename.c_str())) {
117 // If force is not specified, make sure not to overwrite a file!
118 cerr << argv[0] << ": error opening '" << OutputFilename
119 << "': file exists!\n"
120 << "Use -f command line argument to force output\n";
115 std::string ErrorInfo;
116 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
117 Force, ErrorInfo);
118 if (!ErrorInfo.empty()) {
119 errs() << ErrorInfo << '\n';
120 if (!Force)
121 errs() << "Use -f command line argument to force output\n";
122 delete Out;
121123 return 1;
122124 }
123 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
124 std::ios::binary;
125 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
126125 } else { // Specified stdout
127 // FIXME: cout is not binary!
128 Out = &std::cout;
126 // FIXME: errs() is not binary!
127 Out = &errs();
129128 }
130129
131 Passes.add(CreateBitcodeWriterPass(*Out));
130 Passes.add(createBitcodeWriterPass(*Out));
132131 Passes.run(*M.get());
133132
134 if (Out != &std::cout)
133 if (Out != &errs())
135134 delete Out;
136135 return 0;
137136 }
2323 #include "llvm/Support/Streams.h"
2424 #include "llvm/System/Signals.h"
2525 #include "llvm/System/Path.h"
26 #include
27 #include
2826 #include
2927 using namespace llvm;
3028
121119 if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
122120
123121 // FIXME: cout is not binary!
124 std::ostream *Out = &std::cout; // Default to printing to stdout...
122 raw_ostream *Out = &outs(); // Default to printing to stdout...
125123 if (OutputFilename != "-") {
126 if (!Force && std::ifstream(OutputFilename.c_str())) {
127 // If force is not specified, make sure not to overwrite a file!
128 cerr << argv[0] << ": error opening '" << OutputFilename
129 << "': file exists!\n"
130 << "Use -f command line argument to force output\n";
131 return 1;
132 }
133 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
134 std::ios::binary;
135 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
136 if (!Out->good()) {
137 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
124 std::string ErrorInfo;
125 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
126 Force, ErrorInfo);
127 if (!ErrorInfo.empty()) {
128 errs() << ErrorInfo << '\n';
129 if (!Force)
130 errs() << "Use -f command line argument to force output\n";
131 delete Out;
138132 return 1;
139133 }
140134
151145 if (Verbose) cerr << "Writing bitcode...\n";
152146 WriteBitcodeToFile(Composite.get(), *Out);
153147
154 if (Out != &std::cout) delete Out;
148 if (Out != &outs()) delete Out;
155149 return 0;
156150 }
185185 bool genResult = false;
186186 {
187187 raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(),
188 false, errMsg));
188 /*Binary=*/false, /*Force=*/true,
189 errMsg));
189190 formatted_raw_ostream asmFile(asmFD);
190191 if (!errMsg.empty())
191192 return NULL;
3434 #include "llvm/Support/raw_ostream.h"
3535 #include "llvm/LinkAllPasses.h"
3636 #include "llvm/LinkAllVMCore.h"
37 #include
38 #include
3937 #include
4038 #include
4139 using namespace llvm;
341339
342340 // Figure out what stream we are supposed to write to...
343341 // FIXME: cout is not binary!
344 std::ostream *Out = &std::cout; // Default to printing to stdout...
342 raw_ostream *Out = &outs(); // Default to printing to stdout...
345343 if (OutputFilename != "-") {
346 if (!Force && std::ifstream(OutputFilename.c_str())) {
347 // If force is not specified, make sure not to overwrite a file!
348 cerr << argv[0] << ": error opening '" << OutputFilename
349 << "': file exists!\n"
350 << "Use -f command line argument to force output\n";
351 return 1;
352 }
353 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
354 std::ios::binary;
355 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
356
357 if (!Out->good()) {
358 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
344 std::string ErrorInfo;
345 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
346 Force, ErrorInfo);
347 if (!ErrorInfo.empty()) {
348 errs() << ErrorInfo << '\n';
349 if (!Force)
350 errs() << "Use -f command line argument to force output\n";
351 delete Out;
359352 return 1;
360353 }
361354
478471
479472 // Write bitcode out to disk or cout as the last step...
480473 if (!NoOutput && !AnalyzeOnly)
481 Passes.add(CreateBitcodeWriterPass(*Out));
474 Passes.add(createBitcodeWriterPass(*Out));
482475
483476 // Now that we have all of the passes ready, run them.
484477 Passes.run(*M.get());
485478
486479 // Delete the ofstream.
487 if (Out != &std::cout)
480 if (Out != &outs())
488481 delete Out;
489482 return 0;
490483
170170 raw_ostream *Out = &outs();
171171 if (OutputFilename != "-") {
172172 std::string Error;
173 Out = new raw_fd_ostream(OutputFilename.c_str(), false, Error);
173 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
174 /*Force=*/true, Error);
174175
175176 if (!Error.empty()) {
176177 errs() << argv[0] << ": error opening " << OutputFilename