Add support for files with more than 65280 sections. No testcase since
[oota-llvm.git] / include / llvm / Support / SourceMgr.h
index fd56b16639cbade317dfc25ecdb4b4d70e6a4305..816f8943720eb2958f163fc107c879804dc0329c 100644 (file)
@@ -26,11 +26,19 @@ namespace llvm {
   class MemoryBuffer;
   class SourceMgr;
   class SMDiagnostic;
+  class Twine;
   class raw_ostream;
 
 /// SourceMgr - This owns the files read by a parser, handles include stacks,
 /// and handles diagnostic wrangling.
 class SourceMgr {
+public:
+  /// DiagHandlerTy - Clients that want to handle their own diagnostics in a
+  /// custom way can register a function pointer+context as a diagnostic
+  /// handler.  It gets called each time PrintMessage is invoked.
+  typedef void (*DiagHandlerTy)(const SMDiagnostic&, void *Context,
+                                unsigned LocCookie);
+private:
   struct SrcBuffer {
     /// Buffer - The memory buffer for the file.
     MemoryBuffer *Buffer;
@@ -51,16 +59,29 @@ class SourceMgr {
   /// is really private to SourceMgr.cpp.
   mutable void *LineNoCache;
 
+  DiagHandlerTy DiagHandler;
+  void *DiagContext;
+  unsigned DiagLocCookie;
+  
   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
 public:
-  SourceMgr() : LineNoCache(0) {}
+  SourceMgr() : LineNoCache(0), DiagHandler(0), DiagContext(0) {}
   ~SourceMgr();
 
   void setIncludeDirs(const std::vector<std::string> &Dirs) {
     IncludeDirectories = Dirs;
   }
 
+  /// setDiagHandler - Specify a diagnostic handler to be invoked every time
+  /// PrintMessage is called.  Ctx and Cookie are passed into the handler when
+  /// it is invoked.
+  void setDiagHandler(DiagHandlerTy DH, void *Ctx = 0, unsigned Cookie = 0) {
+    DiagHandler = DH;
+    DiagContext = Ctx;
+    DiagLocCookie = Cookie;
+  }
+
   const SrcBuffer &getBufferInfo(unsigned i) const {
     assert(i < Buffers.size() && "Invalid Buffer ID!");
     return Buffers[i];
@@ -76,6 +97,8 @@ public:
     return Buffers[i].IncludeLoc;
   }
 
+  /// AddNewSourceBuffer - Add a new source buffer to this source manager.  This
+  /// takes ownership of the memory buffer.
   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
     SrcBuffer NB;
     NB.Buffer = F;
@@ -103,7 +126,7 @@ public:
   /// @param Type - If non-null, the kind of message (e.g., "error") which is
   /// prefixed to the message.
   /// @param ShowLine - Should the diagnostic show the source line.
-  void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type,
+  void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
                     bool ShowLine = true) const;
 
 
@@ -114,7 +137,7 @@ public:
   /// prefixed to the message.
   /// @param ShowLine - Should the diagnostic show the source line.
   SMDiagnostic GetMessage(SMLoc Loc,
-                          const std::string &Msg, const char *Type,
+                          const Twine &Msg, const char *Type,
                           bool ShowLine = true) const;
 
 
@@ -126,19 +149,39 @@ private:
 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
 /// allowing printing to a raw_ostream as a caret diagnostic.
 class SMDiagnostic {
+  const SourceMgr *SM;
+  SMLoc Loc;
   std::string Filename;
   int LineNo, ColumnNo;
   std::string Message, LineContents;
   unsigned ShowLine : 1;
 
 public:
-  SMDiagnostic() : LineNo(0), ColumnNo(0), ShowLine(0) {}
-  SMDiagnostic(const std::string &FN, int Line, int Col,
+  // Null diagnostic.
+  SMDiagnostic() : SM(0), LineNo(0), ColumnNo(0), ShowLine(0) {}
+  // Diagnostic with no location (e.g. file not found, command line arg error).
+  SMDiagnostic(const std::string &filename, const std::string &Msg,
+               bool showline = true)
+    : SM(0), Filename(filename), LineNo(-1), ColumnNo(-1),
+      Message(Msg), ShowLine(showline) {}
+  
+  // Diagnostic with a location.
+  SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN,
+               int Line, int Col,
                const std::string &Msg, const std::string &LineStr,
                bool showline = true)
-    : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
+    : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
       LineContents(LineStr), ShowLine(showline) {}
 
+  const SourceMgr *getSourceMgr() const { return SM; }
+  SMLoc getLoc() const { return Loc; }
+  const std::string &getFilename() { return Filename; }
+  int getLineNo() const { return LineNo; }
+  int getColumnNo() const { return ColumnNo; }
+  const std::string &getMessage() const { return Message; }
+  const std::string &getLineContents() const { return LineContents; }
+  bool getShowLine() const { return ShowLine; }
+  
   void Print(const char *ProgName, raw_ostream &S) const;
 };