llvm.org GIT mirror llvm / baa2639
Make LLVM command-line tools overwrite their output files without -f. This is conventional command-line tool behavior. -f now just means "enable binary output on terminals". Add a -f option to llvm-extract and llvm-link, for consistency. Remove F_Force from raw_fd_ostream and enable overwriting and truncating by default. Introduce an F_Excl flag to permit users to enable a failure when the file already exists. This flag is currently unused. Update Makefiles and documentation accordingly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79990 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
22 changed file(s) with 48 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
109109 if (OutputFilename != "-") {
110110 std::string ErrInfo;
111111 out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
112 raw_fd_ostream::F_Force|
113112 raw_fd_ostream::F_Binary);
114113 }
115114 }
273273 errs() << "Writing '" << Filename.str() << "'... ";
274274
275275 std::string ErrorInfo;
276 raw_fd_ostream O(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
276 raw_fd_ostream O(Filename.c_str(), ErrorInfo);
277277
278278 if (ErrorInfo.empty()) {
279279 WriteGraph(O, G, ShortNames, Name, Title);
327327 public:
328328
329329 enum {
330 /// F_Force - When opening a file, this flag makes raw_fd_ostream overwrite
331 /// a file if it already exists instead of emitting an error. This may not
332 /// be specified with F_Append.
333 F_Force = 1,
330 /// F_Excl - When opening a file, this flag makes raw_fd_ostream
331 /// report an error if the file already exists.
332 F_Excl = 1,
334333
335334 /// F_Append - When opening a file, if it already exists append to the
336335 /// existing file instead of returning an error. This may not be specified
337 /// with F_Force.
336 /// with F_Excl.
338337 F_Append = 2,
339338
340339 /// F_Binary - The file should be opened in binary mode on platforms that
341 /// support this distinction.
340 /// make this distinction.
342341 F_Binary = 4
343342 };
344343
137137 errs() << "Writing '" << Filename << "'...";
138138
139139 std::string ErrorInfo;
140 raw_fd_ostream File(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
140 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
141141
142142 if (ErrorInfo.empty())
143143 WriteGraph(File, (const Function*)&F);
169169 errs() << "Writing '" << Filename << "'...";
170170
171171 std::string ErrorInfo;
172 raw_fd_ostream File(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
172 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
173173
174174 if (ErrorInfo.empty())
175175 WriteGraph(File, (const Function*)&F, true);
1717 int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
1818 std::string ErrorInfo;
1919 raw_fd_ostream OS(Path, ErrorInfo,
20 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
20 raw_fd_ostream::F_Binary);
2121
2222 if (!ErrorInfo.empty())
2323 return -1;
334334 /// if no error occurred.
335335 raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
336336 unsigned Flags) : pos(0) {
337 // Verify that we don't have both "append" and "force".
338 assert((!(Flags & F_Force) || !(Flags & F_Append)) &&
339 "Cannot specify both 'force' and 'append' file creation flags!");
337 // Verify that we don't have both "append" and "excl".
338 assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
339 "Cannot specify both 'excl' and 'append' file creation flags!");
340340
341341 ErrorInfo.clear();
342342
357357 OpenFlags |= O_BINARY;
358358 #endif
359359
360 if (Flags & F_Force)
361 OpenFlags |= O_TRUNC;
362 else if (Flags & F_Append)
360 if (Flags & F_Append)
363361 OpenFlags |= O_APPEND;
364362 else
363 OpenFlags |= O_TRUNC;
364 if (Flags & F_Excl)
365365 OpenFlags |= O_EXCL;
366366
367367 FD = open(Filename, OpenFlags, 0664);
336336 sys::RemoveFileOnSignal(uniqueFilename);
337337
338338 std::string ErrorInfo;
339 raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo,
340 raw_fd_ostream::F_Force);
339 raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo);
341340 if (!ErrorInfo.empty()) {
342341 outs() << "*** Basic Block extraction failed!\n";
343342 errs() << "Error writing list of blocks to not extract: " << ErrorInfo
5252 Module *M) const {
5353 std::string ErrInfo;
5454 raw_fd_ostream Out(Filename.c_str(), ErrInfo,
55 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
55 raw_fd_ostream::F_Binary);
5656 if (!ErrInfo.empty()) return true;
5757
5858 WriteBitcodeToFile(M ? M : Program, Out);
8484 int BugDriver::runPassesAsChild(const std::vector &Passes) {
8585 std::string ErrInfo;
8686 raw_fd_ostream OutFile(ChildOutput.c_str(), ErrInfo,
87 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
87 raw_fd_ostream::F_Binary);
8888 if (!ErrInfo.empty()) {
8989 errs() << "Error opening bitcode file: " << ChildOutput << "\n";
9090 return 1;
147147
148148 std::string ErrInfo;
149149 raw_fd_ostream InFile(inputFilename.c_str(), ErrInfo,
150 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
150 raw_fd_ostream::F_Binary);
151151
152152
153153 if (!ErrInfo.empty()) {
367367
368368 LLCArgs.push_back ("-o");
369369 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
370 LLCArgs.push_back ("-f"); // Overwrite as necessary...
371370 LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode
372371 LLCArgs.push_back (0);
373372
363363 }
364364 raw_fd_ostream *objFile =
365365 new raw_fd_ostream(uniqueObjPath.c_str(), ErrMsg,
366 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
366 raw_fd_ostream::F_Binary);
367367 if (!ErrMsg.empty()) {
368368 delete objFile;
369369 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
5454 static cl::opt
5555 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
5656
57 static cl::opt Force("f", cl::desc("Overwrite output files"));
57 static cl::opt
58 Force("f", cl::desc("Enable binary output on terminals"));
5859
5960 // Determine optimization level.
6061 static cl::opt
138139 std::string error;
139140 raw_fd_ostream *FDOut =
140141 new raw_fd_ostream(OutputFilename.c_str(), error,
141 (Force ? raw_fd_ostream::F_Force : 0)|
142142 raw_fd_ostream::F_Binary);
143143 if (!error.empty()) {
144144 errs() << error << '\n';
145 if (!Force)
146 errs() << "Use -f command line argument to force output\n";
147145 delete FDOut;
148146 return 0;
149147 }
189187
190188 std::string error;
191189 unsigned OpenFlags = 0;
192 if (Force) OpenFlags |= raw_fd_ostream::F_Force;
193190 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
194191 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), error,
195192 OpenFlags);
196193 if (!error.empty()) {
197194 errs() << error << '\n';
198 if (!Force)
199 errs() << "Use -f command line argument to force output\n";
200195 delete FDOut;
201196 return 0;
202197 }
3737 cl::value_desc("filename"));
3838
3939 static cl::opt
40 Force("f", cl::desc("Overwrite output files"));
40 Force("f", cl::desc("Enable binary output on terminals"));
4141
4242 static cl::opt
4343 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
9797 std::string ErrorInfo;
9898 std::auto_ptr Out
9999 (new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
100 (Force?raw_fd_ostream::F_Force : 0) |
101100 raw_fd_ostream::F_Binary));
102101 if (!ErrorInfo.empty()) {
103102 errs() << ErrorInfo << '\n';
104 if (!Force)
105 errs() << "Use -f command line argument to force output\n";
106103 return 1;
107104 }
108105
3737 cl::value_desc("filename"));
3838
3939 static cl::opt
40 Force("f", cl::desc("Overwrite output files"));
40 Force("f", cl::desc("Enable binary output on terminals"));
4141
4242 static cl::opt
4343 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
9292 std::string ErrorInfo;
9393 std::auto_ptr
9494 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
95 (Force?raw_fd_ostream::F_Force : 0) |
9695 raw_fd_ostream::F_Binary));
9796 if (!ErrorInfo.empty()) {
9897 errs() << ErrorInfo << '\n';
99 if (!Force)
100 errs() << "Use -f command line argument to force output\n";
10198 return 1;
10299 }
103100
2222 #include "llvm/Support/MemoryBuffer.h"
2323 #include "llvm/Support/PrettyStackTrace.h"
2424 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/SystemUtils.h"
2526 #include "llvm/System/Signals.h"
2627 #include
2728 using namespace llvm;
3637 cl::value_desc("filename"), cl::init("-"));
3738
3839 static cl::opt
39 Force("f", cl::desc("Overwrite output files"));
40 Force("f", cl::desc("Enable binary output on terminals"));
4041
4142 static cl::opt
4243 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
112113 std::string ErrorInfo;
113114 std::auto_ptr
114115 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
115 raw_fd_ostream::F_Binary |
116 (Force ? raw_fd_ostream::F_Force : 0)));
116 raw_fd_ostream::F_Binary));
117117 if (!ErrorInfo.empty()) {
118118 errs() << ErrorInfo << '\n';
119 if (!Force)
120 errs() << "Use -f command line argument to force output\n";
121119 return 1;
122120 }
123121
124 Passes.add(createBitcodeWriterPass(*Out));
122 if (Force || !CheckBitcodeOutputToConsole(*Out, true))
123 Passes.add(createBitcodeWriterPass(*Out));
124
125125 Passes.run(*M.get());
126126
127127 return 0;
1111 // Additionally, this program outputs a shell script that is used to invoke LLI
1212 // to execute the program. In this manner, the generated executable (a.out for
1313 // example), is directly executable, whereas the bitcode file actually lives in
14 // the a.out.bc file generated by this program. Also, Force is on by default.
14 // the a.out.bc file generated by this program.
1515 //
1616 // Note that if someone (or a script) deletes the executable program generated,
1717 // the .bc file will be left around. Considering that this is a temporary hack,
230230 // Create the output file.
231231 std::string ErrorInfo;
232232 raw_fd_ostream Out(FileName.c_str(), ErrorInfo,
233 raw_fd_ostream::F_Force | raw_fd_ostream::F_Binary);
233 raw_fd_ostream::F_Binary);
234234 if (!ErrorInfo.empty())
235235 PrintAndExit(ErrorInfo);
236236
427427
428428 // Output the script to start the program...
429429 std::string ErrorInfo;
430 raw_fd_ostream Out2(OutputFilename.c_str(), ErrorInfo,
431 llvm::raw_fd_ostream::F_Force);
430 raw_fd_ostream Out2(OutputFilename.c_str(), ErrorInfo);
432431 if (!ErrorInfo.empty())
433432 PrintAndExit(ErrorInfo);
434433
2121 #include "llvm/Support/MemoryBuffer.h"
2222 #include "llvm/Support/PrettyStackTrace.h"
2323 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/SystemUtils.h"
2425 #include "llvm/System/Signals.h"
2526 #include "llvm/System/Path.h"
2627 #include
3435 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
3536 cl::value_desc("filename"));
3637
37 static cl::opt Force("f", cl::desc("Overwrite output files"));
38 static cl::opt
39 Force("f", cl::desc("Enable binary output on terminals"));
3840
3941 static cl::opt
4042 Verbose("v", cl::desc("Print information about actions taken"));
121123 std::string ErrorInfo;
122124 std::auto_ptr
123125 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
124 raw_fd_ostream::F_Binary |
125 (Force ? raw_fd_ostream::F_Force : 0)));
126 raw_fd_ostream::F_Binary));
126127 if (!ErrorInfo.empty()) {
127128 errs() << ErrorInfo << '\n';
128 if (!Force)
129 errs() << "Use -f command line argument to force output\n";
130129 return 1;
131130 }
132131
141140 }
142141
143142 if (Verbose) errs() << "Writing bitcode...\n";
144 WriteBitcodeToFile(Composite.get(), *Out);
143 if (Force || !CheckBitcodeOutputToConsole(*Out, true))
144 WriteBitcodeToFile(Composite.get(), *Out);
145145
146146 return 0;
147147 }
5252 clEnumValEnd));
5353
5454 static cl::opt
55 Force("f", cl::desc("Overwrite output files"));
55 Force("f", cl::desc("Enable binary output on terminals"));
5656
5757 static cl::list
5858 IncludeDirs("I", cl::desc("Directory of include files"),
183183
184184 std::string Err;
185185 raw_fd_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Err,
186 raw_fd_ostream::F_Binary |
187 (Force ? raw_fd_ostream::F_Force : 0));
186 raw_fd_ostream::F_Binary);
188187 if (!Err.empty()) {
189188 errs() << Err << '\n';
190 if (!Force)
191 errs() << "Use -f command line argument to force output\n";
192189 delete Out;
193190 return 0;
194191 }
147147 // create output file
148148 std::string ErrInfo;
149149 raw_fd_ostream Out(path, ErrInfo,
150 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
150 raw_fd_ostream::F_Binary);
151151 if (!ErrInfo.empty()) {
152152 errMsg = "could not open bitcode file for writing: ";
153153 errMsg += path;
178178 // generate assembly code
179179 bool genResult = false;
180180 {
181 raw_fd_ostream asmFD(uniqueAsmPath.c_str(), errMsg,
182 raw_fd_ostream::F_Force);
181 raw_fd_ostream asmFD(uniqueAsmPath.c_str(), errMsg);
183182 formatted_raw_ostream asmFile(asmFD);
184183 if (!errMsg.empty())
185184 return NULL;
2828 std::string Filename = GraphName + ".dot";
2929 O << "Writing '" << Filename << "'...";
3030 std::string ErrInfo;
31 raw_fd_ostream F(Filename.c_str(), ErrInfo, raw_fd_ostream::F_Force);
31 raw_fd_ostream F(Filename.c_str(), ErrInfo);
3232
3333 if (ErrInfo.empty())
3434 WriteGraph(F, GT);
5454 cl::value_desc("filename"), cl::init("-"));
5555
5656 static cl::opt
57 Force("f", cl::desc("Overwrite output files"));
57 Force("f", cl::desc("Enable binary output on terminals"));
5858
5959 static cl::opt
6060 PrintEachXForm("p", cl::desc("Print module after each transformation"));
366366 if (OutputFilename != "-") {
367367 std::string ErrorInfo;
368368 Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
369 raw_fd_ostream::F_Binary |
370 (Force ? raw_fd_ostream::F_Force : 0));
369 raw_fd_ostream::F_Binary);
371370 if (!ErrorInfo.empty()) {
372371 errs() << ErrorInfo << '\n';
373 if (!Force)
374 errs() << "Use -f command line argument to force output\n";
375372 delete Out;
376373 return 1;
377374 }
6565 outs() << argv[0] << ": Updating '" << OutputFilename
6666 << "', contents changed.\n";
6767 raw_fd_ostream OutStream(OutputFilename.c_str(), ErrorStr,
68 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
68 raw_fd_ostream::F_Binary);
6969 if (!ErrorStr.empty()) {
7070 errs() << argv[0] << ": Unable to write output '"
7171 << OutputFilename << "': " << ErrorStr << '\n';
170170 raw_ostream *Out = &outs();
171171 if (OutputFilename != "-") {
172172 std::string Error;
173 Out = new raw_fd_ostream(OutputFilename.c_str(), Error,
174 raw_fd_ostream::F_Force);
173 Out = new raw_fd_ostream(OutputFilename.c_str(), Error);
175174
176175 if (!Error.empty()) {
177176 errs() << argv[0] << ": error opening " << OutputFilename