Add support to the SourceMgrDiagnosticHandlers to auto-load new source files...
authorRiver Riddle <riverriddle@google.com>
Fri, 10 May 2019 22:00:53 +0000 (15:00 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Sat, 11 May 2019 02:30:15 +0000 (19:30 -0700)
--

PiperOrigin-RevId: 247681779

mlir/include/mlir/IR/Diagnostics.h
mlir/lib/IR/Diagnostics.cpp

index b456bc1..9ab29c7 100644 (file)
@@ -448,9 +448,8 @@ public:
   void emitDiagnostic(Location loc, Twine message, DiagnosticSeverity kind);
 
 protected:
-  /// Get a memory buffer for the given file, or the main file of the source
-  /// manager if one doesn't exist. If no file is available, nullptr is
-  /// returned.
+  /// Get a memory buffer for the given file, or nullptr if no file is
+  /// available.
   const llvm::MemoryBuffer *getBufferForFile(StringRef filename);
 
   /// The source manager that we are wrapping.
index c06539d..10eaed8 100644 (file)
@@ -313,10 +313,11 @@ SourceMgrDiagnosticHandler::getBufferForFile(StringRef filename) {
       return filenameToBuf[filename] = buf;
   }
 
-  // Otherwise, default to the main file buffer, if it exists.
+  // Otherwise, try to load the source file.
   const llvm::MemoryBuffer *newBuf = nullptr;
-  if (mgr.getNumBuffers() != 0)
-    newBuf = mgr.getMemoryBuffer(mgr.getMainFileID());
+  std::string ignored;
+  if (auto newBufID = mgr.AddIncludeFile(filename, llvm::SMLoc(), ignored))
+    newBuf = mgr.getMemoryBuffer(newBufID);
   return filenameToBuf[filename] = newBuf;
 }