2007-04-29 06:58:52 +00:00
|
|
|
//===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-04-29 06:58:52 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the MemoryBuffer interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_MEMORYBUFFER_H
|
|
|
|
#define LLVM_SUPPORT_MEMORYBUFFER_H
|
|
|
|
|
2009-08-24 09:29:24 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2007-05-06 23:32:36 +00:00
|
|
|
#include <string>
|
2007-04-29 06:58:52 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// MemoryBuffer - This interface provides simple read-only access to a block
|
|
|
|
/// of memory, and provides simple methods for reading files and standard input
|
|
|
|
/// into a memory buffer. In addition to basic access to the characters in the
|
|
|
|
/// file, this interface guarantees you can read one character past the end of
|
2009-11-18 22:04:44 +00:00
|
|
|
/// the file, and that this character will read as '\0'.
|
2010-06-22 13:57:08 +00:00
|
|
|
///
|
|
|
|
/// The '\0' guarantee is needed to support an optimization -- it's intended to
|
|
|
|
/// be more efficient for clients which are reading all the data to stop
|
|
|
|
/// reading when they encounter a '\0' than to continually check the file
|
|
|
|
/// position to see if it has reached the end of the file.
|
2007-04-29 06:58:52 +00:00
|
|
|
class MemoryBuffer {
|
|
|
|
const char *BufferStart; // Start of the buffer.
|
|
|
|
const char *BufferEnd; // End of the buffer.
|
|
|
|
|
2010-06-25 11:50:40 +00:00
|
|
|
MemoryBuffer(const MemoryBuffer &); // DO NOT IMPLEMENT
|
|
|
|
MemoryBuffer &operator=(const MemoryBuffer &); // DO NOT IMPLEMENT
|
2007-04-29 06:58:52 +00:00
|
|
|
protected:
|
2010-06-25 11:50:40 +00:00
|
|
|
MemoryBuffer() {}
|
2007-04-29 06:58:52 +00:00
|
|
|
void init(const char *BufStart, const char *BufEnd);
|
|
|
|
public:
|
|
|
|
virtual ~MemoryBuffer();
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
const char *getBufferStart() const { return BufferStart; }
|
|
|
|
const char *getBufferEnd() const { return BufferEnd; }
|
2008-05-05 18:30:58 +00:00
|
|
|
size_t getBufferSize() const { return BufferEnd-BufferStart; }
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-08-24 09:29:24 +00:00
|
|
|
StringRef getBuffer() const {
|
|
|
|
return StringRef(BufferStart, getBufferSize());
|
|
|
|
}
|
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
/// getBufferIdentifier - Return an identifier for this buffer, typically the
|
|
|
|
/// filename it was read from.
|
|
|
|
virtual const char *getBufferIdentifier() const {
|
|
|
|
return "Unknown buffer";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getFile - Open the specified file as a MemoryBuffer, returning a new
|
|
|
|
/// MemoryBuffer if successful, otherwise returning null. If FileSize is
|
|
|
|
/// specified, this means that the client knows that the file exists and that
|
|
|
|
/// it has the specified size.
|
2010-11-23 22:20:27 +00:00
|
|
|
static MemoryBuffer *getFile(StringRef Filename, std::string *ErrStr = 0,
|
2010-11-23 06:09:51 +00:00
|
|
|
int64_t FileSize = -1);
|
2010-11-23 22:20:27 +00:00
|
|
|
static MemoryBuffer *getFile(const char *Filename, std::string *ErrStr = 0,
|
2010-11-23 06:09:51 +00:00
|
|
|
int64_t FileSize = -1);
|
2007-04-29 06:58:52 +00:00
|
|
|
|
2010-11-23 22:20:27 +00:00
|
|
|
/// getOpenFile - Given an already-open file descriptor, read the file and
|
|
|
|
/// return a MemoryBuffer. This takes ownership of the descriptor,
|
|
|
|
/// immediately closing it after reading the file.
|
|
|
|
static MemoryBuffer *getOpenFile(int FD, const char *Filename,
|
|
|
|
std::string *ErrStr = 0,
|
|
|
|
int64_t FileSize = -1);
|
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
|
2010-11-10 07:46:36 +00:00
|
|
|
/// that InputData must be null terminated.
|
2010-04-05 22:42:30 +00:00
|
|
|
static MemoryBuffer *getMemBuffer(StringRef InputData,
|
2010-06-25 11:50:40 +00:00
|
|
|
StringRef BufferName = "");
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-10-09 21:46:38 +00:00
|
|
|
/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
|
2010-11-10 07:46:36 +00:00
|
|
|
/// copying the contents and taking ownership of it. InputData does not
|
|
|
|
/// have to be null terminated.
|
2010-04-05 22:42:30 +00:00
|
|
|
static MemoryBuffer *getMemBufferCopy(StringRef InputData,
|
2010-06-25 11:50:40 +00:00
|
|
|
StringRef BufferName = "");
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
/// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
|
|
|
|
/// is completely initialized to zeros. Note that the caller should
|
|
|
|
/// initialize the memory allocated by this method. The memory is owned by
|
|
|
|
/// the MemoryBuffer object.
|
2010-06-25 11:50:40 +00:00
|
|
|
static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
|
|
|
|
/// that is not initialized. Note that the caller should initialize the
|
|
|
|
/// memory allocated by this method. The memory is owned by the MemoryBuffer
|
|
|
|
/// object.
|
2008-05-05 18:30:58 +00:00
|
|
|
static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
|
2009-11-10 00:43:58 +00:00
|
|
|
StringRef BufferName = "");
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-11-10 00:43:58 +00:00
|
|
|
/// getSTDIN - Read all of stdin into a file buffer, and return it.
|
2010-05-27 17:31:51 +00:00
|
|
|
/// If an error occurs, this returns null and fills in *ErrStr with a reason.
|
|
|
|
static MemoryBuffer *getSTDIN(std::string *ErrStr = 0);
|
2009-02-20 22:51:36 +00:00
|
|
|
|
|
|
|
|
2007-05-06 23:32:36 +00:00
|
|
|
/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
|
|
|
|
/// if the Filename is "-". If an error occurs, this returns null and fills
|
|
|
|
/// in *ErrStr with a reason.
|
2009-11-10 00:43:58 +00:00
|
|
|
static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
|
2007-05-06 23:32:36 +00:00
|
|
|
std::string *ErrStr = 0,
|
2010-11-23 06:09:51 +00:00
|
|
|
int64_t FileSize = -1);
|
2010-06-24 16:25:50 +00:00
|
|
|
static MemoryBuffer *getFileOrSTDIN(const char *Filename,
|
|
|
|
std::string *ErrStr = 0,
|
2010-11-23 06:09:51 +00:00
|
|
|
int64_t FileSize = -1);
|
2007-04-29 06:58:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|