diff options
Diffstat (limited to 'llvm/tools')
-rw-r--r-- | llvm/tools/analyze/analyze.cpp | 8 | ||||
-rw-r--r-- | llvm/tools/as/as.cpp | 44 | ||||
-rw-r--r-- | llvm/tools/dis/dis.cpp | 40 | ||||
-rw-r--r-- | llvm/tools/gccas/gccas.cpp | 8 | ||||
-rw-r--r-- | llvm/tools/link/link.cpp | 24 | ||||
-rw-r--r-- | llvm/tools/llc/llc.cpp | 48 | ||||
-rw-r--r-- | llvm/tools/llvm-as/as.cpp | 44 | ||||
-rw-r--r-- | llvm/tools/llvm-as/llvm-as.cpp | 44 | ||||
-rw-r--r-- | llvm/tools/llvm-dis/dis.cpp | 40 | ||||
-rw-r--r-- | llvm/tools/llvm-dis/llvm-dis.cpp | 40 | ||||
-rw-r--r-- | llvm/tools/llvm-link/llvm-link.cpp | 24 | ||||
-rw-r--r-- | llvm/tools/opt/opt.cpp | 12 |
12 files changed, 242 insertions, 134 deletions
diff --git a/llvm/tools/analyze/analyze.cpp b/llvm/tools/analyze/analyze.cpp index 9a53bd45af1..9241216f347 100644 --- a/llvm/tools/analyze/analyze.cpp +++ b/llvm/tools/analyze/analyze.cpp @@ -28,6 +28,10 @@ #include "llvm/Analysis/FindUsedTypes.h" #include "Support/CommandLine.h" #include <algorithm> +#include <iostream> +using std::cout; +using std::cerr; +using std::pair; static void PrintMethod(Method *M) { cout << M; @@ -38,7 +42,7 @@ static void PrintIntervalPartition(Method *M) { } static void PrintClassifiedExprs(Method *M) { - cout << "Classified expressions for: " << M->getName() << endl; + cout << "Classified expressions for: " << M->getName() << "\n"; Method::inst_iterator I = M->inst_begin(), E = M->inst_end(); for (; I != E; ++I) { cout << *I; @@ -61,7 +65,7 @@ static void PrintClassifiedExprs(Method *M) { if (R.Offset) WriteAsOperand(cout, (Value*)R.Offset); else cout << " 0"; break; } - cout << endl << endl; + cout << "\n\n"; } } diff --git a/llvm/tools/as/as.cpp b/llvm/tools/as/as.cpp index ee664f15249..0fbd1e6cd41 100644 --- a/llvm/tools/as/as.cpp +++ b/llvm/tools/as/as.cpp @@ -16,6 +16,7 @@ #include "Support/CommandLine.h" #include <fstream> #include <string> +#include <memory> cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-"); cl::String OutputFilename("o", "Override output filename", cl::NoFlags, ""); @@ -28,46 +29,55 @@ int main(int argc, char **argv) { ostream *Out = 0; try { // Parse the file now... - Module *C = ParseAssemblyFile(InputFilename); - if (C == 0) { + std::auto_ptr<Module> C(ParseAssemblyFile(InputFilename)); + if (C.get() == 0) { cerr << "assembly didn't read correctly.\n"; return 1; } if (DumpAsm) - cerr << "Here's the assembly:\n" << C; + cerr << "Here's the assembly:\n" << C.get(); if (OutputFilename != "") { // Specified an output filename? - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); } else { if (InputFilename == "-") { OutputFilename = "-"; Out = &cout; } else { - string IFN = InputFilename; + std::string IFN = InputFilename; int Len = IFN.length(); if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { // Source ends in .ll - OutputFilename = string(IFN.begin(), IFN.end()-3); + OutputFilename = std::string(IFN.begin(), IFN.end()-3); } else { OutputFilename = IFN; // Append a .bc to it } OutputFilename += ".bc"; - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + + Out = new std::ofstream(OutputFilename.c_str()); } + } - if (!Out->good()) { - cerr << "Error opening " << OutputFilename << "!\n"; - delete C; - return 1; - } + if (!Out->good()) { + cerr << "Error opening " << OutputFilename << "!\n"; + return 1; } - WriteBytecodeToFile(C, *Out); - - delete C; + WriteBytecodeToFile(C.get(), *Out); } catch (const ParseException &E) { cerr << E.getMessage() << endl; return 1; diff --git a/llvm/tools/dis/dis.cpp b/llvm/tools/dis/dis.cpp index 2a7eb4e06ea..55e8d5d6690 100644 --- a/llvm/tools/dis/dis.cpp +++ b/llvm/tools/dis/dis.cpp @@ -24,6 +24,8 @@ #include "Support/PostOrderIterator.h" #include "Support/CommandLine.h" #include <fstream> +#include <iostream> +using std::cerr; // OutputMode - The different orderings to print basic blocks in... enum OutputMode { @@ -47,7 +49,7 @@ cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags, int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n"); - ostream *Out = &cout; // Default to printing to stdout... + std::ostream *Out = &std::cout; // Default to printing to stdout... Module *C = ParseBytecodeFile(InputFilename); if (C == 0) { @@ -56,31 +58,41 @@ int main(int argc, char **argv) { } if (OutputFilename != "") { // Specified an output filename? - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename + << "': File exists! Sending to standard output.\n"; + } else { + Out = new std::ofstream(OutputFilename.c_str()); + } } else { if (InputFilename == "-") { OutputFilename = "-"; - Out = &cout; } else { - string IFN = InputFilename; + std::string IFN = InputFilename; int Len = IFN.length(); if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { // Source ends in .bc - OutputFilename = string(IFN.begin(), IFN.end()-3); + OutputFilename = std::string(IFN.begin(), IFN.end()-3); } else { OutputFilename = IFN; // Append a .ll to it } OutputFilename += ".ll"; - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename + << "': File exists! Sending to standard output.\n"; + } else { + Out = new std::ofstream(OutputFilename.c_str()); + } } } if (!Out->good()) { cerr << "Error opening " << OutputFilename << ": sending to stdout instead!\n"; - Out = &cout; + Out = &std::cout; } // All that dis does is write the assembly out to a file... which is exactly @@ -100,20 +112,20 @@ int main(int argc, char **argv) { switch (WriteMode) { case dfo: // Depth First ordering copy(df_begin(M), df_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case rdfo: // Reverse Depth First ordering copy(df_begin(M, true), df_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case po: // Post Order copy(po_begin(M), po_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case rpo: { // Reverse Post Order ReversePostOrderTraversal RPOT(M); copy(RPOT.begin(), RPOT.end(), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; } default: @@ -124,6 +136,6 @@ int main(int argc, char **argv) { } delete C; - if (Out != &cout) delete Out; + if (Out != &std::cout) delete Out; return 0; } diff --git a/llvm/tools/gccas/gccas.cpp b/llvm/tools/gccas/gccas.cpp index b9e3a6207db..073b2483499 100644 --- a/llvm/tools/gccas/gccas.cpp +++ b/llvm/tools/gccas/gccas.cpp @@ -44,17 +44,17 @@ int main(int argc, char **argv) { } if (OutputFilename == "") { // Didn't specify an output filename? - string IFN = InputFilename; + std::string IFN = InputFilename; int Len = IFN.length(); if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s? - OutputFilename = string(IFN.begin(), IFN.end()-2); + OutputFilename = std::string(IFN.begin(), IFN.end()-2); } else { OutputFilename = IFN; // Append a .o to it } OutputFilename += ".o"; } - Out = new ofstream(OutputFilename.c_str(), ios::out); + Out = new std::ofstream(OutputFilename.c_str(), ios::out); if (!Out->good()) { cerr << "Error opening " << OutputFilename << "!\n"; return 1; @@ -63,7 +63,7 @@ int main(int argc, char **argv) { // In addition to just parsing the input from GCC, we also want to spiff it up // a little bit. Do this now. // - vector<Pass*> Passes; + std::vector<Pass*> Passes; Passes.push_back(new opt::DeadCodeElimination()); // Remove Dead code/vars Passes.push_back(new CleanupGCCOutput()); // Fix gccisms Passes.push_back(new InductionVariableSimplify()); // Simplify indvars diff --git a/llvm/tools/link/link.cpp b/llvm/tools/link/link.cpp index 83141c86e7d..ad4cbdbe8cb 100644 --- a/llvm/tools/link/link.cpp +++ b/llvm/tools/link/link.cpp @@ -19,6 +19,7 @@ #include <fstream> #include <memory> #include <sys/types.h> // For FileExists +typedef int blksize_t; // SYS/TYPES is broken!!! #include <sys/stat.h> @@ -32,7 +33,7 @@ cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore); // FileExists - Return true if the specified string is an openable file... -static inline bool FileExists(const string &FN) { +static inline bool FileExists(const std::string &FN) { struct stat StatBuf; return stat(FN.c_str(), &StatBuf) != -1; } @@ -40,9 +41,9 @@ static inline bool FileExists(const string &FN) { // LoadFile - Read the specified bytecode file in and return it. This routine // searches the link path for the specified file to try to find it... // -static inline std::auto_ptr<Module> LoadFile(const string &FN) { - string Filename = FN; - string ErrorMessage; +static inline std::auto_ptr<Module> LoadFile(const std::string &FN) { + std::string Filename = FN; + std::string ErrorMessage; unsigned NextLibPathIdx = 0; bool FoundAFile = false; @@ -81,7 +82,7 @@ int main(int argc, char **argv) { assert(InputFilenames.size() > 0 && "OneOrMore is not working"); unsigned BaseArg = 0; - string ErrorMessage; + std::string ErrorMessage; // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack. if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" && @@ -94,7 +95,7 @@ int main(int argc, char **argv) { if (Composite.get() == 0) return 1; for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) { - auto_ptr<Module> M(LoadFile(InputFilenames[i])); + std::auto_ptr<Module> M(LoadFile(InputFilenames[i])); if (M.get() == 0) return 1; if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n"; @@ -111,8 +112,13 @@ int main(int argc, char **argv) { ostream *Out = &cout; // Default to printing to stdout... if (OutputFilename != "-") { - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); if (!Out->good()) { cerr << "Error opening '" << OutputFilename << "'!\n"; return 1; @@ -122,6 +128,6 @@ int main(int argc, char **argv) { if (Verbose) cerr << "Writing bytecode...\n"; WriteBytecodeToFile(Composite.get(), *Out); - if (Out != &cout) delete Out; + if (Out != &std::cout) delete Out; return 0; } diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp index 7c8c3a608b6..6b51f79b380 100644 --- a/llvm/tools/llc/llc.cpp +++ b/llvm/tools/llc/llc.cpp @@ -19,6 +19,7 @@ #include <memory> #include <string> #include <fstream> +using std::string; cl::String InputFilename ("", "Input filename", cl::NoFlags, "-"); cl::String OutputFilename("o", "Output filename", cl::NoFlags, ""); @@ -78,10 +79,10 @@ public: class EmitAssembly : public Pass { const TargetMachine &Target; // Target to compile for - ostream *Out; // Stream to print on + std::ostream *Out; // Stream to print on bool DeleteStream; // Delete stream in dtor? public: - inline EmitAssembly(const TargetMachine &T, ostream *O, bool D) + inline EmitAssembly(const TargetMachine &T, std::ostream *O, bool D) : Target(T), Out(O), DeleteStream(D) {} @@ -105,20 +106,20 @@ int main(int argc, char **argv) { // Allocate a target... in the future this will be controllable on the // command line. - auto_ptr<TargetMachine> target(allocateSparcTargetMachine()); + std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine()); assert(target.get() && "Could not allocate target machine!"); TargetMachine &Target = *target.get(); // Load the module to be compiled... - auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); + std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); if (M.get() == 0) { cerr << "bytecode didn't read correctly.\n"; return 1; } // Build up all of the passes that we want to do to the module... - vector<Pass*> Passes; + std::vector<Pass*> Passes; // Hoist constants out of PHI nodes into predecessor BB's Passes.push_back(new HoistPHIConstants()); @@ -135,8 +136,15 @@ int main(int argc, char **argv) { assert(InputFilename != "-" && "files on stdin not supported with tracing"); string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc"; - ostream *os = new ofstream(traceFileName.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + + std::ostream *os = new std::ofstream(traceFileName.c_str()); if (!os->good()) { cerr << "Error opening " << traceFileName << "! SKIPPING OUTPUT OF TRACE CODE\n"; @@ -161,19 +169,31 @@ int main(int argc, char **argv) { if (!DoNotEmitAssembly) { // If asm output is enabled... // Figure out where we are going to send the output... - ostream *Out = 0; + std::ostream *Out = 0; if (OutputFilename != "") { // Specified an output filename? - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); } else { if (InputFilename == "-") { OutputFilename = "-"; - Out = &cout; + Out = &std::cout; } else { string OutputFilename = GetFileNameRoot(InputFilename); OutputFilename += ".s"; - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + + Out = new std::ofstream(OutputFilename.c_str()); if (!Out->good()) { cerr << "Error opening " << OutputFilename << "!\n"; delete Out; @@ -183,7 +203,7 @@ int main(int argc, char **argv) { } // Output assembly language to the .s file - Passes.push_back(new EmitAssembly(Target, Out, Out != &cout)); + Passes.push_back(new EmitAssembly(Target, Out, Out != &std::cout)); } // Run our queue of passes all at once now, efficiently. This form of diff --git a/llvm/tools/llvm-as/as.cpp b/llvm/tools/llvm-as/as.cpp index ee664f15249..0fbd1e6cd41 100644 --- a/llvm/tools/llvm-as/as.cpp +++ b/llvm/tools/llvm-as/as.cpp @@ -16,6 +16,7 @@ #include "Support/CommandLine.h" #include <fstream> #include <string> +#include <memory> cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-"); cl::String OutputFilename("o", "Override output filename", cl::NoFlags, ""); @@ -28,46 +29,55 @@ int main(int argc, char **argv) { ostream *Out = 0; try { // Parse the file now... - Module *C = ParseAssemblyFile(InputFilename); - if (C == 0) { + std::auto_ptr<Module> C(ParseAssemblyFile(InputFilename)); + if (C.get() == 0) { cerr << "assembly didn't read correctly.\n"; return 1; } if (DumpAsm) - cerr << "Here's the assembly:\n" << C; + cerr << "Here's the assembly:\n" << C.get(); if (OutputFilename != "") { // Specified an output filename? - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); } else { if (InputFilename == "-") { OutputFilename = "-"; Out = &cout; } else { - string IFN = InputFilename; + std::string IFN = InputFilename; int Len = IFN.length(); if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { // Source ends in .ll - OutputFilename = string(IFN.begin(), IFN.end()-3); + OutputFilename = std::string(IFN.begin(), IFN.end()-3); } else { OutputFilename = IFN; // Append a .bc to it } OutputFilename += ".bc"; - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + + Out = new std::ofstream(OutputFilename.c_str()); } + } - if (!Out->good()) { - cerr << "Error opening " << OutputFilename << "!\n"; - delete C; - return 1; - } + if (!Out->good()) { + cerr << "Error opening " << OutputFilename << "!\n"; + return 1; } - WriteBytecodeToFile(C, *Out); - - delete C; + WriteBytecodeToFile(C.get(), *Out); } catch (const ParseException &E) { cerr << E.getMessage() << endl; return 1; diff --git a/llvm/tools/llvm-as/llvm-as.cpp b/llvm/tools/llvm-as/llvm-as.cpp index ee664f15249..0fbd1e6cd41 100644 --- a/llvm/tools/llvm-as/llvm-as.cpp +++ b/llvm/tools/llvm-as/llvm-as.cpp @@ -16,6 +16,7 @@ #include "Support/CommandLine.h" #include <fstream> #include <string> +#include <memory> cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-"); cl::String OutputFilename("o", "Override output filename", cl::NoFlags, ""); @@ -28,46 +29,55 @@ int main(int argc, char **argv) { ostream *Out = 0; try { // Parse the file now... - Module *C = ParseAssemblyFile(InputFilename); - if (C == 0) { + std::auto_ptr<Module> C(ParseAssemblyFile(InputFilename)); + if (C.get() == 0) { cerr << "assembly didn't read correctly.\n"; return 1; } if (DumpAsm) - cerr << "Here's the assembly:\n" << C; + cerr << "Here's the assembly:\n" << C.get(); if (OutputFilename != "") { // Specified an output filename? - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); } else { if (InputFilename == "-") { OutputFilename = "-"; Out = &cout; } else { - string IFN = InputFilename; + std::string IFN = InputFilename; int Len = IFN.length(); if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { // Source ends in .ll - OutputFilename = string(IFN.begin(), IFN.end()-3); + OutputFilename = std::string(IFN.begin(), IFN.end()-3); } else { OutputFilename = IFN; // Append a .bc to it } OutputFilename += ".bc"; - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + + Out = new std::ofstream(OutputFilename.c_str()); } + } - if (!Out->good()) { - cerr << "Error opening " << OutputFilename << "!\n"; - delete C; - return 1; - } + if (!Out->good()) { + cerr << "Error opening " << OutputFilename << "!\n"; + return 1; } - WriteBytecodeToFile(C, *Out); - - delete C; + WriteBytecodeToFile(C.get(), *Out); } catch (const ParseException &E) { cerr << E.getMessage() << endl; return 1; diff --git a/llvm/tools/llvm-dis/dis.cpp b/llvm/tools/llvm-dis/dis.cpp index 2a7eb4e06ea..55e8d5d6690 100644 --- a/llvm/tools/llvm-dis/dis.cpp +++ b/llvm/tools/llvm-dis/dis.cpp @@ -24,6 +24,8 @@ #include "Support/PostOrderIterator.h" #include "Support/CommandLine.h" #include <fstream> +#include <iostream> +using std::cerr; // OutputMode - The different orderings to print basic blocks in... enum OutputMode { @@ -47,7 +49,7 @@ cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags, int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n"); - ostream *Out = &cout; // Default to printing to stdout... + std::ostream *Out = &std::cout; // Default to printing to stdout... Module *C = ParseBytecodeFile(InputFilename); if (C == 0) { @@ -56,31 +58,41 @@ int main(int argc, char **argv) { } if (OutputFilename != "") { // Specified an output filename? - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename + << "': File exists! Sending to standard output.\n"; + } else { + Out = new std::ofstream(OutputFilename.c_str()); + } } else { if (InputFilename == "-") { OutputFilename = "-"; - Out = &cout; } else { - string IFN = InputFilename; + std::string IFN = InputFilename; int Len = IFN.length(); if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { // Source ends in .bc - OutputFilename = string(IFN.begin(), IFN.end()-3); + OutputFilename = std::string(IFN.begin(), IFN.end()-3); } else { OutputFilename = IFN; // Append a .ll to it } OutputFilename += ".ll"; - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename + << "': File exists! Sending to standard output.\n"; + } else { + Out = new std::ofstream(OutputFilename.c_str()); + } } } if (!Out->good()) { cerr << "Error opening " << OutputFilename << ": sending to stdout instead!\n"; - Out = &cout; + Out = &std::cout; } // All that dis does is write the assembly out to a file... which is exactly @@ -100,20 +112,20 @@ int main(int argc, char **argv) { switch (WriteMode) { case dfo: // Depth First ordering copy(df_begin(M), df_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case rdfo: // Reverse Depth First ordering copy(df_begin(M, true), df_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case po: // Post Order copy(po_begin(M), po_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case rpo: { // Reverse Post Order ReversePostOrderTraversal RPOT(M); copy(RPOT.begin(), RPOT.end(), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; } default: @@ -124,6 +136,6 @@ int main(int argc, char **argv) { } delete C; - if (Out != &cout) delete Out; + if (Out != &std::cout) delete Out; return 0; } diff --git a/llvm/tools/llvm-dis/llvm-dis.cpp b/llvm/tools/llvm-dis/llvm-dis.cpp index 2a7eb4e06ea..55e8d5d6690 100644 --- a/llvm/tools/llvm-dis/llvm-dis.cpp +++ b/llvm/tools/llvm-dis/llvm-dis.cpp @@ -24,6 +24,8 @@ #include "Support/PostOrderIterator.h" #include "Support/CommandLine.h" #include <fstream> +#include <iostream> +using std::cerr; // OutputMode - The different orderings to print basic blocks in... enum OutputMode { @@ -47,7 +49,7 @@ cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags, int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n"); - ostream *Out = &cout; // Default to printing to stdout... + std::ostream *Out = &std::cout; // Default to printing to stdout... Module *C = ParseBytecodeFile(InputFilename); if (C == 0) { @@ -56,31 +58,41 @@ int main(int argc, char **argv) { } if (OutputFilename != "") { // Specified an output filename? - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename + << "': File exists! Sending to standard output.\n"; + } else { + Out = new std::ofstream(OutputFilename.c_str()); + } } else { if (InputFilename == "-") { OutputFilename = "-"; - Out = &cout; } else { - string IFN = InputFilename; + std::string IFN = InputFilename; int Len = IFN.length(); if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { // Source ends in .bc - OutputFilename = string(IFN.begin(), IFN.end()-3); + OutputFilename = std::string(IFN.begin(), IFN.end()-3); } else { OutputFilename = IFN; // Append a .ll to it } OutputFilename += ".ll"; - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename + << "': File exists! Sending to standard output.\n"; + } else { + Out = new std::ofstream(OutputFilename.c_str()); + } } } if (!Out->good()) { cerr << "Error opening " << OutputFilename << ": sending to stdout instead!\n"; - Out = &cout; + Out = &std::cout; } // All that dis does is write the assembly out to a file... which is exactly @@ -100,20 +112,20 @@ int main(int argc, char **argv) { switch (WriteMode) { case dfo: // Depth First ordering copy(df_begin(M), df_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case rdfo: // Reverse Depth First ordering copy(df_begin(M, true), df_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case po: // Post Order copy(po_begin(M), po_end(M), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; case rpo: { // Reverse Post Order ReversePostOrderTraversal RPOT(M); copy(RPOT.begin(), RPOT.end(), - ostream_iterator<BasicBlock*>(*Out, "\n")); + std::ostream_iterator<BasicBlock*>(*Out, "\n")); break; } default: @@ -124,6 +136,6 @@ int main(int argc, char **argv) { } delete C; - if (Out != &cout) delete Out; + if (Out != &std::cout) delete Out; return 0; } diff --git a/llvm/tools/llvm-link/llvm-link.cpp b/llvm/tools/llvm-link/llvm-link.cpp index 83141c86e7d..ad4cbdbe8cb 100644 --- a/llvm/tools/llvm-link/llvm-link.cpp +++ b/llvm/tools/llvm-link/llvm-link.cpp @@ -19,6 +19,7 @@ #include <fstream> #include <memory> #include <sys/types.h> // For FileExists +typedef int blksize_t; // SYS/TYPES is broken!!! #include <sys/stat.h> @@ -32,7 +33,7 @@ cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore); // FileExists - Return true if the specified string is an openable file... -static inline bool FileExists(const string &FN) { +static inline bool FileExists(const std::string &FN) { struct stat StatBuf; return stat(FN.c_str(), &StatBuf) != -1; } @@ -40,9 +41,9 @@ static inline bool FileExists(const string &FN) { // LoadFile - Read the specified bytecode file in and return it. This routine // searches the link path for the specified file to try to find it... // -static inline std::auto_ptr<Module> LoadFile(const string &FN) { - string Filename = FN; - string ErrorMessage; +static inline std::auto_ptr<Module> LoadFile(const std::string &FN) { + std::string Filename = FN; + std::string ErrorMessage; unsigned NextLibPathIdx = 0; bool FoundAFile = false; @@ -81,7 +82,7 @@ int main(int argc, char **argv) { assert(InputFilenames.size() > 0 && "OneOrMore is not working"); unsigned BaseArg = 0; - string ErrorMessage; + std::string ErrorMessage; // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack. if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" && @@ -94,7 +95,7 @@ int main(int argc, char **argv) { if (Composite.get() == 0) return 1; for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) { - auto_ptr<Module> M(LoadFile(InputFilenames[i])); + std::auto_ptr<Module> M(LoadFile(InputFilenames[i])); if (M.get() == 0) return 1; if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n"; @@ -111,8 +112,13 @@ int main(int argc, char **argv) { ostream *Out = &cout; // Default to printing to stdout... if (OutputFilename != "-") { - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); if (!Out->good()) { cerr << "Error opening '" << OutputFilename << "'!\n"; return 1; @@ -122,6 +128,6 @@ int main(int argc, char **argv) { if (Verbose) cerr << "Writing bytecode...\n"; WriteBytecodeToFile(Composite.get(), *Out); - if (Out != &cout) delete Out; + if (Out != &std::cout) delete Out; return 0; } diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp index 9dfb51a4871..8a2208d92d3 100644 --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -122,10 +122,16 @@ int main(int argc, char **argv) { for (unsigned i = 0; i < OptimizationList.size(); ++i) RunOptimization(M.get(), OptimizationList[i]); - ostream *Out = &cout; // Default to printing to stdout... + std::ostream *Out = &std::cout; // Default to printing to stdout... if (OutputFilename != "") { - Out = new ofstream(OutputFilename.c_str(), - (Force ? 0 : ios::noreplace)|ios::out); + if (!Force && !std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + cerr << "Error opening '" << OutputFilename << "': File exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); + if (!Out->good()) { cerr << "Error opening " << OutputFilename << "!\n"; return 1; |