llvm.org GIT mirror llvm / 038112a
Change the MemoryBuffer::getFile* methods to take just a pointer to the start of a filename, not a filename+length. All clients can produce a null terminated name, and the system api's require null terminated strings anyway. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49041 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
14 changed file(s) with 36 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
5151 /// MemoryBuffer if successful, otherwise returning null. If FileSize is
5252 /// specified, this means that the client knows that the file exists and that
5353 /// it has the specified size.
54 static MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
54 static MemoryBuffer *getFile(const char *Filename,
5555 std::string *ErrStr = 0,
5656 int64_t FileSize = -1);
5757
8989 /// if the Filename is "-". If an error occurs, this returns null and fills
9090 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
9191 /// returns an empty buffer.
92 static MemoryBuffer *getFileOrSTDIN(const char *FilenameStart,unsigned FnSize,
92 static MemoryBuffer *getFileOrSTDIN(const char *Filename,
9393 std::string *ErrStr = 0,
9494 int64_t FileSize = -1);
9595
9999 static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
100100 std::string *ErrStr = 0,
101101 int64_t FileSize = -1) {
102 return getFileOrSTDIN(&FN[0], FN.size(), ErrStr, FileSize);
102 return getFileOrSTDIN(FN.c_str(), ErrStr, FileSize);
103103 }
104104 };
105105
144144
145145 bool
146146 Archive::mapToMemory(std::string* ErrMsg) {
147 mapfile = MemoryBuffer::getFile(archPath.c_str(), archPath.size(), ErrMsg);
147 mapfile = MemoryBuffer::getFile(archPath.c_str(), ErrMsg);
148148 if (mapfile == 0)
149149 return true;
150150 base = mapfile->getBufferStart();
210210 std::vector& symbols,
211211 std::string* ErrMsg) {
212212 std::auto_ptr Buffer(
213 MemoryBuffer::getFileOrSTDIN(&fName.toString()[0],
214 fName.toString().size()));
213 MemoryBuffer::getFileOrSTDIN(fName.c_str()));
215214 if (!Buffer.get()) {
216215 if (ErrMsg) *ErrMsg = "Could not open file '" + fName.toString() + "'";
217216 return true;
211211 const char *data = (const char*)member.getData();
212212 MemoryBuffer *mFile = 0;
213213 if (!data) {
214 mFile = MemoryBuffer::getFile(member.getPath().c_str(),
215 member.getPath().size(), ErrMsg);
214 mFile = MemoryBuffer::getFile(member.getPath().c_str(), ErrMsg);
216215 if (mFile == 0)
217216 return true;
218217 data = mFile->getBufferStart();
406405
407406 // Map in the archive we just wrote.
408407 {
409 OwningPtr arch(MemoryBuffer::getFile(TmpArchive.c_str(),
410 TmpArchive.size()));
408 OwningPtr arch(MemoryBuffer::getFile(TmpArchive.c_str()));
411409 if (arch == 0) return true;
412410 const char* base = arch->getBufferStart();
413411
2121
2222 Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError* Err) {
2323 std::string ErrorStr;
24 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size(),
25 &ErrorStr);
24 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
2625 if (F == 0) {
2726 if (Err)
2827 Err->setError(Filename, "Could not open input file '" + Filename + "'");
4646 static Module *
4747 getMaterializedModuleProvider(const std::string &Filename) {
4848 std::auto_ptr Buffer;
49 Buffer.reset(MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size()));
49 Buffer.reset(MemoryBuffer::getFileOrSTDIN(Filename.c_str()));
5050 if (Buffer.get())
5151 return ParseBitcodeFile(Buffer.get());
5252 return 0;
1818 #include "llvm/Support/Streams.h"
1919 using namespace llvm;
2020
21 Linker::Linker(const std::string& progname, const std::string& modname, unsigned flags)
21 Linker::Linker(const std::string& progname, const std::string& modname,
22 unsigned flags)
2223 : Composite(0)
2324 , LibPaths()
2425 , Flags(flags)
102103 Module *Result = 0;
103104
104105 const std::string &FNS = FN.toString();
105 std::auto_ptr Buffer(
106 MemoryBuffer::getFileOrSTDIN(&FNS[0], FNS.size()));
106 std::auto_ptr Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
107107 if (Buffer.get())
108108 Result = ParseBitcodeFile(Buffer.get(), &ParseErrorMessage);
109109 else
188188
189189 // Now its safe to mmap the files into memory becasue both files
190190 // have a non-zero size.
191 OwningPtr F1(MemoryBuffer::getFile(FileA.c_str(), FileA.size(),
192 Error));
193 OwningPtr F2(MemoryBuffer::getFile(FileB.c_str(), FileB.size(),
194 Error));
191 OwningPtr F1(MemoryBuffer::getFile(FileA.c_str(), Error));
192 OwningPtr F2(MemoryBuffer::getFile(FileB.c_str(), Error));
195193 if (F1 == 0 || F2 == 0)
196194 return 2;
197195
131131 /// if the Filename is "-". If an error occurs, this returns null and fills
132132 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
133133 /// returns an empty buffer.
134 MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *FilenameStart,
135 unsigned FnSize,
134 MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
136135 std::string *ErrStr,
137136 int64_t FileSize) {
138 if (FnSize != 1 || FilenameStart[0] != '-')
139 return getFile(FilenameStart, FnSize, ErrStr, FileSize);
137 if (Filename[0] != '-' || Filename[1] != 0)
138 return getFile(Filename, ErrStr, FileSize);
140139 MemoryBuffer *M = getSTDIN();
141140 if (M) return M;
142141
171170 };
172171 }
173172
174 MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
175 std::string *ErrStr, int64_t FileSize) {
176 // Null terminate the filename.
177 SmallString<1000> Filename(FilenameStart, FilenameStart+FnSize);
178 Filename.push_back(0);
179
173 MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
174 int64_t FileSize) {
180175 int OpenFlags = 0;
181176 #ifdef O_BINARY
182177 Flags |= O_BINARY; // Open input file in binary mode on win32.
183178 #endif
184 int FD = ::open(&Filename[0], O_RDONLY|OpenFlags);
179 int FD = ::open(Filename, O_RDONLY|OpenFlags);
185180 if (FD == -1) {
186181 if (ErrStr) *ErrStr = "could not open file";
187182 return 0;
210205 if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
211206 // Close the file descriptor, now that the whole file is in memory.
212207 ::close(FD);
213 return new MemoryBufferMMapFile(&Filename[0], Pages, FileSize);
208 return new MemoryBufferMMapFile(Filename, Pages, FileSize);
214209 }
215210 }
216211
217212 OwningPtr SB;
218 SB.reset(MemoryBuffer::getNewUninitMemBuffer(FileSize, &Filename[0]));
213 SB.reset(MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename));
219214 char *BufPtr = const_cast(SB->getBufferStart());
220215
221216 unsigned BytesLeft = FileSize;
13061306 LLVMMemoryBufferRef *OutMemBuf,
13071307 char **OutMessage) {
13081308 std::string Error;
1309 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
1309 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
13101310 *OutMemBuf = wrap(MB);
13111311 return 0;
13121312 }
373373 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
374374 static int AnalyzeBitcode() {
375375 // Read the input file.
376 MemoryBuffer *Buffer;
377 if (InputFilename == "-")
378 Buffer = MemoryBuffer::getSTDIN();
379 else
380 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
376 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str());
381377
382378 if (Buffer == 0)
383379 return Error("Error reading '" + InputFilename + "'.");
185185 delete _nativeObjectFile;
186186
187187 // read .o file into memory buffer
188 _nativeObjectFile = MemoryBuffer::getFile(&uniqueObjStr[0],
189 uniqueObjStr.size(), &errMsg);
188 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
190189 }
191190
192191 // remove temp files
4242 return llvm::sys::Path(path).isBitcodeFile();
4343 }
4444
45 bool LTOModule::isBitcodeFileForTarget(const void* mem,
46 size_t length, const char* triplePrefix)
45 bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
46 const char* triplePrefix)
4747 {
4848 MemoryBuffer* buffer = MemoryBuffer::getMemBuffer((char*)mem,
49 (char*)mem+length);
49 (char*)mem+length);
5050 if ( buffer == NULL )
5151 return false;
5252 return isTargetMatch(buffer, triplePrefix);
5454
5555
5656 bool LTOModule::isBitcodeFileForTarget(const char* path,
57 const char* triplePrefix)
58 {
59 MemoryBuffer* buffer = MemoryBuffer::getFile(path, strlen(path));
60 if ( buffer == NULL )
57 const char* triplePrefix)
58 {
59 MemoryBuffer *buffer = MemoryBuffer::getFile(path);
60 if (buffer == NULL)
6161 return false;
6262 return isTargetMatch(buffer, triplePrefix);
6363 }
8484
8585 LTOModule* LTOModule::makeLTOModule(const char* path, std::string& errMsg)
8686 {
87 OwningPtr buffer(MemoryBuffer::getFile(
88 path, strlen(path), &errMsg));
87 OwningPtr buffer(MemoryBuffer::getFile(path, &errMsg));
8988 if ( !buffer )
9089 return NULL;
9190 return makeLTOModule(buffer.get(), errMsg);
255255 std::string Filename = CurStrVal;
256256
257257 // Try to find the file.
258 MemoryBuffer *NewBuf = MemoryBuffer::getFile(&Filename[0], Filename.size());
258 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
259259
260260 // If the file didn't exist directly, see if it's in an include path.
261261 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
262262 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
263 NewBuf = MemoryBuffer::getFile(&IncFile[0], IncFile.size());
263 NewBuf = MemoryBuffer::getFile(IncFile.c_str());
264264 }
265265
266266 if (NewBuf == 0) {
106106 static bool ParseFile(const std::string &Filename,
107107 const std::vector &IncludeDirs) {
108108 std::string ErrorStr;
109 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size(),
110 &ErrorStr);
109 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
111110 if (F == 0) {
112111 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
113112 return true;