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
This commit is contained in:
Dan Gohman 2009-08-25 15:34:52 +00:00
parent cf48efcf3d
commit baa26395cc
22 changed files with 48 additions and 72 deletions

View File

@ -110,7 +110,6 @@ int main(int argc, char **argv) {
if (OutputFilename != "-") {
std::string ErrInfo;
out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
raw_fd_ostream::F_Force|
raw_fd_ostream::F_Binary);
}
}

View File

@ -274,7 +274,7 @@ sys::Path WriteGraph(const GraphType &G, const std::string &Name,
errs() << "Writing '" << Filename.str() << "'... ";
std::string ErrorInfo;
raw_fd_ostream O(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
raw_fd_ostream O(Filename.c_str(), ErrorInfo);
if (ErrorInfo.empty()) {
WriteGraph(O, G, ShortNames, Name, Title);

View File

@ -328,18 +328,17 @@ class raw_fd_ostream : public raw_ostream {
public:
enum {
/// F_Force - When opening a file, this flag makes raw_fd_ostream overwrite
/// a file if it already exists instead of emitting an error. This may not
/// be specified with F_Append.
F_Force = 1,
/// F_Excl - When opening a file, this flag makes raw_fd_ostream
/// report an error if the file already exists.
F_Excl = 1,
/// F_Append - When opening a file, if it already exists append to the
/// existing file instead of returning an error. This may not be specified
/// with F_Force.
/// with F_Excl.
F_Append = 2,
/// F_Binary - The file should be opened in binary mode on platforms that
/// support this distinction.
/// make this distinction.
F_Binary = 4
};

View File

@ -138,7 +138,7 @@ namespace {
errs() << "Writing '" << Filename << "'...";
std::string ErrorInfo;
raw_fd_ostream File(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
raw_fd_ostream File(Filename.c_str(), ErrorInfo);
if (ErrorInfo.empty())
WriteGraph(File, (const Function*)&F);
@ -170,7 +170,7 @@ namespace {
errs() << "Writing '" << Filename << "'...";
std::string ErrorInfo;
raw_fd_ostream File(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
raw_fd_ostream File(Filename.c_str(), ErrorInfo);
if (ErrorInfo.empty())
WriteGraph(File, (const Function*)&F, true);

View File

@ -18,7 +18,7 @@ using namespace llvm;
int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
std::string ErrorInfo;
raw_fd_ostream OS(Path, ErrorInfo,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
raw_fd_ostream::F_Binary);
if (!ErrorInfo.empty())
return -1;

View File

@ -335,9 +335,9 @@ void format_object_base::home() {
/// if no error occurred.
raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
unsigned Flags) : pos(0) {
// Verify that we don't have both "append" and "force".
assert((!(Flags & F_Force) || !(Flags & F_Append)) &&
"Cannot specify both 'force' and 'append' file creation flags!");
// Verify that we don't have both "append" and "excl".
assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
"Cannot specify both 'excl' and 'append' file creation flags!");
ErrorInfo.clear();
@ -358,11 +358,11 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
OpenFlags |= O_BINARY;
#endif
if (Flags & F_Force)
OpenFlags |= O_TRUNC;
else if (Flags & F_Append)
if (Flags & F_Append)
OpenFlags |= O_APPEND;
else
OpenFlags |= O_TRUNC;
if (Flags & F_Excl)
OpenFlags |= O_EXCL;
FD = open(Filename, OpenFlags, 0664);

View File

@ -337,8 +337,7 @@ Module *BugDriver::ExtractMappedBlocksFromModule(const
sys::RemoveFileOnSignal(uniqueFilename);
std::string ErrorInfo;
raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo,
raw_fd_ostream::F_Force);
raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo);
if (!ErrorInfo.empty()) {
outs() << "*** Basic Block extraction failed!\n";
errs() << "Error writing list of blocks to not extract: " << ErrorInfo

View File

@ -53,7 +53,7 @@ bool BugDriver::writeProgramToFile(const std::string &Filename,
Module *M) const {
std::string ErrInfo;
raw_fd_ostream Out(Filename.c_str(), ErrInfo,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
raw_fd_ostream::F_Binary);
if (!ErrInfo.empty()) return true;
WriteBitcodeToFile(M ? M : Program, Out);
@ -85,7 +85,7 @@ void BugDriver::EmitProgressBitcode(const std::string &ID, bool NoFlyer) {
int BugDriver::runPassesAsChild(const std::vector<const PassInfo*> &Passes) {
std::string ErrInfo;
raw_fd_ostream OutFile(ChildOutput.c_str(), ErrInfo,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
raw_fd_ostream::F_Binary);
if (!ErrInfo.empty()) {
errs() << "Error opening bitcode file: " << ChildOutput << "\n";
return 1;
@ -148,7 +148,7 @@ bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
std::string ErrInfo;
raw_fd_ostream InFile(inputFilename.c_str(), ErrInfo,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
raw_fd_ostream::F_Binary);
if (!ErrInfo.empty()) {

View File

@ -368,7 +368,6 @@ GCC::FileType LLC::OutputCode(const std::string &Bitcode,
LLCArgs.push_back ("-o");
LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
LLCArgs.push_back ("-f"); // Overwrite as necessary...
LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode
LLCArgs.push_back (0);

View File

@ -364,7 +364,7 @@ ld_plugin_status all_symbols_read_hook(void) {
}
raw_fd_ostream *objFile =
new raw_fd_ostream(uniqueObjPath.c_str(), ErrMsg,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
raw_fd_ostream::F_Binary);
if (!ErrMsg.empty()) {
delete objFile;
(*message)(LDPL_ERROR, "%s", ErrMsg.c_str());

View File

@ -55,7 +55,8 @@ InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
static cl::opt<std::string>
OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
static cl::opt<bool>
Force("f", cl::desc("Enable binary output on terminals"));
// Determine optimization level.
static cl::opt<char>
@ -139,12 +140,9 @@ static formatted_raw_ostream *GetOutputStream(const char *TargetName,
std::string error;
raw_fd_ostream *FDOut =
new raw_fd_ostream(OutputFilename.c_str(), error,
(Force ? raw_fd_ostream::F_Force : 0)|
raw_fd_ostream::F_Binary);
if (!error.empty()) {
errs() << error << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
delete FDOut;
return 0;
}
@ -190,14 +188,11 @@ static formatted_raw_ostream *GetOutputStream(const char *TargetName,
std::string error;
unsigned OpenFlags = 0;
if (Force) OpenFlags |= raw_fd_ostream::F_Force;
if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), error,
OpenFlags);
if (!error.empty()) {
errs() << error << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
delete FDOut;
return 0;
}

View File

@ -38,7 +38,7 @@ OutputFilename("o", cl::desc("Override output filename"),
cl::value_desc("filename"));
static cl::opt<bool>
Force("f", cl::desc("Overwrite output files"));
Force("f", cl::desc("Enable binary output on terminals"));
static cl::opt<bool>
DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
@ -98,12 +98,9 @@ int main(int argc, char **argv) {
std::string ErrorInfo;
std::auto_ptr<raw_ostream> Out
(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
(Force?raw_fd_ostream::F_Force : 0) |
raw_fd_ostream::F_Binary));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
return 1;
}

View File

@ -38,7 +38,7 @@ OutputFilename("o", cl::desc("Override output filename"),
cl::value_desc("filename"));
static cl::opt<bool>
Force("f", cl::desc("Overwrite output files"));
Force("f", cl::desc("Enable binary output on terminals"));
static cl::opt<bool>
DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
@ -93,12 +93,9 @@ int main(int argc, char **argv) {
std::string ErrorInfo;
std::auto_ptr<raw_fd_ostream>
Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
(Force?raw_fd_ostream::F_Force : 0) |
raw_fd_ostream::F_Binary));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
return 1;
}

View File

@ -23,6 +23,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/System/Signals.h"
#include <memory>
using namespace llvm;
@ -37,7 +38,7 @@ OutputFilename("o", cl::desc("Specify output filename"),
cl::value_desc("filename"), cl::init("-"));
static cl::opt<bool>
Force("f", cl::desc("Overwrite output files"));
Force("f", cl::desc("Enable binary output on terminals"));
static cl::opt<bool>
DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
@ -113,16 +114,15 @@ int main(int argc, char **argv) {
std::string ErrorInfo;
std::auto_ptr<raw_fd_ostream>
Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
raw_fd_ostream::F_Binary |
(Force ? raw_fd_ostream::F_Force : 0)));
raw_fd_ostream::F_Binary));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
return 1;
}
Passes.add(createBitcodeWriterPass(*Out));
if (Force || !CheckBitcodeOutputToConsole(*Out, true))
Passes.add(createBitcodeWriterPass(*Out));
Passes.run(*M.get());
return 0;

View File

@ -12,7 +12,7 @@
// Additionally, this program outputs a shell script that is used to invoke LLI
// to execute the program. In this manner, the generated executable (a.out for
// example), is directly executable, whereas the bitcode file actually lives in
// the a.out.bc file generated by this program. Also, Force is on by default.
// the a.out.bc file generated by this program.
//
// Note that if someone (or a script) deletes the executable program generated,
// the .bc file will be left around. Considering that this is a temporary hack,
@ -231,7 +231,7 @@ void GenerateBitcode(Module* M, const std::string& FileName) {
// Create the output file.
std::string ErrorInfo;
raw_fd_ostream Out(FileName.c_str(), ErrorInfo,
raw_fd_ostream::F_Force | raw_fd_ostream::F_Binary);
raw_fd_ostream::F_Binary);
if (!ErrorInfo.empty())
PrintAndExit(ErrorInfo);
@ -428,8 +428,7 @@ static void EmitShellScript(char **argv) {
// Output the script to start the program...
std::string ErrorInfo;
raw_fd_ostream Out2(OutputFilename.c_str(), ErrorInfo,
llvm::raw_fd_ostream::F_Force);
raw_fd_ostream Out2(OutputFilename.c_str(), ErrorInfo);
if (!ErrorInfo.empty())
PrintAndExit(ErrorInfo);

View File

@ -22,6 +22,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/System/Signals.h"
#include "llvm/System/Path.h"
#include <memory>
@ -35,7 +36,8 @@ static cl::opt<std::string>
OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
cl::value_desc("filename"));
static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
static cl::opt<bool>
Force("f", cl::desc("Enable binary output on terminals"));
static cl::opt<bool>
Verbose("v", cl::desc("Print information about actions taken"));
@ -122,12 +124,9 @@ int main(int argc, char **argv) {
std::string ErrorInfo;
std::auto_ptr<raw_ostream>
Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
raw_fd_ostream::F_Binary |
(Force ? raw_fd_ostream::F_Force : 0)));
raw_fd_ostream::F_Binary));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
return 1;
}
@ -142,7 +141,8 @@ int main(int argc, char **argv) {
}
if (Verbose) errs() << "Writing bitcode...\n";
WriteBitcodeToFile(Composite.get(), *Out);
if (Force || !CheckBitcodeOutputToConsole(*Out, true))
WriteBitcodeToFile(Composite.get(), *Out);
return 0;
}

View File

@ -53,7 +53,7 @@ FileType("filetype", cl::init(OFT_AssemblyFile),
clEnumValEnd));
static cl::opt<bool>
Force("f", cl::desc("Overwrite output files"));
Force("f", cl::desc("Enable binary output on terminals"));
static cl::list<std::string>
IncludeDirs("I", cl::desc("Directory of include files"),
@ -184,12 +184,9 @@ static formatted_raw_ostream *GetOutputStream() {
std::string Err;
raw_fd_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Err,
raw_fd_ostream::F_Binary |
(Force ? raw_fd_ostream::F_Force : 0));
raw_fd_ostream::F_Binary);
if (!Err.empty()) {
errs() << Err << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
delete Out;
return 0;
}

View File

@ -148,7 +148,7 @@ bool LTOCodeGenerator::writeMergedModules(const char *path,
// create output file
std::string ErrInfo;
raw_fd_ostream Out(path, ErrInfo,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
raw_fd_ostream::F_Binary);
if (!ErrInfo.empty()) {
errMsg = "could not open bitcode file for writing: ";
errMsg += path;
@ -179,8 +179,7 @@ const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
// generate assembly code
bool genResult = false;
{
raw_fd_ostream asmFD(uniqueAsmPath.c_str(), errMsg,
raw_fd_ostream::F_Force);
raw_fd_ostream asmFD(uniqueAsmPath.c_str(), errMsg);
formatted_raw_ostream asmFile(asmFD);
if (!errMsg.empty())
return NULL;

View File

@ -29,7 +29,7 @@ static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
std::string Filename = GraphName + ".dot";
O << "Writing '" << Filename << "'...";
std::string ErrInfo;
raw_fd_ostream F(Filename.c_str(), ErrInfo, raw_fd_ostream::F_Force);
raw_fd_ostream F(Filename.c_str(), ErrInfo);
if (ErrInfo.empty())
WriteGraph(F, GT);

View File

@ -55,7 +55,7 @@ OutputFilename("o", cl::desc("Override output filename"),
cl::value_desc("filename"), cl::init("-"));
static cl::opt<bool>
Force("f", cl::desc("Overwrite output files"));
Force("f", cl::desc("Enable binary output on terminals"));
static cl::opt<bool>
PrintEachXForm("p", cl::desc("Print module after each transformation"));
@ -367,12 +367,9 @@ int main(int argc, char **argv) {
if (OutputFilename != "-") {
std::string ErrorInfo;
Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
raw_fd_ostream::F_Binary |
(Force ? raw_fd_ostream::F_Force : 0));
raw_fd_ostream::F_Binary);
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
delete Out;
return 1;
}

View File

@ -66,7 +66,7 @@ int main(int argc, char **argv) {
outs() << argv[0] << ": Updating '" << OutputFilename
<< "', contents changed.\n";
raw_fd_ostream OutStream(OutputFilename.c_str(), ErrorStr,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
raw_fd_ostream::F_Binary);
if (!ErrorStr.empty()) {
errs() << argv[0] << ": Unable to write output '"
<< OutputFilename << "': " << ErrorStr << '\n';

View File

@ -171,8 +171,7 @@ int main(int argc, char **argv) {
raw_ostream *Out = &outs();
if (OutputFilename != "-") {
std::string Error;
Out = new raw_fd_ostream(OutputFilename.c_str(), Error,
raw_fd_ostream::F_Force);
Out = new raw_fd_ostream(OutputFilename.c_str(), Error);
if (!Error.empty()) {
errs() << argv[0] << ": error opening " << OutputFilename