Fix the Linker testcase regressions, by making MemoryBuffer::getFileOrSTDIN return
[oota-llvm.git] / include / llvm / Support / MemoryBuffer.h
1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the MemoryBuffer interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15 #define LLVM_SUPPORT_MEMORYBUFFER_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include <string>
19
20 namespace llvm {
21
22 /// MemoryBuffer - This interface provides simple read-only access to a block
23 /// of memory, and provides simple methods for reading files and standard input
24 /// into a memory buffer.  In addition to basic access to the characters in the
25 /// file, this interface guarantees you can read one character past the end of
26 /// @verbatim the file, and that this character will read as '\0'. @endverbatim
27 class MemoryBuffer {
28   const char *BufferStart; // Start of the buffer.
29   const char *BufferEnd;   // End of the buffer.
30
31   /// MustDeleteBuffer - True if we allocated this buffer.  If so, the
32   /// destructor must know the delete[] it.
33   bool MustDeleteBuffer;
34 protected:
35   MemoryBuffer() : MustDeleteBuffer(false) {}
36   void init(const char *BufStart, const char *BufEnd);
37   void initCopyOf(const char *BufStart, const char *BufEnd);
38 public:
39   virtual ~MemoryBuffer();
40   
41   const char *getBufferStart() const { return BufferStart; }
42   const char *getBufferEnd() const   { return BufferEnd; }
43   unsigned getBufferSize() const { return BufferEnd-BufferStart; }
44   
45   /// getBufferIdentifier - Return an identifier for this buffer, typically the
46   /// filename it was read from.
47   virtual const char *getBufferIdentifier() const {
48     return "Unknown buffer";
49   }
50
51   /// getFile - Open the specified file as a MemoryBuffer, returning a new
52   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
53   /// specified, this means that the client knows that the file exists and that
54   /// it has the specified size.
55   static MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
56                                std::string *ErrStr = 0,
57                                int64_t FileSize = -1);
58
59   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
60   /// that EndPtr[0] must be a null byte and be accessible!
61   static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr,
62                                     const char *BufferName = "");
63   
64   /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
65   /// copying the contents and taking ownership of it.  This has no requirements
66   /// on EndPtr[0].
67   static MemoryBuffer *getMemBufferCopy(const char *StartPtr,const char *EndPtr,
68                                         const char *BufferName = "");
69   
70   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
71   /// is completely initialized to zeros.  Note that the caller should
72   /// initialize the memory allocated by this method.  The memory is owned by
73   /// the MemoryBuffer object.
74   static MemoryBuffer *getNewMemBuffer(unsigned Size,
75                                        const char *BufferName = "");
76   
77   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
78   /// that is not initialized.  Note that the caller should initialize the
79   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
80   /// object.
81   static MemoryBuffer *getNewUninitMemBuffer(unsigned Size,
82                                              const char *BufferName = "");
83   
84   /// getSTDIN - Read all of stdin into a file buffer, and return it.  This
85   /// returns null if stdin is empty.
86   static MemoryBuffer *getSTDIN();
87   
88   
89   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
90   /// if the Filename is "-".  If an error occurs, this returns null and fills
91   /// in *ErrStr with a reason.  If stdin is empty, this API (unlike getSTDIN)
92   /// returns an empty buffer.
93   static MemoryBuffer *getFileOrSTDIN(const char *FilenameStart,unsigned FnSize,
94                                       std::string *ErrStr = 0,
95                                       int64_t FileSize = -1);
96   
97   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
98   /// if the Filename is "-".  If an error occurs, this returns null and fills
99   /// in *ErrStr with a reason.
100   static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
101                                       std::string *ErrStr = 0,
102                                       int64_t FileSize = -1) {
103     return getFileOrSTDIN(&FN[0], FN.size(), ErrStr, FileSize);
104   }
105 };
106
107 } // end namespace llvm
108
109 #endif