Fix LCSSA to avoid emitting a PHI node for the unwind destination of
[oota-llvm.git] / lib / Debugger / SourceFile.cpp
index f0bd0d8a2212575c72e19a629afe0f876d248c69..03c60f87f7859705b549d6143f29d0666a67924e 100644 (file)
@@ -1,75 +1,43 @@
 //===-- SourceFile.cpp - SourceFile implementation for the debugger -------===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group 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.
+//
 //===----------------------------------------------------------------------===//
-// 
+//
 // This file implements the SourceFile class for the LLVM debugger.
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Debugger/SourceFile.h"
-#include "llvm/Support/SlowOperationInformer.h"
-#include "llvm/Support/FileUtilities.h"
-#include <iostream>
-#include <cerrno>
-#include <fcntl.h>
-#include <unistd.h>
+#include "llvm/Support/MemoryBuffer.h"
+#include <cassert>
 using namespace llvm;
 
-/// readFile - Load Filename into FileStart and FileEnd.
-///
-void SourceFile::readFile() {
-  ssize_t FileSize = getFileSize(Filename);
-  if (FileSize != -1) {
-    FDHandle FD(open(Filename.c_str(), O_RDONLY));
-    if (FD != -1) {
-      char *FilePos = new char[FileSize];
-      FileStart = FilePos;
-
-      // If this takes a long time, inform the user what we are doing.
-      SlowOperationInformer SOI("loading source file '" + Filename + "'");
+static const char EmptyFile = 0;
 
-      try {
-        // Read in the whole buffer.
-        unsigned Amount = FileSize;
-        while (Amount) {
-          unsigned AmountToRead = 512*1024;
-          if (Amount < AmountToRead) AmountToRead = Amount;
-          ssize_t ReadAmount = read(FD, FilePos, AmountToRead);
-          if (ReadAmount < 0 && errno == EINTR)
-            continue;
-          else if (ReadAmount <= 0) {
-            // Couldn't read whole file just free memory and continue.
-            throw "Error reading file '" + Filename + "'!";
-          }
-          Amount -= ReadAmount;
-          FilePos += ReadAmount;
-          
-          SOI.progress(FileSize-Amount, FileSize);
-        }
+SourceFile::SourceFile(const std::string &fn, const GlobalVariable *Desc)
+  : Filename(fn), Descriptor(Desc) {
+  File.reset(MemoryBuffer::getFileOrSTDIN(fn));
+    
+  // On error, return an empty buffer.
+  if (File == 0)
+    File.reset(MemoryBuffer::getMemBuffer(&EmptyFile, &EmptyFile));
+}
 
-      } catch (const std::string &Msg) {
-        std::cout << Msg << "\n";
-        // If the user cancels the operation, clean up after ourselves.
-        delete [] FileStart;
-        FileStart = 0;
-        return;
-      }
-      
-      FileEnd = FileStart+FileSize;
-    }
-  }
+SourceFile::~SourceFile() {
 }
 
+
 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
 ///
 void SourceFile::calculateLineOffsets() const {
   assert(LineOffset.empty() && "Line offsets already computed!");
-  const char *BufPtr = FileStart;
+  const char *BufPtr = File->getBufferStart();
+  const char *FileStart = BufPtr;
+  const char *FileEnd = File->getBufferEnd();
   do {
     LineOffset.push_back(BufPtr-FileStart);
 
@@ -93,19 +61,18 @@ void SourceFile::calculateLineOffsets() const {
 void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
                                const char *&LineEnd) const {
   LineStart = LineEnd = 0;
-  if (FileStart == 0) return;  // Couldn't load file, return null pointers
   if (LineOffset.empty()) calculateLineOffsets();
 
   // Asking for an out-of-range line number?
   if (LineNo >= LineOffset.size()) return;
 
   // Otherwise, they are asking for a valid line, which we can fulfill.
-  LineStart = FileStart+LineOffset[LineNo];
+  LineStart = File->getBufferStart()+LineOffset[LineNo];
 
   if (LineNo+1 < LineOffset.size())
-    LineEnd = FileStart+LineOffset[LineNo+1];
+    LineEnd = File->getBufferStart()+LineOffset[LineNo+1];
   else
-    LineEnd = FileEnd;
+    LineEnd = File->getBufferEnd();
 
   // If the line ended with a newline, strip it off.
   while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
@@ -113,4 +80,3 @@ void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
 
   assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");
 }