llvm.org GIT mirror llvm / 5fb6ed4
Use binary mode for reading/writing bytecode files git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19751 91177308-0d34-0410-b5e6-96231b3b80d8 Jeff Cohen 14 years ago
14 changed file(s) with 87 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
374374 // Ensure we can remove the temporary even in the face of an exception
375375 try {
376376 // Create archive file for output.
377 std::ofstream ArchiveFile(TmpArchive.c_str());
377 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
378 std::ios::binary;
379 std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
378380
379381 // Check for errors opening or creating archive file.
380382 if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) {
412414 const char* base = (const char*) arch.map();
413415
414416 // Open the final file to write and check it.
415 std::ofstream FinalFile(archPath.c_str());
417 std::ofstream FinalFile(archPath.c_str(), io_mode);
416418 if ( !FinalFile.is_open() || FinalFile.bad() ) {
417419 throw std::string("Error opening archive file: ") + archPath.toString();
418420 }
374374 // Ensure we can remove the temporary even in the face of an exception
375375 try {
376376 // Create archive file for output.
377 std::ofstream ArchiveFile(TmpArchive.c_str());
377 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
378 std::ios::binary;
379 std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
378380
379381 // Check for errors opening or creating archive file.
380382 if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) {
412414 const char* base = (const char*) arch.map();
413415
414416 // Open the final file to write and check it.
415 std::ofstream FinalFile(archPath.c_str());
417 std::ofstream FinalFile(archPath.c_str(), io_mode);
416418 if ( !FinalFile.is_open() || FinalFile.bad() ) {
417419 throw std::string("Error opening archive file: ") + archPath.toString();
418420 }
2626 ///
2727 bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB,
2828 std::string *Error) {
29 std::ifstream FileAStream(FileA.c_str());
29 std::ios::openmode io_mode = std::ios::in | std::ios::binary;
30 std::ifstream FileAStream(FileA.c_str(), io_mode);
3031 if (!FileAStream) {
3132 if (Error) *Error = "Couldn't open file '" + FileA + "'";
3233 return true;
3334 }
3435
35 std::ifstream FileBStream(FileB.c_str());
36 std::ifstream FileBStream(FileB.c_str(), io_mode);
3637 if (!FileBStream) {
3738 if (Error) *Error = "Couldn't open file '" + FileB + "'";
3839 return true;
7777
7878 Verifier()
7979 : Broken(false), RealPass(true), action(AbortProcessAction),
80 DS(0), msgs( std::ios_base::app | std::ios_base::out ) {}
80 DS(0), msgs( std::ios::app | std::ios::out ) {}
8181 Verifier( VerifierFailureAction ctn )
8282 : Broken(false), RealPass(true), action(ctn), DS(0),
83 msgs( std::ios_base::app | std::ios_base::out ) {}
83 msgs( std::ios::app | std::ios::out ) {}
8484 Verifier(bool AB )
8585 : Broken(false), RealPass(true),
8686 action( AB ? AbortProcessAction : PrintMessageAction), DS(0),
87 msgs( std::ios_base::app | std::ios_base::out ) {}
87 msgs( std::ios::app | std::ios::out ) {}
8888 Verifier(DominatorSet &ds)
8989 : Broken(false), RealPass(false), action(PrintMessageAction),
90 DS(&ds), msgs( std::ios_base::app | std::ios_base::out ) {}
90 DS(&ds), msgs( std::ios::app | std::ios::out ) {}
9191
9292
9393 bool doInitialization(Module &M) {
1414 //
1515 //===----------------------------------------------------------------------===//
1616
17 // Note: as a short term hack, the old Unix-specific code and platform-
18 // independent code co-exist via conditional compilation until it is verified
19 // that the new code works correctly on Unix.
20
21 #define PLATFORMINDEPENDENT
22
1723 #include "BugDriver.h"
1824 #include "llvm/Module.h"
1925 #include "llvm/PassManager.h"
2329 #include "llvm/Support/FileUtilities.h"
2430 #include "llvm/System/Path.h"
2531 #include
32 #ifndef PLATFORMINDEPENDENT
2633 #include
2734 #include
2835 #include
36 #endif
2937 using namespace llvm;
3038
3139 /// writeProgramToFile - This writes the current "Program" to the named bytecode
3341 ///
3442 bool BugDriver::writeProgramToFile(const std::string &Filename,
3543 Module *M) const {
36 std::ofstream Out(Filename.c_str());
44 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
45 std::ios::binary;
46 std::ofstream Out(Filename.c_str(), io_mode);
3747 if (!Out.good()) return true;
3848 WriteBytecodeToFile(M ? M : Program, Out, /*compression=*/true);
3949 return false;
7585
7686 static void RunChild(Module *Program,const std::vector &Passes,
7787 const std::string &OutFilename) {
78 std::ofstream OutFile(OutFilename.c_str());
88 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
89 std::ios::binary;
90 std::ofstream OutFile(OutFilename.c_str(), io_mode);
7991 if (!OutFile.good()) {
8092 std::cerr << "Error opening bytecode file: " << OutFilename << "\n";
8193 exit(1);
118130 uniqueFilename.makeUnique();
119131 OutputFilename = uniqueFilename.toString();
120132
133 #ifndef PLATFORMINDEPENDENT
121134 pid_t child_pid;
122135 switch (child_pid = fork()) {
123136 case -1: // Error occurred
138151 }
139152
140153 bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
154 #else
155 bool ExitedOK = false;
156 #endif
141157
142158 // If we are supposed to delete the bytecode file or if the passes crashed,
143159 // remove it now. This may fail if the file was never created, but that's ok.
144160 if (DeleteOutput || !ExitedOK)
145161 sys::Path(OutputFilename).destroyFile();
146
162
163 #ifndef PLATFORMINDEPENDENT
147164 if (!Quiet) {
148165 if (ExitedOK)
149166 std::cout << "Success!\n";
158175 else
159176 std::cout << "Failed for unknown reason!\n";
160177 }
178 #endif
161179
162180 // Was the child successful?
163181 return !ExitedOK;
6262 return 1;
6363 }
6464
65 // In addition to deleting all other functions, we also want to spiff it up a
66 // little bit. Do this now.
67 //
65 // In addition to deleting all other functions, we also want to spiff it
66 // up a little bit. Do this now.
6867 PassManager Passes;
6968 Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
7069 // Either isolate the function or delete it from the Module
8382 << "Use -f command line argument to force output\n";
8483 return 1;
8584 }
86 Out = new std::ofstream(OutputFilename.c_str());
85 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
86 std::ios::binary;
87 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
8788 } else { // Specified stdout
89 // FIXME: cout is not binary!
8890 Out = &std::cout;
8991 }
9092
163163 }
164164
165165 if (OutputFilename == "-")
166 // FIXME: cout is not binary!
166167 Out = &std::cout;
167168 else {
168 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
169 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
170 std::ios::binary;
171 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
169172
170173 // Make sure that the Out file gets unlinked from the disk if we get a
171174 // signal
237237 // Create the output file.
238238 std::string RealBytecodeOutput = OutputFilename;
239239 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
240 std::ofstream Out(RealBytecodeOutput.c_str());
240 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
241 std::ios::binary;
242 std::ofstream Out(RealBytecodeOutput.c_str(), io_mode);
241243 if (!Out.good())
242244 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
243245 "' for writing!");
435435 }
436436
437437 // Open up a file stream for writing
438 std::ofstream file(I->getPath().c_str());
438 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
439 std::ios::binary;
440 std::ofstream file(I->getPath().c_str(), io_mode);
439441
440442 // Get the data and its length
441443 const char* data = reinterpret_cast(I->getData());
8383 << "Use -f command line argument to force output\n";
8484 return 1;
8585 }
86 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
87 std::ios_base::trunc | std::ios_base::binary);
86 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
87 std::ios::trunc | std::ios::binary);
8888 } else { // Specified stdout
89 Out = &std::cout;
89 // FIXME: cout is not binary!
90 Out = &std::cout;
9091 }
9192 } else {
9293 if (InputFilename == "-") {
93 OutputFilename = "-";
94 Out = &std::cout;
94 OutputFilename = "-";
95 Out = &std::cout;
9596 } else {
96 std::string IFN = InputFilename;
97 int Len = IFN.length();
98 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
99 // Source ends in .ll
100 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
97 std::string IFN = InputFilename;
98 int Len = IFN.length();
99 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
100 // Source ends in .ll
101 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
101102 } else {
102 OutputFilename = IFN; // Append a .bc to it
103 }
104 OutputFilename += ".bc";
103 OutputFilename = IFN; // Append a .bc to it
104 }
105 OutputFilename += ".bc";
105106
106107 if (!Force && std::ifstream(OutputFilename.c_str())) {
107108 // If force is not specified, make sure not to overwrite a file!
111112 return 1;
112113 }
113114
114 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
115 std::ios_base::trunc | std::ios_base::binary);
115 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
116 std::ios::trunc | std::ios::binary);
116117 // Make sure that the Out file gets unlinked from the disk if we get a
117118 // SIGINT
118119 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
6262 return 1;
6363 }
6464
65 // In addition to deleting all other functions, we also want to spiff it up a
66 // little bit. Do this now.
67 //
65 // In addition to deleting all other functions, we also want to spiff it
66 // up a little bit. Do this now.
6867 PassManager Passes;
6968 Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
7069 // Either isolate the function or delete it from the Module
8382 << "Use -f command line argument to force output\n";
8483 return 1;
8584 }
86 Out = new std::ofstream(OutputFilename.c_str());
85 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
86 std::ios::binary;
87 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
8788 } else { // Specified stdout
89 // FIXME: cout is not binary!
8890 Out = &std::cout;
8991 }
9092
185185 void GenerateBytecode(Module* M, const std::string& FileName) {
186186
187187 // Create the output file.
188 std::ofstream Out(FileName.c_str());
188 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
189 std::ios::binary;
190 std::ofstream Out(FileName.c_str(), io_mode);
189191 if (!Out.good()) {
190192 PrintAndReturn("error opening '" + FileName + "' for writing!");
191193 return;
111111
112112 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
113113
114 // FIXME: cout is not binary!
114115 std::ostream *Out = &std::cout; // Default to printing to stdout...
115116 if (OutputFilename != "-") {
116117 if (!Force && std::ifstream(OutputFilename.c_str())) {
120121 << "Use -f command line argument to force output\n";
121122 return 1;
122123 }
123 Out = new std::ofstream(OutputFilename.c_str());
124 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
125 std::ios::binary;
126 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
124127 if (!Out->good()) {
125128 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
126129 return 1;
9494 }
9595
9696 // Figure out what stream we are supposed to write to...
97 // FIXME: cout is not binary!
9798 std::ostream *Out = &std::cout; // Default to printing to stdout...
9899 if (OutputFilename != "-") {
99100 if (!Force && std::ifstream(OutputFilename.c_str())) {
103104 << "Use -f command line argument to force output\n";
104105 return 1;
105106 }
106 Out = new std::ofstream(OutputFilename.c_str());
107 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
108 std::ios::binary;
109 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
107110
108111 if (!Out->good()) {
109112 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
116119 }
117120
118121 // If the output is set to be emitted to standard out, and standard out is a
119 // console, print out a warning message and refuse to do it. We don't impress
120 // anyone by spewing tons of binary goo to a terminal.
122 // console, print out a warning message and refuse to do it. We don't
123 // impress anyone by spewing tons of binary goo to a terminal.
121124 if (!Force && !NoOutput && CheckBytecodeOutputToConsole(Out,!Quiet)) {
122125 NoOutput = true;
123126 }