Add a C++11 ThreadPool implementation in LLVM
[oota-llvm.git] / include / llvm / Support / SourceMgr.h
index 30eccd99acbc63431fcf475e48e244c0c40b9432..1f8b1a01865f59091dc70c1c644d2b0e5203c0b9 100644 (file)
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SMLoc.h"
 #include <string>
 
 namespace llvm {
-  class MemoryBuffer;
   class SourceMgr;
   class SMDiagnostic;
   class SMFixIt;
@@ -47,10 +47,15 @@ public:
 private:
   struct SrcBuffer {
     /// The memory buffer for the file.
-    MemoryBuffer *Buffer;
+    std::unique_ptr<MemoryBuffer> Buffer;
 
     /// This is the location of the parent include, or null if at the top level.
     SMLoc IncludeLoc;
+
+    SrcBuffer() {}
+
+    SrcBuffer(SrcBuffer &&O)
+        : Buffer(std::move(O.Buffer)), IncludeLoc(O.IncludeLoc) {}
   };
 
   /// This is all of the buffers that we are reading from.
@@ -66,8 +71,10 @@ private:
   DiagHandlerTy DiagHandler;
   void *DiagContext;
 
-  SourceMgr(const SourceMgr&) LLVM_DELETED_FUNCTION;
-  void operator=(const SourceMgr&) LLVM_DELETED_FUNCTION;
+  bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
+
+  SourceMgr(const SourceMgr&) = delete;
+  void operator=(const SourceMgr&) = delete;
 public:
   SourceMgr()
     : LineNoCache(nullptr), DiagHandler(nullptr), DiagContext(nullptr) {}
@@ -88,58 +95,64 @@ public:
   void *getDiagContext() const { return DiagContext; }
 
   const SrcBuffer &getBufferInfo(unsigned i) const {
-    assert(i < Buffers.size() && "Invalid Buffer ID!");
-    return Buffers[i];
+    assert(isValidBufferID(i));
+    return Buffers[i - 1];
   }
 
   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
-    assert(i < Buffers.size() && "Invalid Buffer ID!");
-    return Buffers[i].Buffer;
+    assert(isValidBufferID(i));
+    return Buffers[i - 1].Buffer.get();
   }
 
-  size_t getNumBuffers() const {
+  unsigned getNumBuffers() const {
     return Buffers.size();
   }
 
+  unsigned getMainFileID() const {
+    assert(getNumBuffers());
+    return 1;
+  }
+
   SMLoc getParentIncludeLoc(unsigned i) const {
-    assert(i < Buffers.size() && "Invalid Buffer ID!");
-    return Buffers[i].IncludeLoc;
+    assert(isValidBufferID(i));
+    return Buffers[i - 1].IncludeLoc;
   }
 
   /// Add a new source buffer to this source manager. This takes ownership of
   /// the memory buffer.
-  size_t AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
+  unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
+                              SMLoc IncludeLoc) {
     SrcBuffer NB;
-    NB.Buffer = F;
+    NB.Buffer = std::move(F);
     NB.IncludeLoc = IncludeLoc;
-    Buffers.push_back(NB);
-    return Buffers.size() - 1;
+    Buffers.push_back(std::move(NB));
+    return Buffers.size();
   }
 
   /// Search for a file with the specified name in the current directory or in
   /// one of the IncludeDirs.
   ///
-  /// If no file is found, this returns ~0, otherwise it returns the buffer ID
+  /// If no file is found, this returns 0, otherwise it returns the buffer ID
   /// of the stacked file. The full path to the included file can be found in
   /// \p IncludedFile.
-  size_t AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
-                        std::string &IncludedFile);
+  unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
+                          std::string &IncludedFile);
 
   /// Return the ID of the buffer containing the specified location.
   ///
-  /// -1 is returned if the buffer is not found.
-  int FindBufferContainingLoc(SMLoc Loc) const;
+  /// 0 is returned if the buffer is not found.
+  unsigned FindBufferContainingLoc(SMLoc Loc) const;
 
   /// Find the line number for the specified location in the specified file.
   /// This is not a fast method.
-  unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const {
+  unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
     return getLineAndColumn(Loc, BufferID).first;
   }
 
   /// Find the line and column number for the specified location in the
   /// specified file. This is not a fast method.
-  std::pair<unsigned, unsigned>
-    getLineAndColumn(SMLoc Loc, int BufferID = -1) const;
+  std::pair<unsigned, unsigned> getLineAndColumn(SMLoc Loc,
+                                                 unsigned BufferID = 0) const;
 
   /// Emit a message about the specified location with the specified string.
   ///
@@ -263,8 +276,8 @@ public:
     return FixIts;
   }
 
-  void print(const char *ProgName, raw_ostream &S,
-             bool ShowColors = true) const;
+  void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
+             bool ShowKindLabel = true) const;
 };
 
 }  // end llvm namespace