llvm.org GIT mirror llvm / 5499da8
Enhance MemoryBuffer to return error messages in strings if they occur. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36899 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
2 changed file(s) with 25 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
1414 #define LLVM_SUPPORT_MEMORYBUFFER_H
1515
1616 #include "llvm/Support/DataTypes.h"
17 #include
1718
1819 namespace llvm {
1920
5152 /// specified, this means that the client knows that the file exists and that
5253 /// it has the specified size.
5354 static MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
55 std::string *ErrStr = 0,
5456 int64_t FileSize = -1);
5557
5658 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
7880
7981
8082 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
81 /// if the Filename is "-".
83 /// if the Filename is "-". If an error occurs, this returns null and fills
84 /// in *ErrStr with a reason.
8285 static MemoryBuffer *getFileOrSTDIN(const char *FilenameStart,unsigned FnSize,
86 std::string *ErrStr = 0,
8387 int64_t FileSize = -1) {
8488 if (FnSize == 1 && FilenameStart[0] == '-')
8589 return getSTDIN();
86 return getFile(FilenameStart, FnSize, FileSize);
90 return getFile(FilenameStart, FnSize, ErrStr, FileSize);
8791 }
8892
93 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
94 /// if the Filename is "-". If an error occurs, this returns null and fills
95 /// in *ErrStr with a reason.
96 static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
97 std::string *ErrStr = 0,
98 int64_t FileSize = -1) {
99 return getFileOrSTDIN(&FN[0], FN.size(), ErrStr, FileSize);
100 }
89101 };
90102
91103 } // end namespace llvm
112112 public:
113113 MemoryBufferMMapFile() {}
114114
115 bool open(const sys::Path &Filename);
115 bool open(const sys::Path &Filename, std::string *ErrStr);
116116
117117 virtual const char *getBufferIdentifier() const {
118118 return File.path().c_str();
122122 };
123123 }
124124
125 bool MemoryBufferMMapFile::open(const sys::Path &Filename) {
125 bool MemoryBufferMMapFile::open(const sys::Path &Filename,
126 std::string *ErrStr) {
126127 // FIXME: This does an extra stat syscall to figure out the size, but we
127128 // already know the size!
128 bool Failure = File.open(Filename);
129 bool Failure = File.open(Filename, sys::MappedFile::READ_ACCESS, ErrStr);
129130 if (Failure) return true;
130131
131 File.map();
132 if (!File.map(ErrStr))
133 return true;
132134
133135 size_t Size = File.size();
134136
160162 //===----------------------------------------------------------------------===//
161163
162164 MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
163 int64_t FileSize) {
165 std::string *ErrStr, int64_t FileSize){
166 // FIXME: it would be nice if PathWithStatus didn't copy the filename into a
167 // temporary string. :(
164168 sys::PathWithStatus P(FilenameStart, FnSize);
165169 #if 1
166170 MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
167 if (!M->open(P))
171 if (!M->open(P, ErrStr))
168172 return M;
169173 delete M;
170174 return 0;
185189 // If the file is larger than some threshold, use mmap, otherwise use 'read'.
186190 if (FileSize >= 4096*4) {
187191 MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
188 if (!M->open(P))
192 if (!M->open(P, ErrStr))
189193 return M;
190194 delete M;
191195 return 0;