llvm.org GIT mirror llvm / 0d9eb9b
Add Binary flag to raw_fd_ostream constructor. Document raw_fd_ostream's treatment of "-". git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@59219 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 10 years ago
4 changed file(s) with 25 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
155155 /// error occurs, information about the error is put into ErrorInfo,
156156 /// and the stream should be immediately destroyed; the string will
157157 /// be empty if no error occurred.
158 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
158 ///
159 /// \param Filename - The file to open. If this is "-" then the
160 /// stream will use stdout instead.
161 /// \param Binary - The file should be opened in binary mode on
162 /// platforms that support this distinction.
163 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
159164
160165 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
161166 /// ShouldClose is true, this closes the file when
1212
1313 #include "llvm/Support/raw_ostream.h"
1414 #include "llvm/Support/Format.h"
15 #include "llvm/System/Program.h"
1516 #include "llvm/ADT/SmallVector.h"
1617 #include "llvm/Config/config.h"
1718 #include
199200 /// occurs, information about the error is put into ErrorInfo, and the
200201 /// stream should be immediately destroyed; the string will be empty
201202 /// if no error occurred.
202 raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) {
203 raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
204 std::string &ErrorInfo) {
203205 ErrorInfo.clear();
204206
205207 // Handle "-" as stdout.
206208 if (Filename[0] == '-' && Filename[1] == 0) {
207209 FD = STDOUT_FILENO;
210 // If user requested binary then put stdout into binary mode if
211 // possible.
212 if (Binary)
213 sys::Program::ChangeStdoutToBinary();
208214 ShouldClose = false;
209215 return;
210216 }
211217
212 FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
218 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
219 #ifdef O_BINARY
220 if (Binary)
221 Flags |= O_BINARY;
222 #endif
223 FD = open(Filename, Flags, 0644);
213224 if (FD < 0) {
214225 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
215226 ShouldClose = false;
124124 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
125125
126126 std::string error;
127 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), error);
127 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), true, error);
128128 if (!error.empty()) {
129129 std::cerr << error << '\n';
130130 delete Out;
141141
142142 OutputFilename = GetFileNameRoot(InputFilename);
143143
144 bool Binary = false;
144145 switch (FileType) {
145146 case TargetMachine::AssemblyFile:
146147 if (MArch->Name[0] == 'c') {
155156 break;
156157 case TargetMachine::ObjectFile:
157158 OutputFilename += ".o";
159 Binary = true;
158160 break;
159161 case TargetMachine::DynamicLibrary:
160162 OutputFilename += LTDL_SHLIB_EXT;
163 Binary = true;
161164 break;
162165 }
163166
174177 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
175178
176179 std::string error;
177 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), error);
180 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Binary, error);
178181 if (!error.empty()) {
179182 std::cerr << error << '\n';
180183 delete Out;
164164 // generate assembly code
165165 bool genResult = false;
166166 {
167 raw_fd_ostream asmFile(uniqueAsmPath.c_str(), errMsg);
167 raw_fd_ostream asmFile(uniqueAsmPath.c_str(), false, errMsg);
168168 if (!errMsg.empty())
169169 return NULL;
170170 genResult = this->generateAssemblyCode(asmFile, errMsg);