Add comment.
[oota-llvm.git] / lib / Support / MemoryBuffer.cpp
index f8779e122d5012de64621c8e1c1136d7c5f116a9..4d26d38731a8078b65a3e763207a1fb0389125e3 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Chris Lattner and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -117,6 +117,24 @@ MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size,
 }
 
 
+/// 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.  If stdin is empty, this API (unlike getSTDIN)
+/// returns an empty buffer.
+MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *FilenameStart,
+                                           unsigned FnSize,
+                                           std::string *ErrStr,
+                                           int64_t FileSize) {
+  if (FnSize != 1 || FilenameStart[0] != '-')
+    return getFile(FilenameStart, FnSize, ErrStr, FileSize);
+  MemoryBuffer *M = getSTDIN();
+  if (M) return M;
+
+  // If stdin was empty, M is null.  Cons up an empty memory buffer now.
+  const char *EmptyStr = "";
+  return MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>");
+}
+
 //===----------------------------------------------------------------------===//
 // MemoryBufferMMapFile implementation.
 //===----------------------------------------------------------------------===//