LLVM API Documentation

MemoryBuffer.h
Go to the documentation of this file.
00001 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the MemoryBuffer interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
00015 #define LLVM_SUPPORT_MEMORYBUFFER_H
00016 
00017 #include "llvm-c/Support.h"
00018 #include "llvm/ADT/Twine.h"
00019 #include "llvm/Support/CBindingWrapping.h"
00020 #include "llvm/Support/Compiler.h"
00021 #include "llvm/Support/DataTypes.h"
00022 #include "llvm/Support/ErrorOr.h"
00023 #include <memory>
00024 #include <system_error>
00025 
00026 namespace llvm {
00027 class MemoryBufferRef;
00028 
00029 /// This interface provides simple read-only access to a block of memory, and
00030 /// provides simple methods for reading files and standard input into a memory
00031 /// buffer.  In addition to basic access to the characters in the file, this
00032 /// interface guarantees you can read one character past the end of the file,
00033 /// and that this character will read as '\0'.
00034 ///
00035 /// The '\0' guarantee is needed to support an optimization -- it's intended to
00036 /// be more efficient for clients which are reading all the data to stop
00037 /// reading when they encounter a '\0' than to continually check the file
00038 /// position to see if it has reached the end of the file.
00039 class MemoryBuffer {
00040   const char *BufferStart; // Start of the buffer.
00041   const char *BufferEnd;   // End of the buffer.
00042 
00043   MemoryBuffer(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
00044   MemoryBuffer &operator=(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
00045 protected:
00046   MemoryBuffer() {}
00047   void init(const char *BufStart, const char *BufEnd,
00048             bool RequiresNullTerminator);
00049 public:
00050   virtual ~MemoryBuffer();
00051 
00052   const char *getBufferStart() const { return BufferStart; }
00053   const char *getBufferEnd() const   { return BufferEnd; }
00054   size_t getBufferSize() const { return BufferEnd-BufferStart; }
00055 
00056   StringRef getBuffer() const {
00057     return StringRef(BufferStart, getBufferSize());
00058   }
00059 
00060   /// Return an identifier for this buffer, typically the filename it was read
00061   /// from.
00062   virtual const char *getBufferIdentifier() const {
00063     return "Unknown buffer";
00064   }
00065 
00066   /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
00067   /// if successful, otherwise returning null. If FileSize is specified, this
00068   /// means that the client knows that the file exists and that it has the
00069   /// specified size.
00070   ///
00071   /// \param IsVolatileSize Set to true to indicate that the file size may be
00072   /// changing, e.g. when libclang tries to parse while the user is
00073   /// editing/updating the file.
00074   static ErrorOr<std::unique_ptr<MemoryBuffer>>
00075   getFile(Twine Filename, int64_t FileSize = -1,
00076           bool RequiresNullTerminator = true, bool IsVolatileSize = false);
00077 
00078   /// Given an already-open file descriptor, map some slice of it into a
00079   /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
00080   /// Since this is in the middle of a file, the buffer is not null terminated.
00081   ///
00082   /// \param IsVolatileSize Set to true to indicate that the file size may be
00083   /// changing, e.g. when libclang tries to parse while the user is
00084   /// editing/updating the file.
00085   static ErrorOr<std::unique_ptr<MemoryBuffer>>
00086   getOpenFileSlice(int FD, const char *Filename, uint64_t MapSize,
00087                    int64_t Offset, bool IsVolatileSize = false);
00088 
00089   /// Given an already-open file descriptor, read the file and return a
00090   /// MemoryBuffer.
00091   ///
00092   /// \param IsVolatileSize Set to true to indicate that the file size may be
00093   /// changing, e.g. when libclang tries to parse while the user is
00094   /// editing/updating the file.
00095   static ErrorOr<std::unique_ptr<MemoryBuffer>>
00096   getOpenFile(int FD, const char *Filename, uint64_t FileSize,
00097               bool RequiresNullTerminator = true, bool IsVolatileSize = false);
00098 
00099   /// Open the specified memory range as a MemoryBuffer. Note that InputData
00100   /// must be null terminated if RequiresNullTerminator is true.
00101   static std::unique_ptr<MemoryBuffer>
00102   getMemBuffer(StringRef InputData, StringRef BufferName = "",
00103                bool RequiresNullTerminator = true);
00104 
00105   static std::unique_ptr<MemoryBuffer>
00106   getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
00107 
00108   /// Open the specified memory range as a MemoryBuffer, copying the contents
00109   /// and taking ownership of it. InputData does not have to be null terminated.
00110   static std::unique_ptr<MemoryBuffer>
00111   getMemBufferCopy(StringRef InputData, StringRef BufferName = "");
00112 
00113   /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
00114   /// that the caller need not initialize the memory allocated by this method.
00115   /// The memory is owned by the MemoryBuffer object.
00116   static std::unique_ptr<MemoryBuffer>
00117   getNewMemBuffer(size_t Size, StringRef BufferName = "");
00118 
00119   /// Allocate a new MemoryBuffer of the specified size that is not initialized.
00120   /// Note that the caller should initialize the memory allocated by this
00121   /// method. The memory is owned by the MemoryBuffer object.
00122   static std::unique_ptr<MemoryBuffer>
00123   getNewUninitMemBuffer(size_t Size, StringRef BufferName = "");
00124 
00125   /// Read all of stdin into a file buffer, and return it.
00126   static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
00127 
00128   /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
00129   /// is "-".
00130   static ErrorOr<std::unique_ptr<MemoryBuffer>>
00131   getFileOrSTDIN(StringRef Filename, int64_t FileSize = -1);
00132 
00133   //===--------------------------------------------------------------------===//
00134   // Provided for performance analysis.
00135   //===--------------------------------------------------------------------===//
00136 
00137   /// The kind of memory backing used to support the MemoryBuffer.
00138   enum BufferKind {
00139     MemoryBuffer_Malloc,
00140     MemoryBuffer_MMap
00141   };
00142 
00143   /// Return information on the memory mechanism used to support the
00144   /// MemoryBuffer.
00145   virtual BufferKind getBufferKind() const = 0;
00146 
00147   MemoryBufferRef getMemBufferRef() const;
00148 };
00149 
00150 class MemoryBufferRef {
00151   StringRef Buffer;
00152   StringRef Identifier;
00153 
00154 public:
00155   MemoryBufferRef() {}
00156   MemoryBufferRef(StringRef Buffer, StringRef Identifier)
00157       : Buffer(Buffer), Identifier(Identifier) {}
00158 
00159   StringRef getBuffer() const { return Buffer; }
00160 
00161   StringRef getBufferIdentifier() const { return Identifier; }
00162 
00163   const char *getBufferStart() const { return Buffer.begin(); }
00164   const char *getBufferEnd() const { return Buffer.end(); }
00165   size_t getBufferSize() const { return Buffer.size(); }
00166 };
00167 
00168 // Create wrappers for C Binding types (see CBindingWrapping.h).
00169 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
00170 
00171 } // end namespace llvm
00172 
00173 #endif