llvm.org GIT mirror llvm / 3ff9563
MemoryBuffer now return an error_code and returns a OwningPtr<MemoryBuffer> via an out parm. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121958 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 9 years ago
24 changed file(s) with 171 addition(s) and 172 deletion(s). Raw diff Collapse all Expand all
1818 #ifndef LLVM_SUPPORT_IRREADER_H
1919 #define LLVM_SUPPORT_IRREADER_H
2020
21 #include "llvm/ADT/OwningPtr.h"
2122 #include "llvm/Assembly/Parser.h"
2223 #include "llvm/Bitcode/ReaderWriter.h"
2324 #include "llvm/Support/MemoryBuffer.h"
5657 inline Module *getLazyIRFileModule(const std::string &Filename,
5758 SMDiagnostic &Err,
5859 LLVMContext &Context) {
59 error_code ec;
60 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
61 if (F == 0) {
60 OwningPtr File;
61 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
6262 Err = SMDiagnostic(Filename,
6363 "Could not open input file: " + ec.message());
6464 return 0;
6565 }
6666
67 return getLazyIRModule(F, Err, Context);
67 return getLazyIRModule(File.take(), Err, Context);
6868 }
6969
7070 /// If the given MemoryBuffer holds a bitcode image, return a Module
9494 inline Module *ParseIRFile(const std::string &Filename,
9595 SMDiagnostic &Err,
9696 LLVMContext &Context) {
97 error_code ec;
98 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
99 if (F == 0) {
97 OwningPtr File;
98 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
10099 Err = SMDiagnostic(Filename,
101100 "Could not open input file: " + ec.message());
102101 return 0;
103102 }
104103
105 return ParseIR(F, Err, Context);
104 return ParseIR(File.take(), Err, Context);
106105 }
107106
108107 }
1919 namespace llvm {
2020
2121 class error_code;
22 template class OwningPtr;
2223
2324 /// MemoryBuffer - This interface provides simple read-only access to a block
2425 /// of memory, and provides simple methods for reading files and standard input
6061 /// MemoryBuffer if successful, otherwise returning null. If FileSize is
6162 /// specified, this means that the client knows that the file exists and that
6263 /// it has the specified size.
63 static MemoryBuffer *getFile(StringRef Filename, error_code &ec,
64 int64_t FileSize = -1);
65 static MemoryBuffer *getFile(const char *Filename, error_code &ec,
66 int64_t FileSize = -1);
64 static error_code getFile(StringRef Filename, OwningPtr &result,
65 int64_t FileSize = -1);
66 static error_code getFile(const char *Filename,
67 OwningPtr &result,
68 int64_t FileSize = -1);
6769
6870 /// getOpenFile - Given an already-open file descriptor, read the file and
6971 /// return a MemoryBuffer. This takes ownership of the descriptor,
7072 /// immediately closing it after reading the file.
71 static MemoryBuffer *getOpenFile(int FD, const char *Filename,
72 error_code &ec,
73 int64_t FileSize = -1);
73 static error_code getOpenFile(int FD, const char *Filename,
74 OwningPtr &result,
75 int64_t FileSize = -1);
7476
7577 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
7678 /// that InputData must be null terminated.
98100
99101 /// getSTDIN - Read all of stdin into a file buffer, and return it.
100102 /// If an error occurs, this returns null and sets ec.
101 static MemoryBuffer *getSTDIN(error_code &ec);
103 static error_code getSTDIN(OwningPtr &result);
102104
103105
104106 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
105107 /// if the Filename is "-". If an error occurs, this returns null and sets
106108 /// ec.
107 static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
108 error_code &ec,
109 int64_t FileSize = -1);
110 static MemoryBuffer *getFileOrSTDIN(const char *Filename,
111 error_code &ec,
112 int64_t FileSize = -1);
109 static error_code getFileOrSTDIN(StringRef Filename,
110 OwningPtr &result,
111 int64_t FileSize = -1);
112 static error_code getFileOrSTDIN(const char *Filename,
113 OwningPtr &result,
114 int64_t FileSize = -1);
113115 };
114116
115117 } // end namespace llvm
147147
148148 bool
149149 Archive::mapToMemory(std::string* ErrMsg) {
150 error_code ec;
151 mapfile = MemoryBuffer::getFile(archPath.c_str(), ec);
152 if (mapfile == 0) {
150 OwningPtr File;
151 if (error_code ec = MemoryBuffer::getFile(archPath.c_str(), File)) {
153152 if (ErrMsg)
154153 *ErrMsg = ec.message();
155154 return true;
156155 }
156 mapfile = File.take();
157157 base = mapfile->getBufferStart();
158158 return false;
159159 }
217217 LLVMContext& Context,
218218 std::vector& symbols,
219219 std::string* ErrMsg) {
220 error_code ec;
221 std::auto_ptr Buffer(
222 MemoryBuffer::getFileOrSTDIN(fName.c_str(), ec));
223 if (!Buffer.get()) {
220 OwningPtr Buffer;
221 if (error_code ec = MemoryBuffer::getFileOrSTDIN(fName.c_str(), Buffer)) {
224222 if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'" + ": "
225223 + ec.message();
226224 return true;
245243 std::vector& symbols,
246244 std::string* ErrMsg) {
247245 // Get the module.
248 std::auto_ptr Buffer(
246 OwningPtr Buffer(
249247 MemoryBuffer::getMemBufferCopy(StringRef(BufPtr, Length),ModuleID.c_str()));
250248
251249 Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);
212212 const char *data = (const char*)member.getData();
213213 MemoryBuffer *mFile = 0;
214214 if (!data) {
215 error_code ec;
216 mFile = MemoryBuffer::getFile(member.getPath().c_str(), ec);
217 if (mFile == 0) {
215 OwningPtr File;
216 if (error_code ec = MemoryBuffer::getFile(member.getPath().c_str(), File)) {
218217 if (ErrMsg)
219218 *ErrMsg = ec.message();
220219 return true;
221220 }
221 mFile = File.take();
222222 data = mFile->getBufferStart();
223223 fSize = mFile->getBufferSize();
224224 }
410410
411411 // Map in the archive we just wrote.
412412 {
413 error_code ec;
414 OwningPtr arch(MemoryBuffer::getFile(TmpArchive.c_str(), ec));
415 if (arch == 0) {
413 OwningPtr arch;
414 if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
416415 if (ErrMsg)
417416 *ErrMsg = ec.message();
418417 return true;
4141
4242 Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
4343 LLVMContext &Context) {
44 error_code ec;
45 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
46 if (F == 0) {
44 OwningPtr File;
45 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
4746 Err = SMDiagnostic(Filename,
4847 "Could not open input file: " + ec.message());
4948 return 0;
5049 }
5150
52 return ParseAssembly(F, 0, Err, Context);
51 return ParseAssembly(File.take(), 0, Err, Context);
5352 }
5453
5554 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
160160 // Check for a file of name "-", which means "read standard input"
161161 if (File.str() == "-") {
162162 std::auto_ptr M;
163 OwningPtr Buffer;
163164 error_code ec;
164 if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN(ec)) {
165 if (!(ec = MemoryBuffer::getSTDIN(Buffer))) {
165166 if (!Buffer->getBufferSize()) {
166 delete Buffer;
167167 Error = "standard input is empty";
168168 } else {
169 M.reset(ParseBitcodeFile(Buffer, Context, &Error));
170 delete Buffer;
169 M.reset(ParseBitcodeFile(Buffer.get(), Context, &Error));
171170 if (M.get())
172171 if (!LinkInModule(M.get(), &Error))
173172 return false;
9898 std::string ParseErrorMessage;
9999 Module *Result = 0;
100100
101 error_code ec;
102 std::auto_ptr Buffer(
103 MemoryBuffer::getFileOrSTDIN(FN.c_str(), ec));
104 if (Buffer.get())
105 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
106 else
101 OwningPtr Buffer;
102 if (error_code ec = MemoryBuffer::getFileOrSTDIN(FN.c_str(), Buffer))
107103 ParseErrorMessage = "Error reading file '" + FN.str() + "'" + ": "
108104 + ec.message();
105 else
106 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
109107
110108 if (Result)
111109 return std::auto_ptr(Result);
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/ADT/OwningPtr.h"
1415 #include "llvm/Support/ErrorHandling.h"
1516 #include "llvm/Support/MemoryBuffer.h"
1617 #include "llvm/Support/Path.h"
6263 }
6364
6465 ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
65 error_code ec;
66 return createObjectFile(MemoryBuffer::getFile(ObjectPath, ec));
66 OwningPtr File;
67 if (error_code ec = MemoryBuffer::getFile(ObjectPath, File))
68 return NULL;
69 return createObjectFile(File.take());
6770 }
463463 const sys::FileStatus *FileStat = respFile.getFileStatus();
464464 if (FileStat && FileStat->getSize() != 0) {
465465
466 // Mmap the response file into memory.
467 error_code ec;
468 OwningPtr
469 respFilePtr(MemoryBuffer::getFile(respFile.c_str(), ec));
470
471466 // If we could open the file, parse its contents, otherwise
472467 // pass the @file option verbatim.
473468
476471 // itself contain additional @file options; any such options will be
477472 // processed recursively.")
478473
479 if (respFilePtr != 0) {
474 // Mmap the response file into memory.
475 OwningPtr respFilePtr;
476 if (!MemoryBuffer::getFile(respFile.c_str(), respFilePtr)) {
480477 ParseCStringVector(newArgv, respFilePtr->getBufferStart());
481478 continue;
482479 }
200200 // Now its safe to mmap the files into memory becasue both files
201201 // have a non-zero size.
202202 error_code ec;
203 OwningPtr F1(MemoryBuffer::getFile(FileA.c_str(), ec));
204 if (F1 == 0) {
203 OwningPtr F1;
204 if (error_code ec = MemoryBuffer::getFile(FileA.c_str(), F1)) {
205205 if (Error)
206206 *Error = ec.message();
207207 return 2;
208208 }
209 OwningPtr F2(MemoryBuffer::getFile(FileB.c_str(), ec));
210 if (F2 == 0) {
209 OwningPtr F2;
210 if (error_code ec = MemoryBuffer::getFile(FileB.c_str(), F2)) {
211211 if (Error)
212212 *Error = ec.message();
213213 return 2;
3434 #include
3535 using namespace llvm;
3636
37 namespace { const llvm::error_code success; }
38
3739 //===----------------------------------------------------------------------===//
3840 // MemoryBuffer implementation itself.
3941 //===----------------------------------------------------------------------===//
142144 /// if the Filename is "-". If an error occurs, this returns null and fills
143145 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
144146 /// returns an empty buffer.
145 MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename,
146 error_code &ec,
147 int64_t FileSize) {
147 error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
148 OwningPtr &result,
149 int64_t FileSize) {
148150 if (Filename == "-")
149 return getSTDIN(ec);
150 return getFile(Filename, ec, FileSize);
151 }
152
153 MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
154 error_code &ec,
155 int64_t FileSize) {
151 return getSTDIN(result);
152 return getFile(Filename, result, FileSize);
153 }
154
155 error_code MemoryBuffer::getFileOrSTDIN(const char *Filename,
156 OwningPtr &result,
157 int64_t FileSize) {
156158 if (strcmp(Filename, "-") == 0)
157 return getSTDIN(ec);
158 return getFile(Filename, ec, FileSize);
159 return getSTDIN(result);
160 return getFile(Filename, result, FileSize);
159161 }
160162
161163 //===----------------------------------------------------------------------===//
185187 };
186188 }
187189
188 MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, error_code &ec,
189 int64_t FileSize) {
190 error_code MemoryBuffer::getFile(StringRef Filename,
191 OwningPtr &result,
192 int64_t FileSize) {
190193 // Ensure the path is null terminated.
191194 SmallString<256> PathBuf(Filename.begin(), Filename.end());
192 return MemoryBuffer::getFile(PathBuf.c_str(), ec, FileSize);
193 }
194
195 MemoryBuffer *MemoryBuffer::getFile(const char *Filename, error_code &ec,
196 int64_t FileSize) {
195 return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize);
196 }
197
198 error_code MemoryBuffer::getFile(const char *Filename,
199 OwningPtr &result,
200 int64_t FileSize) {
197201 int OpenFlags = O_RDONLY;
198202 #ifdef O_BINARY
199203 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
200204 #endif
201205 int FD = ::open(Filename, OpenFlags);
202206 if (FD == -1) {
203 ec = error_code(errno, posix_category());
204 return 0;
205 }
206
207 return getOpenFile(FD, Filename, ec, FileSize);
208 }
209
210 MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
211 error_code &ec, int64_t FileSize) {
207 return error_code(errno, posix_category());
208 }
209
210 return getOpenFile(FD, Filename, result, FileSize);
211 }
212
213 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
214 OwningPtr &result,
215 int64_t FileSize) {
212216 FileCloser FC(FD); // Close FD on return.
213217
214218 // If we don't know the file size, use fstat to find out. fstat on an open
217221 struct stat FileInfo;
218222 // TODO: This should use fstat64 when available.
219223 if (fstat(FD, &FileInfo) == -1) {
220 ec = error_code(errno, posix_category());
221 return 0;
224 return error_code(errno, posix_category());
222225 }
223226 FileSize = FileInfo.st_size;
224227 }
233236 if (FileSize >= 4096*4 &&
234237 (FileSize & (sys::Process::GetPageSize()-1)) != 0) {
235238 if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
236 return GetNamedBuffer(StringRef(Pages, FileSize),
237 Filename);
239 result.reset(GetNamedBuffer(
240 StringRef(Pages, FileSize), Filename));
241 return success;
238242 }
239243 }
240244
242246 if (!Buf) {
243247 // Failed to create a buffer. The only way it can fail is if
244248 // new(std::nothrow) returns 0.
245 ec = make_error_code(errc::not_enough_memory);
246 return 0;
249 return make_error_code(errc::not_enough_memory);
247250 }
248251
249252 OwningPtr SB(Buf);
256259 if (errno == EINTR)
257260 continue;
258261 // Error while reading.
259 ec = error_code(errno, posix_category());
260 return 0;
262 return error_code(errno, posix_category());
261263 } else if (NumRead == 0) {
262264 // We hit EOF early, truncate and terminate buffer.
263265 Buf->BufferEnd = BufPtr;
264266 *BufPtr = 0;
265 return SB.take();
267 result.swap(SB);
268 return success;
266269 }
267270 BytesLeft -= NumRead;
268271 BufPtr += NumRead;
269272 }
270273
271 return SB.take();
274 result.swap(SB);
275 return success;
272276 }
273277
274278 //===----------------------------------------------------------------------===//
275279 // MemoryBuffer::getSTDIN implementation.
276280 //===----------------------------------------------------------------------===//
277281
278 MemoryBuffer *MemoryBuffer::getSTDIN(error_code &ec) {
282 error_code MemoryBuffer::getSTDIN(OwningPtr &result) {
279283 // Read in all of the data from stdin, we cannot mmap stdin.
280284 //
281285 // FIXME: That isn't necessarily true, we should try to mmap stdin and
291295 ReadBytes = read(0, Buffer.end(), ChunkSize);
292296 if (ReadBytes == -1) {
293297 if (errno == EINTR) continue;
294 ec = error_code(errno, posix_category());
295 return 0;
298 return error_code(errno, posix_category());
296299 }
297300 Buffer.set_size(Buffer.size() + ReadBytes);
298301 } while (ReadBytes != 0);
299302
300 return getMemBufferCopy(Buffer, "");
301 }
303 result.reset(getMemBufferCopy(Buffer, ""));
304 return success;
305 }
1515 #include "llvm/ADT/Twine.h"
1616 #include "llvm/Support/SourceMgr.h"
1717 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/ADT/OwningPtr.h"
1819 #include "llvm/Support/raw_ostream.h"
1920 #include "llvm/Support/system_error.h"
2021 using namespace llvm;
4849 /// ~0, otherwise it returns the buffer ID of the stacked file.
4950 unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
5051 SMLoc IncludeLoc) {
51 error_code ec;
52 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str(), ec);
52 OwningPtr NewBuf;
53 MemoryBuffer::getFile(Filename.c_str(), NewBuf);
5354
5455 // If the file didn't exist directly, see if it's in an include path.
5556 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
5657 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
57 NewBuf = MemoryBuffer::getFile(IncFile.c_str(), ec);
58 MemoryBuffer::getFile(IncFile.c_str(), NewBuf);
5859 }
5960
6061 if (NewBuf == 0) return ~0U;
6162
62 return AddNewSourceBuffer(NewBuf, IncludeLoc);
63 return AddNewSourceBuffer(NewBuf.take(), IncludeLoc);
6364 }
6465
6566
22202220 LLVMMemoryBufferRef *OutMemBuf,
22212221 char **OutMessage) {
22222222
2223 OwningPtr MB;
22232224 error_code ec;
2224 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, ec)) {
2225 *OutMemBuf = wrap(MB);
2225 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2226 *OutMemBuf = wrap(MB.take());
22262227 return 0;
22272228 }
22282229
22322233
22332234 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
22342235 char **OutMessage) {
2236 OwningPtr MB;
22352237 error_code ec;
2236 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(ec)) {
2237 *OutMemBuf = wrap(MB);
2238 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2239 *OutMemBuf = wrap(MB.take());
22382240 return 0;
22392241 }
22402242
2626 //
2727 //===----------------------------------------------------------------------===//
2828
29 #include "llvm/ADT/OwningPtr.h"
2930 #include "llvm/Analysis/Verifier.h"
3031 #include "llvm/Bitcode/BitstreamReader.h"
3132 #include "llvm/Bitcode/LLVMBitCodes.h"
5758
5859 static cl::opt
5960 NonSymbolic("non-symbolic",
60 cl::desc("Emit numberic info in dump even if"
61 cl::desc("Emit numeric info in dump even if"
6162 " symbolic info is available"));
6263
6364 namespace {
480481 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
481482 static int AnalyzeBitcode() {
482483 // Read the input file.
483 error_code ec;
484 MemoryBuffer *MemBuf =
485 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
486
487 if (MemBuf == 0)
484 OwningPtr MemBuf;
485
486 if (error_code ec =
487 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), MemBuf))
488488 return Error("Error reading '" + InputFilename + "': " + ec.message());
489489
490490 if (MemBuf->getBufferSize() & 3)
8080 std::string ErrorMessage;
8181 error_code ec;
8282 std::auto_ptr M;
83 OwningPtr BufferPtr;
8384
84 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec)) {
85 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
86 delete Buffer;
87 } else
85 if (ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
8886 ErrorMessage = ec.message();
87 else
88 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
89 MemoryBuffer *Buffer = BufferPtr.take();
8990
9091 if (M.get() == 0) {
9192 errs() << argv[0] << ": ";
167167 }
168168
169169 static int AsLexInput(const char *ProgName) {
170 error_code ec;
171 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
172 if (Buffer == 0) {
170 OwningPtr BufferPtr;
171 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
173172 errs() << ProgName << ": " << ec.message() << '\n';
174173 return 1;
175174 }
175 MemoryBuffer *Buffer = BufferPtr.take();
176176
177177 SourceMgr SrcMgr;
178178
280280 if (!TheTarget)
281281 return 1;
282282
283 error_code ec;
284 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
285 if (Buffer == 0) {
283 OwningPtr BufferPtr;
284 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
286285 errs() << ProgName << ": " << ec.message() << '\n';
287286 return 1;
288287 }
288 MemoryBuffer *Buffer = BufferPtr.take();
289289
290290 SourceMgr SrcMgr;
291291
386386 if (!TheTarget)
387387 return 0;
388388
389 error_code ec;
390 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
391 if (Buffer == 0) {
389 OwningPtr Buffer;
390 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) {
392391 errs() << ProgName << ": " << ec.message() << '\n';
393392 return 1;
394393 }
399398
400399 int Res;
401400 if (Enhanced)
402 Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, Out->os());
401 Res =
402 Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
403403 else
404 Res = Disassembler::disassemble(*TheTarget, TripleName, *Buffer, Out->os());
404 Res = Disassembler::disassemble(*TheTarget, TripleName,
405 *Buffer.take(), Out->os());
405406
406407 // Keep output if no errors.
407408 if (Res == 0) Out->keep();
143143 sys::Path aPath(Filename);
144144 // Note: Currently we do not support reading an archive from stdin.
145145 if (Filename == "-" || aPath.isBitcodeFile()) {
146 error_code ec;
147 std::auto_ptr Buffer(
148 MemoryBuffer::getFileOrSTDIN(Filename, ec));
149 if (Buffer.get() == 0)
146 OwningPtr Buffer;
147 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer))
150148 ErrorMessage = ec.message();
151149 Module *Result = 0;
152150 if (Buffer.get())
263263
264264 // Read in the bitcode file...
265265 std::string ErrorMessage;
266 OwningPtr Buffer;
266267 error_code ec;
267268 Module *M = 0;
268 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile, ec)) {
269 M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
270 delete Buffer;
269 if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) {
270 M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
271271 } else
272272 ErrorMessage = ec.message();
273273 if (M == 0) {
223223 delete _nativeObjectFile;
224224
225225 // read .o file into memory buffer
226 error_code ec;
227 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(), ec);
228 if (ec)
226 OwningPtr BuffPtr;
227 if (error_code ec = MemoryBuffer::getFile(uniqueObjStr.c_str(),BuffPtr))
229228 errMsg = ec.message();
229 _nativeObjectFile = BuffPtr.take();
230230 }
231231
232232 // remove temp files
5656
5757 bool LTOModule::isBitcodeFileForTarget(const char *path,
5858 const char *triplePrefix) {
59 error_code ec;
60 MemoryBuffer *buffer = MemoryBuffer::getFile(path, ec);
61 if (buffer == NULL)
59 OwningPtr buffer;
60 if (MemoryBuffer::getFile(path, buffer))
6261 return false;
63 return isTargetMatch(buffer, triplePrefix);
62 return isTargetMatch(buffer.take(), triplePrefix);
6463 }
6564
6665 // Takes ownership of buffer.
6766 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
6867 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
6968 delete buffer;
70 return (strncmp(Triple.c_str(), triplePrefix,
69 return (strncmp(Triple.c_str(), triplePrefix,
7170 strlen(triplePrefix)) == 0);
7271 }
7372
7978
8079 LTOModule *LTOModule::makeLTOModule(const char *path,
8180 std::string &errMsg) {
82 error_code ec;
83 OwningPtr buffer(MemoryBuffer::getFile(path, ec));
84 if (!buffer) {
81 OwningPtr buffer;
82 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
8583 errMsg = ec.message();
8684 return NULL;
8785 }
365365
366366 // Load the input file.
367367 std::string ErrorStr;
368 error_code ec;
369 OwningPtr InputBuffer(
370 MemoryBuffer::getFileOrSTDIN(InputFile, ec));
371 if (!InputBuffer)
368 OwningPtr InputBuffer;
369 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
372370 return Error("unable to read input: '" + ec.message() + "'");
373371
374372 // Construct the Mach-O wrapper object.
1515 //
1616 //===----------------------------------------------------------------------===//
1717
18 #include "llvm/ADT/OwningPtr.h"
1819 #include "llvm/Support/CommandLine.h"
1920 #include "llvm/Support/MemoryBuffer.h"
2021 #include "llvm/Support/PrettyStackTrace.h"
488489 static bool ReadCheckFile(SourceMgr &SM,
489490 std::vector &CheckStrings) {
490491 // Open the check file, and tell SourceMgr about it.
491 error_code ec;
492 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), ec);
493 if (F == 0) {
492 OwningPtr File;
493 if (error_code ec =
494 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), File)) {
494495 errs() << "Could not open check file '" << CheckFilename << "': "
495496 << ec.message() << '\n';
496497 return true;
497498 }
499 MemoryBuffer *F = File.take();
498500
499501 // If we want to canonicalize whitespace, strip excess whitespace from the
500502 // buffer containing the CHECK lines.
647649 return 2;
648650
649651 // Open the file to check and add it to SourceMgr.
650 error_code ec;
651 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
652 if (F == 0) {
652 OwningPtr File;
653 if (error_code ec =
654 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
653655 errs() << "Could not open input file '" << InputFilename << "': "
654656 << ec.message() << '\n';
655657 return true;
656658 }
659 MemoryBuffer *F = File.take();
657660
658661 // Remove duplicate spaces in the input file if requested.
659662 if (!NoCanonicalizeWhiteSpace)
1414
1515 #include "llvm/Support/CommandLine.h"
1616 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/ADT/OwningPtr.h"
1718 #include "llvm/Support/PrettyStackTrace.h"
1819 #include "llvm/Support/ToolOutputFile.h"
1920 #include "llvm/Support/Signals.h"
4243 }
4344
4445 // Get the input data.
45 error_code ec;
46 MemoryBuffer *In =
47 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
48 if (In == 0) {
46 OwningPtr In;
47 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), In)) {
4948 errs() << argv[0] << ": error: Unable to get input '"
5049 << InputFilename << "': " << ec.message() << '\n';
5150 return 1;
5251 }
5352
5453 // Get the output data.
55 MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), ec);
54 OwningPtr Out;
55 MemoryBuffer::getFile(OutputFilename.c_str(), Out);
5656
5757 // If the output exists and the contents match, we are done.
5858 if (Out && In->getBufferSize() == Out->getBufferSize() &&
6363 << "', contents match input.\n";
6464 return 0;
6565 }
66
67 delete Out;
6866
6967 // Otherwise, overwrite the output.
7068 if (!Quiet)
3636 #include "ARMDecoderEmitter.h"
3737 #include "SubtargetEmitter.h"
3838 #include "TGParser.h"
39 #include "llvm/ADT/OwningPtr.h"
3940 #include "llvm/Support/CommandLine.h"
4041 #include "llvm/Support/MemoryBuffer.h"
4142 #include "llvm/Support/PrettyStackTrace.h"
189190 const std::vector &IncludeDirs,
190191 SourceMgr &SrcMgr,
191192 RecordKeeper &Records) {
192 error_code ec;
193 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
194 if (F == 0) {
193 OwningPtr File;
194 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
195195 errs() << "Could not open input file '" << Filename << "': "
196196 << ec.message() <<"\n";
197197 return true;
198198 }
199 MemoryBuffer *F = File.take();
199200
200201 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
201202 SrcMgr.AddNewSourceBuffer(F, SMLoc());