Use SourceManager's Diagnostic object for all file-reading errors,
simplifying the SourceManager interfaces somewhat.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@98598 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp
index 3b49959..dd2528a 100644
--- a/lib/AST/ASTImporter.cpp
+++ b/lib/AST/ASTImporter.cpp
@@ -3089,7 +3089,7 @@
                              FromSLoc.getFile().getFileCharacteristic());
   } else {
     // FIXME: We want to re-use the existing MemoryBuffer!
-    const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
+    const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(getDiags());
     llvm::MemoryBuffer *ToBuf
       = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
                                              FromBuf->getBufferEnd(),
diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp
index ac2fe3d..f0a0d4c 100644
--- a/lib/Basic/SourceManager.cpp
+++ b/lib/Basic/SourceManager.cpp
@@ -22,7 +22,6 @@
 #include <algorithm>
 #include <string>
 #include <cstring>
-#include <cstdio>
 
 using namespace clang;
 using namespace SrcMgr;
@@ -32,75 +31,6 @@
 // SourceManager Helper Classes
 //===----------------------------------------------------------------------===//
 
-struct BufferResult::FailureData {
-  const llvm::MemoryBuffer *Buffer;
-  const char *FileName;
-  std::string ErrorStr;  
-};
-
-BufferResult::BufferResult(const BufferResult &Other) {
-  if (const llvm::MemoryBuffer *Buffer
-                        = Other.Data.dyn_cast<const llvm::MemoryBuffer *>()) {
-    Data = Buffer;
-    return;
-  }
-  
-  Data = new FailureData(*Other.Data.get<FailureData *>());
-}
-
-BufferResult::BufferResult(const char *FileName, llvm::StringRef ErrorStr,
-                           const llvm::MemoryBuffer *Buffer) {
-  FailureData *FD = new FailureData;
-  FD->FileName = FileName;
-  FD->ErrorStr = ErrorStr;
-  FD->Buffer = Buffer;
-  Data = FD;
-}
-
-BufferResult::~BufferResult() {
-  if (FailureData *FD = Data.dyn_cast<FailureData *>())
-    delete FD;
-}
-
-bool BufferResult::isInvalid() const {
-  return Data.is<FailureData *>();
-}
-
-const llvm::MemoryBuffer *BufferResult::getBuffer(Diagnostic &Diags) const {
-  llvm::StringRef FileName;
-  std::string ErrorMsg;
-  const llvm::MemoryBuffer *Result = getBuffer(FileName, ErrorMsg);
-  if (!ErrorMsg.empty()) {
-    Diags.Report(diag::err_cannot_open_file)
-      << FileName << ErrorMsg;
-  }
-  return Result;
-}
-
-const llvm::MemoryBuffer *BufferResult::getBuffer(llvm::StringRef &FileName, 
-                                                  std::string &Error) const {
-  if (const llvm::MemoryBuffer *Buffer
-                                  = Data.dyn_cast<const llvm::MemoryBuffer *>())
-    return Buffer;
- 
-  FailureData *Fail = Data.get<FailureData *>();
-  FileName = Fail->FileName;
-  Error = Fail->ErrorStr;
-  return Fail->Buffer;
-}
-
-BufferResult::operator const llvm::MemoryBuffer *() const {
-  llvm::StringRef FileName;
-  std::string ErrorMsg;
-  const llvm::MemoryBuffer *Result = getBuffer(FileName, ErrorMsg);
-  if (!ErrorMsg.empty()) {
-    fprintf(stderr, "error: cannot open file '%s': %s\n",
-            FileName.str().c_str(), ErrorMsg.c_str());
-  }
-  
-  return Result;
-}
-
 ContentCache::~ContentCache() {
   delete Buffer;
 }
@@ -128,7 +58,11 @@
   Buffer = B;
 }
 
-BufferResult ContentCache::getBuffer() const {
+const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
+                                                  bool *Invalid) const {
+  if (Invalid)
+    *Invalid = false;
+      
   // Lazily create the Buffer for ContentCaches that wrap files.
   if (!Buffer && Entry) {
     std::string ErrorStr;
@@ -152,17 +86,23 @@
       char *Ptr = const_cast<char*>(Buffer->getBufferStart());
       for (unsigned i = 0, e = Entry->getSize(); i != e; ++i)
         Ptr[i] = FillStr[i % FillStr.size()];
-      return BufferResult(Entry->getName(), ErrorStr, Buffer);
+      Diag.Report(diag::err_cannot_open_file)
+        << Entry->getName() << ErrorStr;
+      if (Invalid)
+        *Invalid = true;
     } else {
       // Check that the file's size and modification time is the same as 
       // in the file entry (which may have come from a stat cache).
-      // FIXME: Make these strings localizable.
       if (FileInfo.st_size != Entry->getSize()) {
-        ErrorStr = "file has changed size since it was originally read";
-        return BufferResult(Entry->getName(), ErrorStr, Buffer);
+        Diag.Report(diag::err_file_size_changed)
+          << Entry->getName() << (unsigned)Entry->getSize() 
+          << (unsigned)FileInfo.st_size;
+        if (Invalid)
+          *Invalid = true;
       } else if (FileInfo.st_mtime != Entry->getModificationTime()) {
-        ErrorStr = "file has been modified since it was originally read";
-        return BufferResult(Entry->getName(), ErrorStr, Buffer);
+        Diag.Report(diag::err_file_modified) << Entry->getName();
+        if (Invalid)
+          *Invalid = true;
       }
     }
   }
@@ -516,10 +456,11 @@
   return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
 }
 
-BufferResult SourceManager::getMemoryBufferForFile(const FileEntry *File) {
+const llvm::MemoryBuffer *
+SourceManager::getMemoryBufferForFile(const FileEntry *File) {
   const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
   assert(IR && "getOrCreateContentCache() cannot return NULL");
-  return IR->getBuffer();
+  return IR->getBuffer(Diag);
 }
 
 bool SourceManager::overrideFileContents(const FileEntry *SourceFile,
@@ -537,7 +478,7 @@
   if (Invalid)
     *Invalid = false;
   
-  const llvm::MemoryBuffer *Buf = getBuffer(FID).getBuffer(Diag);
+  const llvm::MemoryBuffer *Buf = getBuffer(FID);
   if (!Buf) {
     if (*Invalid)
       *Invalid = true;
@@ -767,7 +708,7 @@
 
   // Note that calling 'getBuffer()' may lazily page in a source file.
   return getSLocEntry(LocInfo.first).getFile().getContentCache()
-              ->getBuffer()->getBufferStart() + LocInfo.second;
+              ->getBuffer(Diag)->getBufferStart() + LocInfo.second;
 }
 
 
@@ -796,11 +737,13 @@
 
 
 
-static DISABLE_INLINE void ComputeLineNumbers(ContentCache* FI,
+static DISABLE_INLINE void ComputeLineNumbers(Diagnostic &Diag,
+                                              ContentCache* FI,
                                               llvm::BumpPtrAllocator &Alloc);
-static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
+static void ComputeLineNumbers(Diagnostic &Diag, ContentCache* FI, 
+                               llvm::BumpPtrAllocator &Alloc){
   // Note that calling 'getBuffer()' may lazily page in the file.
-  const MemoryBuffer *Buffer = FI->getBuffer();
+  const MemoryBuffer *Buffer = FI->getBuffer(Diag);
 
   // Find the file offsets of all of the *physical* source lines.  This does
   // not look at trigraphs, escaped newlines, or anything else tricky.
@@ -857,7 +800,7 @@
   // If this is the first use of line information for this buffer, compute the
   /// SourceLineCache for it on demand.
   if (Content->SourceLineCache == 0)
-    ComputeLineNumbers(Content, ContentCacheAlloc);
+    ComputeLineNumbers(Diag, Content, ContentCacheAlloc);
 
   // Okay, we know we have a line number table.  Do a binary search to find the
   // line number that this character position lands on.
@@ -1015,7 +958,7 @@
   // before the MemBuffer as this will avoid unnecessarily paging in the
   // MemBuffer.
   const char *Filename =
-    C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
+    C->Entry ? C->Entry->getName() : C->getBuffer(Diag)->getBufferIdentifier();
   unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
   unsigned ColNo  = getColumnNumber(LocInfo.first, LocInfo.second);
   SourceLocation IncludeLoc = FI.getIncludeLoc();
@@ -1072,7 +1015,7 @@
   // If this is the first use of line information for this buffer, compute the
   /// SourceLineCache for it on demand.
   if (Content->SourceLineCache == 0)
-    ComputeLineNumbers(Content, ContentCacheAlloc);
+    ComputeLineNumbers(Diag, Content, ContentCacheAlloc);
 
   // Find the first file ID that corresponds to the given file.
   FileID FirstFID;
@@ -1101,15 +1044,15 @@
     return SourceLocation();
 
   if (Line > Content->NumLines) {
-    unsigned Size = Content->getBuffer()->getBufferSize();
+    unsigned Size = Content->getBuffer(Diag)->getBufferSize();
     if (Size > 0)
       --Size;
     return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size);
   }
 
   unsigned FilePos = Content->SourceLineCache[Line - 1];
-  const char *Buf = Content->getBuffer()->getBufferStart() + FilePos;
-  unsigned BufLength = Content->getBuffer()->getBufferEnd() - Buf;
+  const char *Buf = Content->getBuffer(Diag)->getBufferStart() + FilePos;
+  unsigned BufLength = Content->getBuffer(Diag)->getBufferEnd() - Buf;
   unsigned i = 0;
 
   // Check that the given column is valid.
diff --git a/lib/Frontend/CacheTokens.cpp b/lib/Frontend/CacheTokens.cpp
index 3d7f5c0..02d6cec 100644
--- a/lib/Frontend/CacheTokens.cpp
+++ b/lib/Frontend/CacheTokens.cpp
@@ -474,7 +474,7 @@
     if (!P.isAbsolute())
       continue;
 
-    const llvm::MemoryBuffer *B = C.getBuffer();
+    const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics());
     if (!B) continue;
 
     FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp
index ba406c8..c256b41 100644
--- a/lib/Frontend/PCHWriter.cpp
+++ b/lib/Frontend/PCHWriter.cpp
@@ -1089,7 +1089,8 @@
         // We add one to the size so that we capture the trailing NULL
         // that is required by llvm::MemoryBuffer::getMemBuffer (on
         // the reader side).
-        const llvm::MemoryBuffer *Buffer = Content->getBuffer();
+        const llvm::MemoryBuffer *Buffer
+          = Content->getBuffer(PP.getDiagnostics());
         const char *Name = Buffer->getBufferIdentifier();
         Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
                                   llvm::StringRef(Name, strlen(Name) + 1));
diff --git a/lib/Lex/PPLexerChange.cpp b/lib/Lex/PPLexerChange.cpp
index 4fba7b7..81e6bf8 100644
--- a/lib/Lex/PPLexerChange.cpp
+++ b/lib/Lex/PPLexerChange.cpp
@@ -80,8 +80,7 @@
   }
   
   // Get the MemoryBuffer for this FID, if it fails, we fail.
-  const llvm::MemoryBuffer *InputFile
-    = getSourceManager().getBuffer(FID).getBuffer(getDiagnostics());
+  const llvm::MemoryBuffer *InputFile = getSourceManager().getBuffer(FID);
   if (!InputFile)
     return true;