Remove several heavy includes from Diagnostics.h by a moving a couple of fields...
authorRiver Riddle <riverriddle@google.com>
Sat, 11 May 2019 18:59:01 +0000 (11:59 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Mon, 20 May 2019 20:36:48 +0000 (13:36 -0700)
--

PiperOrigin-RevId: 247768443

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

index 04e35f2..a14838a 100644 (file)
@@ -24,9 +24,6 @@
 
 #include "mlir/IR/Location.h"
 #include "mlir/Support/STLExtras.h"
-#include "llvm/ADT/SmallString.h"
-#include "llvm/ADT/StringMap.h"
-#include "llvm/ADT/Twine.h"
 #include <functional>
 
 namespace llvm {
@@ -200,30 +197,23 @@ public:
 
   /// Stream operator for inserting new diagnostic arguments.
   template <typename Arg>
-  typename std::enable_if<!std::is_convertible<Arg, Twine>::value ||
-                              std::is_integral<Arg>::value,
+  typename std::enable_if<!std::is_convertible<Arg, StringRef>::value,
                           Diagnostic &>::type
   operator<<(Arg &&val) {
     arguments.push_back(DiagnosticArgument(std::forward<Arg>(val)));
     return *this;
   }
+
+  /// Stream in a string literal.
   Diagnostic &operator<<(const char *val) {
     arguments.push_back(DiagnosticArgument(val));
     return *this;
   }
-  Diagnostic &operator<<(char val) { return *this << Twine(val); }
-  Diagnostic &operator<<(const Twine &val) {
-    // Allocate memory to hold this string.
-    llvm::SmallString<0> data;
-    auto strRef = val.toStringRef(data);
-    strings.push_back(std::unique_ptr<char[]>(new char[strRef.size()]));
-    memcpy(&strings.back()[0], strRef.data(), strRef.size());
-
-    // Add the new string to the argument list.
-    strRef = StringRef(&strings.back()[0], strRef.size());
-    arguments.push_back(DiagnosticArgument(strRef));
-    return *this;
-  }
+
+  /// Stream in a Twine argument.
+  Diagnostic &operator<<(char val);
+  Diagnostic &operator<<(const Twine &val);
+  Diagnostic &operator<<(Twine &&val);
 
   /// Stream in an Identifier.
   Diagnostic &operator<<(Identifier val);
@@ -435,10 +425,15 @@ private:
 // SourceMgrDiagnosticHandler
 //===----------------------------------------------------------------------===//
 
+namespace detail {
+struct SourceMgrDiagnosticHandlerImpl;
+} // end namespace detail
+
 /// This class is a utility diagnostic handler for use with llvm::SourceMgr.
 class SourceMgrDiagnosticHandler {
 public:
   SourceMgrDiagnosticHandler(llvm::SourceMgr &mgr, MLIRContext *ctx);
+  ~SourceMgrDiagnosticHandler();
 
   /// Emit the given diagnostic information with the held source manager.
   void emitDiagnostic(Location loc, Twine message, DiagnosticSeverity kind);
@@ -455,8 +450,7 @@ private:
   /// Convert a location into the given memory buffer into an SMLoc.
   llvm::SMLoc convertLocToSMLoc(Location loc);
 
-  /// Mapping between file name and buffer pointer.
-  llvm::StringMap<const llvm::MemoryBuffer *> filenameToBuf;
+  std::unique_ptr<detail::SourceMgrDiagnosticHandlerImpl> impl;
 };
 
 //===----------------------------------------------------------------------===//
index b03a30d..574a135 100644 (file)
@@ -21,8 +21,8 @@
 #include "mlir/IR/Location.h"
 #include "mlir/IR/MLIRContext.h"
 #include "mlir/IR/Types.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringMap.h"
-#include "llvm/ADT/Twine.h"
 #include "llvm/Support/Mutex.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/Support/SourceMgr.h"
@@ -86,6 +86,31 @@ void DiagnosticArgument::print(raw_ostream &os) const {
 // Diagnostic
 //===----------------------------------------------------------------------===//
 
+/// Convert a Twine to a StringRef. Memory used for generating the StringRef is
+/// stored in 'strings'.
+static StringRef twineToStrRef(const Twine &val,
+                               std::vector<std::unique_ptr<char[]>> &strings) {
+  // Allocate memory to hold this string.
+  llvm::SmallString<64> data;
+  auto strRef = val.toStringRef(data);
+  strings.push_back(std::unique_ptr<char[]>(new char[strRef.size()]));
+  memcpy(&strings.back()[0], strRef.data(), strRef.size());
+
+  // Return a reference to the new string.
+  return StringRef(&strings.back()[0], strRef.size());
+}
+
+/// Stream in a Twine argument.
+Diagnostic &Diagnostic::operator<<(char val) { return *this << Twine(val); }
+Diagnostic &Diagnostic::operator<<(const Twine &val) {
+  arguments.push_back(DiagnosticArgument(twineToStrRef(val, strings)));
+  return *this;
+}
+Diagnostic &Diagnostic::operator<<(Twine &&val) {
+  arguments.push_back(DiagnosticArgument(twineToStrRef(val, strings)));
+  return *this;
+}
+
 /// Stream in an Identifier.
 Diagnostic &Diagnostic::operator<<(Identifier val) {
   // An identifier is stored in the context, so we don't need to worry about the
@@ -226,6 +251,37 @@ void DiagnosticEngine::emit(Diagnostic diag) {
 //===----------------------------------------------------------------------===//
 // SourceMgrDiagnosticHandler
 //===----------------------------------------------------------------------===//
+namespace mlir {
+namespace detail {
+struct SourceMgrDiagnosticHandlerImpl {
+  /// Get a memory buffer for the given file, or nullptr if one is not found.
+  const llvm::MemoryBuffer *getBufferForFile(llvm::SourceMgr &mgr,
+                                             StringRef filename) {
+    // Check for an existing mapping to the buffer id for this file.
+    auto bufferIt = filenameToBuf.find(filename);
+    if (bufferIt != filenameToBuf.end())
+      return bufferIt->second;
+
+    // Look for a buffer in the manager that has this filename.
+    for (unsigned i = 1, e = mgr.getNumBuffers() + 1; i != e; ++i) {
+      auto *buf = mgr.getMemoryBuffer(i);
+      if (buf->getBufferIdentifier() == filename)
+        return filenameToBuf[filename] = buf;
+    }
+
+    // Otherwise, try to load the source file.
+    const llvm::MemoryBuffer *newBuf = nullptr;
+    std::string ignored;
+    if (auto newBufID = mgr.AddIncludeFile(filename, llvm::SMLoc(), ignored))
+      newBuf = mgr.getMemoryBuffer(newBufID);
+    return filenameToBuf[filename] = newBuf;
+  }
+
+  /// Mapping between file name and buffer pointer.
+  llvm::StringMap<const llvm::MemoryBuffer *> filenameToBuf;
+};
+} // end namespace detail
+} // end namespace mlir
 
 /// Return a processable FileLineColLoc from the given location.
 static llvm::Optional<FileLineColLoc> getFileLineColLoc(Location loc) {
@@ -264,7 +320,7 @@ static llvm::SourceMgr::DiagKind getDiagKind(DiagnosticSeverity kind) {
 
 SourceMgrDiagnosticHandler::SourceMgrDiagnosticHandler(llvm::SourceMgr &mgr,
                                                        MLIRContext *ctx)
-    : mgr(mgr) {
+    : mgr(mgr), impl(new SourceMgrDiagnosticHandlerImpl()) {
   // Register a simple diagnostic handler.
   ctx->getDiagEngine().setHandler([this](Diagnostic diag) {
     // Emit the diagnostic.
@@ -275,6 +331,7 @@ SourceMgrDiagnosticHandler::SourceMgrDiagnosticHandler(llvm::SourceMgr &mgr,
       emitDiagnostic(note.getLocation(), note.str(), note.getSeverity());
   });
 }
+SourceMgrDiagnosticHandler::~SourceMgrDiagnosticHandler() {}
 
 void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message,
                                                 DiagnosticSeverity kind) {
@@ -284,24 +341,7 @@ void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message,
 /// Get a memory buffer for the given file, or nullptr if one is not found.
 const llvm::MemoryBuffer *
 SourceMgrDiagnosticHandler::getBufferForFile(StringRef filename) {
-  // Check for an existing mapping to the buffer id for this file.
-  auto bufferIt = filenameToBuf.find(filename);
-  if (bufferIt != filenameToBuf.end())
-    return bufferIt->second;
-
-  // Look for a buffer in the manager that has this filename.
-  for (unsigned i = 1, e = mgr.getNumBuffers() + 1; i != e; ++i) {
-    auto *buf = mgr.getMemoryBuffer(i);
-    if (buf->getBufferIdentifier() == filename)
-      return filenameToBuf[filename] = buf;
-  }
-
-  // Otherwise, try to load the source file.
-  const llvm::MemoryBuffer *newBuf = nullptr;
-  std::string ignored;
-  if (auto newBufID = mgr.AddIncludeFile(filename, llvm::SMLoc(), ignored))
-    newBuf = mgr.getMemoryBuffer(newBufID);
-  return filenameToBuf[filename] = newBuf;
+  return impl->getBufferForFile(mgr, filename);
 }
 
 /// Get a memory buffer for the given file, or the main file of the source